/
runtests_test.go
116 lines (107 loc) · 2.3 KB
/
runtests_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
/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */
package jutgelint
import (
"bytes"
"flag"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
var (
write = flag.Bool("write", false, "Write json results")
name = flag.String("name", "", "Test name")
)
func init() {
flag.Parse()
}
const (
testsDir = "tests"
inGlob = "in.*"
)
func inPaths(dir, file string) (string, error) {
pattern := filepath.Join(testsDir, dir, file)
paths, err := filepath.Glob(pattern)
if err != nil {
return "", err
}
return paths[0], nil
}
func doTest(t *testing.T, dir, inPath string) {
base := filepath.Base(inPath)
lang, err := ParseLangFilename(base)
if err != nil {
t.Errorf("Could not infer language: %v", err)
return
}
outPath := filepath.Join(testsDir, dir, "out"+filepath.Ext(base))
in, err := os.Open(inPath)
if err != nil {
t.Errorf("Failed opening in file %s: %v", inPath, err)
return
}
defer in.Close()
var out bytes.Buffer
if err := CheckAndCommentCode(lang, in, &out); err != nil {
t.Errorf("Failed checking and commenting code '%s': %v", inPath, err)
return
}
got := out.Bytes()
if *write {
out, err := os.Create(outPath)
if err != nil {
t.Errorf("Failed opening out file '%s': %v", outPath, err)
return
}
defer out.Close()
_, err = out.Write(got)
if err != nil {
t.Errorf("Failed writing out file '%s': %v", outPath, err)
return
}
} else {
out, err := os.Open(outPath)
if err != nil {
t.Errorf("Failed opening out file '%s': %v", outPath, err)
return
}
defer out.Close()
want, err := ioutil.ReadAll(out)
if err != nil {
t.Errorf("Failed reading out file '%s': %v", outPath, err)
return
}
if string(want) != string(got) {
t.Errorf("Mismatching outputs for '%s'", inPath)
return
}
}
}
func doTestDir(t *testing.T, dir string) {
inPattern := filepath.Join(testsDir, dir, inGlob)
inPaths, err := filepath.Glob(inPattern)
if err != nil {
t.Errorf("Globbing failed: %v", err)
return
}
for _, path := range inPaths {
doTest(t, dir, path)
}
}
func TestCases(t *testing.T) {
entries, err := ioutil.ReadDir(testsDir)
if err != nil {
return
}
if *name != "" {
doTestDir(t, *name)
} else {
for _, e := range entries {
if !e.IsDir() {
continue
}
doTestDir(t, e.Name())
}
}
}