Skip to content

Commit 28c8531

Browse files
committed
Merge pull request #365 from JakeGinnivan/ArgumentParserImprovements
Argument parser improvements
2 parents b7e2826 + 710b088 commit 28c8531

17 files changed

+277
-139
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
assembly-versioning-scheme: MajorMinorPatch
2+
mode: ContinuousDelivery
3+
tag-prefix: '[vV]'
4+
branches:
5+
master:
6+
tag:
7+
increment: Patch
8+
preventIncrementOfMergedBranchVersion: true
9+
release[/-]:
10+
tag: beta
11+
feature[/-]:
12+
tag: useBranchName
13+
increment: Inherit
14+
hotfix[/-]:
15+
tag: beta
16+
support[/-]:
17+
tag:
18+
increment: Patch
19+
preventIncrementOfMergedBranchVersion: true
20+
develop:
21+
mode: ContinuousDeployment
22+
tag: unstable
23+
increment: Minor
24+
(pull|pull\-requests|pr)[/-]:
25+
tag: PullRequest
26+
increment: Inherit
27+
tagNumberPattern: '[/-](?<number>\d+)[-/]'

GitVersionCore.Tests/ConfigProviderTests.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Reflection;
5+
using ApprovalTests;
56
using GitVersion;
67
using GitVersion.Helpers;
78
using NUnit.Framework;
@@ -34,7 +35,7 @@ public void CanReadDocument()
3435
mode: ContinuousDeployment
3536
tag: dev
3637
release[/-]:
37-
mode: ContinuousDeployment
38+
mode: continuousDeployment
3839
tag: rc
3940
";
4041
SetupConfigFileContent(text);
@@ -66,6 +67,34 @@ assemblyVersioningScheme has been replaced by assembly-versioning-scheme
6667
release-branch-tag has been replaced by branch specific configuration.See https://github.com/ParticularLabs/GitVersion/wiki/Branch-Specific-Configuration");
6768
}
6869

