@@ -4,7 +4,12 @@ import (
44 "context"
55 "testing"
66
7+ log "github.com/sirupsen/logrus"
8+ "github.com/stretchr/testify/assert"
9+ "gopkg.in/yaml.v3"
10+
711 "github.com/nektos/act/pkg/common"
12+ "github.com/nektos/act/pkg/model"
813)
914
1015func TestStepContextExecutor (t * testing.T ) {
@@ -27,3 +32,85 @@ func TestStepContextExecutor(t *testing.T) {
2732 runTestJobFile (ctx , t , table )
2833 }
2934}
35+
36+ func createIfTestStepContext (t * testing.T , input string ) * StepContext {
37+ var step * model.Step
38+ err := yaml .Unmarshal ([]byte (input ), & step )
39+ assert .NoError (t , err )
40+
41+ return & StepContext {
42+ RunContext : & RunContext {
43+ Config : & Config {
44+ Workdir : "." ,
45+ Platforms : map [string ]string {
46+ "ubuntu-latest" : "ubuntu-latest" ,
47+ },
48+ },
49+ StepResults : map [string ]* stepResult {},
50+ Env : map [string ]string {},
51+ Run : & model.Run {
52+ JobID : "job1" ,
53+ Workflow : & model.Workflow {
54+ Name : "workflow1" ,
55+ Jobs : map [string ]* model.Job {
56+ "job1" : createJob (t , `runs-on: ubuntu-latest` , "" ),
57+ },
58+ },
59+ },
60+ },
61+ Step : step ,
62+ }
63+ }
64+
65+ func TestStepContextIsEnabled (t * testing.T ) {
66+ log .SetLevel (log .DebugLevel )
67+ assertObject := assert .New (t )
68+
69+ // success()
70+ sc := createIfTestStepContext (t , "if: success()" )
71+ assertObject .True (sc .isEnabled (context .Background ()))
72+
73+ sc = createIfTestStepContext (t , "if: success()" )
74+ sc .RunContext .StepResults ["a" ] = & stepResult {
75+ Success : true ,
76+ }
77+ assertObject .True (sc .isEnabled (context .Background ()))
78+
79+ sc = createIfTestStepContext (t , "if: success()" )
80+ sc .RunContext .StepResults ["a" ] = & stepResult {
81+ Success : false ,
82+ }
83+ assertObject .False (sc .isEnabled (context .Background ()))
84+
85+ // failure()
86+ sc = createIfTestStepContext (t , "if: failure()" )
87+ assertObject .False (sc .isEnabled (context .Background ()))
88+
89+ sc = createIfTestStepContext (t , "if: failure()" )
90+ sc .RunContext .StepResults ["a" ] = & stepResult {
91+ Success : true ,
92+ }
93+ assertObject .False (sc .isEnabled (context .Background ()))
94+
95+ sc = createIfTestStepContext (t , "if: failure()" )
96+ sc .RunContext .StepResults ["a" ] = & stepResult {
97+ Success : false ,
98+ }
99+ assertObject .True (sc .isEnabled (context .Background ()))
100+
101+ // always()
102+ sc = createIfTestStepContext (t , "if: always()" )
103+ assertObject .True (sc .isEnabled (context .Background ()))
104+
105+ sc = createIfTestStepContext (t , "if: always()" )
106+ sc .RunContext .StepResults ["a" ] = & stepResult {
107+ Success : true ,
108+ }
109+ assertObject .True (sc .isEnabled (context .Background ()))
110+
111+ sc = createIfTestStepContext (t , "if: always()" )
112+ sc .RunContext .StepResults ["a" ] = & stepResult {
113+ Success : false ,
114+ }
115+ assertObject .True (sc .isEnabled (context .Background ()))
116+ }
0 commit comments