Skip to content

Commit 42ff7e4

Browse files
sergiorykovJakeGinnivan
authored andcommitted
added tests for verification config files + a bit of refactoring
1 parent c3856fb commit 42ff7e4

File tree

5 files changed

+129
-44
lines changed

5 files changed

+129
-44
lines changed

src/GitVersionCore.Tests/ConfigProviderTests.cs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
@@ -12,14 +13,19 @@
1213
[TestFixture]
1314
public class ConfigProviderTests
1415
{
16+
private const string DefaultRepoPath = "c:\\MyGitRepo";
17+
private const string DefaultWorkingPath = "c:\\MyGitRepo\\Working";
18+
1519
string repoPath;
20+
string workingPath;
1621
IFileSystem fileSystem;
1722

1823
[SetUp]
1924
public void Setup()
2025
{
2126
fileSystem = new TestFileSystem();
22-
repoPath = "c:\\MyGitRepo";
27+
repoPath = DefaultRepoPath;
28+
workingPath = DefaultWorkingPath;
2329
}
2430

2531
[Test]
@@ -147,8 +153,8 @@ public void NextVersionCanHavePatch()
147153
}
148154

149155
[Test]
150-
[Category("NoMono")]
151-
[Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")]
156+
[NUnit.Framework.Category("NoMono")]
157+
[NUnit.Framework.Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")]
152158
[MethodImpl(MethodImplOptions.NoInlining)]
153159
public void CanWriteOutEffectiveConfiguration()
154160
{
@@ -228,18 +234,51 @@ public void VerifyAliases()
228234
propertiesMissingAlias.ShouldBeEmpty();
229235
}
230236

231-
[Test]
232-
public void WarnOnExistingGitVersionConfigYamlFile()
237+
[TestCase(DefaultRepoPath)]
238+
[TestCase(DefaultWorkingPath)]
239+
public void WarnOnExistingGitVersionConfigYamlFile(string path)
233240
{
234-
SetupConfigFileContent(string.Empty, "GitVersionConfig.yaml");
241+
SetupConfigFileContent(string.Empty, ConfigurationProvider.ObsoleteConfigFileName, path);
235242

236-
var s = string.Empty;
237-
Action<string> action = info => { s = info; };
243+
var logOutput = string.Empty;
244+
Action<string> action = info => { logOutput = info; };
238245
Logger.SetLoggers(action, action, action);
239246

240-
ConfigurationProvider.Provide(repoPath, fileSystem);
247+
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
241248

242-
s.Contains("'GitVersionConfig.yaml' is deprecated, use 'GitVersion.yml' instead.").ShouldBe(true);
249+
var configFileDeprecatedWarning = string.Format("{0}' is deprecated, use '{1}' instead", ConfigurationProvider.ObsoleteConfigFileName, ConfigurationProvider.DefaultConfigFileName);
250+
logOutput.Contains(configFileDeprecatedWarning).ShouldBe(true);
251+
}
252+
253+
[TestCase(DefaultRepoPath)]
254+
[TestCase(DefaultWorkingPath)]
255+
public void WarnOnAmbigousConfigFilesAtTheSameProjectRootDirectory(string path)
256+
{
257+
SetupConfigFileContent(string.Empty, ConfigurationProvider.ObsoleteConfigFileName, path);
258+
SetupConfigFileContent(string.Empty, ConfigurationProvider.DefaultConfigFileName, path);
259+
260+
var logOutput = string.Empty;
261+
Action<string> action = info => { logOutput = info; };
262+
Logger.SetLoggers(action, action, action);
263+
264+
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
265+
266+
var configFileDeprecatedWarning = string.Format("Ambigous config files at '{0}'", path);
267+
logOutput.Contains(configFileDeprecatedWarning).ShouldBe(true);
268+
}
269+
270+
[TestCase(ConfigurationProvider.DefaultConfigFileName, ConfigurationProvider.DefaultConfigFileName)]
271+
[TestCase(ConfigurationProvider.DefaultConfigFileName, ConfigurationProvider.ObsoleteConfigFileName)]
272+
[TestCase(ConfigurationProvider.ObsoleteConfigFileName, ConfigurationProvider.DefaultConfigFileName)]
273+
[TestCase(ConfigurationProvider.ObsoleteConfigFileName, ConfigurationProvider.ObsoleteConfigFileName)]
274+
public void ThrowsExceptionOnAmbigousConfigFileLocation(string repoConfigFile, string workingConfigFile)
275+
{
276+
SetupConfigFileContent(string.Empty, repoConfigFile, repoPath);
277+
SetupConfigFileContent(string.Empty, workingConfigFile, workingPath);
278+
279+
Should.Throw<WarningException>(() => {
280+
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
281+
});
243282
}
244283

245284
[Test]
@@ -256,8 +295,13 @@ public void NoWarnOnGitVersionYmlFile()
256295
s.Length.ShouldBe(0);
257296
}
258297

259-
void SetupConfigFileContent(string text, string fileName = "GitVersion.yml")
298+
void SetupConfigFileContent(string text, string fileName = ConfigurationProvider.DefaultConfigFileName)
299+
{
300+
SetupConfigFileContent(text, fileName, repoPath);
301+
}
302+
303+
void SetupConfigFileContent(string text, string fileName, string path)
260304
{
261-
fileSystem.WriteAllText(Path.Combine(repoPath, fileName), text);
305+
fileSystem.WriteAllText(Path.Combine(path, fileName), text);
262306
}
263307
}

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class ConfigurationProvider
1212
{
1313
internal const string DefaultTagPrefix = "[vV]";
1414

15+
public const string DefaultConfigFileName = "GitVersion.yml";
16+
public const string ObsoleteConfigFileName = "GitVersionConfig.yaml";
17+
1518
public static Config Provide(GitPreparer gitPreparer, IFileSystem fileSystem, bool applyDefaults = true, Config overrideConfig = null)
1619
{
1720
var workingDirectory = gitPreparer.WorkingDirectory;
@@ -25,6 +28,19 @@ public static Config Provide(GitPreparer gitPreparer, IFileSystem fileSystem, bo
2528
return Provide(projectRootDirectory, fileSystem, applyDefaults, overrideConfig);
2629
}
2730

31+
public static string SelectConfigFilePath(GitPreparer gitPreparer, IFileSystem fileSystem)
32+
{
33+
var workingDirectory = gitPreparer.WorkingDirectory;
34+
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
35+
36+
if (HasConfigFileAt(workingDirectory, fileSystem))
37+
{
38+
return GetConfigFilePath(workingDirectory, fileSystem);
39+
}
40+
41+
return GetConfigFilePath(projectRootDirectory, fileSystem);
42+
}
43+
2844
public static Config Provide(string workingDirectory, IFileSystem fileSystem, bool applyDefaults = true, Config overrideConfig = null)
2945
{
3046
var readConfig = ReadConfig(workingDirectory, fileSystem);
@@ -170,6 +186,11 @@ public static void Verify(GitPreparer gitPreparer, IFileSystem fileSystem)
170186
var workingDirectory = gitPreparer.WorkingDirectory;
171187
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
172188

189+
Verify(workingDirectory,projectRootDirectory, fileSystem);
190+
}
191+
192+
public static void Verify(string workingDirectory, string projectRootDirectory, IFileSystem fileSystem)
193+
{
173194
if(fileSystem.PathsEqual(workingDirectory, projectRootDirectory))
174195
{
175196
WarnAboutObsoleteConfigFile(workingDirectory, fileSystem);
@@ -190,9 +211,6 @@ public static void Verify(GitPreparer gitPreparer, IFileSystem fileSystem)
190211
}
191212
}
192213

193-
public static readonly string DefaultConfigFileName = "GitVersion.yml";
194-
public static readonly string ObsoleteConfigFileName = "GitVersionConfig.yaml";
195-
196214
public static string GetConfigFilePath(string workingDirectory, IFileSystem fileSystem)
197215
{
198216
var ymlPath = Path.Combine(workingDirectory, DefaultConfigFileName);

src/GitVersionCore/ExecuteCore.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,22 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
5151
// TODO Link to wiki article
5252
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
5353
}
54-
55-
using (var repo = GetRepository(dotGitDirectory))
54+
55+
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer);
56+
if (versionVariables == null)
5657
{
57-
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(repo, dotGitDirectory);
58-
if (versionVariables == null)
58+
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig: overrideConfig);
59+
try
5960
{
60-
versionVariables = ExecuteInternal(targetBranch, commitId, repo, gitPreparer, projectRoot, buildServer, overrideConfig: overrideConfig);
61-
try
62-
{
63-
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
64-
}
65-
catch (AggregateException e)
66-
{
67-
Logger.WriteWarning(string.Format("One or more exceptions during cache write:{0}{1}", Environment.NewLine, e));
68-
}
61+
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
62+
}
63+
catch (AggregateException e)
64+
{
65+
Logger.WriteWarning(string.Format("One or more exceptions during cache write:{0}{1}", Environment.NewLine, e));
6966
}
70-
71-
return versionVariables;
7267
}
68+
69+
return versionVariables;
7370
}
7471

