Skip to content

Commit 7a635bd

Browse files
authored
Merge pull request #3115 from jpeirson/bugfix/issue-2815
Ensuring intermediate output path exists
2 parents 17dcb8e + 940f115 commit 7a635bd

File tree

3 files changed

+189
-4
lines changed

3 files changed

+189
-4
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.Tests/Tasks/UpdateAssemblyInfoTaskTest.cs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,85 @@ public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildInBuildServe
8787
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]");
8888
}
8989

90-
private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty)
90+
[Test]
91+
public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist()
92+
{
93+
var task = new UpdateAssemblyInfo { IntermediateOutputPath = Guid.NewGuid().ToString("N") };
94+
95+
using var result = ExecuteMsBuildTask(task);
96+
97+
result.Success.ShouldBe(true);
98+
result.Errors.ShouldBe(0);
99+
result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull();
100+
101+
var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath);
102+
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]");
103+
}
104+
105+
[Test]
106+
public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExistInBuildServer()
107+
{
108+
var task = new UpdateAssemblyInfo { IntermediateOutputPath = Guid.NewGuid().ToString("N") };
109+
110+
using var result = ExecuteMsBuildTaskInAzurePipeline(task);
111+
112+
result.Success.ShouldBe(true);
113+
result.Errors.ShouldBe(0);
114+
result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull();
115+
116+
var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath);
117+
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]");
118+
}
119+
120+
[Test]
121+
[Category(NoNet48)]
122+
[Category(NoMono)]
123+
public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist()
124+
{
125+
const string taskName = nameof(UpdateAssemblyInfo);
126+
const string outputProperty = nameof(UpdateAssemblyInfo.AssemblyInfoTempFilePath);
127+
var randDir = Guid.NewGuid().ToString("N");
128+
129+
using var result = ExecuteMsBuildExe(project => AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir)));
130+
131+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
132+
result.MsBuild.Count.ShouldBeGreaterThan(0);
133+
result.MsBuild.OverallSuccess.ShouldBe(true);
134+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
135+
result.Output.ShouldNotBeNullOrWhiteSpace();
136+
137+
var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "AssemblyInfo.g.cs");
138+
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");
139+
140+
var fileContent = File.ReadAllText(generatedFilePath);
141+
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]");
142+
}
143+
144+
[Test]
145+
[Category(NoNet48)]
146+
[Category(NoMono)]
147+
public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer()
148+
{
149+
const string taskName = nameof(UpdateAssemblyInfo);
150+
const string outputProperty = nameof(UpdateAssemblyInfo.AssemblyInfoTempFilePath);
151+
var randDir = Guid.NewGuid().ToString("N");
152+
153+
using var result = ExecuteMsBuildExeInAzurePipeline(project => AddUpdateAssemblyInfoTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir)));
154+
155+
result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
156+
result.MsBuild.Count.ShouldBeGreaterThan(0);
157+
result.MsBuild.OverallSuccess.ShouldBe(true);
158+
result.MsBuild.ShouldAllBe(x => x.Succeeded);
159+
result.Output.ShouldNotBeNullOrWhiteSpace();
160+
161+
var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "AssemblyInfo.g.cs");
162+
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");
163+
164+
var fileContent = File.ReadAllText(generatedFilePath);
165+
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]");
166+
}
167+
168+
private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)")
91169
{
92170
var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location;
93171
project.UsingTaskAssemblyFile(taskName, assemblyFileLocation)
@@ -98,7 +176,7 @@ private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string tar
98176
{ "SolutionDirectory", "$(MSBuildProjectDirectory)" },
99177
{ "VersionFile", "$(MSBuildProjectDirectory)/gitversion.json" },
100178
{ "ProjectFile", "$(MSBuildProjectFullPath)" },
101-
{ "IntermediateOutputPath", "$(MSBuildProjectDirectory)" },
179+
{ "IntermediateOutputPath", intermediateOutputPath },
102180
{ "Language", "$(Language)" },
103181
{ "CompileFiles", "@(Compile)" }
104182
})

src/GitVersion.MsBuild/GitVersionTaskExecutor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task)
3535
FileHelper.DeleteTempFiles();
3636
FileHelper.CheckForInvalidFiles(task.CompileFiles, task.ProjectFile);
3737

38+
if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
39+
{
40+
// Ensure provided output path exists first. Fixes issue #2815.
41+
fileSystem.CreateDirectory(task.IntermediateOutputPath);
42+
}
43+
3844
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo");
3945
task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
4046

@@ -50,6 +56,13 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task)
5056
public void GenerateGitVersionInformation(GenerateGitVersionInformation task)
5157
{
5258
var versionVariables = VersionVariables.FromFile(task.VersionFile, fileSystem);
59+
60+
if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
61+
{
62+
// Ensure provided output path exists first. Fixes issue #2815.
63+
fileSystem.CreateDirectory(task.IntermediateOutputPath);
64+
}
65+
5366
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation");
5467
task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
5568

0 commit comments

Comments
 (0)