forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove_test.go
158 lines (135 loc) · 3.96 KB
/
remove_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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "testing"
func TestRemove(t *testing.T) {
needsExternalNetwork(t)
needsGit(t)
tg := testgo(t)
defer tg.cleanup()
tg.tempDir("src")
tg.setenv("GOPATH", tg.path("."))
importPaths := map[string]string{
"github.com/pkg/errors": "v0.8.0", // semver
"github.com/Sirupsen/logrus": "42b84f9ec624953ecbf81a94feccb3f5935c5edf", // random sha
}
// checkout the specified revisions
for ip, rev := range importPaths {
tg.runGo("get", ip)
repoDir := tg.path("src/" + ip)
tg.runGit(repoDir, "checkout", rev)
}
// Build a fake consumer of these packages.
const root = "github.com/golang/notexist"
m := `package main
import (
"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
)
func main() {
err := nil
if err != nil {
errors.Wrap(err, "thing")
}
logrus.Info("whatev")
}`
tg.tempFile("src/"+root+"/thing.go", m)
origm := `{
"dependencies": {
"github.com/not/used": {
"version": "2.0.0"
},
"github.com/Sirupsen/logrus": {
"revision": "42b84f9ec624953ecbf81a94feccb3f5935c5edf"
},
"github.com/pkg/errors": {
"version": ">=0.8.0, <1.0.0"
}
}
}
`
expectedManifest := `{
"dependencies": {
"github.com/Sirupsen/logrus": {
"revision": "42b84f9ec624953ecbf81a94feccb3f5935c5edf"
},
"github.com/pkg/errors": {
"version": ">=0.8.0, <1.0.0"
}
}
}
`
tg.tempFile("src/"+root+"/manifest.json", origm)
tg.cd(tg.path("src/" + root))
tg.run("remove", "-unused")
manifest := tg.readManifest()
if manifest != expectedManifest {
t.Fatalf("expected %s, got %s", expectedManifest, manifest)
}
tg.tempFile("src/"+root+"/manifest.json", origm)
tg.run("remove", "github.com/not/used")
manifest = tg.readManifest()
if manifest != expectedManifest {
t.Fatalf("expected %s, got %s", expectedManifest, manifest)
}
if err := tg.doRun([]string{"remove", "-unused", "github.com/not/used"}); err == nil {
t.Fatal("rm with both -unused and arg should have failed")
}
if err := tg.doRun([]string{"remove", "github.com/not/present"}); err == nil {
t.Fatal("rm with arg not in manifest should have failed")
}
if err := tg.doRun([]string{"remove", "github.com/not/used", "github.com/not/present"}); err == nil {
t.Fatal("rm with one arg not in manifest should have failed")
}
if err := tg.doRun([]string{"remove", "github.com/pkg/errors"}); err == nil {
t.Fatal("rm of arg in manifest and imports should have failed without -force")
}
tg.tempFile("src/"+root+"/manifest.json", origm)
tg.run("remove", "-force", "github.com/pkg/errors", "github.com/not/used")
manifest = tg.readManifest()
if manifest != `{
"dependencies": {
"github.com/Sirupsen/logrus": {
"revision": "42b84f9ec624953ecbf81a94feccb3f5935c5edf"
}
}
}
` {
t.Fatalf("expected %s, got %s", expectedManifest, manifest)
}
sysCommit := tg.getCommit("go.googlesource.com/sys")
expectedLock := `{
"memo": "c2d1935d281595034e4afeb49d13981f4210badea72ac78c3eb2ba01f36c3ceb",
"projects": [
{
"name": "github.com/Sirupsen/logrus",
"revision": "42b84f9ec624953ecbf81a94feccb3f5935c5edf",
"packages": [
"."
]
},
{
"name": "github.com/pkg/errors",
"version": "v0.8.0",
"revision": "645ef00459ed84a119197bfb8d8205042c6df63d",
"packages": [
"."
]
},
{
"name": "golang.org/x/sys",
"branch": "master",
"revision": "` + sysCommit + `",
"packages": [
"unix"
]
}
]
}
`
lock := tg.readLock()
if lock != expectedLock {
t.Fatalf("expected %s, got %s", expectedLock, lock)
}
}