Debian Codegraph @

get_e2e_test.go

raw symbols SCIP
1 // Copyright (c) Bartłomiej Płotka @bwplotka
2 // Licensed under the Apache License 2.0.
3
4 package main_test
5
6 import (
7 "context"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "github.com/Masterminds/semver"
15 "github.com/bwplotka/bingo/pkg/bingo"
16 "github.com/bwplotka/bingo/pkg/runner"
17 "github.com/bwplotka/bingo/pkg/version"
18 "github.com/efficientgo/core/testutil"
19 )
20
21 const (
22 bingoBin = "bingo"
23 defaultModDir = ".bingo"
24 defaultGoProxy = "https://proxy.golang.org"
25 )
26
27 func TestGetList(t *testing.T) {
28 t.Parallel()
29
30 g := newIsolatedGoEnv(t, defaultGoProxy)
31 defer g.Close(t)
32
33 for _, isGoProject := range []bool{false, true} {
34 if ok := t.Run(fmt.Sprintf("isGoProject=%v", isGoProject), func(t *testing.T) {
35 g.Clear(t)
36
37 testutil.Ok(t, os.MkdirAll(filepath.Join(g.tmpDir, "newproject"), os.ModePerm))
38 p := newTestProject(t, filepath.Join(g.tmpDir, "newproject"), filepath.Join(g.tmpDir, "testproject"), isGoProject)
39 p.assertNotChanged(t)
40
41 // We manually build bingo binary to make sure GOCACHE will not hit us.
42 bingoPath := filepath.Join(g.tmpDir, bingoBin)
43 buildInitialGobin(t, bingoPath)
44
45 // TODO(bwplotka): Add buildable back from array version to normal one.
46 var prevBinaries []string
47 for _, tcase := range []struct {
48 name string
49 do func(t *testing.T)
50
51 expectBinaries []string
52 expectSameBinariesAsBefore bool
53 expectRows []row
54 }{
55 {
56 name: "get one module by tag on empty project",
57 do: func(t *testing.T) {
58 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/[email protected]"))
59 testutil.Equals(t, g.ExecOutput(t, p.root, bingoPath, "list", "buildable"), g.ExecOutput(t, p.root, bingoPath, "list"))
60 },
61 expectRows: []row{{name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"}},
62 expectBinaries: []string{"buildable-v1.0.0"},
63 },
64 {
65 name: "get the same module by tag does not change anything",
66 do: func(t *testing.T) {
67 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/[email protected]"))
68 testutil.Equals(t, g.ExecOutput(t, p.root, bingoPath, "list", "buildable"), g.ExecOutput(t, p.root, bingoPath, "list"))
69 },
70 expectRows: []row{{name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"}},
71 expectSameBinariesAsBefore: true,
72 },
73 {
74 name: "get the same module by commit does not change anything",
75 do: func(t *testing.T) {
76 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable@4040b74bfd07be764f2ad16ddbf7fba07b247f1c"))
77 testutil.Equals(t, g.ExecOutput(t, p.root, bingoPath, "list", "buildable"), g.ExecOutput(t, p.root, bingoPath, "list"))
78 },
79 expectRows: []row{{name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"}},
80 expectSameBinariesAsBefore: true,
81 },
82 {
83 name: "get the different module with different tool name by commit",
84 do: func(t *testing.T) {
85 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable2@9d83f47b84c5d9262ecaf649bfa01f0f1cb6ebd2"))
86 },
87 expectRows: []row{
88 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
89 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
90 },
91 expectBinaries: []string{"buildable-v1.0.0", "buildable2-v0.0.0-20221007091238-9d83f47b84c5"},
92 },
93 {
94 name: "latest tag should upgrade the tool.",
95 do: func(t *testing.T) {
96 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "buildable@latest"))
97 },
98 expectRows: []row{
99 {name: "buildable", binName: "buildable-v1.1.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
100 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
101 },
102 expectBinaries: []string{"buildable-v1.0.0", "buildable-v1.1.0", "buildable2-v0.0.0-20221007091238-9d83f47b84c5"},
103 },
104 {
105 name: "downgrade the tool",
106 do: func(t *testing.T) {
107 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/[email protected]"))
108 },
109 expectRows: []row{
110 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
111 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
112 },
113 expectSameBinariesAsBefore: true,
114 },
115 {
116 name: "get same tool under different name",
117 do: func(t *testing.T) {
118 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "-n", "buildable-v2", "github.com/bwplotka/bingo-testmodule/v2/[email protected]"))
119 },
120 expectRows: []row{
121 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
122 {name: "buildable-v2", binName: "buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
123 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
124 },
125 expectBinaries: []string{"buildable-v1.0.0", "buildable-v1.1.0", "buildable-v2-v2.0.0", "buildable2-v0.0.0-20221007091238-9d83f47b84c5"},
126 },
127 {
128 name: "rename the tool",
129 do: func(t *testing.T) {
130 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "-r", "my-buildable-v2", "buildable-v2"))
131 },
132 expectRows: []row{
133 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
134 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
135 {name: "my-buildable-v2", binName: "my-buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
136 },
137 expectBinaries: []string{"buildable-v1.0.0", "buildable-v1.1.0", "buildable-v2-v2.0.0", "buildable2-v0.0.0-20221007091238-9d83f47b84c5", "my-buildable-v2-v2.0.0"},
138 },
139 {
140 name: "build as latest",
141 do: func(t *testing.T) {
142 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "-l", "buildable"))
143 },
144 expectRows: []row{
145 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
146 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
147 {name: "my-buildable-v2", binName: "my-buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
148 },
149 expectBinaries: []string{"buildable", "buildable-v1.0.0", "buildable-v1.1.0", "buildable-v2-v2.0.0", "buildable2-v0.0.0-20221007091238-9d83f47b84c5", "my-buildable-v2-v2.0.0"},
150 },
151 {
152 name: "remove tool",
153 do: func(t *testing.T) {
154 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "buildable@none"))
155 },
156 expectRows: []row{
157 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
158 {name: "my-buildable-v2", binName: "my-buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
159 },
160 expectSameBinariesAsBefore: true,
161 },
162 {
163 name: "install array version",
164 do: func(t *testing.T) {
165 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable@39a7f0ae0b1e1e67a75033fc671ccc2c5b3bbddf,v1.0.0,v1.1.0"))
166 },
167 expectRows: []row{
168 {name: "buildable", binName: "buildable-v0.0.0-20221007091146-39a7f0ae0b1e", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
169 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
170 {name: "buildable", binName: "buildable-v1.1.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
171 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
172 {name: "my-buildable-v2", binName: "my-buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
173 },
174 expectBinaries: []string{"buildable", "buildable-v0.0.0-20221007091146-39a7f0ae0b1e", "buildable-v1.0.0", "buildable-v1.1.0", "buildable-v2-v2.0.0", "buildable2-v0.0.0-20221007091238-9d83f47b84c5", "my-buildable-v2-v2.0.0"},
175 },
176 {
177 name: "get from array version to single one",
178 do: func(t *testing.T) {
179 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "[email protected]"))
180 },
181 expectRows: []row{
182 {name: "buildable", binName: "buildable-v1.1.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
183 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
184 {name: "my-buildable-v2", binName: "my-buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
185 },
186 expectSameBinariesAsBefore: true,
187 },
188 {
189 name: "get from single version back to array",
190 do: func(t *testing.T) {
191 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable@39a7f0ae0b1e1e67a75033fc671ccc2c5b3bbddf,v1.0.0,v1.1.0"))
192 },
193 expectRows: []row{
194 {name: "buildable", binName: "buildable-v0.0.0-20221007091146-39a7f0ae0b1e", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
195 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
196 {name: "buildable", binName: "buildable-v1.1.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
197 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
198 {name: "my-buildable-v2", binName: "my-buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
199 },
200 expectSameBinariesAsBefore: true,
201 },
202 {
203 name: "remove all",
204 do: func(t *testing.T) {
205 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "buildable@none"))
206 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "buildable2@none"))
207 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "my-buildable-v2@none"))
208
209 // Check additionally if Makefile was removed.
210 _, err := os.Stat(filepath.Join(p.root, ".bingo", "Variables.mk"))
211 testutil.NotOk(t, err)
212 },
213 expectSameBinariesAsBefore: true,
214 },
215 } {
216 if ok := t.Run(tcase.name, func(t *testing.T) {
217 defer p.assertNotChanged(t, defaultModDir)
218
219 tcase.do(t)
220
221 expectBingoListRows(t, tcase.expectRows, g.ExecOutput(t, p.root, bingoPath, "list"))
222
223 binaries := g.existingBinaries(t)
224 if tcase.expectSameBinariesAsBefore {
225 testutil.Equals(t, prevBinaries, g.existingBinaries(t))
226 } else {
227 testutil.Equals(t, tcase.expectBinaries, g.existingBinaries(t))
228 }
229 prevBinaries = binaries
230 }); !ok {
231 return
232 }
233 }
234
235 }); !ok {
236 return
237 }
238 }
239 }
240
241 func TestGet_Errors(t *testing.T) {
242 // Existing bug - > on names can be case-sensitive but makefile varialbes are not!
243
244 // name: "error cases",
245 // do: func(t *testing.T) {
246 // // Installing different tool with name clash should fail
247 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "github.com/bwplotka/totally-not-bingo/testdata/module/[email protected]"))
248 // // Installing package with go name should fail. (this is due to clash with go.mod).
249 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "github.com/something/go"))
250 // // Naive installing package that would result with `cmd` name fails - different name is suggested.
251 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "github.com/bwplotka/[email protected]"))
252 // // Updating f4 to multiple versions with none should fail.
253 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "[email protected],v1.1.0,none"))
254 // // Installing by different path that would result in same name
255 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "github.com/bwplotka/bingo/some/module/buildable"))
256 // // Removing by path.
257 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "github.com/bwplotka/bingo/testdata/module/buildable@none"))
258 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "github.com/bwplotka/bingo/some/module/buildable@none"))
259 // // Removing non existing tool.
260 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "buildable2@none"))
261 // },
262 // Get unexisting module
263 // Rename unexisting tool
264 // Removing unexisting tool
265 // Rm by path
266 // Use g.ExpectErr(p...
267 // name: "-r rename error cases",
268 // do: func(t *testing.T) {
269 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=buildable4", "github.com/bwplotka/bingo/testdata/module/buildable2"))
270 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=buildable4", "github.com/bwplotka/bingo/testdata/module/[email protected]"))
271 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=buildable4", "github.com/bwplotka/bingo/testdata/module/buildable2@none"))
272 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=buildable4", "buildable2@none"))
273 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=buildable4", "[email protected]"))
274 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=faillint", "buildable2")) // Renaming to existing name.
275 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r=faiLLint", "buildable2")) // Renaming to existing name (it's not case sensitive).
276 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r", "f3", "x")) // Renaming not existing.
277 // testutil.NotOk(t, g.ExpectErr(p.root, bingoPath, "get", "-r", "f4", "[email protected],v1.0.0"))
278 // },
279 // {
280 // name: "get wr_buildable@ccbd4039b94aac79d926ba5eebfe6a132a728ed8 (dowgrade buildable with different replaces - trickier than you think!)",
281 // do: func(t *testing.T) {
282 // fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "wr_buildable@ccbd4039b94aac79d926ba5eebfe6a132a728ed8"))
283 //
284 // // Check if installed tool is what we expect.
285 // testutil.Equals(t, "module_with_replace.buildable 2.8\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "wr_buildable-v0.0.0-20210110214650-ab990d1be30b")))
286 // testutil.Equals(t, "module_with_replace.buildable 2.7\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a")))
287 // },
288 // expectRows: []row{
289 // {name: "buildable", binName: "buildable-v0.0.0-20210109094001-375d0606849d", pkgVersion: "github.com/bwplotka/bingo/testdata/module/[email protected]"},
290 // {name: "buildable2", binName: "buildable2-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/[email protected]"},
291 // {name: "buildable_old", binName: "buildable_old-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/[email protected]"},
292 // {name: "faillint", binName: "faillint-v1.3.0", pkgVersion: "github.com/fatih/[email protected]"},
293 // {name: "go-bindata", binName: "go-bindata-v3.1.1+incompatible", pkgVersion: "github.com/go-bindata/go-bindata/[email protected]+incompatible"},
294 // {name: "wr_buildable", binName: "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a", pkgVersion: "github.com/bwplotka/bingo/testdata/module_with_replace/[email protected]"},
295 // },
296 // expectBinaries: []string{
297 // "buildable-v0.0.0-20210109093942-2e6391144e85", "buildable-v0.0.0-20210109094001-375d0606849d", "buildable2-v0.0.0-20210109093942-2e6391144e85",
298 // "buildable_old-v0.0.0-20210109093942-2e6391144e85", "buildable_old-v0.0.0-20210109094001-375d0606849d",
299 // "faillint-v1.3.0", "faillint-v1.4.0", "faillint-v1.5.0",
300 // "go-bindata-v3.1.1+incompatible",
301 // "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a", "wr_buildable-v0.0.0-20210110214650-ab990d1be30b",
302 // },
303 // },
304 }
305
306 func TestCompatibilityCurrentVersionCreate(t *testing.T) {
307 currTestCaseDir := fmt.Sprintf("testdata/testproject_with_bingo_%s", strings.ReplaceAll(version.Version, ".", "_"))
308
309 g := newIsolatedGoEnv(t, defaultGoProxy)
310 defer g.Close(t)
311
312 // We manually build bingo binary to make sure GOCACHE will not hit us.
313 bingoPath := filepath.Join(g.tmpDir, bingoBin)
314 buildInitialGobin(t, bingoPath)
315
316 testutil.Ok(t, os.MkdirAll(filepath.Join(g.tmpDir, "newproject"), os.ModePerm))
317 p := newTestProject(t, filepath.Join(g.tmpDir, "newproject"), filepath.Join(g.tmpDir, "testproject"), false)
318 p.assertNotChanged(t)
319
320 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable@39a7f0ae0b1e1e67a75033fc671ccc2c5b3bbddf,v1.0.0,v1.1.0"))
321 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable2@9d83f47b84c5d9262ecaf649bfa01f0f1cb6ebd2"))
322 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "-n", "buildable-v2", "github.com/bwplotka/bingo-testmodule/v2/[email protected]"))
323 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "-n", "buildable-withReplace", "github.com/bwplotka/bingo-testmodule/buildable@fe4d42a37d927cbf6d2c0f30fe1459d493146664"))
324
325 expectBingoListRows(t, bingoExpectedCompatibilityRows, g.ExecOutput(t, p.root, bingoPath, "list"))
326 testutil.Equals(t, bingoExpectedCompatibilityBinaries, g.existingBinaries(t))
327
328 // Generate current version test case for further tests. This should be committed as well if changed.
329 testutil.Ok(t, os.RemoveAll(currTestCaseDir))
330 testutil.Ok(t, os.MkdirAll(filepath.Join(currTestCaseDir, ".bingo"), os.ModePerm))
331 _, err := execCmd("", nil, "cp", "-r", filepath.Join(p.root, ".bingo"), currTestCaseDir)
332 testutil.Ok(t, err)
333 }
334
335 var (
336 bingoExpectedCompatibilityRows = []row{
337 {name: "buildable", binName: "buildable-v0.0.0-20221007091146-39a7f0ae0b1e", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
338 {name: "buildable", binName: "buildable-v1.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
339 {name: "buildable", binName: "buildable-v1.1.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
340 {name: "buildable-v2", binName: "buildable-v2-v2.0.0", pkgVersion: "github.com/bwplotka/bingo-testmodule/v2/[email protected]"},
341 {name: "buildable-withReplace", binName: "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
342 {name: "buildable2", binName: "buildable2-v0.0.0-20221007091238-9d83f47b84c5", pkgVersion: "github.com/bwplotka/bingo-testmodule/[email protected]"},
343 }
344 bingoExpectedCompatibilityBinaries = []string{
345 "buildable-v0.0.0-20221007091146-39a7f0ae0b1e", "buildable-v1.0.0", "buildable-v1.1.0", "buildable-v2-v2.0.0", "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92", "buildable2-v0.0.0-20221007091238-9d83f47b84c5",
346 }
347 )
348
349 func TestCompatibility(t *testing.T) {
350 t.Parallel()
351
352 dirs, err := filepath.Glob("testdata/testproject*")
353 testutil.Ok(t, err)
354
355 g := newIsolatedGoEnv(t, defaultGoProxy)
356 defer g.Close(t)
357
358 var goVersion *semver.Version
359 {
360 r, err := runner.NewRunner(context.Background(), nil, false, "go")
361 testutil.Ok(t, err)
362 goVersion = r.GoVersion()
363 }
364
365 for _, dir := range dirs {
366 t.Run(dir, func(t *testing.T) {
367 for _, isGoProject := range []bool{false, true} {
368 t.Run(fmt.Sprintf("isGoProject=%v", isGoProject), func(t *testing.T) {
369 t.Run("Via bingo get all", func(t *testing.T) {
370 g.Clear(t)
371
372 // Copy testproject at the beginning to temp dir.
373 p := newTestProject(t, dir, filepath.Join(g.tmpDir, "testproject1"), isGoProject)
374 p.assertNotChanged(t, defaultModDir)
375
376 // We manually build bingo binary to make sure GOCACHE will not hit us.
377 bingoPath := filepath.Join(g.tmpDir, bingoBin)
378 buildInitialGobin(t, bingoPath)
379
380 expectBingoListRows(t, bingoExpectedCompatibilityRows, g.ExecOutput(t, p.root, bingoPath, "list"))
381 testutil.Equals(t, []string{}, g.existingBinaries(t))
382
383 defer p.assertNotChanged(t, defaultModDir)
384
385 // Get all binaries by doing 'bingo get'.
386 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get"))
387 expectBingoListRows(t, bingoExpectedCompatibilityRows, g.ExecOutput(t, p.root, bingoPath, "list"))
388 testutil.Equals(t, bingoExpectedCompatibilityBinaries, g.existingBinaries(t))
389
390 // Expect binaries works:
391 // TODO(bwplotka) Check all.
392 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-v1.0.0")))
393 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92")))
394 })
395 t.Run("Via go", func(t *testing.T) {
396 g.Clear(t)
397
398 // Copy testproject at the beginning to temp dir.
399 // NOTE: No bingo binary is required here.
400 p := newTestProject(t, dir, filepath.Join(g.tmpDir, "testproject2"), isGoProject)
401 p.assertNotChanged(t, defaultModDir)
402
403 // We manually build bingo binary to make sure GOCACHE will not hit us.
404 bingoPath := filepath.Join(g.tmpDir, bingoBin)
405 buildInitialGobin(t, bingoPath)
406
407 expectBingoListRows(t, bingoExpectedCompatibilityRows, g.ExecOutput(t, p.root, bingoPath, "list"))
408 testutil.Equals(t, []string{}, g.existingBinaries(t))
409
410 defer p.assertNotChanged(t, defaultModDir)
411
412 if isGoProject {
413 // This should work without cd even.
414 _, err := execCmd(p.root, nil, "go", "build", "-mod=mod", "-modfile="+filepath.Join(defaultModDir, "buildable.1.mod"),
415 "-o="+filepath.Join(g.gobin, "buildable-v1.0.0"), "github.com/bwplotka/bingo-testmodule/buildable")
416 testutil.Ok(t, err)
417 _, err = execCmd(p.root, nil, "go", "build", "-mod=mod", "-modfile="+filepath.Join(defaultModDir, "buildable-withReplace.mod"),
418 "-o="+filepath.Join(g.gobin, "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92"), "github.com/bwplotka/bingo-testmodule/buildable")
419 testutil.Ok(t, err)
420 } else {
421 // For no go projects we have this "bug" that requires go.mod to be present.
422 _, err := execCmd(filepath.Join(p.root, defaultModDir), nil, "go", "build", "-mod=mod", "-modfile=buildable.1.mod",
423 "-o="+filepath.Join(g.gobin, "buildable-v1.0.0"), "github.com/bwplotka/bingo-testmodule/buildable")
424 testutil.Ok(t, err)
425 _, err = execCmd(filepath.Join(p.root, defaultModDir), nil, "go", "build", "-mod=mod", "-modfile=buildable-withReplace.mod",
426 "-o="+filepath.Join(g.gobin, "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92"), "github.com/bwplotka/bingo-testmodule/buildable")
427 testutil.Ok(t, err)
428
429 }
430 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-v1.0.0")))
431 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92")))
432 testutil.Equals(t, []string{"buildable-v1.0.0", "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92"}, g.existingBinaries(t))
433 })
434 // TODO(bwplotka): Test variables.env as well.
435 t.Run("Makefile", func(t *testing.T) {
436 if !goVersion.LessThan(version.Go116) {
437 // These projects are configured with modules but the generated Makefiles do not contain the
438 // `-mod=mod` argument, and that makes those Makefiles incompatible with Go modules in 1.16.
439 // Let's run bingo get to simulate
440 for _, v := range []string{"v0_1_1", "v0_2_0", "v0_2_1", "v0_2_2"} {
441 if strings.HasSuffix(dir, v) {
442 t.Skipf("skipping %q in Go >= 1.16 because the generated Makefile is missing the '-mod-mod' flag and it is needed in Go >= 1.16", dir)
443 }
444 }
445 }
446
447 // Make is one of test requirement.
448 makePath := makePath(t)
449
450 g.Clear(t)
451
452 // Copy testproject at the beginning to temp dir.
453 prjRoot := filepath.Join(g.tmpDir, "testproject")
454 p := newTestProject(t, dir, prjRoot, isGoProject)
455 p.assertNotChanged(t, defaultModDir)
456
457 // We manually build bingo binary to make sure GOCACHE will not hit us.
458 bingoPath := filepath.Join(g.tmpDir, bingoBin)
459 buildInitialGobin(t, bingoPath)
460
461 expectBingoListRows(t, bingoExpectedCompatibilityRows, g.ExecOutput(t, p.root, bingoPath, "list"))
462 testutil.Equals(t, []string{}, g.existingBinaries(t))
463
464 g.ExecOutput(t, p.root, makePath, "buildable-v2-exists")
465 testutil.Equals(t, []string{"buildable-v2-v2.0.0"}, g.existingBinaries(t))
466 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-v2-v2.0.0")))
467 testutil.NotOk(t, g.ExpectErr(p.root, filepath.Join(g.gobin, "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92")))
468
469 testutil.Equals(t, "(re)installing "+g.gobin+"/buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92\nchecking buildable-with-replace\n", g.ExecOutput(t, p.root, makePath, "buildable-withreplace-exists"))
470 testutil.Equals(t, "checking buildable-v2\n", g.ExecOutput(t, p.root, makePath, "buildable-v2-exists"))
471 testutil.Equals(t, []string{"buildable-v2-v2.0.0", "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92"}, g.existingBinaries(t))
472 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-v2-v2.0.0")))
473 testutil.Equals(t, "buildable\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92")))
474
475 t.Run("Delete binary file, expect reinstall", func(t *testing.T) {
476 _, err := execCmd(g.gobin, nil, "rm", "buildable-v2-v2.0.0")
477 testutil.Ok(t, err)
478 testutil.Equals(t, []string{"buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92"}, g.existingBinaries(t))
479
480 testutil.Equals(t, "(re)installing "+g.gobin+"/buildable-v2-v2.0.0\nchecking buildable-v2\n", g.ExecOutput(t, p.root, makePath, "buildable-v2-exists"))
481 testutil.Equals(t, "checking buildable-with-replace\n", g.ExecOutput(t, p.root, makePath, "buildable-withreplace-exists"))
482 testutil.Equals(t, []string{"buildable-v2-v2.0.0", "buildable-withReplace-v0.0.0-20221007091003-fe4d42a37d92"}, g.existingBinaries(t))
483 })
484 })
485 })
486 }
487 })
488 }
489 }
490
491 func TestGet_ModuleCases(t *testing.T) {
492 g := newIsolatedGoEnv(t, defaultGoProxy)
493 defer g.Close(t)
494
495 var goVersion *semver.Version
496 {
497 r, err := runner.NewRunner(context.Background(), nil, false, "go")
498 testutil.Ok(t, err)
499 goVersion = r.GoVersion()
500 }
501
502 t.Run("benchstat: latest in case where no major version is found", func(t *testing.T) {
503 g.Clear(t)
504
505 testutil.Ok(t, os.MkdirAll(filepath.Join(g.tmpDir, "newproject"), os.ModePerm))
506 p := newTestProject(t, filepath.Join(g.tmpDir, "newproject"), filepath.Join(g.tmpDir, "testproject"), false)
507 p.assertNotChanged(t)
508
509 // We manually build bingo binary to make sure GOCACHE will not hit us.
510 bingoPath := filepath.Join(g.tmpDir, bingoBin)
511 buildInitialGobin(t, bingoPath)
512
513 expectBingoListRows(t, []row(nil), g.ExecOutput(t, p.root, bingoPath, "list"))
514 testutil.Equals(t, []string{}, g.existingBinaries(t))
515
516 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "golang.org/x/perf/cmd/benchstat@latest"))
517 })
518
519 t.Run("module with generics", func(t *testing.T) {
520 g.Clear(t)
521
522 testutil.Ok(t, os.MkdirAll(filepath.Join(g.tmpDir, "newproject"), os.ModePerm))
523 p := newTestProject(t, filepath.Join(g.tmpDir, "newproject"), filepath.Join(g.tmpDir, "testproject"), false)
524 p.assertNotChanged(t)
525
526 // We manually build bingo binary to make sure GOCACHE will not hit us.
527 bingoPath := filepath.Join(g.tmpDir, bingoBin)
528 buildInitialGobin(t, bingoPath)
529
530 expectBingoListRows(t, []row(nil), g.ExecOutput(t, p.root, bingoPath, "list"))
531 testutil.Equals(t, []string{}, g.existingBinaries(t))
532
533 if goVersion.LessThan(semver.MustParse("v1.18")) {
534 err := g.ExpectErr(p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable2@d48721795572f7b824f60a5b0623e524b263ed0c")
535 testutil.NotOk(t, err)
536 } else {
537 fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/bwplotka/bingo-testmodule/buildable2@d48721795572f7b824f60a5b0623e524b263ed0c"))
538 }
539 })
540
541 // Tricky cases TODO.
542
543 // Regression against https://github.com/bwplotka/bingo/issues/125
544 //t.Run("golangcilint", func(t *testing.T) {
545 // g.Clear(t)
546 //
547 // testutil.Ok(t, os.MkdirAll(filepath.Join(g.tmpDir, "newproject"), os.ModePerm))
548 // p := newTestProject(t, filepath.Join(g.tmpDir, "newproject"), filepath.Join(g.tmpDir, "testproject"), true)
549 // p.assertNotChanged(t)
550 //
551 // // We manually build bingo binary to make sure GOCACHE will not hit us.
552 // bingoPath := filepath.Join(g.tmpDir, bingoBin)
553 // buildInitialGobin(t, bingoPath)
554 //
555 // expectBingoListRows(t, []row(nil), g.ExecOutput(t, p.root, bingoPath, "list"))
556 // testutil.Equals(t, []string{}, g.existingBinaries(t))
557 //
558 // fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/golangci/golangci-lint/cmd/[email protected]"))
559 //})
560
561 // // Regression test against https://github.com/bwplotka/bingo/issues/65.
562 // name: "get tool with capital letters in name",
563 // do: func(t *testing.T, g *goEnv, p *testProject) {
564 // Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/githubnemo/CompileDaemon@87e39427f4ba26da4400abf3b26b2e58bfc9ebe6"))
565
566 // name: "Get tricky case with replace (thanos)",
567 // do: func(t *testing.T, g *goEnv, p *testProject) {
568 // // For Thanos/Prom/k8s etc without replace even go-get or list fails. This should be handled well.
569
570 // Exclude fields.
571
572 // {
573 // name: "Get tricky case with retract (ginkgo)",
574 // do: func(t *testing.T, g *goEnv, p *testProject) {
575 // if goVersion.LessThan(version.Go116) {
576 // t.Skip("Go version below 1.16 are not understanding go modules with retract directive; skip it.")
577 // fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/onsi/ginkgo/[email protected]"))
578 // }
579
580 // // TODO(bwplotka): Uncomment when https://github.com/githubnemo/CompileDaemon/pull/76 is merged or
581 // // https://github.com/bwplotka/bingo/issues/31
582 // //{
583 // // // Regression test against https://github.com/bwplotka/bingo/issues/65.
584 // // name: "get tool with capital letters in name (pre modules)",
585 // fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "github.com/githubnemo/[email protected]"))
586
587 // name: "get istio.io/tools/cmd/cue-gen@355a0b7a6ba743d14e3a43a3069287086207f35c (short module base path)",
588 // do: func(t *testing.T) {
589 // fmt.Println(g.ExecOutput(t, p.root, bingoPath, "get", "istio.io/tools/cmd/cue-gen@355a0b7a6ba743d14e3a43a3069287086207f35c"))
590 // },
591 }
592
593 type row struct {
594 name, binName, pkgVersion, buildEnvVars, buildFlags string
595 }
596
597 func removeTabDups(s string) string {
598 var b strings.Builder
599 b.Grow(len(s))
600 var last rune
601 for i, r := range s {
602 if r != last || r != '\t' || i == 0 {
603 b.WriteRune(r)
604 last = r
605 }
606 }
607 return b.String()
608 }
609
610 func expectBingoListRows(t testing.TB, expect []row, output string) {
611 t.Helper()
612
613 var (
614 trimmed = strings.TrimPrefix(removeTabDups(output), bingo.PackageRenderablesPrintHeader)
615 got []row
616 )
617 for _, line := range strings.Split(trimmed, "\n") {
618 s := strings.Fields(line)
619 if len(s) == 0 {
620 break
621 }
622 r := row{name: s[0]}
623 if len(s) > 1 {
624 r.binName = s[1]
625 }
626 if len(s) > 2 {
627 r.pkgVersion = s[2]
628 }
629 if len(s) > 3 {
630 r.buildEnvVars = s[3]
631 }
632 if len(s) > 4 {
633 r.buildFlags = s[4]
634 }
635 got = append(got, r)
636 }
637 testutil.Equals(t, expect, got)
638 }
639
640 func TestExpectBingoListRows(t *testing.T) {
641 expectBingoListRows(t, []row{
642 {name: "copyright", binName: "copyright-v0.0.0-20210112004814-138d5e5695fe", pkgVersion: "github.com/efficientgo/tools/[email protected]"},
643 {name: "embedmd", binName: "embedmd-v1.0.0", pkgVersion: "github.com/campoy/[email protected]", buildEnvVars: "CGO_ENABLED=1", buildFlags: "-tags=lol"},
644 {name: "faillint", binName: "faillint-v1.5.0", pkgVersion: "github.com/fatih/[email protected]"},
645 {name: "goimports", binName: "goimports-v0.0.0-20210112230658-8b4aab62c064", pkgVersion: "golang.org/x/tools/cmd/[email protected]"},
646 {name: "golangci-lint", binName: "golangci-lint-v1.26.0", pkgVersion: "github.com/golangci/golangci-lint/cmd/[email protected]"},
647 {name: "mdox", binName: "mdox-v0.2.1", pkgVersion: "github.com/bwplotka/[email protected]"},
648 {name: "misspell", binName: "misspell-v0.3.4", pkgVersion: "github.com/client9/misspell/cmd/[email protected]"},
649 {name: "proxy", binName: "proxy-v0.10.0", pkgVersion: "github.com/gomods/athens/cmd/[email protected]"},
650 }, `Name Binary Name Package @ Version Build EnvVars Build Flags
651 ---- ----------- ----------------- ------------- -----------
652 copyright copyright-v0.0.0-20210112004814-138d5e5695fe github.com/efficientgo/tools/[email protected]
653 embedmd embedmd-v1.0.0 github.com/campoy/[email protected] CGO_ENABLED=1 -tags=lol
654 faillint faillint-v1.5.0 github.com/fatih/[email protected]
655 goimports goimports-v0.0.0-20210112230658-8b4aab62c064 golang.org/x/tools/cmd/[email protected]
656 golangci-lint golangci-lint-v1.26.0 github.com/golangci/golangci-lint/cmd/[email protected]
657 mdox mdox-v0.2.1 github.com/bwplotka/[email protected]
658 misspell misspell-v0.3.4 github.com/client9/misspell/cmd/[email protected]
659 proxy proxy-v0.10.0 github.com/gomods/athens/cmd/[email protected]
660 `)
661 }