Skip to content

Commit 6c38b07

Browse files
committed
added integration tests for msbuild tasks
1 parent 8f85e61 commit 6c38b07

8 files changed

+421
-0
lines changed

src/GitVersionTask.Tests/GenerateGitVersionInformationTest.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
using System.Collections.Generic;
12
using System.IO;
3+
using GitVersion.MSBuildTask;
24
using GitVersion.MSBuildTask.Tasks;
35
using GitVersion.OutputVariables;
6+
using GitVersionTask.Tests.Helpers;
7+
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Utilities.ProjectCreation;
49
using NUnit.Framework;
510
using Shouldly;
611

@@ -46,5 +51,87 @@ public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServer()
4651
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1""");
4752
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
4853
}
54+
55+
[Test]
56+
[Category(NoMono)]
57+
public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuild()
58+
{
59+
const string taskName = nameof(GenerateGitVersionInformation);
60+
const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath);
61+
62+
using var result = ExecuteMsBuildExe(project =>
63+
{
64+
AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty);
65+
});
66+
67+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
68+
result.MsBuild.Count.ShouldBeGreaterThan(0);
69+
result.MsBuild.OverallSuccess.ShouldBe(true);
70+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
71+
result.Output.ShouldNotBeNullOrWhiteSpace();
72+
73+
var generatedFilePath = Path.Combine(Path.GetDirectoryName(result.ProjectPath), "GitVersionInformation.g.cs");
74+
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");
75+
76+
var fileContent = File.ReadAllText(generatedFilePath);
77+
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
78+
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2""");
79+
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4""");
80+
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4""");
81+
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1""");
82+
}
83+
84+
[Test]
85+
[Category(NoMono)]
86+
public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildInBuildServer()
87+
{
88+
const string taskName = nameof(GenerateGitVersionInformation);
89+
const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath);
90+
91+
using var result = ExecuteMsBuildExeInBuildServer(project =>
92+
{
93+
AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty);
94+
});
95+
96+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
97+
result.MsBuild.Count.ShouldBeGreaterThan(0);
98+
result.MsBuild.OverallSuccess.ShouldBe(true);
99+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
100+
result.Output.ShouldNotBeNullOrWhiteSpace();
101+
102+
var generatedFilePath = Path.Combine(Path.GetDirectoryName(result.ProjectPath), "GitVersionInformation.g.cs");
103+
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");
104+
105+
var fileContent = File.ReadAllText(generatedFilePath);
106+
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
107+
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""0""");
108+
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""1""");
109+
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1""");
110+
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
111+
}
112+
113+
private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty)
114+
{
115+
var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location;
116+
project.UsingTaskAssemblyFile(taskName, assemblyFileLocation)
117+
.Property("GenerateAssemblyInfo", "false")
118+
.Target(targetToRun, beforeTargets: "CoreCompile;GetAssemblyVersion;GenerateNuspec")
119+
.Task(taskName, parameters: new Dictionary<string, string>
120+
{
121+
{ "SolutionDirectory", "$(MSBuildProjectDirectory)" },
122+
{ "NoFetch", "false" },
123+
{ "NoNormalize", "false" },
124+
{ "ProjectFile", "$(MSBuildProjectFullPath)" },
125+
{ "IntermediateOutputPath", "$(MSBuildProjectDirectory)" },
126+
{ "Language", "$(Language)" },
127+
})
128+
.TaskOutputProperty(outputProperty, outputProperty)
129+
.ItemGroup()
130+
.ItemCompile($"$({outputProperty})")
131+
.ItemInclude("FileWrites", $"$({outputProperty})")
132+
.ItemInclude("_GeneratedCodeFiles", $"$({outputProperty})")
133+
.Target(MsBuildExeFixture.OutputTarget, dependsOnTargets: targetToRun)
134+
.TaskMessage($"{outputProperty}: $({outputProperty})", MessageImportance.High);
135+
}
49136
}
50137
}

src/GitVersionTask.Tests/GetVersionTaskTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
using System.Collections.Generic;
12
using System.Linq;
3+
using GitVersion.MSBuildTask;
24
using GitVersion.MSBuildTask.Tasks;
35
using GitVersion.OutputVariables;
6+
using GitVersionTask.Tests.Helpers;
47
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Utilities.ProjectCreation;
59
using NUnit.Framework;
610
using Shouldly;
711

