Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 97b2629

Browse files
tro3sdboyer
tro3
authored andcommitted
Moving remove tests to subtest pattern
1 parent f840590 commit 97b2629

15 files changed

+250
-136
lines changed

cmd/dep/remove_test.go

Lines changed: 123 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,148 @@
55
package main
66

77
import (
8-
"strings"
8+
"path/filepath"
99
"testing"
1010

1111
"github.com/golang/dep/test"
1212
)
1313

14+
type removeTestCase struct {
15+
dataRoot string
16+
command []string
17+
importPaths map[string]string
18+
sourceFiles map[string]string
19+
goldenManifest string
20+
goldenLock string
21+
vendorPaths []string
22+
}
23+
1424
func TestRemove(t *testing.T) {
25+
tests := []removeTestCase{
26+
{
27+
dataRoot: "remove/case0",
28+
command: []string{"remove", "-unused"},
29+
importPaths: map[string]string{
30+
"github.com/sdboyer/deptest": "v0.8.0", // semver
31+
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea", // random sha
32+
},
33+
sourceFiles: map[string]string{
34+
"main.go": "main.go",
35+
"manifest.input.json": "manifest.json",
36+
},
37+
goldenManifest: "manifest.golden.json",
38+
goldenLock: "",
39+
},
40+
{
41+
dataRoot: "remove/case1",
42+
command: []string{"remove", "github.com/not/used"},
43+
importPaths: map[string]string{
44+
"github.com/sdboyer/deptest": "v0.8.0", // semver
45+
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea", // random sha
46+
},
47+
sourceFiles: map[string]string{
48+
"main.go": "main.go",
49+
"manifest.input.json": "manifest.json",
50+
},
51+
goldenManifest: "manifest.golden.json",
52+
goldenLock: "",
53+
},
54+
{
55+
dataRoot: "remove/case2",
56+
command: []string{"remove", "-force", "github.com/sdboyer/deptestdos", "github.com/not/used"},
57+
importPaths: map[string]string{
58+
"github.com/sdboyer/deptest": "v0.8.0", // semver
59+
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea", // random sha
60+
},
61+
sourceFiles: map[string]string{
62+
"main.go": "main.go",
63+
"manifest.input.json": "manifest.json",
64+
},
65+
goldenManifest: "manifest.golden.json",
66+
goldenLock: "lock.golden.json",
67+
},
68+
}
69+
1570
test.NeedsExternalNetwork(t)
1671
test.NeedsGit(t)
1772

18-
h := test.NewHelper(t)
19-
defer h.Cleanup()
73+
for _, testCase := range tests {
74+
t.Run(testCase.dataRoot, func(t *testing.T) {
75+
h := test.NewHelper(t)
76+
defer h.Cleanup()
2077

21-
h.TempDir("src")
22-
h.Setenv("GOPATH", h.Path("."))
78+
h.TempDir("src")
79+
h.Setenv("GOPATH", h.Path("."))
2380

24-
importPaths := map[string]string{
25-
"github.com/pkg/errors": "v0.8.0", // semver
26-
"github.com/Sirupsen/logrus": "42b84f9ec624953ecbf81a94feccb3f5935c5edf", // random sha
27-
}
81+
// checkout the specified revisions
82+
for ip, rev := range testCase.importPaths {
83+
h.RunGo("get", ip)
84+
repoDir := h.Path(filepath.Join("src", ip))
85+
h.RunGit(repoDir, "checkout", rev)
86+
}
2887

29-
// checkout the specified revisions
30-
for ip, rev := range importPaths {
31-
h.RunGo("get", ip)
32-
repoDir := h.Path("src/" + ip)
33-
h.RunGit(repoDir, "checkout", rev)
34-
}
88+
// Build a fake consumer of these packages.
89+
root := "src/github.com/golang/notexist"
90+
for src, dest := range testCase.sourceFiles {
91+
h.TempCopy(filepath.Join(root, dest), filepath.Join(testCase.dataRoot, src))
92+
}
3593

36-
// Build a fake consumer of these packages.
37-
const root = "src/github.com/golang/notexist"
38-
h.TempCopy(root+"/thing.go", "remove/main.input.go")
39-
h.TempCopy(root+"/manifest.json", "remove/manifest.input.json")
94+
h.Cd(h.Path(root))
95+
h.Run(testCase.command...)
96+
97+
wantPath := filepath.Join(testCase.dataRoot, testCase.goldenManifest)
98+
wantManifest := h.GetTestFileString(wantPath)
99+
gotManifest := h.ReadManifest()
100+
if wantManifest != gotManifest {
101+
if *test.UpdateGolden {
102+
if err := h.WriteTestFile(wantPath, gotManifest); err != nil {
103+
t.Fatal(err)
104+
}
105+
} else {
106+
t.Errorf("expected %s, got %s", wantManifest, gotManifest)
107+
}
108+
}
40109

41-
h.Cd(h.Path(root))
42-
h.Run("remove", "-unused")
43-
44-
goldenManifest := "remove/manifest0.golden.json"
45-
wantManifest := h.GetTestFileString(goldenManifest)
46-
gotManifest := h.ReadManifest()
47-
if wantManifest != gotManifest {
48-
if *test.UpdateGolden {
49-
if err := h.WriteTestFile(goldenManifest, gotManifest); err != nil {
50-
t.Fatal(err)
110+
if testCase.goldenLock != "" {
111+
wantPath = filepath.Join(testCase.dataRoot, testCase.goldenLock)
112+
wantLock := h.GetTestFileString(wantPath)
113+
gotLock := h.ReadLock()
114+
if wantLock != gotLock {
115+
if *test.UpdateGolden {
116+
if err := h.WriteTestFile(wantPath, gotLock); err != nil {
117+
t.Fatal(err)
118+
}
119+
} else {
120+
t.Errorf("expected %s, got %s", wantLock, gotLock)
121+
}
122+
}
51123
}
52-
} else {
53-
t.Errorf("expected %s, got %s", wantManifest, gotManifest)
54-
}
124+
})
55125
}
126+
}
127+
128+
func TestRemoveErrors(t *testing.T) {
129+
test.NeedsExternalNetwork(t)
130+
test.NeedsGit(t)
56131

57-
h.TempCopy(root+"/manifest.json", "remove/manifest.input.json")
58-
h.Run("remove", "github.com/not/used")
132+
h := test.NewHelper(t)
133+
defer h.Cleanup()
59134

60-
gotManifest = h.ReadManifest()
61-
if wantManifest != gotManifest {
62-
if *test.UpdateGolden {
63-
if err := h.WriteTestFile(goldenManifest, gotManifest); err != nil {
64-
t.Fatal(err)
65-
}
66-
} else {
67-
t.Errorf("expected %s, got %s", wantManifest, gotManifest)
68-
}
135+
h.TempDir("src")
136+
h.Setenv("GOPATH", h.Path("."))
137+
138+
// Build a fake consumer of these packages.
139+
sourceFiles := map[string]string{
140+
"main.go": "main.go",
141+
"manifest.input.json": "manifest.json",
142+
}
143+
root := "src/github.com/golang/notexist"
144+
for src, dest := range sourceFiles {
145+
h.TempCopy(filepath.Join(root, dest), filepath.Join("remove/case0", src))
69146
}
70147

148+
h.Cd(h.Path(root))
149+
71150
if err := h.DoRun([]string{"remove", "-unused", "github.com/not/used"}); err == nil {
72151
t.Fatal("rm with both -unused and arg should have failed")
73152
}
@@ -80,37 +159,7 @@ func TestRemove(t *testing.T) {
80159
t.Fatal("rm with one arg not in manifest should have failed")
81160
}
82161

83-
if err := h.DoRun([]string{"remove", "github.com/pkg/errors"}); err == nil {
162+
if err := h.DoRun([]string{"remove", "github.com/sdboyer/deptest"}); err == nil {
84163
t.Fatal("rm of arg in manifest and imports should have failed without -force")
85164
}
86-
87-
h.TempCopy(root+"/manifest.json", "remove/manifest.input.json")
88-
h.Run("remove", "-force", "github.com/pkg/errors", "github.com/not/used")
89-
90-
goldenManifest = "remove/manifest1.golden.json"
91-
wantManifest = h.GetTestFileString(goldenManifest)
92-
gotManifest = h.ReadManifest()
93-
if wantManifest != gotManifest {
94-
if *test.UpdateGolden {
95-
if err := h.WriteTestFile(goldenManifest, gotManifest); err != nil {
96-
t.Fatal(err)
97-
}
98-
} else {
99-
t.Errorf("expected %s, got %s", wantManifest, gotManifest)
100-
}
101-
}
102-
103-
sysCommit := h.GetCommit("go.googlesource.com/sys")
104-
goldenLock := "remove/lock1.golden.json"
105-
wantLock := strings.Replace(h.GetTestFileString(goldenLock), "{{sysCommit}}", sysCommit, 1)
106-
gotLock := h.ReadLock()
107-
if wantLock != gotLock {
108-
if *test.UpdateGolden {
109-
if err := h.WriteTestFile(goldenLock, strings.Replace(gotLock, sysCommit, "{{sysCommit}}", 1)); err != nil {
110-
t.Fatal(err)
111-
}
112-
} else {
113-
t.Errorf("expected %s, got %s", wantLock, gotLock)
114-
}
115-
}
116165
}

cmd/dep/testdata/remove/main.input.go renamed to cmd/dep/testdata/remove/case0/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
package main
66

77
import (
8-
"github.com/Sirupsen/logrus"
9-
"github.com/pkg/errors"
8+
"github.com/sdboyer/deptest"
9+
"github.com/sdboyer/deptestdos"
1010
)
1111

1212
func main() {
1313
err := nil
1414
if err != nil {
15-
errors.Wrap(err, "thing")
15+
deptest.Map["yo yo!"]
1616
}
17-
logrus.Info("whatev")
17+
deptestdos.diMeLo("whatev")
1818
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"github.com/sdboyer/deptest": {
4+
"version": ">=0.8.0, <1.0.0"
5+
},
6+
"github.com/sdboyer/deptestdos": {
7+
"revision": "a0196baa11ea047dd65037287451d36b861b00ea"
8+
}
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"dependencies": {
3+
"github.com/not/used": {
4+
"version": "2.0.0"
5+
},
6+
"github.com/sdboyer/deptest": {
7+
"version": ">=0.8.0, <1.0.0"
8+
},
9+
"github.com/sdboyer/deptestdos": {
10+
"revision": "a0196baa11ea047dd65037287451d36b861b00ea"
11+
}
12+
}
13+
}

cmd/dep/testdata/remove/case1/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"github.com/sdboyer/deptest"
9+
"github.com/sdboyer/deptestdos"
10+
)
11+
12+
func main() {
13+
err := nil
14+
if err != nil {
15+
deptest.Map["yo yo!"]
16+
}
17+
deptestdos.diMeLo("whatev")
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"github.com/sdboyer/deptest": {
4+
"version": ">=0.8.0, <1.0.0"
5+
},
6+
"github.com/sdboyer/deptestdos": {
7+
"revision": "a0196baa11ea047dd65037287451d36b861b00ea"
8+
}
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"dependencies": {
3+
"github.com/not/used": {
4+
"version": "2.0.0"
5+
},
6+
"github.com/sdboyer/deptest": {
7+
"version": ">=0.8.0, <1.0.0"
8+
},
9+
"github.com/sdboyer/deptestdos": {
10+
"revision": "a0196baa11ea047dd65037287451d36b861b00ea"
11+
}
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"memo": "1792d407a795640a2b821b350f481bc48852535ed17c98cae2cbe2912a9c3e36",
3+
"projects": [
4+
{
5+
"name": "github.com/sdboyer/deptest",
6+
"version": "v1.0.0",
7+
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
8+
"packages": [
9+
"."
10+
]
11+
},
12+
{
13+
"name": "github.com/sdboyer/deptestdos",
14+
"version": "v2.0.0",
15+
"revision": "5c607206be5decd28e6263ffffdcee067266015e",
16+
"packages": [
17+
"."
18+
]
19+
}
20+
]
21+
}

cmd/dep/testdata/remove/case2/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"github.com/sdboyer/deptest"
9+
"github.com/sdboyer/deptestdos"
10+
)
11+
12+
func main() {
13+
err := nil
14+
if err != nil {
15+
deptest.Map["yo yo!"]
16+
}
17+
deptestdos.diMeLo("whatev")
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"github.com/sdboyer/deptest": {
4+
"version": ">=0.8.0, <1.0.0"
5+
}
6+
}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"dependencies": {
3+
"github.com/not/used": {
4+
"version": "2.0.0"
5+
},
6+
"github.com/sdboyer/deptest": {
7+
"version": ">=0.8.0, <1.0.0"
8+
},
9+
"github.com/sdboyer/deptestdos": {
10+
"revision": "a0196baa11ea047dd65037287451d36b861b00ea"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)