Skip to content

Always set exit code #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GitVersion.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ II.2.12 <HandlesEvent />
<s:String x:Key="/Default/CodeStyle/Naming/VBNaming/PredefinedNamingRules/=TypesAndNamespaces/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsCodeFormatterSettingsUpgrader/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/UnitTesting/SeparateAppDomainPerAssembly/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/UnitTesting/ShadowCopy/@EntryValue">False</s:Boolean>



Expand Down
92 changes: 92 additions & 0 deletions GitVersionExe.Tests/ArgumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Text;

public class ArgumentBuilder
{
public ArgumentBuilder(string workingDirectory)
{
this.workingDirectory = workingDirectory;
}


public ArgumentBuilder(string workingDirectory, string exec, string execArgs, string projectFile, string projectArgs, string logFile, bool isTeamCity)
{
this.workingDirectory = workingDirectory;
this.exec = exec;
this.execArgs = execArgs;
this.projectFile = projectFile;
this.projectArgs = projectArgs;
this.logFile = logFile;
this.isTeamCity = isTeamCity;
}

public ArgumentBuilder(string workingDirectory, string additionalArguments, bool isTeamCity)
{
this.workingDirectory = workingDirectory;
this.isTeamCity = isTeamCity;
this.additionalArguments = additionalArguments;
}


public string WorkingDirectory
{
get { return workingDirectory; }
}


public string LogFile
{
get { return logFile; }
}

public bool IsTeamCity
{
get { return isTeamCity; }
}


public override string ToString()
{
var arguments = new StringBuilder();

arguments.AppendFormat("\"{0}\"", workingDirectory);

if (!string.IsNullOrWhiteSpace(exec))
{
arguments.AppendFormat(" /exec \"{0}\"", exec);
}

if (!string.IsNullOrWhiteSpace(execArgs))
{
arguments.AppendFormat(" /execArgs \"{0}\"", execArgs);
}

if (!string.IsNullOrWhiteSpace(projectFile))
{
arguments.AppendFormat(" /proj \"{0}\"", projectFile);
}

if (!string.IsNullOrWhiteSpace(projectArgs))
{
arguments.AppendFormat(" /projargs \"{0}\"", projectArgs);
}

arguments.Append(additionalArguments);

if (!string.IsNullOrWhiteSpace(logFile))
{
arguments.AppendFormat(" /l \"{0}\"", logFile);
}

return arguments.ToString();
}


readonly string additionalArguments;
readonly string exec;
readonly string execArgs;
readonly bool isTeamCity;
readonly string logFile;
readonly string projectArgs;
readonly string projectFile;
readonly string workingDirectory;
}
24 changes: 24 additions & 0 deletions GitVersionExe.Tests/ExecCmdLineArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ public void RunExecViaCommandLine()
result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1");
}
}

[Test]
public void InvalidArgumentsExitCodeShouldNotBeZero()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeATaggedCommit("1.2.3");
fixture.Repository.MakeACommit();

var buildFile = Path.Combine(fixture.RepositoryPath, "RunExecViaCommandLine.proj");
File.Delete(buildFile);
const string buildFileContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<Target Name=""OutputResults"">
<Message Text=""GitVersion_FullSemVer: $(GitVersion_FullSemVer)""/>
</Target>
</Project>";
File.WriteAllText(buildFile, buildFileContent);
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /invalid-argument");