@@ -54,5 +58,68 @@ public void GetVersionTaskShouldReturnVersionOutputVariablesForBuildServer()
5458
result.Task.MajorMinorPatch.ShouldBe("1.0.1");
5559
result.Task.FullSemVer.ShouldBe("1.0.1+1");
5660
}
61+
62+
[TestCase(nameof(VersionVariables.Major), "1")]
63+
[TestCase(nameof(VersionVariables.Minor), "2")]
64+
[TestCase(nameof(VersionVariables.Patch), "4")]
65+
[TestCase(nameof(VersionVariables.MajorMinorPatch), "1.2.4")]
66+
[TestCase(nameof(VersionVariables.FullSemVer), "1.2.4+1")]
67+
[Category(NoMono)]
68+
public void GetVersionTaskShouldReturnVersionOutputVariablesWhenRunWithMsBuild(string outputProperty, string version)
69+
{
70+
const string taskName = nameof(GetVersion);
71+
72+
using var result = ExecuteMsBuildExe(project =>
73+
{
74+
AddGetVersionTask(project, taskName, taskName, outputProperty);
75+
});
76+
77+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
78+
result.MsBuild.Count.ShouldBeGreaterThan(0);
79+
result.MsBuild.OverallSuccess.ShouldBe(true);
80+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
81+
result.Output.ShouldNotBeNullOrWhiteSpace();
82+
result.Output.ShouldContain($"GitVersion_{outputProperty}: {version}");
83+
}
84+
85+
[TestCase(nameof(VersionVariables.Major), "1")]
86+
[TestCase(nameof(VersionVariables.Minor), "0")]
87+
[TestCase(nameof(VersionVariables.Patch), "1")]
88+
[TestCase(nameof(VersionVariables.MajorMinorPatch), "1.0.1")]
89+
[TestCase(nameof(VersionVariables.FullSemVer), "1.0.1+1")]
90+
[Category(NoMono)]
91+
public void GetVersionTaskShouldReturnVersionOutputVariablesWhenRunWithMsBuildInBuildServer(string outputProperty, string version)
92+
{
93+
const string taskName = nameof(GetVersion);
94+
95+
using var result = ExecuteMsBuildExeInBuildServer(project =>
96+
{
97+
AddGetVersionTask(project, taskName, taskName, outputProperty);
98+
});
99+
100+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
101+
result.MsBuild.Count.ShouldBeGreaterThan(0);
102+
result.MsBuild.OverallSuccess.ShouldBe(true);
103+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
104+
result.Output.ShouldNotBeNullOrWhiteSpace();
105+
result.Output.ShouldContain($"GitVersion_{outputProperty}: {version}");
106+
}
107+
108+
private static void AddGetVersionTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty)
109+
{
110+
var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location;
111+
project.UsingTaskAssemblyFile(taskName, assemblyFileLocation)
112+
.Property("GenerateAssemblyInfo", "false")
113+
.Target(targetToRun, beforeTargets: "CoreCompile;GetAssemblyVersion;GenerateNuspec")
114+
.Task(taskName, parameters: new Dictionary<string, string>
115+
{
116+
{ "SolutionDirectory", "$(MSBuildProjectDirectory)" },
117+
{ "NoFetch", "false" },
118+
{ "NoNormalize", "false" }
119+
})
120+
.TaskOutputProperty(outputProperty, $"GitVersion_{outputProperty}")
121+
.Target(MsBuildExeFixture.OutputTarget, dependsOnTargets: targetToRun)
122+
.TaskMessage($"GitVersion_{outputProperty}: $(GitVersion_{outputProperty})", MessageImportance.High);
123+
}
57124
}
58125
}

