-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
184 lines (151 loc) · 7.11 KB
/
main_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main_test
import (
"os"
"os/exec"
"path"
"path/filepath"
"testing"
)
func FuzzBadFile(f *testing.F) {
testbin := path.Join(f.TempDir(), "go-enum-encoding-test")
exec.Command("go", "build", "-cover", "-o", testbin, "main.go").Run()
f.Fuzz(func(t *testing.T, orig string) {
t.Run("when bad go file, then error", func(t *testing.T) {
fname := path.Join(t.TempDir(), "fuzz-test-file.go")
os.WriteFile(fname, []byte(orig), 0644)
cmd := exec.Command(testbin, "--type", "Color")
cmd.Env = append(cmd.Environ(), "GOFILE="+fname, "GOPACKAGE=color")
if err := cmd.Run(); err == nil {
t.Fatal("must be error")
}
})
})
}
func TestMain(t *testing.T) {
coverdir, outdir := t.TempDir(), t.TempDir()
testbin := path.Join(t.TempDir(), "go-enum-encoding-test")
exec.Command("go", "build", "-cover", "-o", testbin, "main.go").Run()
defer exec.Command("go", "tool", "covdata", "textfmt", "-i="+coverdir, "-o", os.Getenv("GOCOVERPROFILE")).Run()
assertFile := func(t *testing.T, a string) {
t.Helper()
assertEqFile(t, filepath.Join(outdir, a), filepath.Join("internal", "exp", a))
}
t.Run("when ok, then file matches expected", func(t *testing.T) {
t.Run("when short mode, then file matches expected", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "color.go"), filepath.Join(outdir, "color.go")).Run()
cmd := exec.Command(testbin, "--type", "Color")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "color.go"), "GOLINE=4", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "color_enum_encoding.go")
assertFile(t, "color_enum_encoding_test.go")
assertFile(t, "color_enum_encoding_json_test.go")
})
t.Run("when auto mode, then long can be detected and file matches expected", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "currency.go"), filepath.Join(outdir, "currency.go")).Run()
cmd := exec.Command(testbin, "--type", "Currency")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "currency.go"), "GOLINE=4", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "currency_enum_encoding.go")
assertFile(t, "currency_enum_encoding_test.go")
assertFile(t, "currency_enum_encoding_json_test.go")
})
t.Run("string", func(t *testing.T) {
t.Run("short", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "color.go"), filepath.Join(outdir, "color.go")).Run()
cmd := exec.Command(testbin, "--type", "ColorString", "--string")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "color.go"), "GOLINE=18", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "colorstring_enum_encoding.go")
assertFile(t, "colorstring_enum_encoding_test.go")
assertFile(t, "colorstring_enum_encoding_string_test.go")
assertFile(t, "colorstring_enum_encoding_json_test.go")
})
t.Run("long", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "currency_string.go"), filepath.Join(outdir, "currency_string.go")).Run()
cmd := exec.Command(testbin, "--type", "CurrencyString", "--mode", "long", "--string")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "currency_string.go"), "GOLINE=4", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "currencystring_enum_encoding.go")
assertFile(t, "currencystring_enum_encoding_test.go")
assertFile(t, "currencystring_enum_encoding_string_test.go")
assertFile(t, "currencystring_enum_encoding_json_test.go")
})
t.Run("custom method", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "currency_string_custom.go"), filepath.Join(outdir, "currency_string_custom.go")).Run()
cmd := exec.Command(
testbin,
"--type", "CurrencyStringCustom",
"--mode", "long",
"--string",
"--encode-method", "MarshalTextName",
"--decode-method", "UnmarshalTextName",
"--string-method", "StringName",
)
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "currency_string_custom.go"), "GOLINE=4", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "currencystringcustom_enum_encoding.go")
assertFile(t, "currencystringcustom_enum_encoding_test.go")
})
})
t.Run("when multiple enums in same file, then file matches expected for each", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "multiple.go"), filepath.Join(outdir, "multiple.go")).Run()
cmd := exec.Command(testbin, "--type", "Color2")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "multiple.go"), "GOLINE=4", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "color2_enum_encoding.go")
assertFile(t, "color2_enum_encoding_test.go")
assertFile(t, "color2_enum_encoding_string_test.go")
assertFile(t, "color2_enum_encoding_json_test.go")
cmd = exec.Command(testbin, "--type", "Currency2")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "multiple.go"), "GOLINE=12", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
cmd.Run()
assertFile(t, "currency2_enum_encoding.go")
assertFile(t, "currency2_enum_encoding_test.go")
assertFile(t, "currency2_enum_encoding_string_test.go")
assertFile(t, "currency2_enum_encoding_json_test.go")
})
})
t.Run("when bad go file, then error", func(t *testing.T) {
exec.Command("cp", filepath.Join("internal", "README.md"), filepath.Join(outdir, "README.md")).Run()
cmd := exec.Command(testbin, "--type", "Color")
cmd.Env = append(cmd.Environ(), "GOFILE=README.md", "GOLINE=5", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
if err := cmd.Run(); err == nil {
t.Fatal("must be error")
}
})
t.Run("when enum values not immediately after go:generate line, then error", func(t *testing.T) {
cmd := exec.Command(testbin, "--type", "Color")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "color.go"), "GOLINE=1", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
if err := cmd.Run(); err == nil {
t.Fatal("must be error")
}
})
t.Run("when invalid package name, then error", func(t *testing.T) {
cmd := exec.Command(testbin, "--type", "Color")
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "color.go"), "GOLINE=5", "GOPACKAGE=\"", "GOCOVERDIR="+coverdir)
if err := cmd.Run(); err == nil {
t.Fatal("must be error")
}
})
t.Run("when not found file, then error", func(t *testing.T) {
cmd := exec.Command(testbin, "--type", "Color")
cmd.Env = append(cmd.Environ(), "GOFILE=asdf.asdf", "GOPACKAGE=color", "GOLINE=5", "GOCOVERDIR="+coverdir)
if err := cmd.Run(); err == nil {
t.Fatal("must be error")
}
})
t.Run("when wrong params, then error", func(t *testing.T) {
cmd := exec.Command(testbin)
cmd.Env = append(cmd.Environ(), "GOFILE="+filepath.Join(outdir, "color.go"), "GOLINE=5", "GOPACKAGE=color", "GOCOVERDIR="+coverdir)
if err := cmd.Run(); err == nil {
t.Fatal("must be error")
}
})
}
func assertEqFile(t *testing.T, a, b string) {
fa, _ := os.ReadFile(a)
fb, _ := os.ReadFile(b)
if string(fa) != string(fb) {
t.Error("files are different (" + a + " <> " + b + "), " + string(fa) + " <> " + string(fb))
}
}