Skip to content

Commit

Permalink
Disable Artifacts if project is using .NET SDK's built-in artifacts f…
Browse files Browse the repository at this point in the history
…unctionality (#458)

If a user has opted into the .NET SDK's artifacts functionality by setting the MSBuild property UseArtifactsOutput, then disable Microsoft.Build.Artifacts since you would only want to be using one or the other.
  • Loading branch information
jeffkl authored Nov 1, 2023
1 parent 10e9110 commit 809463b
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 91 deletions.
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<NoWarn>$(NoWarn);NU5128;SA0001</NoWarn>
<RestoreUseStaticGraphEvaluation>true</RestoreUseStaticGraphEvaluation>
<UseArtifactsOutput>false</UseArtifactsOutput>
<IsTestProject Condition="$(MSBuildProjectName.EndsWith('UnitTests'))">true</IsTestProject>
<IsPackable Condition="'$(IsTestProject)' != 'true'">true</IsPackable>
</PropertyGroup>
</Project>
11 changes: 6 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@
<PackageVersion Include="Microsoft.Build.Tasks.Core" Version="$(MicrosoftBuildPackageVersion)" />
<PackageVersion Update="Microsoft.Build.Tasks.Core" Version="16.9.0" Condition="'$(TargetFramework)' == 'netcoreapp3.1' Or '$(TargetFramework)' == 'netstandard2.0'" />
<PackageVersion Update="Microsoft.Build.Tasks.Core" Version="15.9.20" Condition="'$(TargetFramework)' == 'net46'" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageVersion Include="MSBuild.ProjectCreation" Version="10.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="System.CodeDom" Version="7.0.0" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="6.0.0" Condition=" '$(TargetFramework)' != 'net46' " />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="4.11.1" Condition=" '$(TargetFramework)' == 'net46' " />
</ItemGroup>
<ItemGroup Condition="'$(IsTestProject)' == 'true'">
<PackageVersion Include="AssemblyShader" Version="1.0.3-preview" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="MSBuild.ProjectCreation" Version="10.0.0" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="xunit" Version="2.6.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<ItemGroup>
<GlobalPackageReference Include="Microsoft.Build.Artifacts" Version="6.0.6" Condition="'$(EnableArtifacts)' != 'false'" />
<GlobalPackageReference Include="Microsoft.VisualStudioEng.MicroBuild.Core" Version="1.0.0" Condition="'$(EnableMicroBuild)' != 'false'" />
<GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.6.133" Condition="'$(EnableGitVersioning)' != 'false'" />
</ItemGroup>

<ItemGroup Condition=" '$(EnableStyleCop)' != 'false' ">
<GlobalPackageReference Include="StyleCop.Analyzers" Version="1.1.118" />
<Compile Include="$(MSBuildThisFileDirectory)src\GlobalSuppressions.cs" />
Expand Down
3 changes: 2 additions & 1 deletion azure-pipelines-official.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ variables:
ArtifactsDirectoryName: 'artifacts'
BuildConfiguration: 'Release'
BuildPlatform: 'Any CPU'
DotNetVersion: '7.x'
DotNetVersion: '8.x'
MSBuildArgs: '"/p:Platform=$(BuildPlatform)" "/p:Configuration=$(BuildConfiguration)" "/BinaryLogger:$(Build.SourcesDirectory)\$(ArtifactsDirectoryName)\msbuild.binlog"'
SignType: 'Test'

Expand Down Expand Up @@ -61,6 +61,7 @@ stages:
displayName: 'Install .NET $(DotNetVersion)'
inputs:
version: '$(DotNetVersion)'
includePreviewVersions: true

- task: MicroBuildSigningPlugin@4
displayName: 'Install MicroBuild Signing Plugin'
Expand Down
77 changes: 65 additions & 12 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ variables:
ArtifactsDirectoryName: 'artifacts'
BuildConfiguration: 'Debug'
BuildPlatform: 'Any CPU'
DotNet3Version: '3.x'
DotNet6Version: '6.x'
DotNet7Version: '7.x'
DotNet8Version: '8.x'
MSBuildArgs: '"/p:Platform=$(BuildPlatform)" "/p:Configuration=$(BuildConfiguration)" "/BinaryLogger:$(Build.SourcesDirectory)\$(ArtifactsDirectoryName)\msbuild.binlog"'
SignType: 'Test'

Expand Down Expand Up @@ -36,11 +36,6 @@ jobs:
vmImage: windows-latest
steps:

- task: UseDotNet@2
displayName: 'Install .NET Core $(DotNet3Version)'
inputs:
version: '$(DotNet3Version)'

- task: UseDotNet@2
displayName: 'Install .NET $(DotNet6Version)'
inputs:
Expand All @@ -51,6 +46,12 @@ jobs:
inputs:
version: '$(DotNet7Version)'

- task: UseDotNet@2
displayName: 'Install .NET $(DotNet8Version)'
inputs:
version: '$(DotNet8Version)'
includePreviewVersions: true

- task: DotNetCoreCLI@2
displayName: 'Build Solution'
inputs:
Expand All @@ -66,19 +67,27 @@ jobs:
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET Core 3.0)'
displayName: 'Run Unit Tests (.NET 6.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework netcoreapp3.1 /noautorsp'
testRunTitle: 'Windows .NET Core 3.0'
arguments: '--no-restore --no-build --framework net6.0 /noautorsp'
testRunTitle: 'Windows .NET 6.0'
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET 6.0)'
displayName: 'Run Unit Tests (.NET 7.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net6.0 /noautorsp'
testRunTitle: 'Windows .NET 6.0'
arguments: '--no-restore --no-build --framework net7.0 /noautorsp'
testRunTitle: 'Windows .NET 7.0'
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET 8.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net8.0 /noautorsp'
testRunTitle: 'Windows .NET 8.0'
condition: succeededOrFailed()

