-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
302 lines (240 loc) · 7.72 KB
/
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright © 2016, 2019, 2020, 2022 Yoshiki Shibata. All rights reserved.
package main
import (
"fmt"
"os"
"strings"
"github.com/YoshikiShibata/tools/util/files"
)
var cmdTest = &Command{
UsageLine: "test [-v]",
Short: "compile and run JUnit Java program",
Long: `Test compiles JUnit program and runs JUnit test methods.`,
}
func init() {
cmdTest.Run = testRun
}
func testRun(cmd *Command, args []string) {
recreateBin()
generateAndCompileJUnitRunner()
findTestsAndRunThem(args)
}
var junitPath = junitClassPath()
// createAndCompileJUnitRunner generates the JUnitRunner Java source code,
// and then compile the source code against JUnit libraries.
func generateAndCompileJUnitRunner() {
// determines if JUnitRunner has been already compiled by checking
// the existence of its src (with version) directory.
dPrintf("oakSrcPathWithVersion=%q\n", oakSrcPathWithVersion)
_, err := os.Stat(oakSrcPathWithVersion)
if err == nil {
return
}
src := generateJUnitRunnerSource()
// save the current directory
cwd := getCWD()
changeDirectoryTo(oakSrcPathWithVersion)
compileAsTest("", src)
// restore to the original directory
changeDirectoryTo(cwd)
}
// generateJUnitRunnerSource generates the JUnitRunner Java source code,
// then returns its file path which is relative to oakSrcPath
func generateJUnitRunnerSource() string {
paths := strings.Split(runner, ".")
// compute the directory name where the source file of JUnitRunner should be stored.
dir := oakSrcPathWithVersion + PS + strings.Join(paths[:len(paths)-1], PS)
// create the directory.
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
exit(err, codeError)
}
// compute the source file name of JUnitRunner
javaFile := dir + PS + paths[len(paths)-1] + ".java"
// create the source file of JUnitRunner
f, err := os.Create(javaFile)
if err != nil {
exit(err, codeError)
}
_, _ = f.WriteString(runnerJavaSrc)
f.Close()
return strings.Join(paths, PS) + ".java"
}
func findTestsAndRunThem(args []string) {
testSrcDir, testTopDir, ok, pkgName := findTestSourceDirectory()
if !ok {
exit(fmt.Errorf("No test files are found"), codeTestsFailed)
}
compileAllJavaFilesUnderSrc(testSrcDir, testTopDir, pkgName)
runDir := "."
sourcepath := ""
// If testTopDir ends with "/test" then, tests will be invoked
// from its parent directory("..). In other words, some necessary
// files to be compiled may be located under "src" and "test" directories.
// Therefore "-sourcepath" option is used to specify such directories.
if strings.HasSuffix(testTopDir, PS+"test") {
runDir = ".."
sourcepath = ".." + PS + "src" + PLS + ".." + PS + "test"
}
testFiles := listTestFiles(testSrcDir)
if len(testFiles) == 0 {
exit(fmt.Errorf("No test files are found"), codeTestsFailed)
}
pkgDir := toPackageDirectory(pkgName)
for _, file := range testFiles {
// copmileAndRunTest() will change the current directory.
// So make sure to be the right directory every time.
changeDirectoryTo(testTopDir)
compileAndRunTest(runDir, sourcepath, pkgDir+file, args)
}
}
func toPackageDirectory(pkgName string) string {
if pkgName == "" {
return pkgName
}
return strings.Replace(pkgName, ".", PS, -1) + PS
}
// compileAllJavaFilesUnderSrc compiles all Java source files for the
// specified package under "src" directory.
func compileAllJavaFilesUnderSrc(testSrcDir, testDir, pkgName string) {
// tentative fix for simpleTest1
if pkgName == "" {
return
}
srcSrcDir := replaceToSrc(testSrcDir)
srcDir := replaceToSrc(testDir)
// If there is no corresponding src directory, do nothing
d, err := os.Open(srcSrcDir)
if err != nil {
return
}
d.Close()
javaFiles := listJavaFiles(srcSrcDir)
// save the current directory
cwd := getCWD()
pkgDir := toPackageDirectory(pkgName)
files := []string{}
for _, file := range javaFiles {
if isJUnitTestFile(srcSrcDir, file) {
fmt.Fprintf(os.Stderr, "\nWARNING: %s is ignored: move it into \"test\" directory\n\n", file)
continue
}
files = append(files, pkgDir+file)
}
changeDirectoryTo(srcDir)
srcPath := ".." + PS + "src" + PLS + ".." + PS + "test" + PLS + "."
compile(files, srcPath)
changeDirectoryTo(cwd)
}
func replaceToSrc(testPath string) string {
lastIndex := strings.LastIndex(testPath, PS+"test"+PS)
if lastIndex >= 0 {
return testPath[:lastIndex] + PS + "src" + PS + testPath[lastIndex+len(PS+"test"+PS):]
}
lastIndex = strings.LastIndex(testPath, PS+"test")
if lastIndex >= 0 {
return testPath[:lastIndex] + PS + "src"
}
return testPath
}
// findTestSourceDirectory determines the two directories for test:
// One directory is where all *Test.java files are located, another
// directory is the "test" directory.
func findTestSourceDirectory() (testSrcDir, testDir string, ok bool, pkgName string) {
defer func() {
dPrintf("testSrcDir = %q, testDir = %q, ok = %v\n", testSrcDir, testDir, ok)
}()
pkg := findPackageFromCurrentDirectory()
dPrintf("package = %q\n", pkg)
dir := getCWD()
srcPath := ""
if pkg != "" {
srcPath = PS + strings.Replace(pkg, ".", PS, -1)
lastIndex := strings.LastIndex(dir, srcPath)
if lastIndex < 0 {
exit(fmt.Errorf("directory(%q) doesn't match with the package(%q)", dir, pkg), codeError)
}
dir = dir[:lastIndex]
dPrintf("New dir = %q\n", dir)
}
if strings.HasSuffix(dir, PS+"test") {
return dir + srcPath, dir, true, pkg
}
if strings.HasSuffix(dir, PS+"src") {
testDir = dir[:len(dir)-3] + "test"
f, err := os.Open(testDir)
if err != nil {
// no "test" directory, use "src" directory
return dir + srcPath, dir, true, pkg
}
f.Close()
if len(listJavaFiles(testDir+srcPath)) == 0 {
// If there is no java file under the "test" directory,
// then ingore the "test" directory and use "src" directory.
return dir + srcPath, dir, true, pkg
}
return testDir + srcPath, testDir, true, pkg
}
// This is a corner case in which there is no "test" and "src" directory, but
// all Java files may be put into the same directory
return dir + srcPath, dir, true, pkg
}
func compileAsTest(sourcepath, src string) {
args := []string{"-d", oakBinPath}
args = append(args, []string{"-classpath", "." + PLS + junitPath + PLS + oakBinPath}...)
if sourcepath != "" {
args = append(args, "-sourcepath", sourcepath)
}
if *eFlag != "" {
args = append(args, "-encoding", *eFlag)
}
args = append(args, src)
javac(args)
}
func compileAndRunTest(runDir, sourcepath, srcFilePath string, options []string) {
compileAsTest(sourcepath, srcFilePath)
copyNonJavaFiles()
changeDirectoryTo(runDir)
args := []string{"-classpath", oakBinPath + PLS + "src" + PLS + junitPath}
args = append(args, runner)
if *vFlag {
args = append(args, "-v")
}
// Options will be passed to the Runner
for _, option := range options {
if strings.HasPrefix(option, "-run=") {
args = append(args, option)
}
}
srcPackagePath := strings.Replace(srcFilePath, PS, ".", -1)
srcPackagePath = srcPackagePath[:len(srcPackagePath)-len(".java")]
args = append(args, srcPackagePath)
args = append(javaFXOptions(), args...)
javaOneMinuteTimeout(args, codeTestsFailed)
}
func junitClassPath() string {
oakHome := os.Getenv("OAK_HOME")
if oakHome == "" {
exit(fmt.Errorf("OAK_HOME is not set"), codeError)
}
junitPath := oakHome + "/tools/junit"
jarFiles, err := files.ListFiles(junitPath,
func(fileName string) bool {
return strings.HasPrefix(fileName, "junit-") ||
strings.HasPrefix(fileName, "java-hamcrest-")
})
if err != nil {
fmt.Fprintf(os.Stderr, "OAK_HOME=%s seems incorrect\n", oakHome)
exit(err, codeError)
}
var builder strings.Builder
for i, jarFile := range jarFiles {
builder.WriteString(junitPath)
builder.WriteString(PS)
builder.WriteString(jarFile)
if i < len(jarFiles)-1 {
builder.WriteString(PLS)
}
}
return builder.String()
}