Skip to content

Commit 1c945d7

Browse files
committed
test: strip ANSI colour codes from stdout
1 parent 57f6598 commit 1c945d7

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

AutoTag.CLI.Test/Helpers/CLIFixture.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.Text;
3+
using System.Text.RegularExpressions;
34
using AutoTag.CLI.Test.Helpers;
45
using CliWrap;
56
using CliWrap.Buffered;
@@ -10,10 +11,13 @@
1011

1112
namespace AutoTag.CLI.Test.Helpers;
1213

13-
public class CLIFixture(ITestContextAccessor context) : IAsyncLifetime
14+
public partial class CLIFixture(ITestContextAccessor context) : IAsyncLifetime
1415
{
1516
private string _cliPublishOutput = null!;
1617

18+
[GeneratedRegex(@"\x1b\[[0-9;]*m")]
19+
private static partial Regex AnsiCodeRegex { get; }
20+
1721
public async ValueTask InitializeAsync()
1822
{
1923
if (Debugger.IsAttached)
@@ -78,7 +82,7 @@ public ValueTask DisposeAsync()
7882

7983
var appResult = await app.RunAsync(args);
8084

81-
return (appResult.Output, appResult.ExitCode);
85+
return (RemoveAnsiColourCodes(appResult.Output), appResult.ExitCode);
8286
}
8387

8488
if (string.IsNullOrEmpty(_cliPublishOutput))
@@ -88,11 +92,18 @@ public ValueTask DisposeAsync()
8892

8993
var cmd = Cli.Wrap(_cliPublishOutput)
9094
.WithArguments(args)
91-
.WithValidation(CommandResultValidation.None)
92-
.WithEnvironmentVariables(env => env.Set("TERM", "dumb"));
95+
.WithValidation(CommandResultValidation.None);
9396

9497
var result = await cmd.ExecuteBufferedAsync();
9598

96-
return (result.StandardOutput, result.ExitCode);
99+
if (!result.IsSuccess)
100+
{
101+
context.Current.SendDiagnosticMessage($"CLI returned non-zero exit code ({result.ExitCode})");
102+
context.Current.SendDiagnosticMessage(result.StandardOutput);
103+
}
104+
105+
return (RemoveAnsiColourCodes(result.StandardOutput), result.ExitCode);
97106
}
107+
108+
private static string RemoveAnsiColourCodes(string input) => AnsiCodeRegex.Replace(input, "");
98109
}

AutoTag.CLI.Test/ProcessTests.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ public async Task Should_ProcessFilesWithRenamePattern()
3838
"--rename-subs"
3939
);
4040

41-
if (exitCode != 0)
42-
{
43-
TestContext.Current.SendDiagnosticMessage(stdout);
44-
}
45-
4641
exitCode.Should().Be(0);
4742

4843
AssertFile(

0 commit comments

Comments
 (0)