Skip to content

Commit 8386c6d

Browse files
Jeff PeirsonJeff Peirson
authored andcommitted
Ensuring intermediate output path exists
Fixes #2815
1 parent 0501616 commit 8386c6d

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,101 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildI
104104
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
105105
}
106106

107-
private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty)
107+
[Test]
108+
public void GenerateGitVersionInformationTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist()
109+
{
110+
var task = new GenerateGitVersionInformation { IntermediateOutputPath = Guid.NewGuid().ToString("N") };
111+
112+
using var result = ExecuteMsBuildTask(task);
113+
114+
result.Success.ShouldBe(true);
115+
result.Errors.ShouldBe(0);
116+
result.Task.GitVersionInformationFilePath.ShouldNotBeNull();
117+
118+
var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath);
119+
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
120+
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2""");
121+
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4""");
122+
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4""");
123+
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1""");
124+
}
125+
126+
[Test]
127+
public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServerWhenIntermediateOutputPathDoesNotExist()
128+
{
129+
var task = new GenerateGitVersionInformation { IntermediateOutputPath = Guid.NewGuid().ToString("N") };
130+
131+
using var result = ExecuteMsBuildTaskInAzurePipeline(task);
132+
133+
result.Success.ShouldBe(true);
134+
result.Errors.ShouldBe(0);
135+
result.Task.GitVersionInformationFilePath.ShouldNotBeNull();
136+
137+
var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath);
138+
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
139+
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""0""");
140+
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""1""");
141+
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1""");
142+
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
143+
}
144+
145+
[Test]
146+
[Category(NoNet48)]
147+
[Category(NoMono)]
148+
public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist()
149+
{
150+
const string taskName = nameof(GenerateGitVersionInformation);
151+
const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath);
152+
var randDir = Guid.NewGuid().ToString("N");
153+
154+
using var result = ExecuteMsBuildExe(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir)));
155+
156+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
157+
result.MsBuild.Count.ShouldBeGreaterThan(0);
158+
result.MsBuild.OverallSuccess.ShouldBe(true);
159+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
160+
result.Output.ShouldNotBeNullOrWhiteSpace();
161+
162+
var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs");
163+
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");
164+
165+
var fileContent = File.ReadAllText(generatedFilePath);
166+
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
167+
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2""");
168+
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4""");
169+
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4""");
170+
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1""");
171+
}
172+
173+
[Test]
174+
[Category(NoNet48)]
175+
[Category(NoMono)]
176+
public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer()
177+
{
178+
const string taskName = nameof(GenerateGitVersionInformation);
179+
const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath);
180+
var randDir = Guid.NewGuid().ToString("N");
181+
182+
using var result = ExecuteMsBuildExeInAzurePipeline(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir)));
183+
184+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
185+
result.MsBuild.Count.ShouldBeGreaterThan(0);
186+
result.MsBuild.OverallSuccess.ShouldBe(true);
187+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
188+
result.Output.ShouldNotBeNullOrWhiteSpace();
189+
190+
var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs");
191+
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");
192+
193+
var fileContent = File.ReadAllText(generatedFilePath);
194+
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
195+
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""0""");
196+
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""1""");
197+
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1""");
198+
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
199+
}
200+
201+
private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)")
108202
{
109203
var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location;
110204
project.UsingTaskAssemblyFile(taskName, assemblyFileLocation)
@@ -115,7 +209,7 @@ private static void AddGenerateGitVersionInformationTask(ProjectCreator project,
115209
{ "SolutionDirectory", "$(MSBuildProjectDirectory)" },
116210
{ "VersionFile", "$(MSBuildProjectDirectory)/gitversion.json" },
117211
{ "ProjectFile", "$(MSBuildProjectFullPath)" },
118-
{ "IntermediateOutputPath", "$(MSBuildProjectDirectory)" },
212+
{ "IntermediateOutputPath", intermediateOutputPath },
119213
{ "Language", "$(Language)" }
120214
})
121215
.TaskOutputProperty(outputProperty, outputProperty)

src/GitVersion.MsBuild/GitVersionTaskExecutor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task)
5050
public void GenerateGitVersionInformation(GenerateGitVersionInformation task)
5151
{
5252
var versionVariables = VersionVariables.FromFile(task.VersionFile, fileSystem);
53+
54+
if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
55+
{
56+
// Ensure provided output path exists first. Fixes issue #2815.
57+
this.fileSystem.CreateDirectory(task.IntermediateOutputPath);
58+
}
59+
5360
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation");
5461
task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
5562

0 commit comments

Comments
 (0)