- task: PublishBuildArtifacts@1
Expand All @@ -104,6 +113,12 @@ jobs:
inputs:
version: '$(DotNet7Version)'

- task: UseDotNet@2
displayName: 'Install .NET $(DotNet8Version)'
inputs:
version: '$(DotNet8Version)'
includePreviewVersions: true

- task: DotNetCoreCLI@2
displayName: 'dotnet build'
inputs:
Expand All @@ -118,6 +133,22 @@ jobs:
testRunTitle: 'Linux .NET 6.0'
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET 7.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net7.0 /noautorsp'
testRunTitle: 'Linux .NET 7.0'
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET 8.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net8.0 /noautorsp'
testRunTitle: 'Linux .NET 8.0'
condition: succeededOrFailed()

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifacts'
inputs:
Expand All @@ -141,6 +172,12 @@ jobs:
inputs:
version: '$(DotNet7Version)'

- task: UseDotNet@2
displayName: 'Install .NET $(DotNet8Version)'
inputs:
version: '$(DotNet8Version)'
includePreviewVersions: true

- task: DotNetCoreCLI@2
displayName: 'dotnet build'
inputs:
Expand All @@ -155,6 +192,22 @@ jobs:
testRunTitle: 'MacOS .NET 6.0'
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET 7.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net7.0 /noautorsp'
testRunTitle: 'MacOS .NET 7.0'
condition: succeededOrFailed()

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET 8.0)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net8.0 /noautorsp'
testRunTitle: 'MacOS .NET 8.0'
condition: succeededOrFailed()

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifacts'
inputs:
Expand Down
68 changes: 50 additions & 18 deletions src/Artifacts.UnitTests/ArtifactsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public void ArtifactsShouldTrimDestinationFolder()
string outputPath = $"{Path.Combine("bin", "Debug")}{Path.DirectorySeparatorChar}";

ProjectCreator.Templates.ProjectWithArtifacts(
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
outputPath: outputPath,
appendTargetFrameworkToOutputPath: false,
artifactsPath: artifactPaths)
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryGetPropertyValue("DefaultArtifactsSource", out string defaultArtifactsSource)
.TryBuild(out bool result, out BuildOutput buildOutput);
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryGetPropertyValue("DefaultArtifactsSource", out string defaultArtifactsSource)
.TryBuild(out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(buildOutput.GetConsoleLog());

Expand Down Expand Up @@ -108,9 +109,10 @@ public void BackCompatWithRobocopyItems()
DirectoryInfo distribPath = new DirectoryInfo(Path.Combine(TestRootPath, "artifacts"));

ProjectCreator.Templates.ProjectWithArtifacts(
outputPath: outputPath.FullName)
.ItemRobocopy(outputPath.FullName, distribPath.FullName, "*exe *dll *exe.config")
.TryBuild(out bool result, out BuildOutput buildOutput);
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
outputPath: outputPath.FullName)
.ItemRobocopy(outputPath.FullName, distribPath.FullName, "*exe *dll *exe.config")
.TryBuild(out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(buildOutput.GetConsoleLog());

Expand All @@ -133,9 +135,10 @@ public void CanOverrideDefaultArtifacts(string propertyName, string actual, stri
string artifactsPath = Path.Combine(TestRootPath, "artifacts");

ProjectCreator.Templates.ProjectWithArtifacts(
artifactsPath: artifactsPath)
.Property(propertyName, actual)
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems);
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
artifactsPath: artifactsPath)
.Property(propertyName, actual)
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems);

ProjectItem artifactItem = artifactItems.ShouldHaveSingleItem();

