@@ -2,27 +2,32 @@ package models
2
2
3
3
import (
4
4
"github.com/stretchr/testify/assert"
5
+ "github.com/stretchr/testify/require"
5
6
"gopkg.in/yaml.v3"
6
7
"testing"
7
8
)
8
9
9
10
func TestGithubActionsWorkflowJobs (t * testing.T ) {
10
- cases := []struct {
11
+ tests := []struct {
12
+ Name string
11
13
Input string
12
14
Expected GithubActionsJob
13
15
Error bool
14
16
}{
15
17
{
18
+ Name : "empty" ,
16
19
Input : `[]` ,
17
20
Error : true ,
18
21
},
19
22
{
23
+ Name : "empty job" ,
20
24
Input : `build: {}` ,
21
25
Expected : GithubActionsJob {
22
26
ID : "build" ,
23
27
},
24
28
},
25
29
{
30
+ Name : "env as scalar" ,
26
31
Input : `build: {env: "${{ fromJSON(inputs.env) }}"}` ,
27
32
Expected : GithubActionsJob {
28
33
ID : "build" ,
@@ -34,6 +39,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
34
39
},
35
40
},
36
41
{
42
+ Name : "runs-on list" ,
37
43
Input : `build: {runs-on: [ubuntu-latest]}` ,
38
44
Expected : GithubActionsJob {
39
45
ID : "build" ,
@@ -42,6 +48,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
42
48
},
43
49
},
44
50
{
51
+ Name : "runs-on objects" ,
45
52
Input : `build: {runs-on: { group: runner-group, labels: [runner-label] }}` ,
46
53
Expected : GithubActionsJob {
47
54
ID : "build" ,
@@ -50,6 +57,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
50
57
},
51
58
},
52
59
{
60
+ Name : "runs-on with labels" ,
53
61
Input : `build: {runs-on: { labels: runner-label }}` ,
54
62
Expected : GithubActionsJob {
55
63
ID : "build" ,
@@ -58,50 +66,62 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
58
66
},
59
67
},
60
68
{
69
+ Name : "runs-on with empty labels" ,
61
70
Input : `build: {runs-on: { labels: [ {} ] }}` ,
62
71
Error : true ,
63
72
},
64
73
{
74
+ Name : "runs-on with empty string labels" ,
65
75
Input : `build: {runs-on: { labels: [ "" ] }}` ,
66
76
Error : true ,
67
77
},
68
78
{
79
+ Name : "runs-on with empty string group" ,
69
80
Input : `build: {runs-on: { group: [ "" ] }}` ,
70
81
Error : true ,
71
82
},
72
83
{
84
+ Name : "runs-on with empty object" ,
73
85
Input : `build: {runs-on: [ {}]}` ,
74
86
Error : true ,
75
87
},
76
88
{
89
+ Name : "empty build" ,
77
90
Input : `build: []` ,
78
91
Error : true ,
79
92
},
80
93
{
94
+ Name : "invalid permissions" ,
81
95
Input : `build: {permissions: foobar}` ,
82
96
Error : true ,
83
97
},
84
98
{
99
+ Name : "invalid permissions list" ,
85
100
Input : `build: {permissions: [foobar]}` ,
86
101
Error : true ,
87
102
},
88
103
{
104
+ Name : "invalid env" ,
89
105
Input : `build: {env: foobar}` ,
90
106
Error : true ,
91
107
},
92
108
{
109
+ Name : "invalid steps" ,
93
110
Input : `build: {steps: [foobar]}` ,
94
111
Error : true ,
95
112
},
96
113
{
114
+ Name : "invalid secrets" ,
97
115
Input : `build: {secrets: []}` ,
98
116
Error : true ,
99
117
},
100
118
{
119
+ Name : "invalid outputs" ,
101
120
Input : `build: {outputs: []]}` ,
102
121
Error : true ,
103
122
},
104
123
{
124
+ Name : "container as scalar" ,
105
125
Input : `build: {container: ubuntu:latest}` ,
106
126
Expected : GithubActionsJob {
107
127
ID : "build" ,
@@ -111,6 +131,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
111
131
},
112
132
},
113
133
{
134
+ Name : "container as object" ,
114
135
Input : `build: {container: {image: ubuntu:latest}}` ,
115
136
Expected : GithubActionsJob {
116
137
ID : "build" ,
@@ -120,10 +141,12 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
120
141
},
121
142
},
122
143
{
144
+ Name : "invalid container empty list" ,
123
145
Input : `build: {container: []}` ,
124
146
Error : true ,
125
147
},
126
148
{
149
+ Name : "invalid container empty list" ,
127
150
Input : `build: {permissions: {contents: read}}` ,
128
151
Expected : GithubActionsJob {
129
152
ID : "build" ,
@@ -136,6 +159,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
136
159
},
137
160
},
138
161
{
162
+ Name : "environment as scalar" ,
139
163
Input : `build: {environment: public}` ,
140
164
Expected : GithubActionsJob {
141
165
ID : "build" ,
@@ -147,6 +171,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
147
171
},
148
172
},
149
173
{
174
+ Name : "environment as object" ,
150
175
Input : `build: {environment: {name: dev, url: example.com}}` ,
151
176
Expected : GithubActionsJob {
152
177
ID : "build" ,
@@ -159,25 +184,43 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
159
184
},
160
185
},
161
186
{
187
+ Name : "invalid empty environment" ,
162
188
Input : `build: {environment: []}` ,
163
189
Error : true ,
164
190
},
191
+ {
192
+ Name : "single dimension matrix" ,
193
+ Input : `example_matrix: { strategy: { matrix: { version: [10, 12, 14] } } }` ,
194
+ Expected : GithubActionsJob {
195
+ ID : "example_matrix" ,
196
+ Strategy : GithubActionsStrategy {
197
+ Matrix : map [string ]StringList {
198
+ "version" : {"10" , "12" , "14" },
199
+ },
200
+ },
201
+ },
202
+ },
165
203
}
166
204
167
- for _ , c := range cases {
168
- var jobs GithubActionsJobs
169
- err := yaml .Unmarshal ([]byte (c .Input ), & jobs )
205
+ for _ , tt := range tests {
206
+ t .Run (tt .Name , func (t * testing.T ) {
207
+ var jobs GithubActionsJobs
208
+ err := yaml .Unmarshal ([]byte (tt .Input ), & jobs )
170
209
171
- if c .Error {
172
- assert .NotNil (t , err )
173
- } else {
174
- assert .Nil (t , err )
175
- c .Expected .Line = 1
176
- if c .Expected .Lines == nil {
177
- c .Expected .Lines = map [string ]int {"start" : c .Expected .Line }
210
+ if tt .Error {
211
+ require .Error (t , err )
212
+ return
178
213
}
179
- assert .Equal (t , c .Expected , jobs [0 ])
180
- }
214
+ require .NoError (t , err )
215
+ require .Len (t , jobs , 1 )
216
+
217
+ got := jobs [0 ]
218
+ tt .Expected .Line = 1
219
+ if tt .Expected .Lines == nil {
220
+ tt .Expected .Lines = map [string ]int {"start" : tt .Expected .Line }
221
+ }
222
+ assert .Equal (t , tt .Expected , got )
223
+ })
181
224
}
182
225
}
183
226
0 commit comments