7572
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication)
@@ -100,15 +97,18 @@ static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch
10097
return currentBranch;
10198
}
10299

103-
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot, IBuildServer buildServer, Config overrideConfig = null)
100+
VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPreparer gitPreparer, IBuildServer buildServer, Config overrideConfig = null)
104101
{
105102
var versionFinder = new GitVersionFinder();
106103
var configuration = ConfigurationProvider.Provide(gitPreparer, fileSystem, overrideConfig: overrideConfig);
107104

108-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId : commitId);
109-
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
105+
return gitPreparer.WithRepository(repo =>
106+
{
107+
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
108+
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
110109

111-
return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
110+
return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
111+
});
112112
}
113113

114114
IRepository GetRepository(string gitDirectory)

src/GitVersionCore/GitPreparer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class GitPreparer
1414
bool noFetch;
1515
string targetPath;
1616

17+
public GitPreparer(string targetPath) : this(null, null, null, false, targetPath) { }
1718
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, bool noFetch, string targetPath)
1819
{
1920
this.targetUrl = targetUrl;
@@ -57,6 +58,14 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)
5758
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, currentBranch, noFetch);
5859
}
5960

61+
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
62+
{
63+
using (IRepository repo = new Repository(GetDotGitDirectory()))
64+
{
65+
return action(repo);
66+
}
67+
}
68+
6069
static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
6170
{
6271
var userTemp = dynamicRepositoryLocation ?? Path.GetTempPath();

src/GitVersionCore/GitVersionCache.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ public GitVersionCache(IFileSystem fileSystem)
1919
this.fileSystem = fileSystem;
2020
}
2121

