@@ -14,9 +14,12 @@ public class CommandResultAssertions
14
14
{
15
15
public CommandResult Result { get ; }
16
16
17
- public CommandResultAssertions ( CommandResult commandResult )
17
+ public AssertionChain CurrentAssertionChain { get ; }
18
+
19
+ public CommandResultAssertions ( CommandResult commandResult , AssertionChain assertionChain )
18
20
{
19
21
Result = commandResult ;
22
+ CurrentAssertionChain = assertionChain ;
20
23
}
21
24
22
25
public AndConstraint < CommandResultAssertions > ExitWith ( int expectedExitCode )
@@ -25,128 +28,128 @@ public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
25
28
if ( ! OperatingSystem . IsWindows ( ) )
26
29
expectedExitCode = expectedExitCode & 0xFF ;
27
30
28
- Execute . Assertion . ForCondition ( Result . ExitCode == expectedExitCode )
31
+ CurrentAssertionChain . ForCondition ( Result . ExitCode == expectedExitCode )
29
32
. FailWith ( $ "Expected command to exit with { expectedExitCode } but it did not.{ GetDiagnosticsInfo ( ) } ") ;
30
33
return new AndConstraint < CommandResultAssertions > ( this ) ;
31
34
}
32
35
33
36
public AndConstraint < CommandResultAssertions > Pass ( )
34
37
{
35
- Execute . Assertion . ForCondition ( Result . ExitCode == 0 )
38
+ CurrentAssertionChain . ForCondition ( Result . ExitCode == 0 )
36
39
. FailWith ( $ "Expected command to pass but it did not.{ GetDiagnosticsInfo ( ) } ") ;
37
40
return new AndConstraint < CommandResultAssertions > ( this ) ;
38
41
}
39
42
40
43
public AndConstraint < CommandResultAssertions > Fail ( )
41
44
{
42
- Execute . Assertion . ForCondition ( Result . ExitCode != 0 )
45
+ CurrentAssertionChain . ForCondition ( Result . ExitCode != 0 )
43
46
. FailWith ( $ "Expected command to fail but it did not.{ GetDiagnosticsInfo ( ) } ") ;
44
47
return new AndConstraint < CommandResultAssertions > ( this ) ;
45
48
}
46
49
47
50
public AndConstraint < CommandResultAssertions > HaveStdOut ( )
48
51
{
49
- Execute . Assertion . ForCondition ( ! string . IsNullOrEmpty ( Result . StdOut ) )
52
+ CurrentAssertionChain . ForCondition ( ! string . IsNullOrEmpty ( Result . StdOut ) )
50
53
. FailWith ( $ "Command did not output anything to stdout{ GetDiagnosticsInfo ( ) } ") ;
51
54
return new AndConstraint < CommandResultAssertions > ( this ) ;
52
55
}
53
56
54
57
public AndConstraint < CommandResultAssertions > HaveStdOut ( string expectedOutput )
55
58
{
56
- Execute . Assertion . ForCondition ( Result . StdOut . Equals ( expectedOutput , StringComparison . Ordinal ) )
59
+ CurrentAssertionChain . ForCondition ( Result . StdOut . Equals ( expectedOutput , StringComparison . Ordinal ) )
57
60
. FailWith ( $ "Command did not output with Expected Output. Expected: '{ expectedOutput } '{ GetDiagnosticsInfo ( ) } ") ;
58
61
return new AndConstraint < CommandResultAssertions > ( this ) ;
59
62
}
60
63
61
64
public AndConstraint < CommandResultAssertions > HaveStdOutContaining ( string pattern )
62
65
{
63
- Execute . Assertion . ForCondition ( Result . StdOut . Contains ( pattern ) )
66
+ CurrentAssertionChain . ForCondition ( Result . StdOut . Contains ( pattern ) )
64
67
. FailWith ( $ "The command output did not contain expected result: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
65
68
return new AndConstraint < CommandResultAssertions > ( this ) ;
66
69
}
67
70
68
71
public AndConstraint < CommandResultAssertions > NotHaveStdOutContaining ( string pattern )
69
72
{
70
- Execute . Assertion . ForCondition ( ! Result . StdOut . Contains ( pattern ) )
73
+ CurrentAssertionChain . ForCondition ( ! Result . StdOut . Contains ( pattern ) )
71
74
. FailWith ( $ "The command output contained a result it should not have contained: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
72
75
return new AndConstraint < CommandResultAssertions > ( this ) ;
73
76
}
74
77
75
78
public AndConstraint < CommandResultAssertions > HaveStdOutMatching ( string pattern , RegexOptions options = RegexOptions . None )
76
79
{
77
- Execute . Assertion . ForCondition ( Regex . IsMatch ( Result . StdOut , pattern , options ) )
80
+ CurrentAssertionChain . ForCondition ( Regex . IsMatch ( Result . StdOut , pattern , options ) )
78
81
. FailWith ( $ "Matching the command output failed. Pattern: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
79
82
return new AndConstraint < CommandResultAssertions > ( this ) ;
80
83
}
81
84
82
85
public AndConstraint < CommandResultAssertions > NotHaveStdOutMatching ( string pattern , RegexOptions options = RegexOptions . None )
83
86
{
84
- Execute . Assertion . ForCondition ( ! Regex . IsMatch ( Result . StdOut , pattern , options ) )
87
+ CurrentAssertionChain . ForCondition ( ! Regex . IsMatch ( Result . StdOut , pattern , options ) )
85
88
. FailWith ( $ "The command output matched a pattern is should not have matched. Pattern: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
86
89
return new AndConstraint < CommandResultAssertions > ( this ) ;
87
90
}
88
91
89
92
public AndConstraint < CommandResultAssertions > HaveStdErr ( )
90
93
{
91
- Execute . Assertion . ForCondition ( ! string . IsNullOrEmpty ( Result . StdErr ) )
94
+ CurrentAssertionChain . ForCondition ( ! string . IsNullOrEmpty ( Result . StdErr ) )
92
95
. FailWith ( $ "Command did not output anything to stderr.{ GetDiagnosticsInfo ( ) } ") ;
93
96
return new AndConstraint < CommandResultAssertions > ( this ) ;
94
97
}
95
98
96
99
public AndConstraint < CommandResultAssertions > HaveStdErrContaining ( string pattern )
97
100
{
98
- Execute . Assertion . ForCondition ( Result . StdErr . Contains ( pattern ) )
101
+ CurrentAssertionChain . ForCondition ( Result . StdErr . Contains ( pattern ) )
99
102
. FailWith ( $ "The command error output did not contain expected result: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
100
103
return new AndConstraint < CommandResultAssertions > ( this ) ;
101
104
}
102
105
103
106
public AndConstraint < CommandResultAssertions > NotHaveStdErrContaining ( string pattern )
104
107
{
105
- Execute . Assertion . ForCondition ( ! Result . StdErr . Contains ( pattern ) )
108
+ CurrentAssertionChain . ForCondition ( ! Result . StdErr . Contains ( pattern ) )
106
109
. FailWith ( $ "The command error output contained a result it should not have contained: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
107
110
return new AndConstraint < CommandResultAssertions > ( this ) ;
108
111
}
109
112
110
113
public AndConstraint < CommandResultAssertions > HaveStdErrMatching ( string pattern , RegexOptions options = RegexOptions . None )
111
114
{
112
- Execute . Assertion . ForCondition ( Regex . IsMatch ( Result . StdErr , pattern , options ) )
115
+ CurrentAssertionChain . ForCondition ( Regex . IsMatch ( Result . StdErr , pattern , options ) )
113
116
. FailWith ( $ "Matching the command error output failed. Pattern: '{ pattern } '{ GetDiagnosticsInfo ( ) } ") ;
114
117
return new AndConstraint < CommandResultAssertions > ( this ) ;
115
118
}
116
119
117
120
public AndConstraint < CommandResultAssertions > NotHaveStdOut ( )
118
121
{
119
- Execute . Assertion . ForCondition ( string . IsNullOrEmpty ( Result . StdOut ) )
122
+ CurrentAssertionChain . ForCondition ( string . IsNullOrEmpty ( Result . StdOut ) )
120
123
. FailWith ( $ "Expected command to not output to stdout but it did:{ GetDiagnosticsInfo ( ) } ") ;
121
124
return new AndConstraint < CommandResultAssertions > ( this ) ;
122
125
}
123
126
124
127
public AndConstraint < CommandResultAssertions > NotHaveStdErr ( )
125
128
{
126
- Execute . Assertion . ForCondition ( string . IsNullOrEmpty ( Result . StdErr ) )
129
+ CurrentAssertionChain . ForCondition ( string . IsNullOrEmpty ( Result . StdErr ) )
127
130
. FailWith ( $ "Expected command to not output to stderr but it did:{ GetDiagnosticsInfo ( ) } ") ;
128
131
return new AndConstraint < CommandResultAssertions > ( this ) ;
129
132
}
130
133
131
134
public AndConstraint < CommandResultAssertions > FileExists ( string path )
132
135
{
133
- Execute . Assertion . ForCondition ( System . IO . File . Exists ( path ) )
136
+ CurrentAssertionChain . ForCondition ( System . IO . File . Exists ( path ) )
134
137
. FailWith ( $ "The command did not write the expected file: '{ path } '{ GetDiagnosticsInfo ( ) } ") ;
135
138
return new AndConstraint < CommandResultAssertions > ( this ) ;
136
139
}
137
140
138
141
public AndConstraint < CommandResultAssertions > FileContains ( string path , string pattern )
139
142
{
140
143
string fileContent = System . IO . File . ReadAllText ( path ) ;
141
- Execute . Assertion . ForCondition ( fileContent . Contains ( pattern ) )
144
+ CurrentAssertionChain . ForCondition ( fileContent . Contains ( pattern ) )
142
145
. FailWith ( $ "The command did not write the expected result '{ pattern } ' to the file: '{ path } '{ GetDiagnosticsInfo ( ) } { Environment . NewLine } file content: >>{ fileContent } <<") ;
143
146
return new AndConstraint < CommandResultAssertions > ( this ) ;
144
147
}
145
148
146
149
public AndConstraint < CommandResultAssertions > NotFileContains ( string path , string pattern )
147
150
{
148
151
string fileContent = System . IO . File . ReadAllText ( path ) ;
149
- Execute . Assertion . ForCondition ( ! fileContent . Contains ( pattern ) )
152
+ CurrentAssertionChain . ForCondition ( ! fileContent . Contains ( pattern ) )
150
153
. FailWith ( $ "The command did not write the expected result '{ pattern } ' to the file: '{ path } '{ GetDiagnosticsInfo ( ) } { Environment . NewLine } file content: >>{ fileContent } <<") ;
151
154
return new AndConstraint < CommandResultAssertions > ( this ) ;
152
155
}
0 commit comments