70+
[Test]
71+
public void OverwritesDefaultsWithProvidedConfig()
72+
{
73+
const string text = @"
74+
next-version: 2.0.0
75+
branches:
76+
develop:
77+
mode: ContinuousDeployment
78+
tag: dev";
79+
SetupConfigFileContent(text);
80+
var defaultConfig = new Config();
81+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
82+
83+
config.NextVersion.ShouldBe("2.0.0");
84+
config.AssemblyVersioningScheme.ShouldBe(defaultConfig.AssemblyVersioningScheme);
85+
config.Branches["develop"].Increment.ShouldBe(defaultConfig.Branches["develop"].Increment);
86+
config.Branches["develop"].VersioningMode.ShouldBe(defaultConfig.Branches["develop"].VersioningMode);
87+
config.Branches["develop"].Tag.ShouldBe("dev");
88+
}
89+
90+
[Test]
91+
public void CanWriteOutEffectiveConfiguration()
92+
{
93+
var config = ConfigurationProvider.GetEffectiveConfigAsString(gitDirectory, fileSystem);
94+
95+
Approvals.Verify(config);
96+
}
97+
6998
[Test]
7099
public void CanReadDefaultDocument()
71100
{
@@ -88,7 +117,7 @@ public void VerifyInit()
88117
.Select(p => ((YamlMemberAttribute) p.GetCustomAttribute(typeof(YamlMemberAttribute))).Alias);
89118
var writer = new StringWriter();
90119

91-
ConfigReader.WriteSample(writer);
120+
ConfigSerialiser.WriteSample(writer);
92121
var initFile = writer.GetStringBuilder().ToString();
93122

94123
foreach (var alias in aliases)

GitVersionCore.Tests/GitVersionCore.Tests.v2.ncrunchproject

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
2424
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
2525
<AdditionalFilesToInclude>..\Packages\LibGit2Sharp.0.19.0.0\lib\net40\NativeBinaries\**.*;Resources\**.*</AdditionalFilesToInclude>
26-
<HiddenWarnings>PostBuildEventDisabled</HiddenWarnings>
26+
<HiddenWarnings>PostBuildEventDisabled;CopyReferencedAssembliesToWorkspaceIsOn</HiddenWarnings>
2727
</ProjectConfiguration>

GitVersionCore/Configuration/ConfigReader.cs renamed to GitVersionCore/Configuration/ConfigSerialiser.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using YamlDotNet.Serialization;
55
using YamlDotNet.Serialization.NamingConventions;
66

7-
public class ConfigReader
7+
public class ConfigSerialiser
88
{
99
public static Config Read(TextReader reader)
1010
{
@@ -17,6 +17,12 @@ public static Config Read(TextReader reader)
1717
return deserialize;
1818
}
1919

20+
public static void Write(Config config, TextWriter writer)
21+
{
22+
var serializer = new Serializer(SerializationOptions.None, new HyphenatedNamingConvention());
23+
serializer.Serialize(writer, config);
24+
}
25+
2026
public static void WriteSample(TextWriter writer)
2127
{
2228
writer.WriteLine("# assembly-versioning-scheme: MajorMinorPatchMetadata | MajorMinorPatch | MajorMinor | Major");

GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
namespace GitVersion
22
{
33
using System.IO;
4-
using System.Text.RegularExpressions;
4+
using System.Text;
55
using GitVersion.Helpers;
66

77
public class ConfigurationProvider
88
{
9-
static Regex oldAssemblyVersioningScheme = new Regex("assemblyVersioningScheme", RegexOptions.Compiled | RegexOptions.IgnoreCase);
10-
119
public static Config Provide(string gitDirectory, IFileSystem fileSystem)
1210
{
1311
var configFilePath = GetConfigFilePath(gitDirectory);
@@ -17,12 +15,24 @@ public static Config Provide(string gitDirectory, IFileSystem fileSystem)
1715
var readAllText = fileSystem.ReadAllText(configFilePath);
1816
LegacyConfigNotifier.Notify(new StringReader(readAllText));
1917

20-
return ConfigReader.Read(new StringReader(readAllText));
18+
return ConfigSerialiser.Read(new StringReader(readAllText));
2119
}
2220

2321
return new Config();
2422
}
2523

24+
public static string GetEffectiveConfigAsString(string gitDirectory, IFileSystem fileSystem)
25+
{
26+
var config = Provide(gitDirectory, fileSystem);
27+
var stringBuilder = new StringBuilder();
28+
using (var stream = new StringWriter(stringBuilder))
29+
{
30+
ConfigSerialiser.Write(config, stream);
31+
stream.Flush();
32+
}
33+
return stringBuilder.ToString();
34+
}
35+
2636
public static void WriteSample(string gitDirectory, IFileSystem fileSystem)
2737
{
2838
var configFilePath = GetConfigFilePath(gitDirectory);
@@ -32,7 +42,7 @@ public static void WriteSample(string gitDirectory, IFileSystem fileSystem)
3242
using (var stream = fileSystem.OpenWrite(configFilePath))
3343
using (var writer = new StreamWriter(stream))
3444
{
35-
ConfigReader.WriteSample(writer);
45+
ConfigSerialiser.WriteSample(writer);
3646
}
3747
}
3848
else

GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<Compile Include="BuildServers\TeamCity.cs" />
7575
<Compile Include="Configuration\BranchConfig.cs" />
7676
<Compile Include="Configuration\Config.cs" />
77-
<Compile Include="Configuration\ConfigReader.cs" />
77+
<Compile Include="Configuration\ConfigSerialiser.cs" />
7878
<Compile Include="Configuration\ConfigurationProvider.cs" />
7979
<Compile Include="Configuration\IncrementStrategy.cs" />
8080
<Compile Include="Configuration\LegacyConfig.cs" />

GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,31 @@
77
[TestFixture]
88
public class ArgumentParserTests
99
{
10-
1110
[Test]
1211
public void Empty_means_use_current_directory()
1312
{
1413
var arguments = ArgumentParser.ParseArguments("");
15-
Assert.AreEqual(Environment.CurrentDirectory, arguments.TargetPath);
16-
Assert.IsNull(arguments.LogFilePath);
17-
Assert.IsFalse(arguments.IsHelp);
14+
arguments.TargetPath.ShouldBe(Environment.CurrentDirectory);
15+
arguments.LogFilePath.ShouldBe(null);
16+
arguments.IsHelp.ShouldBe(false);
1817
}
1918

2019
[Test]
2120
public void Single_means_use_as_target_directory()
2221
{
2322
var arguments = ArgumentParser.ParseArguments("path");
24-
Assert.AreEqual("path", arguments.TargetPath);
25-
Assert.IsNull(arguments.LogFilePath);
26-
Assert.IsFalse(arguments.IsHelp);
23+
arguments.TargetPath.ShouldBe("path");
24+
arguments.LogFilePath.ShouldBe(null);
25+
arguments.IsHelp.ShouldBe(false);
2726
}
2827

2928
[Test]
3029
public void No_path_and_logfile_should_use_current_directory_TargetDirectory()
3130
{
3231
var arguments = ArgumentParser.ParseArguments("-l logFilePath");
33-
Assert.AreEqual(Environment.CurrentDirectory, arguments.TargetPath);
34-
Assert.AreEqual("logFilePath", arguments.LogFilePath);
35-
Assert.IsFalse(arguments.IsHelp);
32+
arguments.TargetPath.ShouldBe(Environment.CurrentDirectory);
33+
arguments.LogFilePath.ShouldBe("logFilePath");
34+
arguments.IsHelp.ShouldBe(false);
3635
}
3736

3837
[Test]
@@ -41,14 +40,14 @@ public void h_means_IsHelp()
4140
var arguments = ArgumentParser.ParseArguments("-h");
4241
Assert.IsNull(arguments.TargetPath);
4342
Assert.IsNull(arguments.LogFilePath);
44-
Assert.IsTrue(arguments.IsHelp);
43+
arguments.IsHelp.ShouldBe(true);
4544
}
4645

4746
[Test]
4847
public void exec()
4948
{
5049
var arguments = ArgumentParser.ParseArguments("-exec rake");
51-
Assert.AreEqual("rake", arguments.Exec);
50+
arguments.Exec.ShouldBe("rake");
5251
}
5352

5453
[Test]
@@ -61,15 +60,15 @@ public void exec_with_args()
6160
"-execargs",
6261
"clean build"
6362
});
64-
Assert.AreEqual("rake", arguments.Exec);
65-
Assert.AreEqual("clean build", arguments.ExecArgs);
63+
arguments.Exec.ShouldBe("rake");
64+
arguments.ExecArgs.ShouldBe("clean build");
6665
}
6766

6867
[Test]
6968
public void msbuild()
7069
{
7170
var arguments = ArgumentParser.ParseArguments("-proj msbuild.proj");
72-
Assert.AreEqual("msbuild.proj", arguments.Proj);
71+
arguments.Proj.ShouldBe("msbuild.proj");
7372
}
7473

7574
[Test]
@@ -82,73 +81,80 @@ public void msbuild_with_args()
8281
"-projargs",
8382
"/p:Configuration=Debug /p:Platform=AnyCPU"
8483
});
85-
Assert.AreEqual("msbuild.proj", arguments.Proj);
86-
Assert.AreEqual("/p:Configuration=Debug /p:Platform=AnyCPU", arguments.ProjArgs);
84+
arguments.Proj.ShouldBe("msbuild.proj");
85+
arguments.ProjArgs.ShouldBe("/p:Configuration=Debug /p:Platform=AnyCPU");
8786
}
8887

