Skip to content

Commit 5076a66

Browse files
asbjornuJakeGinnivan
authored andcommitted
Made the VersionAndBranchFinder tests a bit more DRY
1 parent e22e66b commit 5076a66

File tree

1 file changed

+21
-44
lines changed

1 file changed

+21
-44
lines changed

src/GitVersionTask.Tests/VersionAndBranchFinderTests.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,18 @@ public void CacheFileExistsOnDisk()
5151
CommitDate: 2015-11-10
5252
";
5353

54-
var infoBuilder = new StringBuilder();
55-
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
56-
57-
Logger.SetLoggers(infoLogger, null, null);
58-
59-
using (var fixture = new EmptyRepositoryFixture(new Config()))
54+
var info = RepositoryScope((fixture, vv, fs) =>
6055
{
61-
var fileSystem = new FileSystem();
62-
fixture.Repository.MakeACommit();
63-
var vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fileSystem);
64-
65-
vv.AssemblySemVer.ShouldBe("0.1.0.0");
66-
67-
vv.FileName.ShouldNotBeNullOrEmpty();
68-
69-
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
56+
fs.WriteAllText(vv.FileName, versionCacheFileContent);
7057

7158
// I would rather see that VersionAndBranchFinder was non-static and could be reinstantiated to
7259
// clear the in-memory cache, but that's not the case, so I have to perform this ugly hack. @asbjornu
7360
VersionAndBranchFinder.VersionCacheVersions = new ConcurrentDictionary<string, VersionVariables>();
7461

75-
vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fileSystem);
62+
vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fs);
7663

7764
vv.AssemblySemVer.ShouldBe("4.10.3.0");
78-
}
79-
80-
var info = infoBuilder.ToString();
81-
82-
Console.WriteLine(info);
65+
});
8366

8467
info.ShouldContain("Deserializing version variables from cache file", () => info);
8568
}
@@ -88,29 +71,11 @@ public void CacheFileExistsOnDisk()
8871
[Test]
8972
public void CacheFileExistsInMemory()
9073
{
91-
var infoBuilder = new StringBuilder();
92-
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
93-
94-
Logger.SetLoggers(infoLogger, null, null);
95-
96-
using (var fixture = new EmptyRepositoryFixture(new Config()))
74+
var info = RepositoryScope((fixture, vv, fs) =>
9775
{
98-
var fileSystem = new FileSystem();
99-
fixture.Repository.MakeACommit();
100-
var vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fileSystem);
101-
76+
vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fs);
10277
vv.AssemblySemVer.ShouldBe("0.1.0.0");
103-
104-
vv.FileName.ShouldNotBeNullOrEmpty();
105-
106-
vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fileSystem);
107-
108-
vv.AssemblySemVer.ShouldBe("0.1.0.0");
109-
}
110-
111-
var info = infoBuilder.ToString();
112-
113-
Console.WriteLine(info);
78+
});
11479

11580
info.ShouldContain("yml not found", () => info);
11681
info.ShouldNotContain("Deserializing version variables from cache file", () => info);
@@ -119,6 +84,13 @@ public void CacheFileExistsInMemory()
11984

12085
[Test]
12186
public void CacheFileIsMissing()
87+
{
88+
var info = RepositoryScope();
89+
info.ShouldContain("yml not found", () => info);
90+
}
91+
92+
93+
static string RepositoryScope(Action<EmptyRepositoryFixture, VersionVariables, IFileSystem> fixtureAction = null)
12294
{
12395
var infoBuilder = new StringBuilder();
12496
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
@@ -132,9 +104,14 @@ public void CacheFileIsMissing()
132104
var vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fileSystem);
133105

134106
vv.AssemblySemVer.ShouldBe("0.1.0.0");
107+
vv.FileName.ShouldNotBeNullOrEmpty();
108+
109+
if (fixtureAction != null)
110+
{
111+
fixtureAction(fixture, vv, fileSystem);
112+
}
135113
}
136114

137-
var info = infoBuilder.ToString();
138-
info.ShouldContain("yml not found", () => info);
115+
return infoBuilder.ToString();
139116
}
140117
}

0 commit comments

Comments
 (0)