src/GitVersionTask.Tests/GitVersionTask.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
<Import Project="..\test.props" />
88
<ItemGroup>
9+
<PackageReference Include="Buildalyzer" Version="2.5.1" />
910
<PackageReference Include="LibGit2Sharp" Version="$(PackageVersion_LibGit2Sharp)" />
1011
<PackageReference Include="Microsoft.Build" Version="$(PackageVersion_MsBuild)" />
1112
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(PackageVersion_MicrosoftExtensions)" />
13+
<PackageReference Include="MSBuild.ProjectCreation" Version="1.3.7" />
1214
</ItemGroup>
1315
<ItemGroup>
1416
<Compile Update="MsBuildExecutionResult.cs">
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Buildalyzer;
5+
using Buildalyzer.Environment;
6+
using GitTools.Testing;
7+
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Logging;
9+
using Microsoft.Build.Utilities.ProjectCreation;
10+
using NUnit.Framework.Internal;
11+
using StringWriter = System.IO.StringWriter;
12+
13+
namespace GitVersionTask.Tests.Helpers
14+
{
15+
public class MsBuildExeFixture
16+
{
17+
private readonly RepositoryFixtureBase fixture;
18+
private KeyValuePair<string, string>[] environmentVariables;
19+
20+
public void WithEnv(params KeyValuePair<string, string>[] envs)
21+
{
22+
environmentVariables = envs;
23+
}
24+
25+
public const string OutputTarget = "GitVersionOutput";
26+
27+
private readonly AnalyzerManager manager = new AnalyzerManager();
28+
private readonly string ProjectPath;
29+
30+
public MsBuildExeFixture(RepositoryFixtureBase fixture, string workingDirectory = "")
31+
{
32+
this.fixture = fixture;
33+
ProjectPath = Path.Combine(workingDirectory, "app.csproj");
34+
}
35+
36+
public MsBuildExeFixtureResult Execute()
37+
{
38+
var analyzer = manager.GetProject(ProjectPath);
39+
40+
var output = new StringWriter();
41+
analyzer.AddBuildLogger(new ConsoleLogger(LoggerVerbosity.Normal, output.Write, null, null));
42+
43+
var environmentOptions = new EnvironmentOptions { DesignTime = false };
44+
environmentOptions.TargetsToBuild.Clear();
45+
environmentOptions.TargetsToBuild.Add(OutputTarget);
46+
47+
if (environmentVariables != null)
48+
{
49+
foreach (var pair in environmentVariables)
50+
{
51+
analyzer.SetEnvironmentVariable(pair.Key, pair.Value);
52+
}
53+
}
54+
55+
var results = analyzer.Build(environmentOptions);
56+
57+
return new MsBuildExeFixtureResult(fixture)
58+
{
59+
ProjectPath = ProjectPath,
60+
Output = output.ToString(),
61+
MsBuild = results
62+
};
63+
}
64+
65+
public void CreateTestProject(Action<ProjectCreator> extendProject)
66+
{
67+
var project = RuntimeFramework.CurrentFramework.Runtime switch
68+
{
69+
RuntimeType.NetCore => ProjectCreator.Templates.SdkCsproj(ProjectPath),
70+
RuntimeType.Net => ProjectCreator.Templates.LegacyCsproj(ProjectPath, defaultTargets: null, targetFrameworkVersion: "v4.7.2", toolsVersion: "15.0"),
71+
_ => null
72+
};
73+
74+
if (project == null) return;
75+
76+
extendProject(project);
77+
project.Save();
78+
}
79+
}
80+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Buildalyzer;
3+
using GitTools.Testing;
4+
5+
namespace GitVersionTask.Tests.Helpers
6+
{
7+
public class MsBuildExeFixtureResult : IDisposable
8+
{
9+
private readonly RepositoryFixtureBase fixture;
10+
11+
public MsBuildExeFixtureResult(RepositoryFixtureBase fixture)
12+
{
13+
this.fixture = fixture;
14+
}
15+
public AnalyzerResults MsBuild { get; set; }
16+
public string Output { get; set; }
17+
public string ProjectPath { get; set; }
18+
public void Dispose()
19+
{
20+
fixture.Dispose();
21+
}
22+
}
23+
}

src/GitVersionTask.Tests/TestTaskBase.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using GitTools.Testing;
@@ -6,6 +7,7 @@
67
using GitVersionCore.Tests.Helpers;
78
using GitVersionTask.Tests.Helpers;
89
using LibGit2Sharp;
10+
using Microsoft.Build.Utilities.ProjectCreation;
911

1012
namespace GitVersionTask.Tests
1113
{
@@ -26,6 +28,17 @@ protected static MsBuildTaskFixtureResult<T> ExecuteMsBuildTask<T>(T task) where
2628
return msbuildFixture.Execute(task);
2729
}
2830

31+
protected static MsBuildExeFixtureResult ExecuteMsBuildExe(Action<ProjectCreator> extendProject)
32+
{
33+
var fixture = CreateLocalRepositoryFixture();
34+
35+
var msbuildFixture = new MsBuildExeFixture(fixture, fixture.RepositoryPath);
36+
37+
msbuildFixture.CreateTestProject(extendProject);
38+
39+
return msbuildFixture.Execute();
40+
}
41+
2942
protected static MsBuildTaskFixtureResult<T> ExecuteMsBuildTaskInBuildServer<T>(T task) where T : GitVersionTaskBase
3043
{
3144
var fixture = CreateRemoteRepositoryFixture();
@@ -36,6 +49,18 @@ protected static MsBuildTaskFixtureResult<T> ExecuteMsBuildTaskInBuildServer<T>(
3649
return msbuildFixture.Execute(task);
3750
}
3851

52+
protected static MsBuildExeFixtureResult ExecuteMsBuildExeInBuildServer(Action<ProjectCreator> extendProject)
53+
{
54+
var fixture = CreateRemoteRepositoryFixture();
55+
56+
var msbuildFixture = new MsBuildExeFixture(fixture, fixture.LocalRepositoryFixture.RepositoryPath);
57+
58+
msbuildFixture.CreateTestProject(extendProject);
59+
msbuildFixture.WithEnv(env.ToArray());
60+
61+
return msbuildFixture.Execute();
62+
}
63+
3964
private static EmptyRepositoryFixture CreateLocalRepositoryFixture()
4065
{
4166
var fixture = new EmptyRepositoryFixture();

0 commit comments

Comments
 (0)