Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: add merge coverage report capability #51

Merged
merged 3 commits into from
Aug 1, 2023
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
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ test-rerun [somepathtodll] [OPTIONS]
| `path` | Path to a test project .dll file. |

## Options
| option | description |
| -------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `--filter` | Run tests that match the given expression. |
| `--settings, -s` | The run settings file to use when running tests. |
| `--logger, -l` | Specifies a logger for test results. *(default: trx)* |
| `--results-directory, -r` | The directory where the test results are going to be placed. If the specified directory doesn't exist, it's created. |
| `--rerunMaxAttempts` | Maximum # of attempts. *(default: 3)* |
| `--loglevel` | Log Level. *(default: Verbose)* |
| `--no-build` | Do not build the project before testing. Implies --no-restore. |
| `--no-restore` | Do not restore the project before building.* |
| `--delay, -d` | Delay between test runs in seconds. |
| `--blame` | Run the tests in blame mode. |
| `--deleteReports` | Delete the generated report files. |
| `--collect` | Enables data collector for the test run. Example: --collect "Code Coverage" or --collect "XPlat Code Coverage" |
| option | description |
| -------------------- |------------------------------------------------------------------------------------------------------------------------------------|
| `--filter` | Run tests that match the given expression. |
| `--settings, -s` | The run settings file to use when running tests. |
| `--logger, -l` | Specifies a logger for test results. *(default: trx)* |
| `--results-directory, -r` | The directory where the test results are going to be placed. If the specified directory doesn't exist, it's created. |
| `--rerunMaxAttempts` | Maximum # of attempts. *(default: 3)* |
| `--loglevel` | Log Level. *(default: Verbose)* |
| `--no-build` | Do not build the project before testing. Implies --no-restore. |
| `--no-restore` | Do not restore the project before building.* |
| `--delay, -d` | Delay between test runs in seconds. |
| `--blame` | Run the tests in blame mode. |
| `--deleteReports` | Delete the generated report files. |
| `--collect` | Enables data collector for the test run. Example: --collect "Code Coverage" or --collect "XPlat Code Coverage" |
| `--mergeCoverageFormat` | Output coverage format. Possible values: Coverage, Cobertura or Xml <br/><br/>Note: requires dotnet coverage tool to be installed. |

# License
This tool is licensed under GNU General Public License v3.0. See the [LICENSE](/LICENSE) file for details.
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/TestResultsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public string GetFailedTestsFilter(IFileInfo trxFile)

