Skip to content

Commit

Permalink
Add support for reporting to github
Browse files Browse the repository at this point in the history
Two features here:
- Report summary and details as a comment on the associated PR (if any)
- Issue :error: notices with the paths of the failed paths and the error message.
  • Loading branch information
kzu committed Jul 4, 2024
1 parent bbe22b7 commit 1cbe670
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 54 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ jobs:
echo 'export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"' >> .bash_profile
- name: 🧪 test
uses: ./.github/workflows/test
uses: |
dotnet test -l trx src/Demo/ -v:q
dotnet run --no-build --project ./src/dotnet-trx/ --output
- name: 🐛 logs
uses: actions/upload-artifact@v3
Expand Down
4 changes: 1 addition & 3 deletions .netconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
weak
[file ".github/workflows/build.yml"]
url = https://github.com/devlooped/oss/blob/main/.github/workflows/build.yml
sha = 14deaea5cecc64df51781d29891a2f67caf8be16
etag = d9fa5d91dc601f10d19099abb55c86df065cd1c23b1f6fab98ad883cb443bf5c
weak
skip
[file ".github/workflows/changelog.config"]
url = https://github.com/devlooped/oss/blob/main/.github/workflows/changelog.config
sha = 055a8b7c94b74ae139cce919d60b83976d2a9942
Expand Down
16 changes: 11 additions & 5 deletions src/Demo/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Xunit.Abstractions;

Expand All @@ -7,10 +7,10 @@ namespace Demo;
public class Tests(ITestOutputHelper output)
{
[Fact]
public void ICanHasOutput() => output.WriteLine("Hello, world from xunit ITestOutputHelper!");
public void Test_With_Output() => output.WriteLine("Hello, world from xunit ITestOutputHelper!");

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

[Theory]
[InlineData("en")]
Expand All @@ -22,10 +22,16 @@ public void SampleSkipped() { }
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters

[Fact]
public void OhNoh() => Assert.Equal(42, 22);
public void Fails_With_Output()
{
output.WriteLine("It was going so well... ");
output.WriteLine("Yet you never know");
output.WriteLine("Which is why you sprinkle all these WriteLines :eyes:");
Assert.Equal(42, 22);
}

[Fact]
public void CleanStackTrace()
public void Fails_With_Complex_StackTrace()
{
Action runner = () => Run();

Expand Down
15 changes: 15 additions & 0 deletions src/dotnet-trx/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Text;

namespace Devlooped;

static class Extensions
{
public static StringBuilder AppendLineIndented(this StringBuilder builder, string value, string indent)
{
foreach (var line in value.ReplaceLineEndings().Split(Environment.NewLine))
builder.Append(indent).AppendLine(line);

return builder;
}
}
64 changes: 64 additions & 0 deletions src/dotnet-trx/Process.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Diagnostics;

namespace Devlooped;

static class Process
{
public static bool TryExecute(string program, string arguments, out string? output)
=> TryExecuteCore(program, arguments, null, out output);

public static bool TryExecute(string program, string arguments, string input, out string? output)
=> TryExecuteCore(program, arguments, input, out output);

static bool TryExecuteCore(string program, string arguments, string? input, out string? output)
{
var info = new ProcessStartInfo(program, arguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = input != null
};

try
{
var proc = System.Diagnostics.Process.Start(info);
if (proc == null)
{
output = null;
return false;
}

var gotError = false;
proc.ErrorDataReceived += (_, __) => gotError = true;

if (input != null)
{
// Write the input to the standard input stream
proc.StandardInput.WriteLine(input);
proc.StandardInput.Close();
}

output = proc.StandardOutput.ReadToEnd();
if (!proc.WaitForExit(5000))
{
proc.Kill();
output = null;
return false;
}

var error = proc.StandardError.ReadToEnd();
gotError |= error.Length > 0;
output = output.Trim();
if (string.IsNullOrEmpty(output))
output = null;

return !gotError && proc.ExitCode == 0;
}
catch (Exception ex)
{
output = ex.Message;
return false;
}
}
}
Loading

0 comments on commit 1cbe670

Please sign in to comment.