8988
[Test]
9089
public void execwith_targetdirectory()
9190
{
9291
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -exec rake");
93-
Assert.AreEqual("targetDirectoryPath", arguments.TargetPath);
94-
Assert.AreEqual("rake", arguments.Exec);
92+
arguments.TargetPath.ShouldBe("targetDirectoryPath");
93+
arguments.Exec.ShouldBe("rake");
9594
}
9695

9796
[Test]
9897
public void TargetDirectory_and_LogFilePath_can_be_parsed()
9998
{
10099
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -l logFilePath");
101-
Assert.AreEqual("targetDirectoryPath", arguments.TargetPath);
102-
Assert.AreEqual("logFilePath", arguments.LogFilePath);
103-
Assert.IsFalse(arguments.IsHelp);
100+
arguments.TargetPath.ShouldBe("targetDirectoryPath");
101+
arguments.LogFilePath.ShouldBe("logFilePath");
102+
arguments.IsHelp.ShouldBe(false);
104103
}
105104

106105
[Test]
107106
public void Username_and_Password_can_be_parsed()
108107
{
109108
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -u [username] -p [password]");
110-
Assert.AreEqual("targetDirectoryPath", arguments.TargetPath);
111-
Assert.AreEqual("[username]", arguments.Authentication.Username);
112-
Assert.AreEqual("[password]", arguments.Authentication.Password);
113-
Assert.IsFalse(arguments.IsHelp);
109+
arguments.TargetPath.ShouldBe("targetDirectoryPath");
110+
arguments.Authentication.Username.ShouldBe("[username]");
111+
arguments.Authentication.Password.ShouldBe("[password]");
112+
arguments.IsHelp.ShouldBe(false);
114113
}
115114

