@@ -11,248 +11,155 @@ import (
1111 "github.com/stretchr/testify/require"
1212)
1313
14- func TestCondition_Eval (t * testing.T ) {
14+ func newTestContext () context.Context {
15+ ctx := context .Background ()
16+ return runtime .WithEnv (ctx , runtime .NewEnv (ctx , core.Step {}))
17+ }
18+
19+ func TestEvalConditions (t * testing.T ) {
1520 tests := []struct {
16- name string
17- condition []* core.Condition
18- wantErr bool
21+ name string
22+ conditions []* core.Condition
23+ wantErr bool
24+ wantConditionNotMet bool // true if error should be ErrConditionNotMet
25+ notConditionNotMet bool // true if error should NOT be ErrConditionNotMet
1926 }{
2027 {
21- name : "CommandSubstitution" ,
22- condition : []* core.Condition {{Condition : "`echo 1`" , Expected : "1" }},
28+ name : "CommandSubstitution" ,
29+ conditions : []* core.Condition {{Condition : "`echo 1`" , Expected : "1" }},
2330 },
2431 {
25- name : "EnvVar" ,
26- condition : []* core.Condition {{Condition : "${TEST_CONDITION}" , Expected : "100" }},
32+ name : "EnvVar" ,
33+ conditions : []* core.Condition {{Condition : "${TEST_CONDITION}" , Expected : "100" }},
2734 },
2835 {
2936 name : "MultipleCond" ,
30- condition : []* core.Condition {
31- {
32- Condition : "`echo 1`" ,
33- Expected : "1" ,
34- },
35- {
36- Condition : "`echo 100`" ,
37- Expected : "100" ,
38- },
37+ conditions : []* core.Condition {
38+ {Condition : "`echo 1`" , Expected : "1" },
39+ {Condition : "`echo 100`" , Expected : "100" },
3940 },
4041 },
4142 {
4243 name : "MultipleCondOneMet" ,
43- condition : []* core.Condition {
44- {
45- Condition : "`echo 1`" ,
46- Expected : "1" ,
47- },
48- {
49- Condition : "`echo 100`" ,
50- Expected : "1" ,
51- },
44+ conditions : []* core.Condition {
45+ {Condition : "`echo 1`" , Expected : "1" },
46+ {Condition : "`echo 100`" , Expected : "1" },
5247 },
53- wantErr : true ,
48+ wantErr : true ,
49+ wantConditionNotMet : true ,
5450 },
5551 {
56- name : "CommandResultMet" ,
57- condition : []* core.Condition {
58- {
59- Condition : "true" ,
60- },
61- },
52+ name : "CommandResultMet" ,
53+ conditions : []* core.Condition {{Condition : "true" }},
6254 },
6355 {
64- name : "CommandResultNotMet" ,
65- condition : []* core.Condition {
66- {
67- Condition : "false" ,
68- },
69- },
70- wantErr : true ,
56+ name : "CommandResultNotMet" ,
57+ conditions : []* core.Condition {{Condition : "false" }},
58+ wantErr : true ,
59+ wantConditionNotMet : true ,
7160 },
7261 {
73- name : "ComplexCommand" ,
74- condition : []* core.Condition {
75- {
76- Condition : "test 1 -eq 1" ,
77- },
78- },
62+ name : "ComplexCommand" ,
63+ conditions : []* core.Condition {{Condition : "test 1 -eq 1" }},
7964 },
8065 {
81- name : "EvenMoreComplexCommand" ,
82- condition : []* core.Condition {
83- {
84- Condition : "df / | awk 'NR==2 {exit $4 > 5000 ? 0 : 1}'" ,
85- },
86- },
66+ name : "EvenMoreComplexCommand" ,
67+ conditions : []* core.Condition {{Condition : "df / | awk 'NR==2 {exit $4 > 5000 ? 0 : 1}'" }},
8768 },
8869 {
89- name : "CommandResultTest" ,
90- condition : []* core.Condition {
91- {
92- Condition : "test 1 -eq 1" ,
93- },
94- },
70+ name : "CommandResultTest" ,
71+ conditions : []* core.Condition {{Condition : "test 1 -eq 1" }},
9572 },
9673 {
97- name : "RegexMatch" ,
98- condition : []* core.Condition {
99- {
100- Condition : "test" ,
101- Expected : "re:^test$" ,
102- },
103- },
74+ name : "RegexMatch" ,
75+ conditions : []* core.Condition {{Condition : "test" , Expected : "re:^test$" }},
10476 },
10577 // Negate tests
10678 {
10779 name : "NegateMatchingCondition" ,
108- condition : []* core.Condition {
109- {
110- Condition : "`echo success`" ,
111- Expected : "success" ,
112- Negate : true ,
113- },
80+ conditions : []* core.Condition {
81+ {Condition : "`echo success`" , Expected : "success" , Negate : true },
11482 },
115- wantErr : true , // condition matches, but negate is true, so it should fail
83+ wantErr : true ,
84+ wantConditionNotMet : true ,
11685 },
11786 {
11887 name : "NegateNonMatchingCondition" ,
119- condition : []* core.Condition {
120- {
121- Condition : "`echo failure`" ,
122- Expected : "success" ,
123- Negate : true ,
124- },
88+ conditions : []* core.Condition {
89+ {Condition : "`echo failure`" , Expected : "success" , Negate : true },
12590 },
126- wantErr : false , // condition doesn't match, and negate is true, so it should pass
12791 },
12892 {
12993 name : "NegateCommandSuccess" ,
130- condition : []* core.Condition {
131- {
132- Condition : "true" ,
133- Negate : true ,
134- },
94+ conditions : []* core.Condition {
95+ {Condition : "true" , Negate : true },
13596 },
136- wantErr : true , // command succeeds, but negate is true, so it should fail
97+ wantErr : true ,
98+ wantConditionNotMet : true ,
13799 },
138100 {
139101 name : "NegateCommandFailure" ,
140- condition : []* core.Condition {
141- {
142- Condition : "false" ,
143- Negate : true ,
144- },
102+ conditions : []* core.Condition {
103+ {Condition : "false" , Negate : true },
145104 },
146- wantErr : false , // command fails, and negate is true, so it should pass
147105 },
148106 {
149107 name : "NegateEnvVar" ,
150- condition : []* core.Condition {
151- {
152- Condition : "${TEST_CONDITION}" ,
153- Expected : "wrong_value" ,
154- Negate : true ,
155- },
108+ conditions : []* core.Condition {
109+ {Condition : "${TEST_CONDITION}" , Expected : "wrong_value" , Negate : true },
156110 },
157- wantErr : false , // env var is 100, not wrong_value, so with negate it should pass
158111 },
159112 {
160113 name : "NegateEnvVarMatching" ,
161- condition : []* core.Condition {
162- {
163- Condition : "${TEST_CONDITION}" ,
164- Expected : "100" ,
165- Negate : true ,
166- },
114+ conditions : []* core.Condition {
115+ {Condition : "${TEST_CONDITION}" , Expected : "100" , Negate : true },
167116 },
168- wantErr : true , // env var is 100, matches expected, so with negate it should fail
117+ wantErr : true ,
118+ wantConditionNotMet : true ,
169119 },
170- }
171-
172- // Set environment variable for testing
173- _ = os .Setenv ("TEST_CONDITION" , "100" )
174- t .Cleanup (func () {
175- _ = os .Unsetenv ("TEST_CONDITION" )
176- })
177-
178- for _ , tt := range tests {
179- t .Run (tt .name , func (t * testing.T ) {
180- ctx := context .Background ()
181- ctx = runtime .WithEnv (ctx , runtime .NewEnv (ctx , core.Step {}))
182- err := runtime .EvalConditions (ctx , []string {"sh" }, tt .condition )
183- if tt .wantErr {
184- require .Error (t , err , "expected error but got nil" )
185- } else {
186- require .NoError (t , err , "expected no error but got %v" , err )
187- }
188- if err != nil {
189- require .ErrorIs (t , err , runtime .ErrConditionNotMet )
190- require .NotEmpty (t , tt .condition [0 ].GetErrorMessage ())
191- }
192- })
193- }
194- }
195-
196- // TestNegateDoesNotSwallowEvaluationErrors verifies that when Negate is true,
197- // evaluation/runtime errors are NOT swallowed - only ErrConditionNotMet is inverted.
198- func TestNegateDoesNotSwallowEvaluationErrors (t * testing.T ) {
199- tests := []struct {
200- name string
201- condition * core.Condition
202- wantErr bool
203- wantConditionNotMet bool // true if error should be ErrConditionNotMet
204- notConditionNotMet bool // true if error should NOT be ErrConditionNotMet
205- }{
120+ // Error handling tests
206121 {
207122 name : "EvalStringErrorNotSwallowed" ,
208- condition : & core.Condition {
209- // Command substitution with non-existent binary fails during evaluation
210- // This error is NOT ErrConditionNotMet, so it should NOT be inverted
211- Condition : "`/nonexistent_binary_xyz_123_456` " ,
212- Expected : "anything" ,
213- Negate : true ,
123+ conditions : [] * core.Condition {
124+ {
125+ Condition : "`/nonexistent_binary_xyz_123_456`" ,
126+ Expected : "anything " ,
127+ Negate : true ,
128+ } ,
214129 },
215130 wantErr : true ,
216- notConditionNotMet : true , // evaluation errors should not be ErrConditionNotMet
131+ notConditionNotMet : true ,
217132 },
218133 {
219134 name : "CommandNotFoundInvertedToSuccess" ,
220- condition : & core.Condition {
221- // Command not found returns ErrConditionNotMet (wrapped around exec error)
222- // With Negate: true, ErrConditionNotMet is inverted to success
223- Condition : "/nonexistent/path/to/command_xyz_123_abc" ,
224- Negate : true ,
135+ conditions : [] * core.Condition {
136+ {
137+ Condition : "/nonexistent/path/to/command_xyz_123_abc" ,
138+ Negate : true ,
139+ } ,
225140 },
226- wantErr : false , // ErrConditionNotMet is inverted to success
227141 },
228142 {
229143 name : "FalseCommandInvertedToSuccess" ,
230- condition : & core.Condition {
231- // "false" command exits with code 1, which wraps as ErrConditionNotMet
232- // With Negate: true, it should be inverted to success
233- Condition : "false" ,
234- Negate : true ,
235- },
236- wantErr : false , // ErrConditionNotMet is inverted to success
237- },
238- {
239- name : "MatchingConditionWithNegateFailsAsConditionNotMet" ,
240- condition : & core.Condition {
241- // When condition matches but negate is true, it should fail with ErrConditionNotMet
242- Condition : "hello" ,
243- Expected : "hello" ,
244- Negate : true ,
144+ conditions : []* core.Condition {
145+ {
146+ Condition : "false" ,
147+ Negate : true ,
148+ },
245149 },
246- wantErr : true ,
247- wantConditionNotMet : true ,
248150 },
249151 }
250152
153+ // Set environment variable for testing
154+ _ = os .Setenv ("TEST_CONDITION" , "100" )
155+ t .Cleanup (func () {
156+ _ = os .Unsetenv ("TEST_CONDITION" )
157+ })
158+
251159 for _ , tt := range tests {
252160 t .Run (tt .name , func (t * testing.T ) {
253- ctx := context .Background ()
254- ctx = runtime .WithEnv (ctx , runtime .NewEnv (ctx , core.Step {}))
255- err := runtime .EvalCondition (ctx , []string {"sh" }, tt .condition )
161+ ctx := newTestContext ()
162+ err := runtime .EvalConditions (ctx , []string {"sh" }, tt .conditions )
256163
257164 if tt .wantErr {
258165 require .Error (t , err )
0 commit comments