diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateAssemblyInfo.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateAssemblyInfo.targets index e3205af2a077..e9804ff16bb7 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateAssemblyInfo.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateAssemblyInfo.targets @@ -34,6 +34,7 @@ Copyright (c) .NET Foundation. All rights reserved. true true true + true + <_InformationalVersionContainsPlus>false + <_InformationalVersionContainsPlus Condition="$(InformationalVersion.Contains('+'))">true + + $(InformationalVersion)+$(SourceRevisionId) + $(InformationalVersion).$(SourceRevisionId) + + + + DependsOnTargets="GetAssemblyVersion;AddSourceRevisionToInformationalVersion"> <_Parameter1>$(Company) diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs index a6391afd6717..3dd917c6c27d 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs @@ -11,6 +11,8 @@ using FluentAssertions; using System.Runtime.InteropServices; using Xunit.Abstractions; +using Microsoft.NET.TestFramework.ProjectConstruction; +using System.Xml.Linq; namespace Microsoft.NET.Build.Tests { @@ -89,6 +91,174 @@ public void It_respects_opt_outs(string attributeToOptOut) actualInfo.Should().Equal(expectedInfo); } + [Fact] + public void It_does_not_include_source_revision_id_if_initialize_source_control_target_not_available() + { + TestProject testProject = new TestProject() + { + Name = "ProjectWithSourceRevisionId", + IsSdkProject = true, + TargetFrameworks = "netcoreapp2.0", + }; + + var testAsset = _testAssetsManager.CreateTestProject(testProject); + + testAsset.Restore(Log, testProject.Name); + + var command = new GetValuesCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name), testProject.TargetFrameworks, valueName: "InformationalVersion"); + command.Execute().Should().Pass(); + + command.GetValues().ShouldBeEquivalentTo(new[] { "1.0.0" }); + } + + [Fact] + public void It_does_not_include_source_revision_id_if_source_revision_id_not_set() + { + TestProject testProject = new TestProject() + { + Name = "ProjectWithSourceRevisionId", + IsSdkProject = true, + TargetFrameworks = "netcoreapp2.0", + }; + + var testAsset = _testAssetsManager.CreateTestProject(testProject) + .WithProjectChanges((path, project) => + { + var ns = project.Root.Name.Namespace; + + project.Root.Add( + new XElement(ns + "Target", + new XAttribute("Name", "InitializeSourceControlInformation"), + new XElement(ns + "PropertyGroup", + new XElement("SourceRevisionId", "")))); + + project.Root.Add( + new XElement(ns + "PropertyGroup", + new XElement("SourceControlInformationFeatureSupported", "true"))); + }); + + testAsset.Restore(Log, testProject.Name); + + var command = new GetValuesCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name), testProject.TargetFrameworks, valueName: "InformationalVersion"); + command.Execute().Should().Pass(); + + command.GetValues().ShouldBeEquivalentTo(new[] { "1.0.0" }); + } + + [Fact] + public void It_does_not_include_source_revision_id_if_disabled() + { + TestProject testProject = new TestProject() + { + Name = "ProjectWithSourceRevisionId", + IsSdkProject = true, + TargetFrameworks = "netcoreapp2.0", + }; + + var testAsset = _testAssetsManager.CreateTestProject(testProject) + .WithProjectChanges((path, project) => + { + var ns = project.Root.Name.Namespace; + + project.Root.Add( + new XElement(ns + "Target", + new XAttribute("Name", "InitializeSourceControlInformation"), + new XElement(ns + "PropertyGroup", + new XElement("SourceRevisionId", "xyz")))); + + project.Root.Add( + new XElement(ns + "PropertyGroup", + new XElement("SourceControlInformationFeatureSupported", "true"), + new XElement("IncludeSourceRevisionInInformationalVersion", "false"))); + }); + + testAsset.Restore(Log, testProject.Name); + + var command = new GetValuesCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name), testProject.TargetFrameworks, valueName: "InformationalVersion"); + command.Execute().Should().Pass(); + + command.GetValues().ShouldBeEquivalentTo(new[] { "1.0.0" }); + } + + [Fact] + public void It_includes_source_revision_id_if_available__version_without_plus() + { + TestProject testProject = new TestProject() + { + Name = "ProjectWithSourceRevisionId", + IsSdkProject = true, + TargetFrameworks = "netcoreapp2.0", + }; + + var testAsset = _testAssetsManager.CreateTestProject(testProject) + .WithProjectChanges((path, project) => + { + var ns = project.Root.Name.Namespace; + + project.Root.Add( + new XElement(ns + "Target", + new XAttribute("Name", "_SetSourceRevisionId"), + new XAttribute("BeforeTargets", "InitializeSourceControlInformation"), + new XElement(ns + "PropertyGroup", + new XElement("SourceRevisionId", "xyz")))); + + project.Root.Add( + new XElement(ns + "Target", + new XAttribute("Name", "InitializeSourceControlInformation"))); + + project.Root.Add( + new XElement(ns + "PropertyGroup", + new XElement("SourceControlInformationFeatureSupported", "true"))); + }); + + testAsset.Restore(Log, testProject.Name); + + var command = new GetValuesCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name), testProject.TargetFrameworks, valueName: "InformationalVersion"); + command.Execute().Should().Pass(); + + command.GetValues().ShouldBeEquivalentTo(new[] { "1.0.0+xyz" }); + } + + [Fact] + public void It_includes_source_revision_id_if_available__version_with_plus() + { + TestProject testProject = new TestProject() + { + Name = "ProjectWithSourceRevisionId", + IsSdkProject = true, + TargetFrameworks = "netcoreapp2.0", + }; + + var testAsset = _testAssetsManager.CreateTestProject(testProject) + .WithProjectChanges((path, project) => + { + var ns = project.Root.Name.Namespace; + + project.Root.Add( + new XElement(ns + "Target", + new XAttribute("Name", "_SetSourceRevisionId"), + new XAttribute("BeforeTargets", "InitializeSourceControlInformation"), + new XElement(ns + "PropertyGroup", + new XElement("SourceRevisionId", "xyz")))); + + project.Root.Add( + new XElement(ns + "Target", + new XAttribute("Name", "InitializeSourceControlInformation"))); + + project.Root.Add( + new XElement(ns + "PropertyGroup", + new XElement("SourceControlInformationFeatureSupported", "true"), + new XElement("InformationalVersion", "1.2.3+abc"))); + }); + + testAsset.Restore(Log, testProject.Name); + + var command = new GetValuesCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name), testProject.TargetFrameworks, valueName: "InformationalVersion"); + command.Execute().Should().Pass(); + + command.GetValues().ShouldBeEquivalentTo(new[] { "1.2.3+abc.xyz" }); + } + [WindowsOnlyTheory] [InlineData("netcoreapp1.1")] [InlineData("net45")] diff --git a/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs b/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs index 8c77c073a256..dabfa8645454 100644 --- a/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -68,7 +68,7 @@ public TestAsset CreateTestProject( var project = projectStack.Pop(); if (!createdProjects.Contains(project)) { - project.Create(testAsset, ProjectsRoot); + project.Create(testAsset, ProjectsRoot); createdProjects.Add(project); foreach (var referencedProject in project.ReferencedProjects)