Skip to content

Commit b77694a

Browse files
committed
Merge pull request #282 from okb/always-set-exit-code
Always set exit code
2 parents 83ded31 + c457b11 commit b77694a

File tree

5 files changed

+163
-35
lines changed

5 files changed

+163
-35
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Text;
2+
3+
public class ArgumentBuilder
4+
{
5+
public ArgumentBuilder(string workingDirectory)
6+
{
7+
this.workingDirectory = workingDirectory;
8+
}
9+
10+
11+
public ArgumentBuilder(string workingDirectory, string exec, string execArgs, string projectFile, string projectArgs, string logFile, bool isTeamCity)
12+
{
13+
this.workingDirectory = workingDirectory;
14+
this.exec = exec;
15+
this.execArgs = execArgs;
16+
this.projectFile = projectFile;
17+
this.projectArgs = projectArgs;
18+
this.logFile = logFile;
19+
this.isTeamCity = isTeamCity;
20+
}
21+
22+
public ArgumentBuilder(string workingDirectory, string additionalArguments, bool isTeamCity)
23+
{
24+
this.workingDirectory = workingDirectory;
25+
this.isTeamCity = isTeamCity;
26+
this.additionalArguments = additionalArguments;
27+
}
28+
29+
30+
public string WorkingDirectory
31+
{
32+
get { return workingDirectory; }
33+
}
34+
35+
36+
public string LogFile
37+
{
38+
get { return logFile; }
39+
}
40+
41+
public bool IsTeamCity
42+
{
43+
get { return isTeamCity; }
44+
}
45+
46+
47+
public override string ToString()
48+
{
49+
var arguments = new StringBuilder();
50+
51+
arguments.AppendFormat("\"{0}\"", workingDirectory);
52+
53+
if (!string.IsNullOrWhiteSpace(exec))
54+
{
55+
arguments.AppendFormat(" /exec \"{0}\"", exec);
56+
}
57+
58+
if (!string.IsNullOrWhiteSpace(execArgs))
59+
{
60+
arguments.AppendFormat(" /execArgs \"{0}\"", execArgs);
61+
}
62+
63+
if (!string.IsNullOrWhiteSpace(projectFile))
64+
{
65+
arguments.AppendFormat(" /proj \"{0}\"", projectFile);
66+
}
67+
68+
if (!string.IsNullOrWhiteSpace(projectArgs))
69+
{
70+
arguments.AppendFormat(" /projargs \"{0}\"", projectArgs);
71+
}
72+
73+
arguments.Append(additionalArguments);
74+
75+
if (!string.IsNullOrWhiteSpace(logFile))
76+
{
77+
arguments.AppendFormat(" /l \"{0}\"", logFile);
78+
}
79+
80+
return arguments.ToString();
81+
}
82+
83+
84+
readonly string additionalArguments;
85+
readonly string exec;
86+
readonly string execArgs;
87+
readonly bool isTeamCity;
88+
readonly string logFile;
89+
readonly string projectArgs;
90+
readonly string projectFile;
91+
readonly string workingDirectory;
92+
}

GitVersionExe.Tests/ExecCmdLineArgumentTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,28 @@ public void RunExecViaCommandLine()
3030
result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1");
3131
}
3232
}
33+
34+
[Test]
35+
public void InvalidArgumentsExitCodeShouldNotBeZero()
36+
{
37+
using (var fixture = new EmptyRepositoryFixture())
38+
{
39+
fixture.Repository.MakeATaggedCommit("1.2.3");
40+
fixture.Repository.MakeACommit();
41+
42+
var buildFile = Path.Combine(fixture.RepositoryPath, "RunExecViaCommandLine.proj");
43+
File.Delete(buildFile);
44+
const string buildFileContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
45+
<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
46+
<Target Name=""OutputResults"">
47+
<Message Text=""GitVersion_FullSemVer: $(GitVersion_FullSemVer)""/>
48+
</Target>
49+
</Project>";
50+
File.WriteAllText(buildFile, buildFileContent);
51+
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /invalid-argument");
52+
53+
result.ExitCode.ShouldBe(1);
54+
result.Output.ShouldContain("Failed to parse arguments");
55+
}
56+
}
3357
}

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: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,47 @@ public static ExecutionResults ExecuteIn(string workingDirectory,
1212
bool isTeamCity = false)
1313
{
1414
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);
15+
var args = new ArgumentBuilder(workingDirectory, exec, execArgs, projectFile, projectArgs, logFile, isTeamCity);
16+
return ExecuteIn(args);
17+
}
18+
19+
public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false)
20+
{
21+
var args = new ArgumentBuilder(workingDirectory, arguments, isTeamCity);
22+
return ExecuteIn(args);
23+
}
2324

