5
5
package main
6
6
7
7
import (
8
- "strings "
8
+ "path/filepath "
9
9
"testing"
10
10
11
11
"github.com/golang/dep/test"
12
12
)
13
13
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
+
14
24
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
+
15
70
test .NeedsExternalNetwork (t )
16
71
test .NeedsGit (t )
17
72
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 ()
20
77
21
- h .TempDir ("src" )
22
- h .Setenv ("GOPATH" , h .Path ("." ))
78
+ h .TempDir ("src" )
79
+ h .Setenv ("GOPATH" , h .Path ("." ))
23
80
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
+ }
28
87
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
+ }
35
93
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
+ }
40
109
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
+ }
51
123
}
52
- } else {
53
- t .Errorf ("expected %s, got %s" , wantManifest , gotManifest )
54
- }
124
+ })
55
125
}
126
+ }
127
+
128
+ func TestRemoveErrors (t * testing.T ) {
129
+ test .NeedsExternalNetwork (t )
130
+ test .NeedsGit (t )
56
131
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 ( )
59
134
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 ))
69
146
}
70
147
148
+ h .Cd (h .Path (root ))
149
+
71
150
if err := h .DoRun ([]string {"remove" , "-unused" , "github.com/not/used" }); err == nil {
72
151
t .Fatal ("rm with both -unused and arg should have failed" )
73
152
}
@@ -80,37 +159,7 @@ func TestRemove(t *testing.T) {
80
159
t .Fatal ("rm with one arg not in manifest should have failed" )
81
160
}
82
161
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 {
84
163
t .Fatal ("rm of arg in manifest and imports should have failed without -force" )
85
164
}
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
- }
116
165
}
0 commit comments