1
1
using System ;
2
2
using System . Diagnostics ;
3
+ using System . Linq ;
4
+ using System . Text ;
3
5
4
6
namespace Serilog . Settings . Configuration . Tests . Support
5
7
{
@@ -19,43 +21,63 @@ public CommandResult(string output, string error)
19
21
20
22
public static void RunDotnet ( string workingDirectory , params string [ ] args )
21
23
{
22
- RunCommand ( "dotnet" , useShellExecute : true , workingDirectory , args ) ;
24
+ RunCommand ( "dotnet" , workingDirectory , args ) ;
23
25
}
24
26
25
27
public static CommandResult RunCommand ( string command , params string [ ] args )
26
28
{
27
- return RunCommand ( command , useShellExecute : false , "" , args ) ;
29
+ return RunCommand ( command , "" , args ) ;
28
30
}
29
31
30
- static CommandResult RunCommand ( string command , bool useShellExecute , string workingDirectory , params string [ ] args )
32
+ static CommandResult RunCommand ( string command , string workingDirectory , params string [ ] args )
31
33
{
32
- var arguments = $ "\" { string . Join ( "\" \" " , args ) } \" ";
33
- var redirect = ! useShellExecute ;
34
- var startInfo = new ProcessStartInfo ( command , arguments )
34
+ var arguments = new StringBuilder ( args . Select ( e => e . Length + 3 ) . Sum ( ) ) ;
35
+ foreach ( var arg in args )
36
+ {
37
+ var hasSpace = arg . Contains ( " " ) ;
38
+ if ( hasSpace ) arguments . Append ( '"' ) ;
39
+ arguments . Append ( arg ) ;
40
+ if ( hasSpace ) arguments . Append ( '"' ) ;
41
+ arguments . Append ( ' ' ) ;
42
+ }
43
+ var startInfo = new ProcessStartInfo ( command , arguments . ToString ( ) )
35
44
{
36
45
CreateNoWindow = true ,
37
46
WindowStyle = ProcessWindowStyle . Hidden ,
38
- UseShellExecute = useShellExecute ,
47
+ UseShellExecute = false ,
39
48
WorkingDirectory = workingDirectory ,
40
- RedirectStandardOutput = redirect ,
41
- RedirectStandardError = redirect ,
49
+ RedirectStandardOutput = true ,
50
+ RedirectStandardError = true ,
42
51
} ;
43
52
var process = new Process { StartInfo = startInfo } ;
44
53
process . Start ( ) ;
45
54
var timeout = TimeSpan . FromSeconds ( 30 ) ;
46
55
var exited = process . WaitForExit ( ( int ) timeout . TotalMilliseconds ) ;
47
56
if ( ! exited )
48
57
{
58
+ process . Kill ( ) ;
49
59
throw new TimeoutException ( $ "The command '{ command } { arguments } ' did not execute within { timeout . TotalSeconds } seconds") ;
50
60
}
51
61
52
- var error = redirect ? process . StandardError . ReadToEnd ( ) : "" ;
62
+ var output = process . StandardOutput . ReadToEnd ( ) ;
63
+ var error = process . StandardError . ReadToEnd ( ) ;
53
64
if ( process . ExitCode != 0 )
54
65
{
55
- throw new InvalidOperationException ( $ "The command '{ command } { arguments } ' exited with code { process . ExitCode } ") ;
66
+ var message = new StringBuilder ( ) ;
67
+ message . AppendLine ( $ "The command '{ command } { arguments } ' exited with code { process . ExitCode } ") ;
68
+ if ( output . Length > 0 )
69
+ {
70
+ message . AppendLine ( "*** Output ***" ) ;
71
+ message . AppendLine ( output ) ;
72
+ }
73
+ if ( error . Length > 0 )
74
+ {
75
+ message . AppendLine ( "*** Error ***" ) ;
76
+ message . AppendLine ( error ) ;
77
+ }
78
+ throw new InvalidOperationException ( message . ToString ( ) ) ;
56
79
}
57
80
58
- var output = redirect ? process . StandardOutput . ReadToEnd ( ) : "" ;
59
81
return new CommandResult ( output , error ) ;
60
82
}
61
83
}
0 commit comments