-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgolang_test.go
118 lines (109 loc) · 2.53 KB
/
golang_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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestGolangApplier_ApplyHeader(t *testing.T) {
tc := newGolangTagContext(t)
defer func() { _ = tc.templateFiles.dTemplateFile.Close() }()
tmpDir := t.TempDir()
files := []string{
"go.basic",
"go.generated",
"go.single-buildtag",
"go.multiple-buildtags",
"go.multiple-buildtags-legacy",
}
g := golangApplier{}
for _, f := range files {
fileName := f
t.Run(fileName, func(t *testing.T) {
testfile := filepath.Join(tmpDir, fileName)
copyFile(t, "./testdata/"+fileName, testfile)
err := g.ApplyHeader(testfile, &tc)
if err != nil {
t.Fatalf("failed to apply header to %s: %+v", testfile, err)
}
compareFiles(t, testfile, "./testdata/"+fileName+".golden")
})
}
}
func TestGolangApplier_CheckHeader(t *testing.T) {
tests := []struct {
fileName string
expected bool
}{
{
fileName: "go.basic",
expected: false,
},
{
fileName: "go.generated",
expected: true, // Generated files are not checked, and always "ok"
},
{
fileName: "go.single-buildtag",
expected: false,
},
{
fileName: "go.multiple-buildtags",
expected: false,
},
{
fileName: "go.multiple-buildtags-legacy",
expected: false,
},
{
fileName: "go.basic.golden",
expected: true, // Generated files are not checked, and always "ok"
},
{
fileName: "go.generated.golden",
expected: true, // Generated files are not checked, and always "ok"
},
{
fileName: "go.single-buildtag.golden",
expected: true,
},
{
fileName: "go.multiple-buildtags.golden",
expected: true,
},
{
fileName: "go.multiple-buildtags-legacy.golden",
expected: true,
},
}
g := golangApplier{}
tagContext := newGolangTagContext(t)
defer func() { _ = tagContext.templateFiles.dTemplateFile.Close() }()
for _, tc := range tests {
tc := tc
t.Run(tc.fileName, func(t *testing.T) {
f, err := os.Open("./testdata/" + tc.fileName)
if err != nil {
t.Fatalf("failed to open file %s: %+v", tc.fileName, err)
}
defer func() { _ = f.Close() }()
found, err := g.CheckHeader(f, &tagContext)
if err != nil {
t.Fatalf("failed to check header: %+v", err)
}
if found != tc.expected {
t.Fail()
}
})
}
}
func newGolangTagContext(t *testing.T) TagContext {
t.Helper()
templateFile, err := loadTemplate("./testdata/templates/", "go.txt")
if err != nil {
t.Fatalf("failed to load Go template")
}
return TagContext{
templateFiles: TemplateFiles{goTemplateFile: templateFile},
templatePath: "./testdata/templates/",
}
}