116115
[Test]
117116
public void Unknown_output_should_throw()
118117
{
119118
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments("targetDirectoryPath -output invalid_value"));
120-
Assert.AreEqual("Value 'invalid_value' cannot be parsed as output type, please use 'json' or 'buildserver'", exception.Message);
119+
exception.Message.ShouldBe("Value 'invalid_value' cannot be parsed as output type, please use 'json' or 'buildserver'");
121120
}
122121

123122
[Test]
124123
public void Output_defaults_to_json()
125124
{
126125
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath");
127-
Assert.AreEqual(OutputType.Json, arguments.Output);
126+
arguments.Output.ShouldBe(OutputType.Json);
128127
}
129128

130129
[Test]
131130
public void Output_json_can_be_parsed()
132131
{
133132
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -output json");
134-
Assert.AreEqual(OutputType.Json, arguments.Output);
133+
arguments.Output.ShouldBe(OutputType.Json);
135134
}
136135

137136
[Test]
138137
public void Output_buildserver_can_be_parsed()
139138
{
140139
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -output buildserver");
141-
Assert.AreEqual(OutputType.BuildServer, arguments.Output);
140+
arguments.Output.ShouldBe(OutputType.BuildServer);
141+
}
142+
143+
[Test]
144+
public void MultipleArgsAndFlag()
145+
{
146+
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -output buildserver -updateAssemblyInfo");
147+
arguments.Output.ShouldBe(OutputType.BuildServer);
142148
}
143149

144150
[Test]
145151
public void Url_and_BranchName_can_be_parsed()
146152
{
147153
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -url http://github.com/Particular/GitVersion.git -b somebranch");
148-
Assert.AreEqual("targetDirectoryPath", arguments.TargetPath);
149-
Assert.AreEqual("http://github.com/Particular/GitVersion.git", arguments.TargetUrl);
150-
Assert.AreEqual("somebranch", arguments.TargetBranch);
151-
Assert.IsFalse(arguments.IsHelp);
154+
arguments.TargetPath.ShouldBe("targetDirectoryPath");
155+
arguments.TargetUrl.ShouldBe("http://github.com/Particular/GitVersion.git");
156+
arguments.TargetBranch.ShouldBe("somebranch");
157+
arguments.IsHelp.ShouldBe(false);
152158
}
153159

154160
[Test]
@@ -162,11 +168,12 @@ public void Wrong_number_of_arguments_should_throw()
162168
public void Unknown_argument_should_throw()
163169
{
164170
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments("targetDirectoryPath -x logFilePath"));
165-
Assert.AreEqual("Could not parse command line parameter '-x'.", exception.Message);
171+
exception.Message.ShouldBe("Could not parse command line parameter '-x'.");
166172
}
167173

168174
[TestCase("-updateAssemblyInfo true")]
169175
[TestCase("-updateAssemblyInfo 1")]
176+
[TestCase("-updateAssemblyInfo")]
170177
[TestCase("-updateAssemblyInfo -proj foo.sln")]
171178
public void update_assembly_info_true(string command)
172179
{
@@ -198,23 +205,6 @@ public void update_assembly_info_with_relative_filename()
198205
arguments.UpdateAssemblyInfoFileName.ShouldBe("..\\..\\CommonAssemblyInfo.cs");
199206
}
200207

201-
[Test]
202-
public void update_assembly_info_with_assembly_version_format()
203-
{
204-
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo true -assemblyVersionFormat MajorMinorPatch");
205-
arguments.UpdateAssemblyInfo.ShouldBe(true);
206-
arguments.AssemblyVersionFormat.ShouldBe("MajorMinorPatch");
207-
}
208-
209-
[Test]
210-
public void update_assembly_info_with_filename_and_assembly_version_format()
211-
{
212-
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs -assemblyVersionFormat MajorMinorPatch");
213-
arguments.UpdateAssemblyInfo.ShouldBe(true);
214-
arguments.AssemblyVersionFormat.ShouldBe("MajorMinorPatch");
215-
arguments.UpdateAssemblyInfoFileName.ShouldBe("CommonAssemblyInfo.cs");
216-
}
217-
218208
[Test]
219209
public void can_log_to_console()
220210
{

0 commit comments

Comments
 (0)