-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_test.go
141 lines (104 loc) · 2.92 KB
/
go_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
package patchy_test
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestGo(t *testing.T) {
t.Parallel()
dir, testDir, env, tests := buildGo(t)
t.Cleanup(func() {
os.RemoveAll(dir)
})
for _, test := range tests {
test := test
t.Run(
test,
func(t *testing.T) {
t.Parallel()
testGoPath(t, testDir, env, test)
},
)
}
}
func testGoPath(t *testing.T, testDir string, env map[string]string, test string) {
ctx := context.Background()
ta := newTestAPI(t)
defer ta.shutdown(t)
env2 := map[string]string{}
for k, v := range env {
env2[k] = v
}
env2["BASE_URL"] = ta.baseBaseURL
runNoError(ctx, t, testDir, env2, goCmd(), "test", "-run", fmt.Sprintf("^%s$", test))
ta.checkTests(t)
}
func buildGo(t *testing.T) (string, string, map[string]string, []string) {
dir, err := os.MkdirTemp("", "go_test")
require.NoError(t, err)
goPathDir := filepath.Join(dir, "path")
goCacheDir := filepath.Join(dir, "cache")
workDir := filepath.Join(dir, "work")
goClientDir := filepath.Join(dir, "work/goclient")
testDir := filepath.Join(dir, "work/gotest")
require.NoError(t, os.MkdirAll(goPathDir, 0o700))
require.NoError(t, os.MkdirAll(goCacheDir, 0o700))
require.NoError(t, os.MkdirAll(goClientDir, 0o700))
require.NoError(t, os.MkdirAll(testDir, 0o700))
paths, err := filepath.Glob("go_test/*")
require.NoError(t, err)
for _, path := range paths {
src, err := filepath.Abs(path)
require.NoError(t, err)
base := strings.TrimSuffix(filepath.Base(path), ".src")
err = os.Symlink(src, filepath.Join(testDir, base))
require.NoError(t, err)
}
ta := newTestAPI(t)
defer ta.shutdown(t)
ctx := context.Background()
resp, err := ta.r().Get("_client.go")
require.NoError(t, err)
require.True(t, resp.IsSuccess(), resp.String())
gc := resp.String()
require.NotEmpty(t, gc)
err = os.WriteFile(filepath.Join(dir, "work/goclient/client.go"), []byte(gc), 0o600)
require.NoError(t, err)
env := map[string]string{
"GOPATH": goPathDir,
"GOCACHE": goCacheDir,
}
runNoError(ctx, t, goClientDir, env, goCmd(), "mod", "init", "test/goclient")
runNoError(ctx, t, goClientDir, env, goCmd(), "mod", "tidy")
runNoError(ctx, t, testDir, env, goCmd(), "mod", "init", "test/gotest")
runNoError(ctx, t, testDir, env, goCmd(), "mod", "tidy")
err = os.WriteFile(filepath.Join(workDir, "go.work"), []byte(`
go 1.20
use (
./goclient
./gotest
)
`), 0o600)
require.NoError(t, err)
runNoError(ctx, t, goClientDir, env, goCmd(), "vet")
runNoError(ctx, t, testDir, env, goCmd(), "vet")
testBlob := runNoError(ctx, t, testDir, env, goCmd(), "test", "-list", ".")
tests := []string{}
for _, line := range strings.Split(testBlob, "\n") {
if strings.HasPrefix(line, "Test") {
tests = append(tests, line)
}
}
return dir, testDir, env, tests
}
func goCmd() string {
gocmd := os.Getenv("GOCMD")
if gocmd != "" {
return gocmd
}
return "go"
}