Skip to content

Ensuring intermediate output path exists #3115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,101 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildI
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
}

private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty)
[Test]
public void GenerateGitVersionInformationTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist()
{
var task = new GenerateGitVersionInformation { IntermediateOutputPath = Guid.NewGuid().ToString("N") };

using var result = ExecuteMsBuildTask(task);

result.Success.ShouldBe(true);
result.Errors.ShouldBe(0);
result.Task.GitVersionInformationFilePath.ShouldNotBeNull();

var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath);
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4""");
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4""");
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1""");
}

[Test]
public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServerWhenIntermediateOutputPathDoesNotExist()
{
var task = new GenerateGitVersionInformation { IntermediateOutputPath = Guid.NewGuid().ToString("N") };

using var result = ExecuteMsBuildTaskInAzurePipeline(task);

result.Success.ShouldBe(true);
result.Errors.ShouldBe(0);
result.Task.GitVersionInformationFilePath.ShouldNotBeNull();

var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath);
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""0""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
}

[Test]
[Category(NoNet48)]
[Category(NoMono)]
public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist()
{
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)));

result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
result.MsBuild.Count.ShouldBeGreaterThan(0);
result.MsBuild.OverallSuccess.ShouldBe(true);
result.MsBuild.ShouldAllBe(x => x.Succeeded);
result.Output.ShouldNotBeNullOrWhiteSpace();

var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs");
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");

var fileContent = File.ReadAllText(generatedFilePath);
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4""");
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4""");
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1""");
}

[Test]
[Category(NoNet48)]
[Category(NoMono)]
public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer()
{
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)));

result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
result.MsBuild.Count.ShouldBeGreaterThan(0);
result.MsBuild.OverallSuccess.ShouldBe(true);
result.MsBuild.ShouldAllBe(x => x.Succeeded);
result.Output.ShouldNotBeNullOrWhiteSpace();

var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs");
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");

var fileContent = File.ReadAllText(generatedFilePath);
fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""0""");
fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1""");
fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1""");
}

private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)")
{
var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location;
project.UsingTaskAssemblyFile(taskName, assemblyFileLocation)
Expand All @@ -115,7 +209,7 @@ private static void AddGenerateGitVersionInformationTask(ProjectCreator project,
{ "SolutionDirectory", "$(MSBuildProjectDirectory)" },
{ "VersionFile", "$(MSBuildProjectDirectory)/gitversion.json" },
{ "ProjectFile", "$(MSBuildProjectFullPath)" },
{ "IntermediateOutputPath", "$(MSBuildProjectDirectory)" },
{ "IntermediateOutputPath", intermediateOutputPath },
{ "Language", "$(Language)" }
})
.TaskOutputProperty(outputProperty, outputProperty)
Expand Down
82 changes: 80 additions & 2 deletions src/GitVersion.MsBuild.Tests/Tasks/UpdateAssemblyInfoTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,85 @@ public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildInBuildServe
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]");
}

private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty)
[Test]
public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExist()
{
var task = new UpdateAssemblyInfo { IntermediateOutputPath = Guid.NewGuid().ToString("N") };

using var result = ExecuteMsBuildTask(task);

result.Success.ShouldBe(true);
result.Errors.ShouldBe(0);
result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull();

var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath);
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]");
}

[Test]
public void UpdateAssemblyInfoTaskShouldCreateFileWhenIntermediateOutputPathDoesNotExistInBuildServer()
{
var task = new UpdateAssemblyInfo { IntermediateOutputPath = Guid.NewGuid().ToString("N") };

using var result = ExecuteMsBuildTaskInAzurePipeline(task);

result.Success.ShouldBe(true);
result.Errors.ShouldBe(0);
result.Task.AssemblyInfoTempFilePath.ShouldNotBeNull();

var fileContent = File.ReadAllText(result.Task.AssemblyInfoTempFilePath);
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]");
}

[Test]
[Category(NoNet48)]
[Category(NoMono)]
public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExist()
{
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)));

result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
result.MsBuild.Count.ShouldBeGreaterThan(0);
result.MsBuild.OverallSuccess.ShouldBe(true);
result.MsBuild.ShouldAllBe(x => x.Succeeded);
result.Output.ShouldNotBeNullOrWhiteSpace();

var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "AssemblyInfo.g.cs");
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");

var fileContent = File.ReadAllText(generatedFilePath);
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.2.4.0"")]");
}

[Test]
[Category(NoNet48)]
[Category(NoMono)]
public void UpdateAssemblyInfoTaskShouldCreateFileWhenRunWithMsBuildAndIntermediateOutputPathDoesNotExistInBuildServer()
{
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)));

result.ProjectPath.ShouldNotBeNullOrWhiteSpace();
result.MsBuild.Count.ShouldBeGreaterThan(0);
result.MsBuild.OverallSuccess.ShouldBe(true);
result.MsBuild.ShouldAllBe(x => x.Succeeded);
result.Output.ShouldNotBeNullOrWhiteSpace();

var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "AssemblyInfo.g.cs");
result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}");

var fileContent = File.ReadAllText(generatedFilePath);
fileContent.ShouldContain(@"[assembly: AssemblyVersion(""1.0.1.0"")]");
}

private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)")
{
var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location;
project.UsingTaskAssemblyFile(taskName, assemblyFileLocation)
Expand All @@ -98,7 +176,7 @@ private static void AddUpdateAssemblyInfoTask(ProjectCreator project, string tar
{ "SolutionDirectory", "$(MSBuildProjectDirectory)" },
{ "VersionFile", "$(MSBuildProjectDirectory)/gitversion.json" },
{ "ProjectFile", "$(MSBuildProjectFullPath)" },
{ "IntermediateOutputPath", "$(MSBuildProjectDirectory)" },
{ "IntermediateOutputPath", intermediateOutputPath },
{ "Language", "$(Language)" },
{ "CompileFiles", "@(Compile)" }
})
Expand Down
13 changes: 13 additions & 0 deletions src/GitVersion.MsBuild/GitVersionTaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task)
FileHelper.DeleteTempFiles();
FileHelper.CheckForInvalidFiles(task.CompileFiles, task.ProjectFile);

if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
{
// Ensure provided output path exists first. Fixes issue #2815.
fileSystem.CreateDirectory(task.IntermediateOutputPath);
}

var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo");
task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);

Expand All @@ -50,6 +56,13 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task)
public void GenerateGitVersionInformation(GenerateGitVersionInformation task)
{
var versionVariables = VersionVariables.FromFile(task.VersionFile, fileSystem);

if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
{
// Ensure provided output path exists first. Fixes issue #2815.
fileSystem.CreateDirectory(task.IntermediateOutputPath);
}

var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation");
task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);

Expand Down