22-
public void WriteVariablesToDiskCache(IRepository repo, string gitDir, VersionVariables variablesFromCache)
22+
public void WriteVariablesToDiskCache(GitPreparer gitPreparer, VersionVariables variablesFromCache)
2323
{
24-
var cacheFileName = GetCacheFileName(GetKey(repo, gitDir), GetCacheDir(gitDir));
24+
var cacheDir = PrepareCacheDirectory(gitPreparer);
25+
var cacheFileName = GetCacheFileName(GetKey(gitPreparer), cacheDir);
2526
variablesFromCache.FileName = cacheFileName;
2627

2728
Dictionary<string, string> dictionary;
@@ -49,15 +50,24 @@ public void WriteVariablesToDiskCache(IRepository repo, string gitDir, VersionVa
4950
retryOperation.Execute();
5051
}
5152

52-
public VersionVariables LoadVersionVariablesFromDiskCache(IRepository repo, string gitDir)
53+
private string PrepareCacheDirectory(GitPreparer gitPreparer)
54+
{
55+
var gitDir = gitPreparer.GetDotGitDirectory();
56+
57+
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
58+
var cacheDir = GetCacheDir(gitDir);
59+
fileSystem.CreateDirectory(cacheDir);
60+
61+
return cacheDir;
62+
}
63+
64+
public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPreparer)
5365
{
5466
using (Logger.IndentLog("Loading version variables from disk cache"))
5567
{
56-
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
68+
var cacheDir = PrepareCacheDirectory(gitPreparer);
5769

58-
var cacheDir = GetCacheDir(gitDir);
59-
fileSystem.CreateDirectory(cacheDir);
60-
var cacheFileName = GetCacheFileName(GetKey(repo, gitDir), cacheDir);
70+
var cacheFileName = GetCacheFileName(GetKey(gitPreparer), cacheDir);
6171
VersionVariables vv = null;
6272
if (fileSystem.Exists(cacheFileName))
6373
{
@@ -91,14 +101,18 @@ public VersionVariables LoadVersionVariablesFromDiskCache(IRepository repo, stri
91101
}
92102
}
93103

94-
string GetKey(IRepository repo, string gitDir)
104+
string GetKey(GitPreparer gitPreparer)
95105
{
106+
var gitDir = gitPreparer.GetDotGitDirectory();
107+
96108
// Maybe using timestamp in .git/refs directory is enough?
97109
var ticks = fileSystem.GetLastDirectoryWrite(Path.Combine(gitDir, "refs"));
98-
var configPath = Path.Combine(repo.GetRepositoryDirectory(), "GitVersionConfig.yaml");
110+
111+
var configPath = ConfigurationProvider.SelectConfigFilePath(gitPreparer, fileSystem);
99112
var configText = fileSystem.Exists(configPath) ? fileSystem.ReadAllText(configPath) : null;
100113
var configHash = configText != null ? GetHash(configText) : null;
101-
return string.Join(":", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks, configHash);
114+
115+
return gitPreparer.WithRepository(repo => string.Join(":", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks, configHash));
102116
}
103117

104118
static string GetCacheFileName(string key, string cacheDir)

0 commit comments

Comments
 (0)