-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbash_test.go
93 lines (83 loc) · 2.1 KB
/
bash_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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestBashApplier_ApplyHeader(t *testing.T) {
tc := newBashTagContext(t)
defer func() { _ = tc.templateFiles.dTemplateFile.Close() }()
tmpDir := t.TempDir()
files := []string{
"bash.simple",
"bash.comments",
"bash.shebang",
"bash.env.shebang",
"bash.sh.shebang",
"bash.make.shebang",
}
d := bashApplier{}
for _, f := range files {
fileName := f
t.Run(fileName, func(t *testing.T) {
testfile := filepath.Join(tmpDir, fileName)
copyFile(t, "./testdata/"+fileName, testfile)
err := d.ApplyHeader(testfile, &tc)
if err != nil {
t.Fatalf("failed to apply header to %s: %+v", testfile, err)
}
compareFiles(t, testfile, "./testdata/"+fileName+".golden")
})
}
}
func TestBashFileApplier_CheckHeader(t *testing.T) {
files := []string{
// The non-golden files don't have a header present
"bash.simple",
"bash.comments",
"bash.shebang",
"bash.env.shebang",
"bash.sh.shebang",
"bash.make.shebang",
// The golden files should have the header present
"bash.simple.golden",
"bash.comments.golden",
"bash.shebang.golden",
"bash.env.shebang.golden",
"bash.sh.shebang.golden",
"bash.make.shebang.golden",
}
d := bashApplier{}
tc := newBashTagContext(t)
defer func() { _ = tc.templateFiles.dTemplateFile.Close() }()
for _, f := range files {
fileName := f
t.Run(fileName, func(t *testing.T) {
f, err := os.Open("./testdata/" + fileName)
if err != nil {
t.Fatalf("failed to optn file %s: %+v", fileName, err)
}
defer func() { _ = f.Close() }()
found, err := d.CheckHeader(f, &tc)
if err != nil {
t.Fatalf("failed to check header: %+v", err)
}
expected := strings.HasSuffix(fileName, ".golden")
if found != expected {
t.Fail()
}
})
}
}
func newBashTagContext(t *testing.T) TagContext {
t.Helper()
templateFile, err := loadTemplate("./testdata/templates/", "bash.txt")
if err != nil {
t.Fatalf("failed to load bash template")
}
return TagContext{
templateFiles: TemplateFiles{shTemplateFile: templateFile},
templatePath: "./testdata/templates/",
}
}