Skip to content

Commit

Permalink
Add support for rendering test output and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Jul 2, 2024
1 parent 1d177ac commit 4b78c5f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
Binary file modified assets/img/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions dotnet-trx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 17.11.35005.142
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-trx", "src\dotnet-trx\dotnet-trx.csproj", "{CB303249-74AB-468D-BD69-E443081C4690}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "src\Demo\Demo.csproj", "{106AB77F-E544-4BC1-99F9-FE82E62DC44A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -17,10 +15,6 @@ Global
{CB303249-74AB-468D-BD69-E443081C4690}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB303249-74AB-468D-BD69-E443081C4690}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB303249-74AB-468D-BD69-E443081C4690}.Release|Any CPU.Build.0 = Release|Any CPU
{106AB77F-E544-4BC1-99F9-FE82E62DC44A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{106AB77F-E544-4BC1-99F9-FE82E62DC44A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{106AB77F-E544-4BC1-99F9-FE82E62DC44A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{106AB77F-E544-4BC1-99F9-FE82E62DC44A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ Pretty-print test results in TRX format.

```shell
USAGE:
trx [PATH] [OPTIONS]

ARGUMENTS:
[PATH] Optional base directory for *.trx files discovery. Defaults to current directory
trx [OPTIONS]

OPTIONS:
DEFAULT
-h, --help Prints help information
-p, --path Optional base directory for *.trx files discovery. Defaults to current directory
-o, --output Include test output
-r, --recursive True Recursively search for *.trx files
--version Show version information
```
Expand Down
22 changes: 20 additions & 2 deletions src/Demo/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace Demo;

public class Tests
public class Tests(ITestOutputHelper output)
{
[Fact]
public void ICanHasOutput() => output.WriteLine("Hello, world from xunit ITestOutputHelper!");

[Fact(Skip = "Shouldn't run for now :)")]
public void SampleSkipped() { }

Expand All @@ -13,8 +17,22 @@ public void SampleSkipped() { }
[InlineData("fr")]
[InlineData("de")]
[InlineData("es")]
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
public async Task Parameterized(string culture) => await Task.Delay(Random.Shared.Next(100, 2000));
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters

[Fact]
public void OhNoh() => Assert.Equal(42, 22);

[Fact]
public void OhNoh() => Assert.Fail();
public void CleanStackTrace()
{
Action runner = () => Run();

runner();
}

void Run() => Unexpected();

void Unexpected() => throw new InvalidOperationException("This should not happen!");
}
1 change: 1 addition & 0 deletions src/dotnet-trx/Properties/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
launchSettings.json
32 changes: 27 additions & 5 deletions src/dotnet-trx/TrxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Devlooped.Web;
using Humanizer;
using Spectre.Console;
using Spectre.Console.Cli;
using static Spectre.Console.AnsiConsole;

Expand All @@ -17,12 +18,16 @@ public class TrxSettings : CommandSettings
[CommandOption("-p|--path")]
public string? Path { get; init; }

[Description("Recursively search for *.trx files.")]
[Description("Include test output")]
[CommandOption("-o|--output")]
public bool? Output { get; init; }

[Description("Recursively search for *.trx files")]
[CommandOption("-r|--recursive")]
[DefaultValue(true)]
public bool Recursive { get; init; } = true;

[Description("Show version information.")]
[Description("Show version information")]
[CommandOption("--version")]
public bool? Version { get; init; }
}
Expand Down Expand Up @@ -50,6 +55,14 @@ public override int Execute(CommandContext context, TrxSettings settings)
break;
case "Failed":
MarkupLine($":cross_mark: {test}");
var error = new Panel(
$"""
[red]{result.CssSelectElement("Message")?.Value.Trim()}[/]
[dim]{result.CssSelectElement("StackTrace")?.Value.ReplaceLineEndings()}[/]
""");
error.Padding = new Padding(5, 0, 0, 0);
error.Border = BoxBorder.None;
Write(error);
break;
case "NotExecuted":
MarkupLine($":fast_forward_button: {test}");
Expand All @@ -58,6 +71,13 @@ public override int Execute(CommandContext context, TrxSettings settings)
default:
break;
}

if (settings.Output == true && result.CssSelectElement("StdOut")?.Value is { } output)
Write(new Panel($"[dim]{output.ReplaceLineEndings()}[/]")
{
Border = BoxBorder.None,
Padding = new Padding(5, 0, 0, 0),
});
}

var counters = doc.CssSelectElement("ResultSummary > Counters");
Expand All @@ -83,13 +103,15 @@ public override int Execute(CommandContext context, TrxSettings settings)
MarkupLine($" :check_mark_button:");

if (passed > 0)
MarkupLine($" :check_mark_button: {passed} passed");
MarkupLine($" :check_mark_button: {passed} passed");

if (failed > 0)
MarkupLine($" :cross_mark: {failed} failed");
MarkupLine($" :cross_mark: {failed} failed");

if (skipped > 0)
MarkupLine($" :fast_forward_button: {skipped} skipped");
MarkupLine($" :fast_forward_button: {skipped} skipped");

WriteLine();
}

return 0;
Expand Down

0 comments on commit 4b78c5f

Please sign in to comment.