-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for /t:Publish /p:NoBuild=true #2111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Xml.Linq; | ||
using FluentAssertions; | ||
using FluentAssertions.Json; | ||
using Microsoft.Extensions.DependencyModel; | ||
|
@@ -13,7 +15,6 @@ | |
using Microsoft.NET.TestFramework.Commands; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
using System.Xml.Linq; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.NET.Publish.Tests | ||
|
@@ -28,26 +29,7 @@ public GivenThatWeWantToPublishAProjectWithAllFeatures(ITestOutputHelper log) : | |
[MemberData(nameof(PublishData))] | ||
public void It_publishes_the_project_correctly(string targetFramework, string [] expectedPublishFiles) | ||
{ | ||
TestAsset testAsset = _testAssetsManager | ||
.CopyTestAsset("KitchenSink", "KitchenSinkPublish_", targetFramework) | ||
.WithSource() | ||
.WithProjectChanges((path, project) => | ||
{ | ||
if (Path.GetFileName(path).Equals("TestApp.csproj", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var ns = project.Root.Name.Namespace; | ||
|
||
var targetFrameworkElement = project.Root.Elements(ns + "PropertyGroup").Elements(ns + "TargetFramework").Single(); | ||
targetFrameworkElement.SetValue(targetFramework); | ||
} | ||
}); | ||
|
||
testAsset.Restore(Log, "TestApp"); | ||
testAsset.Restore(Log, "TestLibrary"); | ||
|
||
var appProjectDirectory = Path.Combine(testAsset.TestRoot, "TestApp"); | ||
|
||
PublishCommand publishCommand = new PublishCommand(Log, appProjectDirectory); | ||
PublishCommand publishCommand = GetPublishCommand(targetFramework); | ||
publishCommand | ||
.Execute() | ||
.Should() | ||
|
@@ -107,6 +89,87 @@ public void It_publishes_the_project_correctly(string targetFramework, string [] | |
.BeEquivalentTo(baselineConfigJsonObject); | ||
} | ||
|
||
[Fact] | ||
public void It_fails_when_nobuild_is_set_and_build_was_not_performed_previously() | ||
{ | ||
var publishCommand = GetPublishCommand("netcoreapp1.0").Execute("/p:NoBuild=true"); | ||
publishCommand.Should().Fail().And.HaveStdOutContaining("MSB3030"); // "Could not copy ___ because it was not found." | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(PublishData))] | ||
public void It_does_not_build_when_nobuild_is_set(string targetFramework, string[] expectedPublishFiles) | ||
{ | ||
var publishCommand = GetPublishCommand(targetFramework); | ||
|
||
// do a separate build invocation before publish | ||
var buildCommand = new BuildCommand(Log, publishCommand.ProjectRootPath); | ||
buildCommand.Execute().Should().Pass(); | ||
|
||
// modify all project files, which would force recompilation if we were to build during publish | ||
WaitForUtcNowToAdvance(); | ||
foreach (string projectFile in EnumerateFiles(buildCommand, "*.csproj")) | ||
{ | ||
File.AppendAllText(projectFile, " "); | ||
} | ||
|
||
// capture modification time of all binaries before publish | ||
var modificationTimes = GetLastWriteTimesUtc(buildCommand, "*.exe", "*.dll", "*.resources", "*.pdb"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should probably be a "sleep until current time advances" loop here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
// publish (with NoBuild set) | ||
WaitForUtcNowToAdvance(); | ||
publishCommand.Execute("/p:NoBuild=true").Should().Pass(); | ||
publishCommand.GetOutputDirectory(targetFramework).Should().OnlyHaveFiles(expectedPublishFiles); | ||
|
||
// check that publish did not modify any of the build output | ||
foreach (var (file, modificationTime) in modificationTimes) | ||
{ | ||
File.GetLastWriteTimeUtc(file) | ||
.Should().Be( | ||
modificationTime, | ||
because: $"Publish with NoBuild=true should not overwrite {file}"); | ||
} | ||
} | ||
|
||
private static List<(string, DateTime)> GetLastWriteTimesUtc(MSBuildCommand command, params string[] searchPatterns) | ||
{ | ||
return EnumerateFiles(command, searchPatterns) | ||
.Select(file => (file, File.GetLastWriteTimeUtc(file))) | ||
.ToList(); | ||
} | ||
|
||
private static IEnumerable<string> EnumerateFiles(MSBuildCommand command, params string[] searchPatterns) | ||
{ | ||
return searchPatterns.SelectMany( | ||
pattern => Directory.EnumerateFiles( | ||
Path.Combine(command.ProjectRootPath, ".."), // up one level from TestApp to also get TestLibrary P2P files | ||
pattern, | ||
SearchOption.AllDirectories)); | ||
} | ||
|
||
private PublishCommand GetPublishCommand(string targetFramework, [CallerMemberName] string callingMethod = null) | ||
{ | ||
TestAsset testAsset = _testAssetsManager | ||
.CopyTestAsset("KitchenSink", callingMethod, identifier: targetFramework) | ||
.WithSource() | ||
.WithProjectChanges((path, project) => | ||
{ | ||
if (Path.GetFileName(path).Equals("TestApp.csproj", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var ns = project.Root.Name.Namespace; | ||
|
||
var targetFrameworkElement = project.Root.Elements(ns + "PropertyGroup").Elements(ns + "TargetFramework").Single(); | ||
targetFrameworkElement.SetValue(targetFramework); | ||
} | ||
}); | ||
|
||
testAsset.Restore(Log, "TestApp"); | ||
|
||
var appProjectDirectory = Path.Combine(testAsset.TestRoot, "TestApp"); | ||
|
||
return new PublishCommand(Log, appProjectDirectory); | ||
} | ||
|
||
private static void VerifyDependency( | ||
DependencyContext dependencyContext, | ||
string name, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use a less generic name for this property? Perhaps
PublishWithoutBuild
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches pack --no-build so I think it's worth the consistency. Otherwise it should have been
PackWithoutBuilding
.