Skip to content

Commit d08b0bc

Browse files
test: refactor to remove code duplication
1 parent e01d3f4 commit d08b0bc

1 file changed

Lines changed: 45 additions & 43 deletions

File tree

src/GitVersion.Output.Tests/Output/GitVersionInfoGeneratorTests.cs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,9 @@ public class GitVersionInfoGeneratorTests : TestBase
2020
[TestCase("vb")]
2121
public void ShouldCreateFile(string fileExtension)
2222
{
23-
var versionSourceSemVer = new SemanticVersion(1, 2, 2);
24-
var semanticVersion = new SemanticVersion
25-
{
26-
Major = 1,
27-
Minor = 2,
28-
Patch = 3,
29-
PreReleaseTag = "unstable4",
30-
BuildMetaData = new(versionSourceSemVer, "versionSourceSha", 5,
31-
"feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z", CultureInfo.InvariantCulture), 0)
32-
};
33-
34-
var sp = ConfigureServices();
35-
36-
var fileSystem = sp.GetRequiredService<IFileSystem>();
37-
38-
var directory = FileSystemHelper.Path.Combine(FileSystemHelper.Path.GetTempPath(), "GitVersionInfoGeneratorTests", Guid.NewGuid().ToString());
39-
if (!fileSystem.Directory.Exists(directory))
40-
fileSystem.Directory.CreateDirectory(directory);
41-
var fileName = "GitVersionInformation.g." + fileExtension;
42-
var fullPath = FileSystemHelper.Path.Combine(directory, fileName);
43-
44-
var variableProvider = sp.GetRequiredService<IVariableProvider>();
45-
var variables = variableProvider.GetVariablesFor(semanticVersion, EmptyConfigurationBuilder.New.Build(), 0);
46-
using var generator = sp.GetRequiredService<IGitVersionInfoGenerator>();
47-
48-
generator.Execute(variables, new(directory, fileName));
49-
50-
fileSystem.File.ReadAllText(fullPath).ShouldMatchApproved(c => c.SubFolder(FileSystemHelper.Path.Combine("Approved", fileExtension)));
23+
var fileContents = GenerateGitVersionInformationFile(fileExtension);
5124

52-
FileSystemHelper.Directory.DeleteDirectory(directory);
25+
fileContents.ShouldMatchApproved(c => c.SubFolder(FileSystemHelper.Path.Combine("Approved", fileExtension)));
5326
}
5427

5528
/// <summary>
@@ -62,35 +35,64 @@ public void ShouldProperlyOutputNamespaceDeclaration(string fileExtension)
6235
{
6336
const string targetNamespace = "My.Custom.Namespace";
6437

38+
var fileContents = GenerateGitVersionInformationFile(fileExtension, targetNamespace);
39+
40+
fileContents.ShouldMatchApproved(c => c.SubFolder(FileSystemHelper.Path.Combine("Approved", fileExtension)));
41+
}
42+
43+
private static SemanticVersion CreateSemanticVersion()
44+
{
6545
var versionSourceSemVer = new SemanticVersion(1, 2, 2);
66-
var semanticVersion = new SemanticVersion
46+
return new SemanticVersion
6747
{
6848
Major = 1,
6949
Minor = 2,
7050
Patch = 3,
7151
PreReleaseTag = "unstable4",
72-
BuildMetaData = new(versionSourceSemVer, "versionSourceSha", 5,
73-
"feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z", CultureInfo.InvariantCulture), 0)
52+
BuildMetaData = new(
53+
versionSourceSemVer,
54+
"versionSourceSha",
55+
5,
56+
"feature1",
57+
"commitSha",
58+
"commitShortSha",
59+
DateTimeOffset.Parse("2014-03-06 23:59:59Z", CultureInfo.InvariantCulture),
60+
0)
7461
};
62+
}
7563

76-
var sp = ConfigureServices();
77-
78-
var fileSystem = sp.GetRequiredService<IFileSystem>();
79-
80-
var directory = FileSystemHelper.Path.Combine(FileSystemHelper.Path.GetTempPath(), "GitVersionInfoGeneratorTests", Guid.NewGuid().ToString());
64+
private static (string Directory, string FileName, string FullPath) CreateTempOutputPath(IFileSystem fileSystem, string fileExtension)
65+
{
66+
var directory = FileSystemHelper.Path.Combine(FileSystemHelper.Path.GetTempPath(), nameof(GitVersionInfoGeneratorTests), Guid.NewGuid().ToString());
8167
if (!fileSystem.Directory.Exists(directory))
8268
fileSystem.Directory.CreateDirectory(directory);
69+
8370
var fileName = "GitVersionInformation.g." + fileExtension;
8471
var fullPath = FileSystemHelper.Path.Combine(directory, fileName);
72+
return (directory, fileName, fullPath);
73+
}
8574

86-
var variableProvider = sp.GetRequiredService<IVariableProvider>();
87-
var variables = variableProvider.GetVariablesFor(semanticVersion, EmptyConfigurationBuilder.New.Build(), 0);
88-
using var generator = sp.GetRequiredService<IGitVersionInfoGenerator>();
75+
private string GenerateGitVersionInformationFile(string fileExtension, string? targetNamespace = null)
76+
{
77+
var semanticVersion = CreateSemanticVersion();
8978

90-
generator.Execute(variables, new(directory, fileName, targetNamespace));
79+
var sp = ConfigureServices();
80+
var fileSystem = sp.GetRequiredService<IFileSystem>();
81+
82+
var (directory, fileName, fullPath) = CreateTempOutputPath(fileSystem, fileExtension);
83+
try
84+
{
85+
var variables = sp.GetRequiredService<IVariableProvider>()
86+
.GetVariablesFor(semanticVersion, EmptyConfigurationBuilder.New.Build(), 0);
9187

92-
fileSystem.File.ReadAllText(fullPath).ShouldMatchApproved(c => c.SubFolder(FileSystemHelper.Path.Combine("Approved", fileExtension)));
88+
using var generator = sp.GetRequiredService<IGitVersionInfoGenerator>();
89+
generator.Execute(variables, new(directory, fileName, targetNamespace));
9390

94-
FileSystemHelper.Directory.DeleteDirectory(directory);
91+
return fileSystem.File.ReadAllText(fullPath);
92+
}
93+
finally
94+
{
95+
FileSystemHelper.Directory.DeleteDirectory(directory);
96+
}
9597
}
9698
}

0 commit comments

Comments
 (0)