This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.go
232 lines (197 loc) · 4.67 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
package test
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/meta"
"github.com/gobuffalo/pop/v6"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func runE(c *cobra.Command, args []string) error {
os.Setenv("GO_ENV", "test")
if _, err := os.Stat("database.yml"); err == nil {
// there's a database
test, err := pop.Connect("test")
if err != nil {
return err
}
// drop the test db:
if err := test.Dialect.DropDB(); err != nil {
// not an error, since the database will be created in the next step anyway
logrus.Info("no test database to drop")
}
// create the test db:
err = test.Dialect.CreateDB()
if err != nil {
return err
}
// Read and remove --force-migrations flag from args:
forceMigrations := strings.Contains(strings.Join(args, ""), "--force-migrations")
args = cutArg("--force-migrations", args)
if forceMigrations {
fm, err := pop.NewFileMigrator("./migrations", test)
if err != nil {
return err
}
if err := fm.Up(); err != nil {
return err
}
return testRunner(args)
}
if schema := findSchema(); schema != nil {
err = test.Dialect.LoadSchema(schema)
if err != nil {
return err
}
}
}
return testRunner(args)
}
func findSchema() io.Reader {
if f, err := os.Open(filepath.Join("migrations", "schema.sql")); err == nil {
return f
}
if dev, err := pop.Connect("development"); err == nil {
schema := &bytes.Buffer{}
if err = dev.Dialect.DumpSchema(schema); err == nil {
return schema
}
}
if test, err := pop.Connect("test"); err == nil {
fm, err := pop.NewFileMigrator("./migrations", test)
if err != nil {
return nil
}
if err := fm.Up(); err == nil {
return nil
}
}
return nil
}
func testRunner(args []string) error {
var mFlag bool
var query string
commandArgs := []string{}
packageArgs := []string{}
var lastArg string
for index, arg := range args {
switch arg {
case "-run", "-m":
query = args[index+1]
mFlag = true
case "-v", "-timeout":
commandArgs = append(commandArgs, arg)
default:
if lastArg == "-timeout" {
commandArgs = append(commandArgs, arg)
} else if lastArg != "-run" && lastArg != "-m" {
packageArgs = append(packageArgs, arg)
}
}
lastArg = arg
}
cmd := newTestCmd(commandArgs)
if mFlag {
return mFlagRunner{
query: query,
args: commandArgs,
pargs: packageArgs,
}.Run()
}
pkgs, err := testPackages(packageArgs)
if err != nil {
return err
}
cmd.Args = append(cmd.Args, pkgs...)
logrus.Info(strings.Join(cmd.Args, " "))
return cmd.Run()
}
type mFlagRunner struct {
query string
args []string
pargs []string
}
func (m mFlagRunner) Run() error {
app := meta.New(".")
pwd, _ := os.Getwd()
defer os.Chdir(pwd)
pkgs, err := testPackages(m.pargs)
if err != nil {
return err
}
var errs bool
for _, p := range pkgs {
os.Chdir(pwd)
if p == app.PackagePkg {
continue
}
cmd := newTestCmd(m.args)
p = strings.TrimPrefix(p, app.PackagePkg+string(filepath.Separator))
os.Chdir(p)
if hasTestify(cmd.Args) {
cmd.Args = append(cmd.Args, "-testify.m", m.query)
} else {
cmd.Args = append(cmd.Args, "-run", m.query)
}
logrus.Info(strings.Join(cmd.Args, " "))
if err := cmd.Run(); err != nil {
errs = true
}
}
if errs {
return fmt.Errorf("errors running tests")
}
return nil
}
func hasTestify(args []string) bool {
cmd := exec.Command(args[0], args[1:]...)
cmd.Args = append(cmd.Args, "-unknownflag")
b, _ := cmd.Output()
return bytes.Contains(b, []byte("-testify.m"))
}
func testPackages(givenArgs []string) ([]string, error) {
// If there are args, then assume these are the packages to test.
//
// Instead of always returning all packages from 'go list ./...', just
// return the given packages in this case
if len(givenArgs) > 0 {
return givenArgs, nil
}
args := []string{}
out, err := exec.Command(envy.Get("GO_BIN", "go"), "list", "./...").Output()
if err != nil {
return args, err
}
pkgs := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
for _, p := range pkgs {
if !strings.Contains(string(p), "/vendor/") {
args = append(args, string(p))
}
}
return args, nil
}
func newTestCmd(args []string) *exec.Cmd {
cargs := []string{"test", "-p", "1"}
app := meta.New(".")
cargs = append(cargs, "-tags", app.BuildTags("development").String())
cargs = append(cargs, args...)
cmd := exec.Command(envy.Get("GO_BIN", "go"), cargs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func cutArg(arg string, args []string) []string {
for i, v := range args {
if v == arg {
return append(args[:i], args[i+1:]...)
}
}
return args
}