@@ -14,9 +14,12 @@ public class CommandResultAssertions
1414 {
1515 public CommandResult Result { get ; }
1616
17- public CommandResultAssertions ( CommandResult commandResult )
17+ public AssertionChain CurrentAssertionChain { get ; }
18+
19+ public CommandResultAssertions ( CommandResult commandResult , AssertionChain assertionChain )
1820 {
1921 Result = commandResult ;
22+ CurrentAssertionChain = assertionChain ;
2023 }
2124
2225 public AndConstraint < CommandResultAssertions > ExitWith ( int expectedExitCode )
@@ -25,128 +28,128 @@ public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
2528 if ( ! OperatingSystem . IsWindows ( ) )
2629 expectedExitCode = expectedExitCode & 0xFF ;
2730
28- Execute . Assertion . ForCondition ( Result . ExitCode == expectedExitCode )
31+ CurrentAssertionChain . ForCondition ( Result . ExitCode == expectedExitCode )
2932 . FailWith ( $ "Expected command to exit with { expectedExitCode } but it did not.{ GetDiagnosticsInfo ( ) } ") ;
3033 return new AndConstraint < CommandResultAssertions > ( this ) ;
3134 }
3235
3336 public AndConstraint < CommandResultAssertions > Pass ( )
3437 {
35- Execute . Assertion . ForCondition ( Result . ExitCode == 0 )
38+ CurrentAssertionChain . ForCondition ( Result . ExitCode == 0 )
3639 . FailWith ( $ "Expected command to pass but it did not.{ GetDiagnosticsInfo ( ) } ") ;
3740 return new AndConstraint < CommandResultAssertions > ( this ) ;
3841 }
3942
4043 public AndConstraint < CommandResultAssertions > Fail ( )
4144 {
42- Execute . Assertion . ForCondition ( Result . ExitCode != 0 )
45+ CurrentAssertionChain . ForCondition ( Result . ExitCode != 0 )
4346 . FailWith ( $ "Expected command to fail but it did not.{ GetDiagnosticsInfo ( ) } ") ;
4447 return new AndConstraint < CommandResultAssertions > ( this ) ;
4548 }
4649
4750 public AndConstraint < CommandResultAssertions > HaveStdOut ( )
4851 {
49- Execute . Assertion . ForCondition ( ! string . IsNullOrEmpty ( Result . StdOut ) )
52+ CurrentAssertionChain . ForCondition ( ! string . IsNullOrEmpty ( Result . StdOut ) )
5053 . FailWith ( $ "Command did not output anything to stdout{ GetDiagnosticsInfo ( ) } ") ;
5154 return new AndConstraint < CommandResultAssertions > ( this ) ;
5255 }
5356
5457 public AndConstraint < CommandResultAssertions > HaveStdOut ( string expectedOutput )
5558 {
56- Execute . Assertion . ForCondition ( Result . StdOut . Equals ( expectedOutput , StringComparison . Ordinal ) )
59+ CurrentAssertionChain . ForCondition ( Result . StdOut . Equals ( expectedOutput , StringComparison . Ordinal ) )
5760 . FailWith ( $ "Command did not output with Expected Output. Expected: '{ expectedOutput } '{ GetDiagnosticsInfo ( ) } ") ;
5861 return new AndConstraint < CommandResultAssertions > ( this ) ;
5962 }
6063
6164 public AndConstraint < CommandResultAssertions > HaveStdOutContaining ( string pattern )
6265 {
63- Execute . Assertion . ForCondition ( Result . StdOut . Contains ( pattern ) )
66+ CurrentAssertionChain . ForCondition ( Result . StdOut . Contains ( pattern ) )
6467 . FailWith ( $ "The command output did not contain expected result: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
6568 return new AndConstraint < CommandResultAssertions > ( this ) ;
6669 }
6770
6871 public AndConstraint < CommandResultAssertions > NotHaveStdOutContaining ( string pattern )
6972 {
70- Execute . Assertion . ForCondition ( ! Result . StdOut . Contains ( pattern ) )
73+ CurrentAssertionChain . ForCondition ( ! Result . StdOut . Contains ( pattern ) )
7174 . FailWith ( $ "The command output contained a result it should not have contained: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
7275 return new AndConstraint < CommandResultAssertions > ( this ) ;
7376 }
7477
7578 public AndConstraint < CommandResultAssertions > HaveStdOutMatching ( string pattern , RegexOptions options = RegexOptions . None )
7679 {
77- Execute . Assertion . ForCondition ( Regex . IsMatch ( Result . StdOut , pattern , options ) )
80+ CurrentAssertionChain . ForCondition ( Regex . IsMatch ( Result . StdOut , pattern , options ) )
7881 . FailWith ( $ "Matching the command output failed. Pattern: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
7982 return new AndConstraint < CommandResultAssertions > ( this ) ;
8083 }
8184
8285 public AndConstraint < CommandResultAssertions > NotHaveStdOutMatching ( string pattern , RegexOptions options = RegexOptions . None )
8386 {
84- Execute . Assertion . ForCondition ( ! Regex . IsMatch ( Result . StdOut , pattern , options ) )
87+ CurrentAssertionChain . ForCondition ( ! Regex . IsMatch ( Result . StdOut , pattern , options ) )
8588 . FailWith ( $ "The command output matched a pattern is should not have matched. Pattern: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
8689 return new AndConstraint < CommandResultAssertions > ( this ) ;
8790 }
8891
8992 public AndConstraint < CommandResultAssertions > HaveStdErr ( )
9093 {
91- Execute . Assertion . ForCondition ( ! string . IsNullOrEmpty ( Result . StdErr ) )
94+ CurrentAssertionChain . ForCondition ( ! string . IsNullOrEmpty ( Result . StdErr ) )
9295 . FailWith ( $ "Command did not output anything to stderr.{ GetDiagnosticsInfo ( ) } ") ;
9396 return new AndConstraint < CommandResultAssertions > ( this ) ;
9497 }
9598
9699 public AndConstraint < CommandResultAssertions > HaveStdErrContaining ( string pattern )
97100 {
98- Execute . Assertion . ForCondition ( Result . StdErr . Contains ( pattern ) )
101+ CurrentAssertionChain . ForCondition ( Result . StdErr . Contains ( pattern ) )
99102 . FailWith ( $ "The command error output did not contain expected result: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
100103 return new AndConstraint < CommandResultAssertions > ( this ) ;
101104 }
102105
103106 public AndConstraint < CommandResultAssertions > NotHaveStdErrContaining ( string pattern )
104107 {
105- Execute . Assertion . ForCondition ( ! Result . StdErr . Contains ( pattern ) )
108+ CurrentAssertionChain . ForCondition ( ! Result . StdErr . Contains ( pattern ) )
106109 . FailWith ( $ "The command error output contained a result it should not have contained: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
107110 return new AndConstraint < CommandResultAssertions > ( this ) ;
108111 }
109112
110113 public AndConstraint < CommandResultAssertions > HaveStdErrMatching ( string pattern , RegexOptions options = RegexOptions . None )
111114 {
112- Execute . Assertion . ForCondition ( Regex . IsMatch ( Result . StdErr , pattern , options ) )
115+ CurrentAssertionChain . ForCondition ( Regex . IsMatch ( Result . StdErr , pattern , options ) )
113116 . FailWith ( $ "Matching the command error output failed. Pattern: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
114117 return new AndConstraint < CommandResultAssertions > ( this ) ;
115118 }
116119
117120 public AndConstraint < CommandResultAssertions > NotHaveStdOut ( )
118121 {
119- Execute . Assertion . ForCondition ( string . IsNullOrEmpty ( Result . StdOut ) )
122+ CurrentAssertionChain . ForCondition ( string . IsNullOrEmpty ( Result . StdOut ) )
120123 . FailWith ( $ "Expected command to not output to stdout but it did:{ GetDiagnosticsInfo ( ) } ") ;
121124 return new AndConstraint < CommandResultAssertions > ( this ) ;
122125 }
123126
124127 public AndConstraint < CommandResultAssertions > NotHaveStdErr ( )
125128 {
126- Execute . Assertion . ForCondition ( string . IsNullOrEmpty ( Result . StdErr ) )
129+ CurrentAssertionChain . ForCondition ( string . IsNullOrEmpty ( Result . StdErr ) )
127130 . FailWith ( $ "Expected command to not output to stderr but it did:{ GetDiagnosticsInfo ( ) } ") ;
128131 return new AndConstraint < CommandResultAssertions > ( this ) ;
129132 }
130133
131134 public AndConstraint < CommandResultAssertions > FileExists ( string path )
132135 {
133- Execute . Assertion . ForCondition ( System . IO . File . Exists ( path ) )
136+ CurrentAssertionChain . ForCondition ( System . IO . File . Exists ( path ) )
134137 . FailWith ( $ "The command did not write the expected file: '{ path } '{ GetDiagnosticsInfo ( ) } ") ;
135138 return new AndConstraint < CommandResultAssertions > ( this ) ;
136139 }
137140
138141 public AndConstraint < CommandResultAssertions > FileContains ( string path , string pattern )
139142 {
140143 string fileContent = System . IO . File . ReadAllText ( path ) ;
141- Execute . Assertion . ForCondition ( fileContent . Contains ( pattern ) )
144+ CurrentAssertionChain . ForCondition ( fileContent . Contains ( pattern ) )
142145 . FailWith ( $ "The command did not write the expected result '{ pattern } ' to the file: '{ path } '{ GetDiagnosticsInfo ( ) } { Environment . NewLine } file content: >>{ fileContent } <<") ;
143146 return new AndConstraint < CommandResultAssertions > ( this ) ;
144147 }
145148
146149 public AndConstraint < CommandResultAssertions > NotFileContains ( string path , string pattern )
147150 {
148151 string fileContent = System . IO . File . ReadAllText ( path ) ;
149- Execute . Assertion . ForCondition ( ! fileContent . Contains ( pattern ) )
152+ CurrentAssertionChain . ForCondition ( ! fileContent . Contains ( pattern ) )
150153 . FailWith ( $ "The command did not write the expected result '{ pattern } ' to the file: '{ path } '{ GetDiagnosticsInfo ( ) } { Environment . NewLine } file content: >>{ fileContent } <<") ;
151154 return new AndConstraint < CommandResultAssertions > ( this ) ;
152155 }
0 commit comments