diff --git a/src/coverlet.console/ExitCodes.cs b/src/coverlet.console/ExitCodes.cs new file mode 100644 index 000000000..2677b3f19 --- /dev/null +++ b/src/coverlet.console/ExitCodes.cs @@ -0,0 +1,34 @@ +using System; + +/// +/// Exit Codes returned from Coverlet console process. +/// +[Flags] +internal enum CommandExitCodes +{ + /// + /// Indicates successful run of dotnet test without any test failure and coverage percentage above threshold if provided. + /// + Success = 0, + + /// + /// Indicates test failure by dotnet test. + /// + TestFailed = 1, + + /// + /// Indicates coverage percentage is below given threshold for one or more threshold type. + /// + CoverageBelowThreshold = 2, + + /// + /// Indicates exception occurred during Coverlet process. + /// + Exception = 101, + + /// + /// Indicates missing options or empty arguments for Coverlet process. + /// + CommandParsingException = 102 +} + diff --git a/src/coverlet.console/Program.cs b/src/coverlet.console/Program.cs index 6fb305488..e0635a43c 100644 --- a/src/coverlet.console/Program.cs +++ b/src/coverlet.console/Program.cs @@ -23,6 +23,7 @@ static int Main(string[] args) app.FullName = "Cross platform .NET Core code coverage tool"; app.HelpOption("-h|--help"); app.VersionOption("-v|--version", GetAssemblyVersion()); + int exitCode = (int)CommandExitCodes.Success; CommandArgument module = app.Argument("", "Path to the test assembly."); CommandOption target = app.Option("-t|--target", "Path to the test runner application.", CommandOptionType.SingleValue); @@ -185,10 +186,14 @@ static int Main(string[] args) coverageTable.AddRow("Average", $"{totalLinePercent / numModules}%", $"{totalBranchPercent / numModules}%", $"{totalMethodPercent / numModules}%"); logger.LogInformation(coverageTable.ToStringAlternative()); - + if (process.ExitCode > 0) + { + exitCode += (int)CommandExitCodes.TestFailed; + } thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, dThreshold, thresholdTypeFlags, dThresholdStat); if (thresholdTypeFlags != ThresholdTypeFlags.None) { + exitCode += (int)CommandExitCodes.CoverageBelowThreshold; var exceptionMessageBuilder = new StringBuilder(); if ((thresholdTypeFlags & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None) { @@ -208,7 +213,7 @@ static int Main(string[] args) throw new Exception(exceptionMessageBuilder.ToString()); } - return process.ExitCode == 0 ? 0 : process.ExitCode; + return exitCode; }); try @@ -219,12 +224,12 @@ static int Main(string[] args) { logger.LogError(ex.Message); app.ShowHelp(); - return 1; + return (int)CommandExitCodes.CommandParsingException; } catch (Exception ex) { logger.LogError(ex.Message); - return 1; + return exitCode > 0 ? exitCode : (int)CommandExitCodes.Exception; } }