From 0ae2c0bb1b5450696f15074efd7b04770ea41aef Mon Sep 17 00:00:00 2001 From: Artur Stolear Date: Tue, 11 Apr 2023 16:48:13 +0200 Subject: [PATCH] #2595 - update unit tests --- .../Core/Abstractions/IRepositoryStore.cs | 4 +- .../NextVersionCalculator.cs | 3 - .../Helpers/MsBuildExeFixture.cs | 5 +- .../GenerateGitVersionInformationTest.cs | 174 +++++++++++------- .../Tasks/GetVersionTaskTests.cs | 3 +- .../Tasks/TestTaskBase.cs | 12 +- .../Tasks/UpdateAssemblyInfoTaskTest.cs | 107 +++++++---- .../Tasks/WriteVersionInfoTest.cs | 3 +- src/GitVersion.MsBuild/Helpers/FileHelper.cs | 8 + 9 files changed, 196 insertions(+), 123 deletions(-) diff --git a/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs b/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs index 06c9edc605..480bd3d2c1 100644 --- a/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs +++ b/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs @@ -45,9 +45,7 @@ public interface IRepositoryStore IReadOnlyList GetTaggedSemanticVersions(string? labelPrefix, SemanticVersionFormat format); - IReadOnlyList GetTaggedSemanticVersionsOnBranch( - IBranch branch, string? labelPrefix, SemanticVersionFormat format - ); + IReadOnlyList GetTaggedSemanticVersionsOnBranch(IBranch branch, string? labelPrefix, SemanticVersionFormat format); bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit firstMatchingCommit); diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index 6f259d3ce0..cd8a0a8378 100644 --- a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs @@ -10,7 +10,6 @@ internal class NextVersionCalculator : INextVersionCalculator { private readonly ILog log; private readonly IMainlineVersionCalculator mainlineVersionCalculator; - private readonly ITrunkBasedVersionCalculator trunkBasedVersionCalculator; private readonly IContinuousDeploymentVersionCalculator continuousDeploymentVersionCalculator; private readonly IContinuousDeliveryVersionCalculator continuousDeliveryVersionCalculator; private readonly IManualDeploymentVersionCalculator manualDeploymentVersionCalculator; @@ -24,7 +23,6 @@ internal class NextVersionCalculator : INextVersionCalculator public NextVersionCalculator(ILog log, Lazy versionContext, IMainlineVersionCalculator mainlineVersionCalculator, - ITrunkBasedVersionCalculator trunkBasedVersionCalculator, IContinuousDeploymentVersionCalculator continuousDeploymentVersionCalculator, IContinuousDeliveryVersionCalculator continuousDeliveryVersionCalculator, IManualDeploymentVersionCalculator manualDeploymentVersionCalculator, @@ -35,7 +33,6 @@ public NextVersionCalculator(ILog log, this.log = log.NotNull(); this.versionContext = versionContext.NotNull(); this.mainlineVersionCalculator = mainlineVersionCalculator.NotNull(); - this.trunkBasedVersionCalculator = trunkBasedVersionCalculator.NotNull(); this.continuousDeploymentVersionCalculator = continuousDeploymentVersionCalculator.NotNull(); this.continuousDeliveryVersionCalculator = continuousDeliveryVersionCalculator.NotNull(); this.manualDeploymentVersionCalculator = manualDeploymentVersionCalculator.NotNull(); diff --git a/src/GitVersion.MsBuild.Tests/Helpers/MsBuildExeFixture.cs b/src/GitVersion.MsBuild.Tests/Helpers/MsBuildExeFixture.cs index 0f792d6dd6..ead5310c34 100644 --- a/src/GitVersion.MsBuild.Tests/Helpers/MsBuildExeFixture.cs +++ b/src/GitVersion.MsBuild.Tests/Helpers/MsBuildExeFixture.cs @@ -20,10 +20,11 @@ public class MsBuildExeFixture private readonly AnalyzerManager manager = new(); private readonly string ProjectPath; - public MsBuildExeFixture(RepositoryFixtureBase fixture, string workingDirectory = "") + public MsBuildExeFixture(RepositoryFixtureBase fixture, string workingDirectory = "", string language = "C#") { + var projectExtension = FileHelper.GetProjectExtension(language); this.fixture = fixture; - this.ProjectPath = PathHelper.Combine(workingDirectory, "app.csproj"); + this.ProjectPath = PathHelper.Combine(workingDirectory, $"app.{projectExtension}"); var versionFile = PathHelper.Combine(workingDirectory, "gitversion.json"); diff --git a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs index 1445f052aa..3c6a143d51 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs @@ -10,51 +10,65 @@ namespace GitVersion.MsBuild.Tests.Tasks; [TestFixture] public class GenerateGitVersionInformationTest : TestTaskBase { - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFile() + private const string regexPattern = @".*{0}.*=.*""{1}"".*"; + private static readonly object[] Languages = { - var task = new GenerateGitVersionInformation(); + new object[] { "C#" }, + new object[] { "F#" }, + new object[] { "VB" }, + }; + + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFile(string language) + { + var extension = FileHelper.GetFileExtension(language); + var task = new GenerateGitVersionInformation { Language = language }; using var result = ExecuteMsBuildTask(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + result.Task.GitVersionInformationFilePath.ShouldMatch($@"GitVersionInformation.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""2"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.2.4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.2.4-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServer() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServer(string language) { - var task = new GenerateGitVersionInformation(); + var extension = FileHelper.GetFileExtension(language); + var task = new GenerateGitVersionInformation { Language = language }; using var result = ExecuteMsBuildTaskInAzurePipeline(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + result.Task.GitVersionInformationFilePath.ShouldMatch($@"GitVersionInformation.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""0"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.0.1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.0.1-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "0")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.0.1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.0.1-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuild() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuild(string language) { const string taskName = nameof(GenerateGitVersionInformation); const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); + var extension = FileHelper.GetFileExtension(language); - using var result = ExecuteMsBuildExe(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty)); + using var result = ExecuteMsBuildExe(project => + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language), language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -62,24 +76,26 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuild( result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), "GitVersionInformation.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), $"GitVersionInformation.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""2"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.2.4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.2.4-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildInBuildServer() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildInBuildServer(string language) { const string taskName = nameof(GenerateGitVersionInformation); const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); + var extension = FileHelper.GetFileExtension(language); - using var result = ExecuteMsBuildExeInAzurePipeline(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty)); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language), language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -87,63 +103,72 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildI result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), "GitVersionInformation.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), $"GitVersionInformation.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""0"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.0.1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.0.1-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "0")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.0.1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.0.1-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist(string language) { - var task = new GenerateGitVersionInformation { IntermediateOutputPath = Guid.NewGuid().ToString("N") }; + var extension = FileHelper.GetFileExtension(language); + var task = new GenerateGitVersionInformation { Language = language, IntermediateOutputPath = Guid.NewGuid().ToString("N") }; using var result = ExecuteMsBuildTask(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + result.Task.GitVersionInformationFilePath.ShouldMatch($@"GitVersionInformation.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""2"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.2.4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.2.4-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServerWhenIntermediateOutputPathDoesNotExist() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServerWhenIntermediateOutputPathDoesNotExist(string language) { - var task = new GenerateGitVersionInformation { IntermediateOutputPath = Guid.NewGuid().ToString("N") }; + var extension = FileHelper.GetFileExtension(language); + var task = new GenerateGitVersionInformation { Language = language, IntermediateOutputPath = Guid.NewGuid().ToString("N") }; using var result = ExecuteMsBuildTaskInAzurePipeline(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + result.Task.GitVersionInformationFilePath.ShouldMatch($@"GitVersionInformation.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""0"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.0.1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.0.1-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "0")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.0.1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.0.1-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist(string language) { const string taskName = nameof(GenerateGitVersionInformation); const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); var randDir = Guid.NewGuid().ToString("N"); - using var result = ExecuteMsBuildExe(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir))); + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExe(project => + { + var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath); + }, language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -151,25 +176,30 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, $"GitVersionInformation.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""2"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.2.4"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.2.4-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); } - [Test] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer() + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer(string language) { const string taskName = nameof(GenerateGitVersionInformation); const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); var randDir = Guid.NewGuid().ToString("N"); - using var result = ExecuteMsBuildExeInAzurePipeline(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir))); + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + { + var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath); + }, language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -177,30 +207,34 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, $"GitVersionInformation.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Major)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Minor)} = ""0"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.Patch)} = ""1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.MajorMinorPatch)} = ""1.0.1"""); - fileContent.ShouldContain($@"{nameof(GitVersionVariables.FullSemVer)} = ""1.0.1-1"""); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "0")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.0.1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.0.1-1")); } - private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)") + private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, + string outputProperty, string language, + string intermediateOutputPath = "$(MSBuildProjectDirectory)") { var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location; project.UsingTaskAssemblyFile(taskName, assemblyFileLocation) + .Property("ManagePackageVersionsCentrally", "false") .Property("GenerateAssemblyInfo", "false") + .Property("Language", language) .Target(targetToRun, beforeTargets: "CoreCompile;GetAssemblyVersion;GenerateNuspec") .Task(taskName, parameters: new Dictionary { { "SolutionDirectory", "$(MSBuildProjectDirectory)" }, { "VersionFile", "$(MSBuildProjectDirectory)/gitversion.json" }, { "ProjectFile", "$(MSBuildProjectFullPath)" }, + { "Language", "$(Language)" }, { "IntermediateOutputPath", intermediateOutputPath }, - { "Language", "$(Language)" } }) .TaskOutputProperty(outputProperty, outputProperty) .ItemGroup() diff --git a/src/GitVersion.MsBuild.Tests/Tasks/GetVersionTaskTests.cs b/src/GitVersion.MsBuild.Tests/Tasks/GetVersionTaskTests.cs index 974e010f54..fe86b8dd46 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/GetVersionTaskTests.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/GetVersionTaskTests.cs @@ -82,7 +82,8 @@ public void GetVersionTaskShouldReturnVersionOutputVariablesWhenRunWithMsBuildIn { const string taskName = nameof(GetVersion); - using var result = ExecuteMsBuildExeInAzurePipeline(project => AddGetVersionTask(project, taskName, taskName, outputProperty)); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + AddGetVersionTask(project, taskName, taskName, outputProperty)); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); diff --git a/src/GitVersion.MsBuild.Tests/Tasks/TestTaskBase.cs b/src/GitVersion.MsBuild.Tests/Tasks/TestTaskBase.cs index 9b5e7b4e91..c7444791a2 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/TestTaskBase.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/TestTaskBase.cs @@ -29,11 +29,11 @@ protected static MsBuildTaskFixtureResult ExecuteMsBuildTask(T task) where return result; } - protected static MsBuildExeFixtureResult ExecuteMsBuildExe(Action extendProject) + protected static MsBuildExeFixtureResult ExecuteMsBuildExe(Action extendProject, string language = "C#") { var fixture = CreateLocalRepositoryFixture(); - var msbuildFixture = new MsBuildExeFixture(fixture, fixture.RepositoryPath); + var msbuildFixture = new MsBuildExeFixture(fixture, fixture.RepositoryPath, language); msbuildFixture.CreateTestProject(extendProject); @@ -51,7 +51,7 @@ protected static MsBuildTaskFixtureResult ExecuteMsBuildTaskInAzurePipeline>(env.ToArray()); if (buildNumber != null) { - environmentVariables.Add(new KeyValuePair("BUILD_BUILDNUMBER", buildNumber)); + environmentVariables.Add(new("BUILD_BUILDNUMBER", buildNumber)); } msbuildFixture.WithEnv(environmentVariables.ToArray()); if (configurationText != null) @@ -81,11 +81,11 @@ protected static MsBuildTaskFixtureResult ExecuteMsBuildTaskInGitHubActions extendProject) + protected static MsBuildExeFixtureResult ExecuteMsBuildExeInAzurePipeline(Action extendProject, string language = "C#") { var fixture = CreateRemoteRepositoryFixture(); - var msbuildFixture = new MsBuildExeFixture(fixture, fixture.LocalRepositoryFixture.RepositoryPath); + var msbuildFixture = new MsBuildExeFixture(fixture, fixture.LocalRepositoryFixture.RepositoryPath, language); msbuildFixture.CreateTestProject(extendProject); msbuildFixture.WithEnv(env.ToArray()); @@ -118,7 +118,7 @@ private static RemoteRepositoryFixture CreateRemoteRepositoryFixture() fixture.Repository.MakeACommit(); fixture.Repository.CreateBranch("develop"); - Commands.Fetch(fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty(), new FetchOptions(), null); + Commands.Fetch(fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty(), new(), null); Commands.Checkout(fixture.LocalRepositoryFixture.Repository, fixture.Repository.Head.Tip); fixture.LocalRepositoryFixture.Repository.Branches.Remove(MainBranch); fixture.InitializeRepo(); diff --git a/src/GitVersion.MsBuild.Tests/Tasks/UpdateAssemblyInfoTaskTest.cs b/src/GitVersion.MsBuild.Tests/Tasks/UpdateAssemblyInfoTaskTest.cs index 8a54fd9910..a4878030d1 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/UpdateAssemblyInfoTaskTest.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/UpdateAssemblyInfoTaskTest.cs @@ -9,43 +9,56 @@ namespace GitVersion.MsBuild.Tests.Tasks; [TestFixture] public class UpdateAssemblyInfoTaskTest : TestTaskBase { - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFile() + private static readonly object[] Languages = { - var task = new UpdateAssemblyInfo(); + new object[] { "C#" }, + new object[] { "F#" }, + new object[] { "VB" }, + }; + + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFile(string language) + { + var extension = FileHelper.GetFileExtension(language); + var task = new UpdateAssemblyInfo { Language = language }; using var result = ExecuteMsBuildTask(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull(); + result.Task.AssemblyInfoTempFilePath.ShouldMatch($@"AssemblyInfo.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.2.4.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileInBuildServer() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileInBuildServer(string language) { - var task = new UpdateAssemblyInfo(); + var extension = FileHelper.GetFileExtension(language); + var task = new UpdateAssemblyInfo { Language = language }; using var result = ExecuteMsBuildTaskInAzurePipeline(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull(); + result.Task.AssemblyInfoTempFilePath.ShouldMatch($@"AssemblyInfo.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.0.1.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuild() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuild(string language) { const string taskName = nameof(UpdateAssemblyInfo); const string outputProperty = nameof(UpdateAssemblyInfo.AssemblyInfoTempFilePath); - using var result = ExecuteMsBuildExe(project => AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty)); + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExe(project => + AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, language), language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -53,20 +66,22 @@ public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuild() result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), "AssemblyInfo.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), $"AssemblyInfo.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.2.4.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildInBuildServer() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildInBuildServer(string language) { const string taskName = nameof(UpdateAssemblyInfo); const string outputProperty = nameof(UpdateAssemblyInfo.AssemblyInfoTempFilePath); - using var result = ExecuteMsBuildExeInAzurePipeline(project => AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty)); + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, language), language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -74,51 +89,60 @@ public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildInBuildServe result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), "AssemblyInfo.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), $"AssemblyInfo.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.0.1.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist(string language) { - var task = new UpdateAssemblyInfo { IntermediateOutputPath = Guid.NewGuid().ToString("N") }; + var extension = FileHelper.GetFileExtension(language); + var task = new UpdateAssemblyInfo { Language = language, IntermediateOutputPath = Guid.NewGuid().ToString("N") }; using var result = ExecuteMsBuildTask(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull(); + result.Task.AssemblyInfoTempFilePath.ShouldMatch($@"AssemblyInfo.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.2.4.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExistInBuildServer() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExistInBuildServer(string language) { - var task = new UpdateAssemblyInfo { IntermediateOutputPath = Guid.NewGuid().ToString("N") }; + var extension = FileHelper.GetFileExtension(language); + var task = new UpdateAssemblyInfo { Language = language, IntermediateOutputPath = Guid.NewGuid().ToString("N") }; using var result = ExecuteMsBuildTaskInAzurePipeline(task); result.Success.ShouldBe(true); result.Errors.ShouldBe(0); result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull(); + result.Task.AssemblyInfoTempFilePath.ShouldMatch($@"AssemblyInfo.*\.g\.{extension}"); var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.0.1.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist(string language) { const string taskName = nameof(UpdateAssemblyInfo); const string outputProperty = nameof(UpdateAssemblyInfo.AssemblyInfoTempFilePath); var randDir = Guid.NewGuid().ToString("N"); - using var result = ExecuteMsBuildExe(project => AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir))); + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExe(project => + { + var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); + AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath); + }, language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -126,21 +150,26 @@ public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermedi result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "AssemblyInfo.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, $"AssemblyInfo.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.2.4.0"")"); } - [Test] - public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer() + [TestCaseSource(nameof(Languages))] + public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer(string language) { const string taskName = nameof(UpdateAssemblyInfo); const string outputProperty = nameof(UpdateAssemblyInfo.AssemblyInfoTempFilePath); var randDir = Guid.NewGuid().ToString("N"); - using var result = ExecuteMsBuildExeInAzurePipeline(project => AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir))); + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + { + var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); + AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath); + }, language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); @@ -148,18 +177,22 @@ public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermedi result.MsBuild.ShouldAllBe(x => x.Succeeded); result.Output.ShouldNotBeNullOrWhiteSpace(); - var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "AssemblyInfo.g.cs"); + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, $"AssemblyInfo.g.{extension}"); result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); var fileContent = File.ReadAllText(generatedFilePath); - fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]"); + fileContent.ShouldContain(@"assembly: AssemblyVersion(""1.0.1.0"")"); } - private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)") + private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string targetToRun, string taskName, + string outputProperty, string language, + string intermediateOutputPath = "$(MSBuildProjectDirectory)") { var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location; project.UsingTaskAssemblyFile(taskName, assemblyFileLocation) + .Property("ManagePackageVersionsCentrally", "false") .Property("GenerateAssemblyInfo", "false") + .Property("Language", language) .Target(targetToRun, beforeTargets: "CoreCompile;GetAssemblyVersion;GenerateNuspec") .Task(taskName, parameters: new Dictionary { diff --git a/src/GitVersion.MsBuild.Tests/Tasks/WriteVersionInfoTest.cs b/src/GitVersion.MsBuild.Tests/Tasks/WriteVersionInfoTest.cs index df0632d1b9..c68522ad16 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/WriteVersionInfoTest.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/WriteVersionInfoTest.cs @@ -104,7 +104,8 @@ public void WriteVersionInfoTaskShouldLogOutputVariablesToBuildOutputWhenRunWith { const string taskName = nameof(WriteVersionInfoToBuildLog); - using var result = ExecuteMsBuildExeInAzurePipeline(project => AddWriteVersionInfoToBuildLogTask(project, taskName, taskName)); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + AddWriteVersionInfoToBuildLogTask(project, taskName, taskName)); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); result.MsBuild.Count.ShouldBeGreaterThan(0); diff --git a/src/GitVersion.MsBuild/Helpers/FileHelper.cs b/src/GitVersion.MsBuild/Helpers/FileHelper.cs index 94cab42142..306fdcc1e9 100644 --- a/src/GitVersion.MsBuild/Helpers/FileHelper.cs +++ b/src/GitVersion.MsBuild/Helpers/FileHelper.cs @@ -51,6 +51,14 @@ public static void DeleteTempFiles() _ => throw new ArgumentException($"Unknown language detected: '{language}'") }; + public static string GetProjectExtension(string language) => language switch + { + "C#" => "csproj", + "F#" => "fsproj", + "VB" => "vbproj", + _ => throw new ArgumentException($"Unknown language detected: '{language}'") + }; + public static void CheckForInvalidFiles(IEnumerable compileFiles, string projectFile) { if (GetInvalidFiles(compileFiles, projectFile).FirstOrDefault() is { } invalidCompileFile)