11using System . Diagnostics ;
22using System . Text ;
3+ using System . Text . RegularExpressions ;
34using AutoTag . CLI . Test . Helpers ;
45using CliWrap ;
56using CliWrap . Buffered ;
1011
1112namespace 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}
0 commit comments