Skip to content

Commit 06f55b1

Browse files
committed
Added ArgumentBuilder class to handle argument building and overloads to GitVersionHelper in preparation for a test that needs to pass in invalid arguments.
1 parent 36ea690 commit 06f55b1

File tree

3 files changed

+96
-12
lines changed

3 files changed

+96
-12
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.IO;
2+
using System.Text;
3+
4+
public class ArgumentBuilder
5+
{
6+
public ArgumentBuilder(string workingDirectory)
7+
{
8+
this.workingDirectory = workingDirectory;
9+
}
10+
11+
public string WorkingDirectory
12+
{
13+
get { return workingDirectory; }
14+
}
15+
16+
public string Exec { get; set; }
17+
18+
public string ExecArgs { get; set; }
19+
20+
public string ProjectFile { get; set; }
21+
22+
public string ProjectArgs { get; set; }
23+
24+
public string LogFile { get; set; }
25+
26+
public bool IsTeamCity { get; set; }
27+
28+
public string AdditionalArguments { get; set; }
29+
30+
public override string ToString()
31+
{
32+
var arguments = new StringBuilder();
33+
34+
arguments.AppendFormat("\"{0}\"", WorkingDirectory);
35+
36+
if (!string.IsNullOrWhiteSpace(Exec))
37+
arguments.AppendFormat(" /exec \"{0}\"", Exec);
38+
39+
if (!string.IsNullOrWhiteSpace(ExecArgs))
40+
arguments.AppendFormat(" /execArgs \"{0}\"", ExecArgs);
41+
42+
if (!string.IsNullOrWhiteSpace(ProjectFile))
43+
arguments.AppendFormat(" /proj \"{0}\"", ProjectFile);
44+
45+
if (!string.IsNullOrWhiteSpace(ProjectArgs))
46+
arguments.AppendFormat(" /projargs \"{0}\"", ProjectArgs);
47+
48+
arguments.Append(AdditionalArguments);
49+
50+
if (!string.IsNullOrWhiteSpace(LogFile))
51+
{
52+
arguments.AppendFormat(" /l \"{0}\"", LogFile);
53+
}
54+
55+
return arguments.ToString();
56+
}
57+
58+
59+
readonly string workingDirectory;
60+
}

GitVersionExe.Tests/GitVersionExe.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<None Include="TestBuildFile.proj" />
7474
</ItemGroup>
7575
<ItemGroup>
76+
<Compile Include="ArgumentBuilder.cs" />
7677
<Compile Include="ArgumentParserTests.cs" />
7778
<Compile Include="ExecCmdLineArgumentTest.cs" />
7879
<Compile Include="ExecutionResults.cs" />

GitVersionExe.Tests/GitVersionHelper.cs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,61 @@ public static ExecutionResults ExecuteIn(string workingDirectory,
1111
string exec = null, string execArgs = null, string projectFile = null, string projectArgs = null,
1212
bool isTeamCity = false)
1313
{
14-
var logFile = Path.Combine(workingDirectory, "log.txt");
15-
var gitHubFlowVersion = Path.Combine(PathHelper.GetCurrentDirectory(), "GitVersion.exe");
16-
var execArg = exec == null ? null : string.Format(" /exec \"{0}\"", exec);
17-
var execArgsArg = execArgs == null ? null : string.Format(" /execArgs \"{0}\"", execArgs);
18-
var projectFileArg = projectFile == null ? null : string.Format(" /proj \"{0}\"", projectFile);
19-
var targetsArg = projectArgs == null ? null : string.Format(" /projargs \"{0}\"", projectArgs);
20-
var logArg = string.Format(" /l \"{0}\"", logFile);
21-
var arguments = string.Format("\"{0}\"{1}{2}{3}{4}{5}", workingDirectory, execArg, execArgsArg,
22-
projectFileArg, targetsArg, logArg);
14+
var args = new ArgumentBuilder(workingDirectory)
15+
{
16+
Exec = exec,
17+
ExecArgs = execArgs,
18+
ProjectFile = projectFile,
19+
ProjectArgs = projectArgs,
20+
IsTeamCity = isTeamCity,
21+
LogFile = Path.Combine(workingDirectory, "log.txt")
22+
};
23+
24+
return ExecuteIn(args);
25+
}
2326

27+
public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false)
28+
{
29+
var args = new ArgumentBuilder(workingDirectory)
30+
{
31+
AdditionalArguments = arguments,
32+
IsTeamCity = isTeamCity,
33+
};
34+
35+
return ExecuteIn(args);
36+
}
37+
38+
static ExecutionResults ExecuteIn(ArgumentBuilder arguments)
39+
{
40+
var gitHubFlowVersion = Path.Combine(PathHelper.GetCurrentDirectory(), "GitVersion.exe");
2441
var output = new StringBuilder();
2542

2643
Console.WriteLine("Executing: {0} {1}", gitHubFlowVersion, arguments);
2744
Console.WriteLine();
2845
var environmentalVariables =
2946
new[]
3047
{
31-
new KeyValuePair<string, string>("TEAMCITY_VERSION", isTeamCity ? "8.0.0" : null)
48+
new KeyValuePair<string, string>("TEAMCITY_VERSION", arguments.IsTeamCity ? "8.0.0" : null)
3249
};
3350

3451
var exitCode = ProcessHelper.Run(
3552
s => output.AppendLine(s), s => output.AppendLine(s), null,
36-
gitHubFlowVersion, arguments, workingDirectory,
53+
gitHubFlowVersion, arguments.ToString(), arguments.WorkingDirectory,
3754
environmentalVariables);
3855

39-
var logContents = File.ReadAllText(logFile);
4056
Console.WriteLine("Output from GitVersion.exe");
4157
Console.WriteLine("-------------------------------------------------------");
4258
Console.WriteLine(output.ToString());
4359
Console.WriteLine();
4460
Console.WriteLine();
4561
Console.WriteLine("-------------------------------------------------------");
62+
63+
if (string.IsNullOrWhiteSpace(arguments.LogFile))
64+
{
65+
return new ExecutionResults(exitCode, output.ToString(), null);
66+
}
67+
68+
var logContents = File.ReadAllText(arguments.LogFile);
4669
Console.WriteLine("Log from GitVersion.exe");
4770
Console.WriteLine("-------------------------------------------------------");
4871
Console.WriteLine(logContents);

0 commit comments

Comments
 (0)