forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
140 lines (128 loc) · 3.21 KB
/
config_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
package actionlint
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestConfigParseOK(t *testing.T) {
testCases := []struct {
what string
input string
labels []string
}{
{
what: "empty config",
input: "",
labels: nil,
},
{
what: "empty self-hosted-runner",
input: "self-hosted-runner:\n",
labels: nil,
},
{
what: "null self-hosted-runner labels",
input: "self-hosted-runner:\n labels:",
labels: nil,
},
{
what: "empty self-hosted-runner labels",
input: "self-hosted-runner:\n labels: []",
labels: []string{},
},
{
what: "self-hosted-runner labels",
input: "self-hosted-runner:\n labels: [foo, bar]",
labels: []string{"foo", "bar"},
},
}
for _, tc := range testCases {
t.Run(tc.what, func(t *testing.T) {
c, err := parseConfig([]byte(tc.input), "/path/to/file.yml")
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(c.SelfHostedRunner.Labels, tc.labels) {
t.Fatal(cmp.Diff(c.SelfHostedRunner.Labels, tc.labels))
}
})
}
}
func TestConfigParseError(t *testing.T) {
input := "self-hosted-runner: 42\n"
_, err := parseConfig([]byte(input), "/path/to/file.yml")
if err == nil {
t.Fatal("error did not occur")
}
msg := err.Error()
if !strings.Contains(msg, "could not parse config file \"/path/to/file.yml\"") {
t.Fatalf("unexpected error message: %q", msg)
}
}
func TestConfigReadFileOK(t *testing.T) {
p := filepath.Join("testdata", "config", "ok.yml")
c, err := ReadConfigFile(p)
if err != nil {
t.Fatal(err)
}
labels := []string{"foo", "bar"}
if !cmp.Equal(c.SelfHostedRunner.Labels, labels) {
t.Fatal(cmp.Diff(c.SelfHostedRunner.Labels, labels))
}
}
func TestConfigReadFileReadError(t *testing.T) {
p := filepath.Join("testdata", "config", "does-not-exist.yml")
_, err := ReadConfigFile(p)
if err == nil {
t.Fatal("error did not occur")
}
msg := err.Error()
if !strings.Contains(msg, "could not read config file") {
t.Fatalf("unexpected error message: %q", msg)
}
}
func TestConfigReadFileParseError(t *testing.T) {
p := filepath.Join("testdata", "config", "broken.yml")
_, err := ReadConfigFile(p)
if err == nil {
t.Fatal("error did not occur")
}
msg := err.Error()
if !strings.Contains(msg, "could not parse config file") {
t.Fatalf("unexpected error message: %q", msg)
}
}
func TestConfigGenerateDefaultConfigFileOK(t *testing.T) {
dir, err := os.MkdirTemp(filepath.Join("testdata", "config"), "generate")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
f := filepath.Join(dir, "test.yml")
if err := writeDefaultConfigFile(f); err != nil {
t.Fatal(err)
}
c, err := ReadConfigFile(f)
if err != nil {
t.Fatal(err)
}
if len(c.SelfHostedRunner.Labels) != 0 {
t.Fatal(c.SelfHostedRunner.Labels)
}
if c.ConfigVariables != nil {
t.Fatal(c.SelfHostedRunner.Labels)
}
}
func TestConfigGenerateDefaultConfigFileError(t *testing.T) {
p := filepath.Join("testdata", "config", "dir-does-not-exist", "test.yml")
err := writeDefaultConfigFile(p)
if err == nil {
t.Fatal("error did not occur")
}
msg := err.Error()
if !strings.Contains(msg, "could not write default configuration file") {
t.Fatalf("unexpected error message: %q", msg)
}
}