Skip to content

Commit 9d48684

Browse files
committed
rewrite test as table test
1 parent 610385c commit 9d48684

File tree

1 file changed

+56
-13
lines changed

1 file changed

+56
-13
lines changed

models/github_actions_test.go

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@ package models
22

33
import (
44
"github.com/stretchr/testify/assert"
5+
"github.com/stretchr/testify/require"
56
"gopkg.in/yaml.v3"
67
"testing"
78
)
89

910
func TestGithubActionsWorkflowJobs(t *testing.T) {
10-
cases := []struct {
11+
tests := []struct {
12+
Name string
1113
Input string
1214
Expected GithubActionsJob
1315
Error bool
1416
}{
1517
{
18+
Name: "empty",
1619
Input: `[]`,
1720
Error: true,
1821
},
1922
{
23+
Name: "empty job",
2024
Input: `build: {}`,
2125
Expected: GithubActionsJob{
2226
ID: "build",
2327
},
2428
},
2529
{
30+
Name: "env as scalar",
2631
Input: `build: {env: "${{ fromJSON(inputs.env) }}"}`,
2732
Expected: GithubActionsJob{
2833
ID: "build",
@@ -34,6 +39,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
3439
},
3540
},
3641
{
42+
Name: "runs-on list",
3743
Input: `build: {runs-on: [ubuntu-latest]}`,
3844
Expected: GithubActionsJob{
3945
ID: "build",
@@ -42,6 +48,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
4248
},
4349
},
4450
{
51+
Name: "runs-on objects",
4552
Input: `build: {runs-on: { group: runner-group, labels: [runner-label] }}`,
4653
Expected: GithubActionsJob{
4754
ID: "build",
@@ -50,6 +57,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
5057
},
5158
},
5259
{
60+
Name: "runs-on with labels",
5361
Input: `build: {runs-on: { labels: runner-label }}`,
5462
Expected: GithubActionsJob{
5563
ID: "build",
@@ -58,50 +66,62 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
5866
},
5967
},
6068
{
69+
Name: "runs-on with empty labels",
6170
Input: `build: {runs-on: { labels: [ {} ] }}`,
6271
Error: true,
6372
},
6473
{
74+
Name: "runs-on with empty string labels",
6575
Input: `build: {runs-on: { labels: [ "" ] }}`,
6676
Error: true,
6777
},
6878
{
79+
Name: "runs-on with empty string group",
6980
Input: `build: {runs-on: { group: [ "" ] }}`,
7081
Error: true,
7182
},
7283
{
84+
Name: "runs-on with empty object",
7385
Input: `build: {runs-on: [ {}]}`,
7486
Error: true,
7587
},
7688
{
89+
Name: "empty build",
7790
Input: `build: []`,
7891
Error: true,
7992
},
8093
{
94+
Name: "invalid permissions",
8195
Input: `build: {permissions: foobar}`,
8296
Error: true,
8397
},
8498
{
99+
Name: "invalid permissions list",
85100
Input: `build: {permissions: [foobar]}`,
86101
Error: true,
87102
},
88103
{
104+
Name: "invalid env",
89105
Input: `build: {env: foobar}`,
90106
Error: true,
91107
},
92108
{
109+
Name: "invalid steps",
93110
Input: `build: {steps: [foobar]}`,
94111
Error: true,
95112
},
96113
{
114+
Name: "invalid secrets",
97115
Input: `build: {secrets: []}`,
98116
Error: true,
99117
},
100118
{
119+
Name: "invalid outputs",
101120
Input: `build: {outputs: []]}`,
102121
Error: true,
103122
},
104123
{
124+
Name: "container as scalar",
105125
Input: `build: {container: ubuntu:latest}`,
106126
Expected: GithubActionsJob{
107127
ID: "build",
@@ -111,6 +131,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
111131
},
112132
},
113133
{
134+
Name: "container as object",
114135
Input: `build: {container: {image: ubuntu:latest}}`,
115136
Expected: GithubActionsJob{
116137
ID: "build",
@@ -120,10 +141,12 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
120141
},
121142
},
122143
{
144+
Name: "invalid container empty list",
123145
Input: `build: {container: []}`,
124146
Error: true,
125147
},
126148
{
149+
Name: "invalid container empty list",
127150
Input: `build: {permissions: {contents: read}}`,
128151
Expected: GithubActionsJob{
129152
ID: "build",
@@ -136,6 +159,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
136159
},
137160
},
138161
{
162+
Name: "environment as scalar",
139163
Input: `build: {environment: public}`,
140164
Expected: GithubActionsJob{
141165
ID: "build",
@@ -147,6 +171,7 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
147171
},
148172
},
149173
{
174+
Name: "environment as object",
150175
Input: `build: {environment: {name: dev, url: example.com}}`,
151176
Expected: GithubActionsJob{
152177
ID: "build",
@@ -159,25 +184,43 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
159184
},
160185
},
161186
{
187+
Name: "invalid empty environment",
162188
Input: `build: {environment: []}`,
163189
Error: true,
164190
},
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+
},
165203
}
166204

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)
170209

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
178213
}
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+
})
181224
}
182225
}
183226

0 commit comments

Comments
 (0)