-
Notifications
You must be signed in to change notification settings - Fork 3
/
save_test.go
264 lines (247 loc) · 5.87 KB
/
save_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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"text/template"
)
// node represents a file tree or a VCS repo
type node struct {
path string // file name or commit type
body interface{} // file contents or commit tag
entries []*node // nil if the entry is a file
}
var (
pkgtpl = template.Must(template.New("package").Parse(`package {{.Name}}
import (
{{range .Imports}} {{printf "%q" .}}
{{end}})
`))
)
func pkg(name string, pkg ...string) string {
v := struct {
Name string
Imports []string
}{name, pkg}
var buf bytes.Buffer
err := pkgtpl.Execute(&buf, v)
if err != nil {
panic(err)
}
return buf.String()
}
func decl(name string) string {
return "var " + name + " int\n"
}
func deppy(importpath string, keyval ...string) *Deps {
g := &Deps{
ImportPath: importpath,
}
for i := 0; i < len(keyval); i += 2 {
g.Deps = append(g.Deps, Dependency{
ImportPath: keyval[i],
Comment: keyval[i+1],
})
}
return g
}
func TestSave(t *testing.T) {
var cases = []struct {
cwd string
args []string
flagR bool
start []*node
altstart []*node
want []*node
wdep Deps
werr bool
}{
{
// dependency on parent directory in same repo
// see bug https://github.com/tools/godep/issues/70
cwd: "P",
args: []string{"./..."},
start: []*node{
{
"P",
"",
[]*node{
{"main.go", pkg("P"), nil},
{"Q/main.go", pkg("Q", "P"), nil},
{"+git", "C1", nil},
},
},
},
want: []*node{
{"P/main.go", pkg("P"), nil},
{"P/Q/main.go", pkg("Q", "P"), nil},
},
wdep: Deps{
ImportPath: "P",
Deps: []Dependency{},
},
},
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
const scratch = "goderptest"
defer os.RemoveAll(scratch)
for _, test := range cases {
err = os.RemoveAll(scratch)
if err != nil {
t.Fatal(err)
}
altsrc := filepath.Join(scratch, "r2", "src")
if test.altstart != nil {
makeTree(t, &node{altsrc, "", test.altstart}, "")
}
src := filepath.Join(scratch, "r1", "src")
makeTree(t, &node{src, "", test.start}, altsrc)
dir := filepath.Join(wd, src, test.cwd)
err = os.Chdir(dir)
if err != nil {
panic(err)
}
root1 := filepath.Join(wd, scratch, "r1")
root2 := filepath.Join(wd, scratch, "r2")
err = os.Setenv("GOPATH", root1+string(os.PathListSeparator)+root2)
if err != nil {
panic(err)
}
err = save(test.args)
if g := err != nil; g != test.werr {
if err != nil {
t.Log(err)
}
t.Errorf("save err = %v want %v", g, test.werr)
}
err = os.Chdir(wd)
if err != nil {
panic(err)
}
checkTree(t, &node{src, "", test.want})
f, err := os.Open(filepath.Join(dir, "Deps"))
if err != nil {
t.Error(err)
}
g := new(Deps)
err = json.NewDecoder(f).Decode(g)
if err != nil {
t.Error(err)
}
f.Close()
if g.ImportPath != test.wdep.ImportPath {
t.Errorf("ImportPath = %s want %s", g.ImportPath, test.wdep.ImportPath)
}
for i := range g.Deps {
g.Deps[i].Rev = ""
}
if !reflect.DeepEqual(g.Deps, test.wdep.Deps) {
t.Errorf("Deps = %v want %v", g.Deps, test.wdep.Deps)
}
}
}
func makeTree(t *testing.T, tree *node, altpath string) (gopath string) {
walkTree(tree, tree.path, func(path string, n *node) {
g, isDeps := n.body.(*Deps)
body, _ := n.body.(string)
switch {
case isDeps:
for i, dep := range g.Deps {
rel := filepath.FromSlash(dep.ImportPath)
dir := filepath.Join(tree.path, rel)
if _, err := os.Stat(dir); os.IsNotExist(err) {
dir = filepath.Join(altpath, rel)
}
tag := dep.Comment
rev := strings.TrimSpace(run(t, dir, "git", "rev-parse", tag))
g.Deps[i].Rev = rev
}
os.MkdirAll(filepath.Dir(path), 0770)
f, err := os.Create(path)
if err != nil {
t.Errorf("makeTree: %v", err)
return
}
defer f.Close()
err = json.NewEncoder(f).Encode(g)
if err != nil {
t.Errorf("makeTree: %v", err)
}
case n.path == "+git":
dir := filepath.Dir(path)
run(t, dir, "git", "init") // repo might already exist, but ok
run(t, dir, "git", "add", ".")
run(t, dir, "git", "commit", "-m", "goderp")
if body != "" {
run(t, dir, "git", "tag", body)
}
case n.entries == nil && strings.HasPrefix(body, "symlink:"):
target := strings.TrimPrefix(body, "symlink:")
os.Symlink(target, path)
case n.entries == nil && body == "(absent)":
panic("is this gonna be forever")
case n.entries == nil:
os.MkdirAll(filepath.Dir(path), 0770)
err := ioutil.WriteFile(path, []byte(body), 0660)
if err != nil {
t.Errorf("makeTree: %v", err)
}
default:
os.MkdirAll(path, 0770)
}
})
return gopath
}
func checkTree(t *testing.T, want *node) {
walkTree(want, want.path, func(path string, n *node) {
body := n.body.(string)
switch {
case n.path == "+git":
panic("is this real life")
case n.entries == nil && strings.HasPrefix(body, "symlink:"):
panic("why is this happening to me")
case n.entries == nil && body == "(absent)":
body, err := ioutil.ReadFile(path)
if !os.IsNotExist(err) {
t.Errorf("checkTree: %s = %s want absent", path, string(body))
return
}
case n.entries == nil:
gbody, err := ioutil.ReadFile(path)
if err != nil {
t.Errorf("checkTree: %v", err)
return
}
if got := string(gbody); got != body {
t.Errorf("%s = %s want %s", path, got, body)
}
default:
os.MkdirAll(path, 0770)
}
})
}
func walkTree(n *node, path string, f func(path string, n *node)) {
f(path, n)
for _, e := range n.entries {
walkTree(e, filepath.Join(path, filepath.FromSlash(e.path)), f)
}
}
func run(t *testing.T, dir, name string, args ...string) string {
cmd := exec.Command(name, args...)
cmd.Dir = dir
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
panic(name + " " + strings.Join(args, " ") + ": " + err.Error())
}
return string(out)
}