Skip to content

Commit e5d941c

Browse files
committed
Support older configuration files, warn user but upgrade file
1 parent 93460fa commit e5d941c

18 files changed

+151
-32
lines changed

GitVersionCore.Tests/ConfigReaderTests.cs renamed to GitVersionCore.Tests/ConfigProviderTests.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22
using System.Linq;
33
using System.Reflection;
44
using GitVersion;
5+
using GitVersion.Helpers;
6+
using GitVersionCore.Tests;
57
using NUnit.Framework;
68
using Shouldly;
79
using YamlDotNet.Serialization;
810

911
[TestFixture]
10-
public class ConfigReaderTests
12+
public class ConfigProviderTests
1113
{
14+
string gitDirectory;
15+
IFileSystem fileSystem;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
fileSystem = new TestFileSystem();
21+
gitDirectory = "c:\\MyGitRepo\\.git";
22+
}
1223

1324
[Test]
1425
public void CanReadDocument()
@@ -20,19 +31,31 @@ public void CanReadDocument()
2031
next-version: 2.0.0
2132
tag-prefix: '[vV|version-]'
2233
";
23-
var config = ConfigReader.Read(new StringReader(text));
34+
SetupConfigFileContent(text);
35+
36+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
2437
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
2538
config.DevelopBranchTag.ShouldBe("alpha");
2639
config.ReleaseBranchTag.ShouldBe("rc");
2740
config.NextVersion.ShouldBe("2.0.0");
2841
config.TagPrefix.ShouldBe("[vV|version-]");
2942
}
3043

44+
[Test]
45+
public void CanReadOldDocument()
46+
{
47+
const string text = @"assemblyVersioningScheme: MajorMinor";
48+
SetupConfigFileContent(text);
49+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
50+
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
51+
}
52+
3153
[Test]
3254
public void CanReadDefaultDocument()
3355
{
3456
const string text = "";
35-
var config = ConfigReader.Read(new StringReader(text));
57+
SetupConfigFileContent(text);
58+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
3659
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinorPatch);
3760
config.DevelopBranchTag.ShouldBe("unstable");
3861
config.ReleaseBranchTag.ShouldBe("beta");
@@ -64,4 +87,9 @@ public void VerifyAliases()
6487

6588
propertiesMissingAlias.ShouldBeEmpty();
6689
}
90+
91+
void SetupConfigFileContent(string text)
92+
{
93+
fileSystem.WriteAllText(Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml"), text);
94+
}
6795
}

GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<Link>Helpers\DirectoryHelper.cs</Link>
7171
</Compile>
7272
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
73-
<Compile Include="ConfigReaderTests.cs" />
73+
<Compile Include="ConfigProviderTests.cs" />
7474
<Compile Include="GitDirFinderTests.cs" />
7575
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
7676
<Compile Include="IntegrationTests\GitFlow\DevelopScenarios.cs" />
@@ -89,12 +89,13 @@
8989
<Compile Include="JsonVersionBuilderTests.cs" />
9090
<Compile Include="ModuleInitializer.cs" />
9191
<Compile Include="Fixtures\EmptyRepositoryFixture.cs" />
92-
<Compile Include="Helpers\GitHelper.cs" />
92+
<Compile Include="Helpers\GitTestExtensions.cs" />
9393
<Compile Include="Helpers\PathHelper.cs" />
9494
<Compile Include="IntegrationTests\GitHubFlow\MasterTests.cs" />
9595
<Compile Include="ApprovalTestsConfig.cs" />
9696
<Compile Include="Fixtures\RepositoryFixtureBase.cs" />
9797
<Compile Include="SemanticVersionTests.cs" />
98+
<Compile Include="TestFileSystem.cs" />
9899
<Compile Include="VariableProviderTests.cs" />
99100
</ItemGroup>
100101
<ItemGroup>

GitVersionCore.Tests/Helpers/GitHelper.cs renamed to GitVersionCore.Tests/Helpers/GitTestExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using GitVersion;
55
using LibGit2Sharp;
66

