-
Notifications
You must be signed in to change notification settings - Fork 320
/
tests.go
166 lines (143 loc) · 3.87 KB
/
tests.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
// Package build supports building and testing Encore applications
// with codegen and rewrite overlays.
package build
import (
"context"
"fmt"
"go/token"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"encr.dev/internal/etrace"
"encr.dev/pkg/paths"
"encr.dev/v2/internals/overlay"
"encr.dev/v2/internals/perr"
)
type TestConfig struct {
Config
// Args are additional arguments to "go test".
Args []string
// Stdout specifies the stdout to use.
Stdout io.Writer
// Stderr specifies the stderr to use.
Stderr io.Writer
// WorkingDir is the working directory to invoke
// the "go test" command from.
WorkingDir paths.FS
}
func Test(ctx context.Context, cfg *TestConfig) {
b := &builder{
ctx: ctx,
cfg: &cfg.Config,
testCfg: cfg,
errs: cfg.Ctx.Errs,
}
b.Test()
}
func (b *builder) Test() {
workdir, err := os.MkdirTemp("", "encore-test")
if err != nil {
b.errs.AddStd(err)
return
}
// NOTE(andre): There appears to be a bug in go's handling of overlays
// when the source or destination is a symlink.
// I haven't dug into the root cause exactly, but it causes weird issues
// with tests since macOS's /var/tmp is a symlink to /private/var/tmp.
if d, err := filepath.EvalSymlinks(workdir); err == nil {
workdir = d
}
b.workdir = paths.RootedFSPath(workdir, ".")
defer func() {
// If we have a bailout or any errors, delete the workdir.
if _, ok := perr.CatchBailout(recover()); ok || b.errs.Len() > 0 {
if !b.cfg.KeepOutput && workdir != "" {
_ = os.RemoveAll(workdir)
}
}
}()
if b.cfg.KeepOutput && workdir != "" {
_, _ = fmt.Fprintf(b.testCfg.Stdout, "wrote generated code to: %s\n", workdir)
}
for _, fn := range []func(){
b.writeModFile,
b.writeSumFile,
b.runTests,
} {
fn()
// Abort early if we encountered any errors.
if b.errs.Len() > 0 {
break
}
}
}
func (b *builder) runTests() {
etrace.Sync0(b.ctx, "", "runTests", func(ctx context.Context) {
overlayFiles := append(b.overlays, b.cfg.Overlays...)
overlayPath, err := overlay.Write(b.workdir, overlayFiles)
if err != nil {
b.errs.Addf(token.NoPos, "unable to write overlay file: %v", err)
return
}
build := b.cfg.Ctx.Build
tags := append([]string{"encore", "encore_internal", "encore_app"}, build.BuildTags...)
args := []string{
"test",
"-tags=" + strings.Join(tags, ","),
"-overlay=" + overlayPath.ToIO(),
"-modfile=" + b.gomodPath().ToIO(),
"-mod=mod",
"-vet=off",
}
if b.cfg.Ctx.Build.StaticLink {
var ldflags string
// Enable external linking if we use cgo.
if b.cfg.Ctx.Build.CgoEnabled {
ldflags = "-linkmode external "
}
ldflags += `-extldflags "-static"`
args = append(args, "-ldflags", ldflags)
}
if b.cfg.Ctx.Build.Debug {
// Disable inlining for better debugging.
args = append(args, `-gcflags "all=-N -l"`)
}
args = append(args, b.testCfg.Args...)
goroot := build.GOROOT
cmd := exec.CommandContext(b.cfg.Ctx.Ctx, goroot.Join("bin", "go"+b.exe()).ToIO(), args...)
// Copy the env before we add additional env vars
// to avoid accidentally sharing the same backing array.
env := make([]string, len(b.cfg.Env))
copy(env, b.cfg.Env)
env = append(env,
"GO111MODULE=on",
"GOROOT="+goroot.ToIO(),
)
if goos := build.GOOS; goos != "" {
env = append(env, "GOOS="+goos)
}
if goarch := build.GOARCH; goarch != "" {
env = append(env, "GOARCH="+goarch)
}
if !build.CgoEnabled {
env = append(env, "CGO_ENABLED=0")
}
cmd.Env = append(os.Environ(), env...)
cmd.Dir = b.testCfg.WorkingDir.ToIO()
cmd.Stdout = b.testCfg.Stdout
cmd.Stderr = b.testCfg.Stderr
err = cmd.Run()
if err != nil {
if err.Error() == "exit status 1" {
// This is a standard error code for failed tests.
// so we don't need to wrap it.
b.errs.Add(ErrTestFailed)
} else {
// Otherwise we wrap the error.
b.errs.Add(ErrTestFailed.Wrapping(err))
}
}
})
}