-
Notifications
You must be signed in to change notification settings - Fork 158
/
rule_shellcheck_test.go
199 lines (191 loc) · 3.45 KB
/
rule_shellcheck_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package actionlint
import (
"fmt"
"testing"
)
func TestRuleShellcheckSanitizeExpressionsInScript(t *testing.T) {
testCases := []struct {
input string
want string
}{
{
"",
"",
},
{
"foo",
"foo",
},
{
"${{}}",
"_____",
},
{
"${{ matrix.foo }}",
"_________________",
},
{
"aaa ${{ matrix.foo }} bbb",
"aaa _________________ bbb",
},
{
"${{}}${{}}",
"__________",
},
{
"p${{a}}q${{b}}r",
"p______q______r",
},
{
"${{",
"${{",
},
{
"}}",
"}}",
},
{
"aaa${{foo",
"aaa${{foo",
},
{
"a${{b}}${{c",
"a______${{c",
},
{
"a${{b}}c}}d",
"a______c}}d",
},
{
"a}}b${{c}}d",
"a}}b______d",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d_%s", i, tc.input), func(t *testing.T) {
have := sanitizeExpressionsInScript(tc.input)
if tc.want != have {
t.Fatalf("sanitized result is unexpected.\nwant: %q\nhave: %q", tc.want, have)
}
})
}
}
// Regression for #409
func TestRuleShellcheckDetectShell(t *testing.T) {
tests := []struct {
what string
want string
workflow string // Shell name set at 'defaults' in Workflow node
job string // Shell name set at 'defaults' in Job node
step string // Shell name set at 'shell' in Step node
runner string // Runner name at 'runs-on' in Job node
}{
{
what: "no default shell",
want: "bash",
},
{
what: "workflow default",
want: "pwsh",
workflow: "pwsh",
},
{
what: "job default",
want: "pwsh",
job: "pwsh",
},
{
what: "step config",
want: "pwsh",
step: "pwsh",
},
{
what: "job default is more proioritized than workflow",
want: "pwsh",
workflow: "bash",
job: "pwsh",
},
{
what: "step config is more proioritized than job",
want: "pwsh",
workflow: "sh",
job: "bash",
step: "pwsh",
},
{
what: "default shell detected from runner",
want: "pwsh",
runner: "windows-latest",
},
{
what: "workflow default is more proioritized than runner",
want: "bash",
workflow: "bash",
runner: "windows-latest",
},
{
what: "job default is more proioritized than runner",
want: "bash",
job: "bash",
runner: "windows-latest",
},
{
what: "step config is more proioritized than runner",
want: "bash",
step: "bash",
runner: "windows-latest",
},
{
what: "no shell is detected from Ubuntu runner",
want: "bash",
runner: "ubuntu-latest",
},
{
what: "custom bash",
want: "bash -e {0}",
workflow: "bash -e {0}",
},
{
what: "custom sh",
want: "sh -e {0}",
workflow: "sh -e {0}",
},
}
for _, tc := range tests {
t.Run(tc.what, func(t *testing.T) {
r := newRuleShellcheck(&externalCommand{})
w := &Workflow{}
if tc.workflow != "" {
w.Defaults = &Defaults{
Run: &DefaultsRun{
Shell: &String{Value: tc.workflow},
},
}
}
r.VisitWorkflowPre(w)
j := &Job{}
if tc.job != "" {
j.Defaults = &Defaults{
Run: &DefaultsRun{
Shell: &String{Value: tc.job},
},
}
}
if tc.runner != "" {
j.RunsOn = &Runner{
Labels: []*String{
{Value: tc.runner},
},
}
}
r.VisitJobPre(j)
e := &ExecRun{}
if tc.step != "" {
e.Shell = &String{Value: tc.step}
}
if s := r.getShellName(e); s != tc.want {
t.Fatalf("detected shell %q but wanted %q", s, tc.want)
}
})
}
}