Skip to content

Commit 8cb31cb

Browse files
sasivishnutonerdo
authored andcommitted
Different exit codes to indicate particular failures (#412)
* Different exit codes for coverlet process to indicate particular error if any. * ExitCodes in a separate file and using [Flags] attribute on Enum. * Adding up the enum flag values for exit code aggregation always after initial assignment. Co-Authored-By: Marco Rossignoli <[email protected]> * Fix formating - empty space
1 parent 53a3242 commit 8cb31cb

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/coverlet.console/ExitCodes.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
/// <summary>
4+
/// Exit Codes returned from Coverlet console process.
5+
/// </summary>
6+
[Flags]
7+
internal enum CommandExitCodes
8+
{
9+
/// <summary>
10+
/// Indicates successful run of dotnet test without any test failure and coverage percentage above threshold if provided.
11+
/// </summary>
12+
Success = 0,
13+
14+
/// <summary>
15+
/// Indicates test failure by dotnet test.
16+
/// </summary>
17+
TestFailed = 1,
18+
19+
/// <summary>
20+
/// Indicates coverage percentage is below given threshold for one or more threshold type.
21+
/// </summary>
22+
CoverageBelowThreshold = 2,
23+
24+
/// <summary>
25+
/// Indicates exception occurred during Coverlet process.
26+
/// </summary>
27+
Exception = 101,
28+
29+
/// <summary>
30+
/// Indicates missing options or empty arguments for Coverlet process.
31+
/// </summary>
32+
CommandParsingException = 102
33+
}
34+

src/coverlet.console/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static int Main(string[] args)
2323
app.FullName = "Cross platform .NET Core code coverage tool";
2424
app.HelpOption("-h|--help");
2525
app.VersionOption("-v|--version", GetAssemblyVersion());
26+
int exitCode = (int)CommandExitCodes.Success;
2627

2728
CommandArgument module = app.Argument("<ASSEMBLY>", "Path to the test assembly.");
2829
CommandOption target = app.Option("-t|--target", "Path to the test runner application.", CommandOptionType.SingleValue);
@@ -191,10 +192,14 @@ static int Main(string[] args)
191192
coverageTable.AddRow("Average", $"{totalLinePercent / numModules}%", $"{totalBranchPercent / numModules}%", $"{totalMethodPercent / numModules}%");
192193

193194
logger.LogInformation(coverageTable.ToStringAlternative());
194-
195+
if (process.ExitCode > 0)
196+
{
197+
exitCode += (int)CommandExitCodes.TestFailed;
198+
}
195199
thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, dThreshold, thresholdTypeFlags, dThresholdStat);
196200
if (thresholdTypeFlags != ThresholdTypeFlags.None)
197201
{
202+
exitCode += (int)CommandExitCodes.CoverageBelowThreshold;
198203
var exceptionMessageBuilder = new StringBuilder();
199204
if ((thresholdTypeFlags & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
200205
{
@@ -214,7 +219,7 @@ static int Main(string[] args)
214219
throw new Exception(exceptionMessageBuilder.ToString());
215220
}
216221

217-
return process.ExitCode == 0 ? 0 : process.ExitCode;
222+
return exitCode;
218223
});
219224

220225
try
@@ -225,12 +230,12 @@ static int Main(string[] args)
225230
{
226231
logger.LogError(ex.Message);
227232
app.ShowHelp();
228-
return 1;
233+
return (int)CommandExitCodes.CommandParsingException;
229234
}
230235
catch (Exception ex)
231236
{
232237
logger.LogError(ex.Message);
233-
return 1;
238+
return exitCode > 0 ? exitCode : (int)CommandExitCodes.Exception;
234239
}
235240
}
236241

0 commit comments

Comments
 (0)