Skip to content

Fixes #1796 by using a constant file name #2054

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 4 commits into from
Jan 22, 2020
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
46 changes: 46 additions & 0 deletions src/GitVersionTask.Tests/FileHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using GitVersion.MSBuildTask;
using NUnit.Framework;

namespace GitVersionTask.Tests
{
[TestFixture]
public class FileHelperTests
{
[Test]
[TestCase("C#", "cs")]
[TestCase("F#", "fs")]
[TestCase("VB", "vb")]
[TestCase("XY", null)]
public void GetFileExtensionShouldReturnCorrectExtension(string language, string expectedExtension)
{
if (expectedExtension != null)
{
Assert.That(FileHelper.GetFileExtension(language), Is.EqualTo(expectedExtension));
}
else
{
Assert.That(() => FileHelper.GetFileExtension(language), Throws.ArgumentException.With.Message.EqualTo($"Unknown language detected: '{language}'"));
}
}

[Test]
public void GetFileWriteInfoShouldCreateConstantNamedFileWhenIntermediateOutputPath()
{
var fileInfo = FileHelper.GetFileWriteInfo("MyIntermediateOutputPath", "C#", "MyProject.csproj", "GeneratedVersionInformation");

Assert.That(fileInfo.WorkingDirectory, Is.EqualTo("MyIntermediateOutputPath"));
Assert.That(fileInfo.FileName, Is.EqualTo("GeneratedVersionInformation.g.cs"));
Assert.That(fileInfo.FileExtension, Is.EqualTo("cs"));
}

[Test]
public void GetFileWriteInfoShouldCreateRandomNamedFileWhenNoIntermediateOutputPath()
{
var fileInfo = FileHelper.GetFileWriteInfo(null, "C#", "MyProject.csproj", "GeneratedVersionInformation");

Assert.That(fileInfo.WorkingDirectory, Is.EqualTo(FileHelper.TempPath));
Assert.That(fileInfo.FileName, Does.StartWith("GeneratedVersionInformation_MyProject_").And.EndsWith(".g.cs"));
Assert.That(fileInfo.FileExtension, Is.EqualTo("cs"));
}
}
}
4 changes: 2 additions & 2 deletions src/GitVersionTask/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public static FileWriteInfo GetFileWriteInfo(this string intermediateOutputPath,

if (intermediateOutputPath == null)
{
fileName = $"{outputFileName}.g.{fileExtension}";
fileName = $"{outputFileName}_{Path.GetFileNameWithoutExtension(projectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
workingDirectory = TempPath;
}
else
{
fileName = $"{outputFileName}_{Path.GetFileNameWithoutExtension(projectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
fileName = $"{outputFileName}.g.{fileExtension}";
workingDirectory = intermediateOutputPath;
}
return new FileWriteInfo(workingDirectory, fileName, fileExtension);
Expand Down