This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
retool_test.go
301 lines (256 loc) · 9.28 KB
/
retool_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/pkg/errors"
)
func TestRetool(t *testing.T) {
// These integration tests require more than most go tests: they require a go compiler to build
// retool, a working version of git to perform retool's operations, and network access to do the
// git fetches.
retool, err := buildRetool()
if err != nil {
t.Fatal(err)
}
defer func() {
_ = os.RemoveAll(filepath.Dir(retool))
}()
t.Run("retool tests", func(t *testing.T) {
t.Run("cache pollution", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
// This should fail because this version of mockery has an import line that points to uber's
// internal repo, which can't be reached:
cmd := exec.Command(retool, "-base-dir", dir, "add",
"github.com/vektra/mockery/cmd/mockery", "d895b9fcc32730719faaccd7840ad7277c94c2d0",
)
cmd.Dir = dir
_, err := cmd.Output()
if err == nil {
t.Fatal("expected error when adding mockery at broken commit d895b9, but got no error")
}
// Now, without cleaning the cache, try again on a healthy commit. In
// ff9a1fda7478ede6250ee3c7e4ce32dc30096236 of retool and earlier, this would still fail because
// the cache would be polluted with a bad source tree.
runRetoolCmd(t, dir, retool, "add", "github.com/vektra/mockery/cmd/mockery", "origin/master")
})
t.Run("version", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
// Should work even in a directory without tools.json
out := runRetoolCmd(t, dir, retool, "version")
if want := fmt.Sprintf("retool %s", version); string(out) != want {
t.Errorf("have=%q, want=%q", string(out), want)
}
})
t.Run("sync", func(t *testing.T) {
dir, cleanup := setupTempDir(t)
defer cleanup()
runRetoolCmd(t, dir, retool, "add", "github.com/twitchtv/retool", "origin/master")
// Delete existing tools directory to try and trigger out of date
_ = os.RemoveAll(filepath.Join(dir, "_tools"))
// Should be able to sync
runRetoolCmd(t, dir, retool, "sync")
assertBinInstalled(t, dir, "retool")
})
t.Run("build", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
runRetoolCmd(t, dir, retool, "add", "github.com/twitchtv/retool", "origin/master")
// Suppose we only have _tools/src available. Does `retool build` work?
_ = os.RemoveAll(filepath.Join(dir, "_tools", "bin"))
_ = os.RemoveAll(filepath.Join(dir, "_tools", "pkg"))
_ = os.RemoveAll(filepath.Join(dir, "_tools", "manifest.json"))
runRetoolCmd(t, dir, retool, "build")
// Now the binary should be installed
assertBinInstalled(t, dir, "retool")
// Legal files should be kept around
_, err := os.Stat(filepath.Join(dir, "_tools", "src", "github.com", "twitchtv", "retool", "LICENSE"))
if err != nil {
t.Error("missing license file")
}
})
t.Run("build_with_fork", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
runRetoolCmd(t, dir, retool, "-f", "https://github.com/franciscocpg/retool.git", "add", "github.com/twitchtv/retool", "origin/master")
// Suppose we only have _tools/src available. Does `retool build` work?
_ = os.RemoveAll(filepath.Join(dir, "_tools", "bin"))
_ = os.RemoveAll(filepath.Join(dir, "_tools", "pkg"))
_ = os.RemoveAll(filepath.Join(dir, "_tools", "manifest.json"))
runRetoolCmd(t, dir, retool, "build")
// Now the binary should be installed
assertBinInstalled(t, dir, "retool")
// Legal files should be kept around
_, err := os.Stat(filepath.Join(dir, "_tools", "src", "github.com", "twitchtv", "retool", "LICENSE"))
if err != nil {
t.Error("missing license file")
}
})
t.Run("build_with_gobin_set", func(t *testing.T) {
// Even if GOBIN is set to a directory not controlled by retool, running
// 'retool build' should still put built binaries in _tools/bin.
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
runRetoolCmd(t, dir, retool, "add", "github.com/twitchtv/retool", "origin/master")
cmd := makeRetoolCmd(t, dir, retool, "build")
cmd.Env = append(os.Environ(), "GOBIN="+dir)
err := cmd.Run()
if err != nil {
t.Fatalf("fatal go build err: %v", err)
}
assertBinInstalled(t, dir, "retool")
})
t.Run("dep_added", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
// Use a package which used to have a dependency (in this case, one on
// github.com/spenczar/retool_test_lib), but doesn't have that dependency for HEAD of
// origin/master today.
runRetoolCmd(t, dir, retool, "add", "github.com/spenczar/retool_test_app", "origin/has_dep")
})
t.Run("clean", func(t *testing.T) {
// Clean should be a noop, but kept around for compatibility
cmd := exec.Command(retool, "clean")
_, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
t.Fatalf("expected no errors when using retool clean, have this:\n%s", string(exitErr.Stderr))
} else {
t.Fatalf("unexpected err when running %q: %q", strings.Join(cmd.Args, " "), err)
}
}
})
t.Run("do", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
runRetoolCmd(t, dir, retool, "add", "github.com/twitchtv/retool", "v1.0.1")
output := runRetoolCmd(t, dir, retool, "do", "retool", "version")
if want := "retool v1.0.1"; output != want {
t.Errorf("have=%q, want=%q", output, want)
}
})
t.Run("upgrade", func(t *testing.T) {
t.Parallel()
dir, cleanup := setupTempDir(t)
defer cleanup()
runRetoolCmd(t, dir, retool, "add", "github.com/twitchtv/retool", "v1.0.1")
runRetoolCmd(t, dir, retool, "upgrade", "github.com/twitchtv/retool", "v1.0.3")
out := runRetoolCmd(t, dir, retool, "do", "retool", "version")
if want := "retool v1.0.3"; string(out) != want {
t.Errorf("have=%q, want=%q", string(out), want)
}
})
t.Run("gometalinter exemption", func(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to make temp dir: %s", err)
}
defer func() {
_ = os.RemoveAll(dir)
}()
runRetoolCmd(t, dir, retool, "add", "github.com/alecthomas/gometalinter", "origin/master")
runRetoolCmd(t, dir, retool, "do", "gometalinter", "--install")
// Create a dummy go file so gometalinter runs. If we don't do this,
// gometalinter will exit without doing any work, and we'll get a false
// positive.
//
// The file will be removed with the deferred os.RemoveAll(dir) call, no
// need to remove it here.
f, err := os.Create(filepath.Join(dir, "main.go"))
if err != nil {
t.Fatalf("unable to create file for gometalinter to run against: %s", err)
}
defer func() {
if closeErr := f.Close(); closeErr != nil {
t.Errorf("unable to close gometalinter test file: %s", closeErr)
}
}()
_, err = io.WriteString(f, `package main
func main() {}`)
if err != nil {
t.Fatalf("unable to write gometalinter test file: %s", err)
}
// If gometalinter can't find its tools, it will exit with code 2.
runRetoolCmd(t, dir, retool, "do", "gometalinter", ".")
// Make sure gometalinter installs to the tool directory, not to the global
// GOPATH.
assertBinInstalled(t, dir, "structcheck")
})
})
}
func makeRetoolCmd(t *testing.T, dir, retool string, args ...string) *exec.Cmd {
args = append([]string{"-base-dir", dir}, args...)
cmd := exec.Command(retool, args...)
cmd.Dir = dir
return cmd
}
func runRetoolCmd(t *testing.T, dir, retool string, args ...string) (output string) {
cmd := makeRetoolCmd(t, dir, retool, args...)
out, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
t.Fatalf("command %q failed, stderr:\n%s\n\nstdout:%s", "retool "+strings.Join(cmd.Args[1:], " "), string(exitErr.Stderr), string(out))
} else {
t.Fatalf("unexpected err when running %q: %q", strings.Join(cmd.Args, " "), err)
}
}
return string(out)
}
func nameOfTest(t *testing.T) string {
// t.Name() was added in go1.8. If it's available, use it. Otherwise, return "".
v, ok := interface{}(t).(interface {
Name() string
})
if ok {
return v.Name()
}
return ""
}
func setupTempDir(t *testing.T) (dir string, cleanup func()) {
dir, err := ioutil.TempDir("", strings.Replace(nameOfTest(t), "/", "_", -1))
if err != nil {
t.Fatalf("unable to make temp dir: %s", err)
}
cleanup = func() {
if err := os.RemoveAll(dir); err != nil {
t.Errorf("unable to clean up temp dir: %s", err)
}
}
return dir, cleanup
}
// buildRetool builds retool in a temporary directory and returns the path to
// the built binary
func buildRetool() (string, error) {
dir, err := ioutil.TempDir("", "")
if err != nil {
return "", errors.Wrap(err, "unable to create temporary build directory")
}
output := filepath.Join(dir, "retool"+osBinSuffix)
cmd := exec.Command("go", "build", "-o", output, ".")
_, err = cmd.Output()
if err != nil {
return "", errors.Wrap(err, "unable to build retool binary")
}
return output, nil
}
func assertBinInstalled(t *testing.T, wd, bin string) {
_, err := os.Stat(filepath.Join(wd, "_tools", "bin", bin+osBinSuffix))
if err != nil {
t.Errorf("unable to find %s: %s", bin+osBinSuffix, err)
}
}