result.ExitCode.ShouldBe(1);
result.Output.ShouldContain("Failed to parse arguments");
}
}
}
1 change: 1 addition & 0 deletions GitVersionExe.Tests/GitVersionExe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<None Include="TestBuildFile.proj" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentBuilder.cs" />
<Compile Include="ArgumentParserTests.cs" />
<Compile Include="ExecCmdLineArgumentTest.cs" />
<Compile Include="ExecutionResults.cs" />
Expand Down
32 changes: 21 additions & 11 deletions GitVersionExe.Tests/GitVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,47 @@ public static ExecutionResults ExecuteIn(string workingDirectory,
bool isTeamCity = false)
{
var logFile = Path.Combine(workingDirectory, "log.txt");
var gitHubFlowVersion = Path.Combine(PathHelper.GetCurrentDirectory(), "GitVersion.exe");
var execArg = exec == null ? null : string.Format(" /exec \"{0}\"", exec);
var execArgsArg = execArgs == null ? null : string.Format(" /execArgs \"{0}\"", execArgs);
var projectFileArg = projectFile == null ? null : string.Format(" /proj \"{0}\"", projectFile);
var targetsArg = projectArgs == null ? null : string.Format(" /projargs \"{0}\"", projectArgs);
var logArg = string.Format(" /l \"{0}\"", logFile);
var arguments = string.Format("\"{0}\"{1}{2}{3}{4}{5}", workingDirectory, execArg, execArgsArg,
projectFileArg, targetsArg, logArg);
var args = new ArgumentBuilder(workingDirectory, exec, execArgs, projectFile, projectArgs, logFile, isTeamCity);
return ExecuteIn(args);
}

public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false)
{
var args = new ArgumentBuilder(workingDirectory, arguments, isTeamCity);
return ExecuteIn(args);
}

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

Console.WriteLine("Executing: {0} {1}", gitHubFlowVersion, arguments);
Console.WriteLine();
var environmentalVariables =
new[]
{
new KeyValuePair<string, string>("TEAMCITY_VERSION", isTeamCity ? "8.0.0" : null)
new KeyValuePair<string, string>("TEAMCITY_VERSION", arguments.IsTeamCity ? "8.0.0" : null)
};

var exitCode = ProcessHelper.Run(
s => output.AppendLine(s), s => output.AppendLine(s), null,
gitHubFlowVersion, arguments, workingDirectory,
gitHubFlowVersion, arguments.ToString(), arguments.WorkingDirectory,
environmentalVariables);

var logContents = File.ReadAllText(logFile);
Console.WriteLine("Output from GitVersion.exe");
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine(output.ToString());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("-------------------------------------------------------");

if (string.IsNullOrWhiteSpace(arguments.LogFile))
{
return new ExecutionResults(exitCode, output.ToString(), null);
}

var logContents = File.ReadAllText(arguments.LogFile);
Console.WriteLine("Log from GitVersion.exe");
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine(logContents);
Expand Down
49 changes: 25 additions & 24 deletions GitVersionExe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@ class Program

static void Main()
{
int? exitCode = null;
var exitCode = Run();

if (Debugger.IsAttached)
{
Console.ReadKey();
}

if (exitCode != 0)
{
// Dump log to console if we fail to complete successfully
Console.Write(log.ToString());
}

Environment.Exit(exitCode);
}

static int Run()
{
try
{
Arguments arguments;
Expand All @@ -29,17 +45,19 @@ static void Main()
Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName));

HelpWriter.Write();
return;
return 1;
}

if (arguments.IsHelp)
{
HelpWriter.Write();
return;
return 0;
}

if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec))
{
arguments.Output = OutputType.BuildServer;
}

ConfigureLogging(arguments);

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

var workingDirectory = Directory.GetParent(gitDirectory).FullName;
Expand Down Expand Up @@ -119,33 +137,16 @@ static void Main()
{
var error = string.Format("An error occurred:\r\n{0}", exception.Message);
Logger.WriteWarning(error);

exitCode = 1;
return 1;
}
catch (Exception exception)
{
var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
Logger.WriteError(error);

exitCode = 1;
}

if (Debugger.IsAttached)
{
Console.ReadKey();
}

if (!exitCode.HasValue)
{
exitCode = 0;
}
else
{
// Dump log to console if we fail to complete successfully
Console.Write(log.ToString());
return 1;
}

Environment.Exit(exitCode.Value);
return 0;
}

static IEnumerable<IBuildServer> GetApplicableBuildServers(Authentication authentication)
Expand Down