forked from exercism/go-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
176 lines (155 loc) · 5.17 KB
/
integration_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
package main
import (
"bytes"
"go/build"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var regexReplacements = []struct {
regexp *regexp.Regexp
replaceStr string
}{
{
// Test duration
regexp: regexp.MustCompile(`\\t[0-9]+\.[0-9]+s`),
replaceStr: "",
},
{
// Test duration in brackets
regexp: regexp.MustCompile(`\([0-9]+\.[0-9]+s\)`),
replaceStr: "",
},
{
// Pointer
regexp: regexp.MustCompile(`\+?\(?0x[0-9a-f]+\)?`),
replaceStr: "",
},
{
// Goroutine
regexp: regexp.MustCompile(`goroutine [0-9]+`),
replaceStr: "goroutine x",
},
{
// Line number
regexp: regexp.MustCompile(`\.go:[0-9]+(:[0-9]+)?`),
replaceStr: ".go",
},
}
func TestIntegration(t *testing.T) {
tests := []struct {
inputDir string
expected string
}{
{
// This test case covers the case the code under test does not compile,
// i.e. "go build ." would fail.
inputDir: filepath.Join("testrunner", "testdata", "practice", "broken"),
expected: filepath.Join("testrunner", "testdata", "expected", "broken.json"),
},
{
// This test case covers the case that the test code does not compile,
// i.e. "go build ." would succeed but "go test" returns compilation errors.
inputDir: filepath.Join("testrunner", "testdata", "practice", "missing_func"),
expected: filepath.Join("testrunner", "testdata", "expected", "missing_func.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "practice", "broken_import"),
expected: filepath.Join("testrunner", "testdata", "expected", "broken_import.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "practice", "passing"),
expected: filepath.Join("testrunner", "testdata", "expected", "passing.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "practice", "pkg_level_error"),
expected: filepath.Join("testrunner", "testdata", "expected", "pkg_level_error.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "practice", "failing"),
expected: filepath.Join("testrunner", "testdata", "expected", "failing.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "concept", "auto_assigned_task_ids"),
expected: filepath.Join("testrunner", "testdata", "expected", "auto_assigned_task_ids.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "concept", "explicit_task_ids"),
expected: filepath.Join("testrunner", "testdata", "expected", "explicit_task_ids.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "concept", "missing_task_ids"),
expected: filepath.Join("testrunner", "testdata", "expected", "missing_task_ids.json"),
},
{
inputDir: filepath.Join("testrunner", "testdata", "concept", "non_executed_tests"),
expected: filepath.Join("testrunner", "testdata", "expected", "non_executed_tests.json"),
},
}
goExe, err := exec.LookPath("go")
require.NoError(t, err, "failed to find go executable")
goRoot := os.Getenv("GOROOT")
if goRoot == "" {
goRoot = build.Default.GOROOT
}
currentDir, err := os.Getwd()
require.NoError(t, err, "failed to determine current directory")
for _, tt := range tests {
t.Run(tt.inputDir, func(t *testing.T) {
err := os.RemoveAll("outdir")
require.NoError(t, err, "failed to clean up output directory")
var stdout, stderr bytes.Buffer
cmd := &exec.Cmd{
Path: goExe,
Args: []string{goExe, "run", ".", tt.inputDir, "outdir"},
Stdout: &stdout,
Stderr: &stderr,
}
err = cmd.Run()
require.NoErrorf(t, err, "failed to execute test runner: %s %s", stdout.String(), stderr.String())
resultBytes, err := os.ReadFile(filepath.Join("outdir", "results.json"))
require.NoError(t, err, "failed to read results")
result := sanitizeResult(string(resultBytes), []string{goExe, currentDir, goRoot})
expected, err := os.ReadFile(tt.expected)
require.NoError(t, err, "failed to read expected result file")
assert.JSONEq(t, string(expected), result)
})
}
}
func sanitizeResult(s string, paths []string) string {
result := s
for _, p := range pathVariations(paths) {
result = strings.ReplaceAll(result, p, "PATH_PLACEHOLDER")
}
if runtime.GOOS == "windows" {
result = strings.ReplaceAll(result, `\n.//`, `\n./`)
result = strings.ReplaceAll(result, `\n.\\`, `\n./`)
result = strings.ReplaceAll(result, `\n.\`, `\n./`)
}
for _, replacement := range regexReplacements {
result = replacement.regexp.ReplaceAllString(result, replacement.replaceStr)
}
return result
}
func pathVariations(paths []string) []string {
result := []string{}
for _, p := range paths {
normalizedPath := filepath.ToSlash(p)
result = append(result, normalizedPath)
if runtime.GOOS == "windows" {
// On windows, the paths that are included in the test results can have
// various formats. We try to include all variants here so we catch
// everything when we do the replace later.
result = append(result, strings.ReplaceAll(normalizedPath, "/", "//"))
result = append(result, strings.ReplaceAll(normalizedPath, "/", `\`))
result = append(result, strings.ReplaceAll(normalizedPath, "/", `\\`))
}
}
return result
}