public void AddLastTrxFile(IDirectoryInfo resultsDirectory)
{
var fileInfo = resultsDirectory.EnumerateFiles("*.trx").MaxBy(f => f.Name);
var fileInfo = GetTrxFile(resultsDirectory);

if (fileInfo is not null)
reportFiles.Add(fileInfo.FullName);
Expand Down
15 changes: 15 additions & 0 deletions src/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

All notable changes to this project will be documented in this file. See [versionize](https://github.com/versionize/versionize) for commit guidelines.

<a name="1.4.0-alpha.2"></a>
## [1.4.0-alpha.2](https://github.com/joaoopereira/dotnet-test-rerun/releases/tag/v1.4.0-alpha.2) (2023-8-1)

### Features

* add merge coverage report capability ([49391f3](https://github.com/joaoopereira/dotnet-test-rerun/commit/49391f3ea89882d2b92ed174d27c690944011c87))

<a name="1.4.0-alpha.1"></a>
## [1.4.0-alpha.1](https://github.com/joaoopereira/dotnet-test-rerun/releases/tag/v1.4.0-alpha.1) (2023-7-26)

### Features

* add merge coverage report capability ([e5190f8](https://github.com/joaoopereira/dotnet-test-rerun/commit/e5190f89ed098cde647e6cbe9320de4ca3b3a931))
* add option to collect coverage reports ([c0afe43](https://github.com/joaoopereira/dotnet-test-rerun/commit/c0afe43b58a04821f56ee06cd67b1dbe983184e7))

<a name="1.4.0-alpha.0"></a>
## [1.4.0-alpha.0](https://github.com/joaoopereira/dotnet-test-rerun/releases/tag/v1.4.0-alpha.0) (2023-7-17)

Expand Down
96 changes: 96 additions & 0 deletions src/DotNetRunner/DotNetCoverageRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Diagnostics;
using dotnet.test.rerun.Enums;
using dotnet.test.rerun.Logging;
using dotnet.test.rerun.RerunCommand;

namespace dotnet.test.rerun.DotNetRunner;

public class DotNetCoverageRunner : IDotNetCoverageRunner
{
private int ExitCode;
private readonly ILogger Log;
private readonly ProcessStartInfo ProcessStartInfo;
private readonly IProcessExecution ProcessExecution;
private string[] SearchPatterns = { "*.coverage", "*.cobertura*" };

public DotNetCoverageRunner(ILogger logger,
IProcessExecution processExecution)
{
ProcessStartInfo = new()
{
FileName = "dotnet-coverage",
WorkingDirectory = "/",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Log = logger;
ProcessExecution = processExecution;
}

/// <summary>
/// Runs dotnet test with the specified arguments
/// </summary>
/// <param name="config">The config.</param>
/// <param name="resultsDirectory">The results directory.</param>
/// <param name="startDate">Initial date to look for files.</param>
public async Task Merge(RerunCommandConfiguration config, string resultsDirectory, DateTime startDate)
{
string arguments = config.GetMergeCoverageArgumentList(GetCoverageFiles(resultsDirectory, startDate), resultsDirectory);

if (string.IsNullOrEmpty(resultsDirectory) is false)
arguments = $"{arguments} --results-directory {resultsDirectory}";

await Run(arguments);
}

/// <summary>
/// Runs dotnet command with the specified arguments.
/// </summary>
/// <param name="arguments">The arguments.</param>
private async Task Run(string arguments)
{
Log.Debug($"working directory: {ProcessStartInfo.WorkingDirectory}");
Log.Debug($"forking {arguments}");
ProcessStartInfo.Arguments = arguments;
string unexpectedError = string.Empty;
try
{
using Process? ps = await ProcessExecution.Start(ProcessStartInfo);
ProcessExecution.FetchOutput(ps!);
ProcessExecution.FetchError(ps!);
ExitCode = await ProcessExecution.End(ps!);
}
catch (Exception e)
{
ExitCode = 1;
unexpectedError = e.Message;
}
HandleProcessEnd(unexpectedError);
}

/// <summary>
/// Handles the process end.
/// </summary>
/// <exception cref="RerunException">command:\n\n\t\tdotnet {ProcessStartInfo.Arguments}</exception>
private void HandleProcessEnd(string unexpectedError)
{
if (ExitCode != 0)
{
Log.Verbose(GetErrorMessage(unexpectedError));
Log.Verbose($"Exit code {ExitCode}.");
throw new RerunException($"command failed:\ndotnet {ProcessStartInfo.Arguments} \n" +
$"reason: {GetErrorMessage(unexpectedError)}");
}
}

private string GetCoverageFiles(string resultsDirectory, DateTime startDate)
=> string.Join(" ", SearchPatterns.SelectMany(pattern =>
Directory.GetFiles(resultsDirectory, pattern, SearchOption.AllDirectories))
.Select(file => new FileInfo(file))
.Where(fileInfo => fileInfo.CreationTime >= startDate)
.Select(fileInfo => fileInfo.FullName));

private string GetErrorMessage(string unexpectedError)
=> string.IsNullOrWhiteSpace(unexpectedError) ? ProcessExecution.GetError() : unexpectedError;
}
114 changes: 114 additions & 0 deletions src/DotNetRunner/DotNetTestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System.Diagnostics;
using dotnet.test.rerun.Enums;
using dotnet.test.rerun.Logging;
using dotnet.test.rerun.RerunCommand;

namespace dotnet.test.rerun.DotNetRunner;

public class DotNetTestRunner : IDotNetTestRunner
{
private ErrorCode ErrorCode;
private int ExitCode;
private readonly ILogger Log;
private readonly ProcessStartInfo ProcessStartInfo;
private readonly IProcessExecution ProcessExecution;
private string[] WellKnownErrors = { "No test source files were specified." };

public DotNetTestRunner(ILogger logger,
IProcessExecution processExecution)
{
ProcessStartInfo = new()
{
FileName = "dotnet",
WorkingDirectory = "/",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Log = logger;
ProcessExecution = processExecution;
}

/// <summary>
/// Runs dotnet test with the specified arguments
/// </summary>
/// <param name="config">The config.</param>
/// <param name="resultsDirectory">The results directory.</param>
public async Task Test(RerunCommandConfiguration config, string resultsDirectory)
{
string arguments = config.GetTestArgumentList();

if (string.IsNullOrEmpty(resultsDirectory) is false)
arguments = $"{arguments} --results-directory {resultsDirectory}";

await Run(arguments);
}

public ErrorCode GetErrorCode()
=> ErrorCode;

/// <summary>
/// Runs dotnet test with the specified arguments.
/// </summary>
/// <param name="arguments">The arguments.</param>
private async Task Run(string arguments)
{
Log.Debug($"working directory: {ProcessStartInfo.WorkingDirectory}");
Log.Debug($"forking {arguments}");
ProcessStartInfo.Arguments = arguments;

using Process? ps = await ProcessExecution.Start(ProcessStartInfo);
ProcessExecution.FetchOutput(ps!);
ProcessExecution.FetchError(ps!);
ExitCode = await ProcessExecution.End(ps!);

HandleProcessEnd();
}

/// <summary>
/// Handles the process end.
/// </summary>
/// <exception cref="RerunException">command:\n\n\t\tdotnet {ProcessStartInfo.Arguments}</exception>
private void HandleProcessEnd()
{
if (ExitCode != 0)
{
if (IsWellKnownError())
{
ErrorCode = ErrorCode.WellKnownError;
Log.Warning(ProcessExecution.GetError());
}
else if (HaveFailedTests())
{
ErrorCode = ErrorCode.FailedTests;
}
else
{
ErrorCode = ErrorCode.Error;
Log.Verbose(ProcessExecution.GetError());
Log.Verbose($"Exit code {ExitCode}.");
throw new RerunException($"command:\ndotnet {ProcessStartInfo.Arguments}");
}
}
else
{
ErrorCode = ErrorCode.Success;
}
}

/// <summary>
/// Determines whether [is well known error] [the specified exit code].
/// </summary>
/// <returns>
/// <c>true</c> if [is well known error] [the specified exit code]; otherwise, <c>false</c>.
/// </returns>
private bool IsWellKnownError() => ExitCode == 1 && WellKnownErrors.Contains(ProcessExecution.GetError());

/// <summary>
/// Check if the output of dotnet test have in the last line failed tests
/// </summary>
/// <returns></returns>
private bool HaveFailedTests() => ExitCode == 1 &&
(ProcessExecution.GetOutput().Contains("Failed! - Failed:") &&
ProcessExecution.GetOutput().Split("\n")[^2].StartsWith("Failed! - Failed:"));
}
8 changes: 8 additions & 0 deletions src/DotNetRunner/IDotNetCoverageRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using dotnet.test.rerun.RerunCommand;

namespace dotnet.test.rerun.DotNetRunner;

public interface IDotNetCoverageRunner
{
public Task Merge(RerunCommandConfiguration config, string resultsDirectory, DateTime startDate);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using dotnet.test.rerun.Enums;
using dotnet.test.rerun.RerunCommand;

namespace dotnet.test.rerun.DotNetTestRunner;
namespace dotnet.test.rerun.DotNetRunner;

public interface IDotNetTestRunner
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;

namespace dotnet.test.rerun.DotNetTestRunner;
namespace dotnet.test.rerun.DotNetRunner;

public interface IProcessExecution
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;
using dotnet.test.rerun.Logging;

namespace dotnet.test.rerun.DotNetTestRunner;
namespace dotnet.test.rerun.DotNetRunner;

public class ProcessExecution : IProcessExecution
{
Expand Down
Loading