Expand Down Expand Up @@ -166,12 +169,13 @@ public void DefaultArtifactsUseOutputPath(bool appendTargetFrameworkToOutputPath
string outputPath = $"{(appendTargetFrameworkToOutputPath ? Path.Combine("bin", "Debug", "net472") : Path.Combine("bin", "Debug"))}{Path.DirectorySeparatorChar}";

ProjectCreator.Templates.ProjectWithArtifacts(
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
outputPath: outputPath,
appendTargetFrameworkToOutputPath: appendTargetFrameworkToOutputPath,
artifactsPath: artifactsPath.FullName)
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryGetPropertyValue("DefaultArtifactsSource", out string defaultArtifactsSource)
.TryBuild(out bool result, out BuildOutput buildOutput);
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryGetPropertyValue("DefaultArtifactsSource", out string defaultArtifactsSource)
.TryBuild(out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(buildOutput.GetConsoleLog());

Expand Down Expand Up @@ -218,11 +222,12 @@ public void DirExcludeUsesCorrectSeparators(string separator)
DirectoryInfo artifactsPath = new DirectoryInfo(Path.Combine(TestRootPath, "artifacts"));

ProjectCreator.Templates.ProjectWithArtifacts(
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
outputPath: outputPath.FullName,
artifactsPath: artifactsPath.FullName)
.Property("DefaultArtifactsDirExclude", string.Join(separator, new[] { "one", "two" }))
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryBuild(out bool result, out BuildOutput buildOutput);
.Property("DefaultArtifactsDirExclude", string.Join(separator, new[] { "one", "two" }))
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryBuild(out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(buildOutput.GetConsoleLog());

Expand All @@ -238,6 +243,31 @@ public void DirExcludeUsesCorrectSeparators(string separator)
ignoreOrder: true);
}

[Fact]
public void DisabledWhenBuiltInArtifactsEnabled()
{
DirectoryInfo projectDirectory = CreateFiles("ClassLibrary1");

ProjectCreator.Create()
.Property("UseArtifactsOutput", bool.TrueString)
.Property("ArtifactsPath", Path.Combine(TestRootPath, "artifacts"))
.Save(Path.Combine(TestRootPath, "Directory.Build.props"));

ProjectCreator.Templates.ProjectWithArtifacts(
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
sdk: "Microsoft.NET.Sdk")
.Save()
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> artifactItems)
.TryGetPropertyValue("DefaultArtifactsSource", out string defaultArtifactsSource)
.TryGetPropertyValue("EnableArtifacts", out string enableArtifacts)
.TryGetPropertyValue("UsingMicrosoftArtifactsSdk", out string usingMicrosoftArtifactsSdk);

artifactItems.ShouldBeEmpty();
defaultArtifactsSource.ShouldBe(string.Empty);
enableArtifacts.ShouldBe(bool.FalseString, StringCompareShould.IgnoreCase);
usingMicrosoftArtifactsSdk.ShouldBe(string.Empty);
}

[Fact]
public void InvalidDestinationFolderShouldLogAnErrorRegardingDestinationFolder()
{
Expand All @@ -259,12 +289,13 @@ public void InvalidDestinationFolderShouldLogAnErrorRegardingDestinationFolder()
string outputPath = $"{Path.Combine("bin", "Debug")}{Path.DirectorySeparatorChar}";

ProjectCreator.Templates.ProjectWithArtifacts(
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
outputPath: outputPath,
appendTargetFrameworkToOutputPath: false,
artifactsPath: artifactPaths)
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> _)
.TryGetPropertyValue("DefaultArtifactsSource", out string _)
.TryBuild(out bool result, out BuildOutput buildOutput);
.TryGetItems("Artifact", out IReadOnlyCollection<ProjectItem> _)
.TryGetPropertyValue("DefaultArtifactsSource", out string _)
.TryBuild(out bool result, out BuildOutput buildOutput);

string consoleLog = buildOutput.GetConsoleLog();
result.ShouldBeFalse(consoleLog);
Expand Down Expand Up @@ -329,6 +360,7 @@ public void UsingSdkLogic(bool appendTargetFrameworkToOutputPath)
DirectoryInfo artifactsPath = new DirectoryInfo(Path.Combine(TestRootPath, "artifacts"));

ProjectCreator.Templates.SdkProjectWithArtifacts(
path: Path.Combine(TestRootPath, "ProjectA.csproj"),
outputPath: appendTargetFrameworkToOutputPath ? Path.Combine("bin", "Debug", "net472") : Path.Combine("bin", "Debug"),
artifactsPath: artifactsPath.FullName,
appendTargetFrameworkToOutputPath: appendTargetFrameworkToOutputPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<TargetFrameworks>net472;netcoreapp3.1;net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AssemblyShader" />
<PackageReference Include="CopyOnWrite" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.NET.Test.Sdk"
ShadeDependencies="NuGet.Frameworks" />
<PackageReference Include="MSBuild.ProjectCreation" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Shouldly" />
Expand Down
Loading

0 comments on commit 809463b

Please sign in to comment.