25+
static ExecutionResults ExecuteIn(ArgumentBuilder arguments)
26+
{
27+
var gitHubFlowVersion = Path.Combine(PathHelper.GetCurrentDirectory(), "GitVersion.exe");
2428
var output = new StringBuilder();
2529

2630
Console.WriteLine("Executing: {0} {1}", gitHubFlowVersion, arguments);
2731
Console.WriteLine();
2832
var environmentalVariables =
2933
new[]
3034
{
31-
new KeyValuePair<string, string>("TEAMCITY_VERSION", isTeamCity ? "8.0.0" : null)
35+
new KeyValuePair<string, string>("TEAMCITY_VERSION", arguments.IsTeamCity ? "8.0.0" : null)
3236
};
3337

3438
var exitCode = ProcessHelper.Run(
3539
s => output.AppendLine(s), s => output.AppendLine(s), null,
36-
gitHubFlowVersion, arguments, workingDirectory,
40+
gitHubFlowVersion, arguments.ToString(), arguments.WorkingDirectory,
3741
environmentalVariables);
3842

39-
var logContents = File.ReadAllText(logFile);
4043
Console.WriteLine("Output from GitVersion.exe");
4144
Console.WriteLine("-------------------------------------------------------");
4245
Console.WriteLine(output.ToString());
4346
Console.WriteLine();
4447
Console.WriteLine();
4548
Console.WriteLine("-------------------------------------------------------");
49+
50+
if (string.IsNullOrWhiteSpace(arguments.LogFile))
51+
{
52+
return new ExecutionResults(exitCode, output.ToString(), null);
53+
}
54+
55+
var logContents = File.ReadAllText(arguments.LogFile);
4656
Console.WriteLine("Log from GitVersion.exe");
4757
Console.WriteLine("-------------------------------------------------------");
4858
Console.WriteLine(logContents);

GitVersionExe/Program.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,24 @@ class Program
1414

1515
static void Main()
1616
{
17-
int? exitCode = null;
17+
var exitCode = Run();
1818

19+
if (Debugger.IsAttached)
20+
{
21+
Console.ReadKey();
22+
}
23+
24+
if (exitCode != 0)
25+
{
26+
// Dump log to console if we fail to complete successfully
27+
Console.Write(log.ToString());
28+
}
29+
30+
Environment.Exit(exitCode);
31+
}
32+
33+
static int Run()
34+
{
1935
try
2036
{
2137
Arguments arguments;
@@ -29,17 +45,19 @@ static void Main()
2945
Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName));
3046

3147
HelpWriter.Write();
32-
return;
48+
return 1;
3349
}
3450

3551
if (arguments.IsHelp)
3652
{
3753
HelpWriter.Write();
38-
return;
54+
return 0;
3955
}
4056

4157
if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec))
58+
{
4259
arguments.Output = OutputType.BuildServer;
60+
}
4361

4462
ConfigureLogging(arguments);
4563

@@ -48,7 +66,7 @@ static void Main()
4866
if (string.IsNullOrEmpty(gitDirectory))
4967
{
5068
Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath);
51-
Environment.Exit(1);
69+
return 1;
5270
}
5371

5472
var workingDirectory = Directory.GetParent(gitDirectory).FullName;
@@ -119,33 +137,16 @@ static void Main()
119137
{
120138
var error = string.Format("An error occurred:\r\n{0}", exception.Message);
121139
Logger.WriteWarning(error);
122-
123-
exitCode = 1;
140+
return 1;
124141
}
125142
catch (Exception exception)
126143
{
127144
var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
128145
Logger.WriteError(error);
129-
130-
exitCode = 1;
131-
}
132-
133-
if (Debugger.IsAttached)
134-
{
135-
Console.ReadKey();
136-
}
137-
138-
if (!exitCode.HasValue)
139-
{
140-
exitCode = 0;
141-
}
142-
else
143-
{
144-
// Dump log to console if we fail to complete successfully
145-
Console.Write(log.ToString());
146+
return 1;
146147
}
147148

148-
Environment.Exit(exitCode.Value);
149+
return 0;
149150
}
150151

151152
static IEnumerable<IBuildServer> GetApplicableBuildServers(Authentication authentication)

0 commit comments

Comments
 (0)