-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
272 lines (223 loc) · 6.06 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/goradd/gofile/pkg/sys"
"github.com/goradd/got/internal/got"
"github.com/stretchr/testify/assert"
)
// This file runs the tests found in the test directory. It is set up so that code coverage can be checked as well.
// Doing this is a little tricky, since got generates go code that then gets compiled and run again. Each part of the
// process may generate errors. We test the process from end to end, but to do code coverage, we must directly access
// the main file as part of the test.
func TestGot(t *testing.T) {
// args is a global in the main package just for testing
testPath := filepath.Join(`./internal`, `testdata`)
runnerPath := filepath.Join(testPath, "runner", "runner.go")
comparePath := filepath.Join(testPath, "compare")
expectedPath := filepath.Join(testPath, "expected")
cmd := "go run " + runnerPath + " " + comparePath
curDir, _ := os.Getwd()
resetTemplates()
args = "-t got -i -o github.com/goradd/got/internal/testdata/template -I github.com/goradd/got/internal/testdata/src/inc2:github.com/goradd/got/internal/testdata/src/inc:github.com/goradd/got/internal/testdata/src/inc/testInclude4.inc -d github.com/goradd/got/internal/testdata/src"
main()
// main seems to be changing working dir
_ = os.Chdir(curDir)
if _, err := sys.ExecuteShellCommand(cmd); err != nil {
if e, ok := err.(*exec.Error); ok {
_, _ = fmt.Fprintln(os.Stderr, "error testing runner.go :"+e.Error()) // perhaps goimports is not installed?
os.Exit(1)
} else if err2, ok2 := err.(*exec.ExitError); ok2 {
// Likely a syntax error in the resulting file
_, _ = fmt.Fprintln(os.Stderr, string(err2.Stderr))
os.Exit(1)
}
}
// compare the outputs and report differences
files, _ := filepath.Glob(expectedPath + string(os.PathSeparator) + "*.out")
for _, file := range files {
compare, _ := os.ReadFile(file)
if expected, err := os.ReadFile(filepath.Join(comparePath, filepath.Base(file))); err != nil {
t.Error(err)
} else {
expected = bytes.Replace(expected, []byte("\r\n"), []byte("\n"), -1)
compare = bytes.Replace(compare, []byte("\r\n"), []byte("\n"), -1)
if bytes.Compare(expected, compare) != 0 {
t.Errorf("File %s is not a match.", filepath.Base(file))
}
}
}
}
// TestRecursiveGot is an alternate test for testing some of the command line options.
func TestRecursiveGot(t *testing.T) {
// args is a global in the main package just for testing
outPath1 := filepath.Join(`./internal`, `testdata`, `src`, `recurse`)
outPath2 := filepath.Join(outPath1, `rdir`)
curDir, _ := os.Getwd()
resetTemplates()
var b bytes.Buffer
got.OutWriter = &b
err := got.Run("",
"got",
false,
"",
"github.com/goradd/got/internal/testdata/src/recurse",
nil,
true,
true,
false)
assert.NoError(t, err)
// main seems to be changing working dir
_ = os.Chdir(curDir)
// verify outputs were created
files1, _ := filepath.Glob(outPath1 + string(os.PathSeparator) + "*.go")
files2, _ := filepath.Glob(outPath2 + string(os.PathSeparator) + "*.go")
assert.Len(t, files1, 1)
assert.Equal(t, "r1.tpl.go", filepath.Base(files1[0]))
assert.Len(t, files2, 1)
assert.Equal(t, "r2.tpl.go", filepath.Base(files2[0]))
assert.True(t, strings.HasPrefix(b.String(), "Processing"))
// Running this again shows that files were not processed
b.Reset()
err = got.Run("",
"got",
false,
"",
"github.com/goradd/got/internal/testdata/src/recurse",
nil,
true,
true,
false)
assert.NoError(t, err)
assert.False(t, strings.HasPrefix(b.String(), "Processing"))
// Running it again with force on shows that files were processed
b.Reset()
err = got.Run("",
"got",
false,
"",
"github.com/goradd/got/internal/testdata/src/recurse",
nil,
true,
true,
true)
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(b.String(), "Processing"))
resetTemplates()
}
func Test_badFlags1(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
false,
"",
"",
nil,
false,
true,
true)
assert.Error(t, err)
}
func Test_badIncludeFail(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
false,
"",
"",
[]string{"./internal/testdata/src/failureTests/badInclude.tpl.got"},
false,
false,
true)
assert.Error(t, err)
}
func Test_badInclude2Fail(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
true,
"",
"",
[]string{"./internal/testdata/src/failureTests/badInclude2.tpl.got"},
false,
false,
true)
assert.Error(t, err)
}
func Test_tooManyParams(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
false,
"",
"",
[]string{"./internal/testdata/src/failureTests/tooManyParams.tpl.got"},
false,
false,
true)
assert.Error(t, err)
}
func Test_badGo2(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
true,
"",
"",
[]string{"./internal/testdata/src/failureTests/badGo.tpl.got"},
false,
false,
true)
assert.Error(t, err)
}
func Test_badBlock(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
true,
"",
"",
[]string{"./internal/testdata/src/failureTests/badBlock.tpl.got"},
false,
false,
true)
assert.Error(t, err)
}
func Test_tooManyEnds(t *testing.T) {
resetTemplates()
err := got.Run("./internal/testdata/template",
"",
true,
"",
"",
[]string{"./internal/testdata/src/failureTests/tooManyEnds.tpl.got"},
false,
false,
true)
assert.Error(t, err)
}
func TestInfo(t *testing.T) {
resetTemplates()
// args is a global in the main package just for testing
args = "testEmpty"
main()
}
func resetTemplates() {
files, _ := filepath.Glob("./internal/testdata/template/*.tpl.go")
for _, f := range files {
_ = os.Remove(f)
}
files, _ = filepath.Glob("./internal/testdata/src/recurse/*.tpl.go")
for _, f := range files {
_ = os.Remove(f)
}
files, _ = filepath.Glob("./internal/testdata/src/recurse/rdir/*.tpl.go")
for _, f := range files {
_ = os.Remove(f)
}
}