7-
public static class GitHelper
7+
public static class GitTestExtensions
88
{
99
public static Commit MakeACommit(this IRepository repository)
1010
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace GitVersionCore.Tests
2+
{
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using GitVersion.Helpers;
6+
7+
public class TestFileSystem : IFileSystem
8+
{
9+
Dictionary<string, string> fileSystem = new Dictionary<string, string>();
10+
11+
public void Copy(string @from, string to, bool overwrite)
12+
{
13+
throw new System.NotImplementedException();
14+
}
15+
16+
public void Move(string @from, string to)
17+
{
18+
throw new System.NotImplementedException();
19+
}
20+
21+
public bool Exists(string file)
22+
{
23+
return fileSystem.ContainsKey(file);
24+
}
25+
26+
public void Delete(string path)
27+
{
28+
throw new System.NotImplementedException();
29+
}
30+
31+
public string ReadAllText(string path)
32+
{
33+
return fileSystem[path];
34+
}
35+
36+
public void WriteAllText(string file, string fileContents)
37+
{
38+
if (fileSystem.ContainsKey(file))
39+
fileSystem[file] = fileContents;
40+
else
41+
fileSystem.Add(file, fileContents);
42+
}
43+
44+
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
45+
{
46+
throw new System.NotImplementedException();
47+
}
48+
49+
public Stream OpenWrite(string path)
50+
{
51+
throw new System.NotImplementedException();
52+
}
53+
}
54+
}

GitVersionCore/Configuration/ConfigReader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System.IO;
2-
using YamlDotNet.Serialization;
3-
using YamlDotNet.Serialization.NamingConventions;
4-
5-
namespace GitVersion
1+
namespace GitVersion
62
{
3+
using System.IO;
4+
using YamlDotNet.Serialization;
5+
using YamlDotNet.Serialization.NamingConventions;
6+
77
public class ConfigReader
88
{
99
public static Config Read(TextReader reader)
1010
{
11-
var deserializer = new Deserializer(null, new CamelCaseNamingConvention());
11+
var deserializer = new Deserializer(null, new HyphenatedNamingConvention());
1212
var deserialize = deserializer.Deserialize<Config>(reader);
1313
if (deserialize == null)
1414
{

GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
namespace GitVersion
22
{
33
using System.IO;
4+
using System.Text.RegularExpressions;
5+
using GitVersion.Helpers;
46

57
public class ConfigurationProvider
68
{
7-
public static Config Provide(string gitDirectory)
9+
static Regex oldAssemblyVersioningScheme = new Regex("assemblyVersioningScheme", RegexOptions.Compiled | RegexOptions.IgnoreCase);
10+
11+
public static Config Provide(string gitDirectory, IFileSystem fileSystem)
812
{
913
var configFilePath = GetConfigFilePath(gitDirectory);
10-
if (File.Exists(configFilePath))
14+
15+
if (fileSystem.Exists(configFilePath))
1116
{
12-
using (var reader = File.OpenText(configFilePath))
17+
var readAllText = fileSystem.ReadAllText(configFilePath);
18+
if (oldAssemblyVersioningScheme.IsMatch(readAllText))
1319
{
14-
return ConfigReader.Read(reader);
20+
readAllText = oldAssemblyVersioningScheme.Replace(readAllText, "assembly-versioning-scheme");
21+
fileSystem.WriteAllText(configFilePath, readAllText);
22+
Logger.WriteWarning("Found legacy configuration value 'assemblyVersioningScheme', replaced with 'assembly-versioning-scheme");
1523
}
24+
25+
return ConfigReader.Read(new StringReader(readAllText));
1626
}
1727

1828
return new Config();
1929
}
2030

21-
public static void WriteSample(string gitDirectory)
31+
public static void WriteSample(string gitDirectory, IFileSystem fileSystem)
2232
{
2333
var configFilePath = GetConfigFilePath(gitDirectory);
2434

25-
if (!File.Exists(configFilePath))
35+
if (!fileSystem.Exists(configFilePath))
2636
{
27-
using (var stream = File.OpenWrite(configFilePath))
37+
using (var stream = fileSystem.OpenWrite(configFilePath))
2838
using (var writer = new StreamWriter(stream))
2939
{
3040
ConfigReader.WriteSample(writer);
3141
}
3242
}
33-
// TODO else write warning?
43+
else
44+
{
45+
Logger.WriteError("Cannot write sample, GitVersionConfig.yaml already exists");
46+
}
3447
}
3548

3649
static string GetConfigFilePath(string gitDirectory)

GitVersionCore/GitVersionCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
<Compile Include="Configuration\ConfigurationProvider.cs" />
7676
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
7777
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
78+
<Compile Include="Helpers\FileSystem.cs" />
79+
<Compile Include="Helpers\IFileSystem.cs" />
7880
<Compile Include="LastMinorVersionFinder.cs" />
7981
<Compile Include="SemanticVersionExtensions.cs" />
8082
<Compile Include="WarningException.cs" />

GitVersionExe/FileSystem.cs renamed to GitVersionCore/Helpers/FileSystem.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
namespace GitVersion
1+
namespace GitVersion.Helpers
22
{
33
using System.Collections.Generic;
44
using System.IO;
55

6-
class FileSystem : IFileSystem
6+
public class FileSystem : IFileSystem
77
{
88
public void Copy(string @from, string to, bool overwrite)
99
{
@@ -39,5 +39,10 @@ public IEnumerable<string> DirectoryGetFiles(string directory, string searchPatt
3939
{
4040
return Directory.GetFiles(directory, searchPattern, searchOption);
4141
}
42+
43+
public Stream OpenWrite(string path)
44+
{
45+
return File.OpenWrite(path);
46+
}
4247
}
4348
}

GitVersionExe/IFileSystem.cs renamed to GitVersionCore/Helpers/IFileSystem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GitVersion
1+
namespace GitVersion.Helpers
22
{
33
using System.Collections.Generic;
44
using System.IO;
@@ -12,5 +12,6 @@ public interface IFileSystem
1212
string ReadAllText(string path);
1313
void WriteAllText(string file, string fileContents);
1414
IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption);
15+
Stream OpenWrite(string path);
1516
}
1617
}

GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using GitVersion;
7+
using GitVersion.Helpers;
78
using NSubstitute;
89
using NUnit.Framework;
910

GitVersionExe/AssemblyInfoFileUpdate.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace GitVersion
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using GitVersion.Helpers;
67

78
class AssemblyInfoFileUpdate : IDisposable
89
{

GitVersionExe/GitVersionExe.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@
5555
<Compile Include="ArgumentParser.cs" />
5656
<Compile Include="Arguments.cs" />
5757
<Compile Include="ExtensionMethods.cs" />
58-
<Compile Include="FileSystem.cs" />
5958
<Compile Include="GitPreparer.cs" />
6059
<Compile Include="HelpWriter.cs" />
61-
<Compile Include="IFileSystem.cs" />
6260
<Compile Include="ProcessHelper.cs" />
6361
<Compile Include="Program.cs" />
6462
<Compile Include="AssemblyInfo.cs" />

GitVersionExe/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace GitVersion
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9+
using GitVersion.Helpers;
910

1011
class Program
1112
{
@@ -69,9 +70,10 @@ static int Run()
6970
return 1;
7071
}
7172

73+
var fileSystem = new FileSystem();
7274
if (arguments.Init)
7375
{
74-
ConfigurationProvider.WriteSample(gitDirectory);
76+
ConfigurationProvider.WriteSample(gitDirectory, fileSystem);
7577
return 0;
7678
}
7779

@@ -85,7 +87,7 @@ static int Run()
8587
}
8688
SemanticVersion semanticVersion;
8789
var versionFinder = new GitVersionFinder();
88-
var configuration = ConfigurationProvider.Provide(gitDirectory);
90+
var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem);
8991
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
9092
{
9193
var gitVersionContext = new GitVersionContext(repo, configuration);
@@ -128,7 +130,7 @@ static int Run()
128130
}
129131

130132

131-
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, new FileSystem()))
133+
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, fileSystem))
132134
{
133135
var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables);
134136
var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables);

GitVersionTask.Tests/GitVersionTask.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@
231231
<Content Include="Resources\.gitattributes" />
232232
</ItemGroup>
233233
<ItemGroup>
234+
<ProjectReference Include="..\GitVersionCore.Tests\GitVersionCore.Tests.csproj">
235+
<Project>{BF905F84-382C-440D-92F5-C61108626D8D}</Project>
236+
<Name>GitVersionCore.Tests</Name>
237+
</ProjectReference>
234238
<ProjectReference Include="..\GitVersionCore\GitVersionCore.csproj">
235239
<Project>{F9741A0D-B9D7-4557-9A1C-A7252C1071F5}</Project>
236240
<Name>GitVersionCore</Name>

GitVersionTask.Tests/UpdateAssemblyInfoTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void StandardExecutionMode_ThrowsUponUnexpectedAssemblyVersioningSchemes(
112112
AssemblyVersioningScheme = "Boom"
113113
};
114114

115-
var exception = Assert.Throws<WarningException>(task.InnerExecute);
115+
var exception = Assert.Throws<WarningException>(() => task.InnerExecute());
116116
Assert.AreEqual("Unexpected assembly versioning scheme 'Boom'.", exception.Message);
117117
}
118118

0 commit comments

Comments
 (0)