forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_env_var_test.go
77 lines (68 loc) · 1.55 KB
/
rule_env_var_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
package actionlint
import "testing"
func testValidateEnvVarName(t *testing.T, name string) []*Error {
n := &String{Value: name, Pos: &Pos{}}
e := &Env{
Vars: map[string]*EnvVar{
name: {n, &String{Value: "", Pos: &Pos{}}},
},
}
w := &Workflow{Env: e}
r := NewRuleEnvVar()
if err := r.VisitWorkflowPre(w); err != nil {
t.Fatal(err)
}
return r.Errs()
}
func TestRuleEnvVarCheckValidName(t *testing.T) {
valids := []string{
"foo_bar",
"FOO_BAR",
"foo-bar",
"_",
"-",
}
for _, n := range valids {
t.Run(n, func(t *testing.T) {
if errs := testValidateEnvVarName(t, n); len(errs) > 0 {
t.Fatalf("env var %q should be valid but got errors: %v", n, errs)
}
})
}
}
func TestRuleEnvVarCheckInvalidName(t *testing.T) {
invalids := []string{
"a b",
"a=b",
"a=b",
}
for _, n := range invalids {
t.Run(n, func(t *testing.T) {
if errs := testValidateEnvVarName(t, n); len(errs) == 0 {
t.Fatalf("env var %q should be invalid but got no error", n)
}
})
}
}
func TestRuleEnvVarSkipEdgeCaseEnv(t *testing.T) {
w := &Workflow{Env: nil}
r := NewRuleEnvVar()
if err := r.VisitWorkflowPre(w); err != nil {
t.Fatal(err)
}
if errs := r.Errs(); len(errs) > 0 {
t.Fatal("no error should be detected when `Env` is nil but got", errs)
}
w = &Workflow{
Env: &Env{
Expression: &String{Value: ""},
},
}
r = NewRuleEnvVar()
if err := r.VisitWorkflowPre(w); err != nil {
t.Fatal(err)
}
if errs := r.Errs(); len(errs) > 0 {
t.Fatal("no error should be detected when `Env` is constructed by expression", errs)
}
}