forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
119 lines (101 loc) · 2.59 KB
/
example_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
package actionlint
import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
"golang.org/x/sys/execabs"
)
func TestExamples(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
dir := filepath.Join(wd, "testdata", "examples")
entries, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
infiles := make([]string, 0, len(entries))
for _, info := range entries {
if info.IsDir() {
continue
}
n := info.Name()
if strings.HasSuffix(n, ".yaml") || strings.HasSuffix(n, ".yml") {
infiles = append(infiles, filepath.Join(dir, n))
}
}
proj := &Project{root: dir}
for _, infile := range infiles {
base := strings.TrimSuffix(infile, filepath.Ext(infile))
outfile := base + ".out"
testName := filepath.Base(base)
t.Run(testName, func(t *testing.T) {
b, err := ioutil.ReadFile(infile)
if err != nil {
panic(err)
}
opts := LinterOptions{}
if strings.Contains(testName, "shellcheck") {
p, err := execabs.LookPath("shellcheck")
if err != nil {
t.Skip("skipped because \"shellcheck\" command does not exist in system")
}
opts.Shellcheck = p
}
if strings.Contains(testName, "pyflakes") {
p, err := execabs.LookPath("pyflakes")
if err != nil {
t.Skip("skipped because \"pyflakes\" command does not exist in system")
}
opts.Pyflakes = p
}
linter, err := NewLinter(ioutil.Discard, &opts)
if err != nil {
t.Fatal(err)
}
config := Config{}
linter.defaultConfig = &config
expected := []string{}
{
f, err := os.Open(outfile)
if err != nil {
panic(err)
}
s := bufio.NewScanner(f)
for s.Scan() {
expected = append(expected, s.Text())
}
if err := s.Err(); err != nil {
panic(err)
}
}
errs, err := linter.Lint("test.yaml", b, proj)
if err != nil {
t.Fatal(err)
}
if len(errs) != len(expected) {
t.Fatalf("%d errors are expected but actually got %d errors: %# v", len(expected), len(errs), errs)
}
sort.Sort(ByErrorPosition(errs))
for i := 0; i < len(errs); i++ {
want, have := expected[i], errs[i].Error()
if strings.HasPrefix(want, "/") && strings.HasSuffix(want, "/") {
want := regexp.MustCompile(want[1 : len(want)-1])
if !want.MatchString(have) {
t.Errorf("error message mismatch at %dth error does not match to regular expression\n want: /%s/\n have: %q", i+1, want, have)
}
} else {
if want != have {
t.Errorf("error message mismatch at %dth error does not match exactly\n want: %q\n have: %q", i+1, want, have)
}
}
}
})
}
}