forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxn_writer_test.go
209 lines (176 loc) · 4.7 KB
/
txn_writer_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
// Copyright 2017 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 (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
func TestTxnWriterBadInputs(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.tempDir("txnwriter")
td := tg.path(".")
sw := safeWriter{}
// no root
if err := sw.writeAllSafe(false); err == nil {
t.Errorf("should have errored without a root path, but did not")
}
sw.root = td
if err := sw.writeAllSafe(false); err != nil {
t.Errorf("write with only root should be fine, just a no-op, but got err %q", err)
}
if err := sw.writeAllSafe(true); err == nil {
t.Errorf("should fail because no source manager was provided for writing vendor")
}
if err := sw.writeAllSafe(true); err == nil {
t.Errorf("should fail because no lock was provided from which to write vendor")
}
// now check dir validation
sw.root = filepath.Join(td, "nonexistent")
if err := sw.writeAllSafe(false); err == nil {
t.Errorf("should have errored with nonexistent dir for root path, but did not")
}
sw.root = filepath.Join(td, "myfile")
srcf, err := os.Create(sw.root)
if err != nil {
t.Fatal(err)
}
srcf.Close()
if err := sw.writeAllSafe(false); err == nil {
t.Errorf("should have errored when root path is a file, but did not")
}
}
func TestTxnWriter(t *testing.T) {
needsExternalNetwork(t)
needsGit(t)
tg := testgo(t)
tg.tempDir("")
defer tg.cleanup()
var c *ctx
c = &ctx{
GOPATH: tg.path("."),
}
sm, err := c.sourceManager()
defer sm.Release()
tg.must(err)
var sw safeWriter
var mpath, lpath, vpath string
var count int
reset := func() {
pr := filepath.Join("src", "txnwriter"+strconv.Itoa(count))
tg.tempDir(pr)
sw = safeWriter{
root: tg.path(pr),
sm: sm,
}
tg.cd(sw.root)
mpath = filepath.Join(sw.root, manifestName)
lpath = filepath.Join(sw.root, lockName)
vpath = filepath.Join(sw.root, "vendor")
count++
}
reset()
// super basic manifest and lock
expectedManifest := `{
"dependencies": {
"github.com/sdboyer/dep-test": {
"version": "1.0.0"
}
}
}
`
expectedLock := `{
"memo": "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c",
"projects": [
{
"name": "github.com/sdboyer/dep-test",
"version": "1.0.0",
"revision": "2a3a211e171803acb82d1d5d42ceb53228f51751",
"packages": [
"."
]
}
]
}
`
m, err := readManifest(strings.NewReader(expectedManifest))
tg.must(err)
l, err := readLock(strings.NewReader(expectedLock))
tg.must(err)
// Just write manifest
sw.m = m
tg.must(sw.writeAllSafe(false))
tg.mustExist(mpath)
tg.mustNotExist(lpath)
tg.mustNotExist(vpath)
diskm := tg.readManifest()
if diskm != expectedManifest {
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
}
// Manifest and lock, but no vendor
sw.l = l
tg.must(sw.writeAllSafe(false))
tg.mustExist(mpath)
tg.mustExist(lpath)
tg.mustNotExist(vpath)
diskm = tg.readManifest()
if diskm != expectedManifest {
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
}
diskl := tg.readLock()
if diskl != expectedLock {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
}
tg.must(sw.writeAllSafe(true))
tg.mustExist(mpath)
tg.mustExist(lpath)
tg.mustExist(vpath)
tg.mustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
diskm = tg.readManifest()
if diskm != expectedManifest {
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
}
diskl = tg.readLock()
if diskl != expectedLock {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
}
// start fresh, ignoring the manifest now
reset()
sw.l = l
sw.nl = l
tg.must(sw.writeAllSafe(false))
// locks are equivalent, so nothing gets written
tg.mustNotExist(mpath)
tg.mustNotExist(lpath)
tg.mustNotExist(vpath)
l2 := &lock{}
*l2 = *l
// zero out the input hash to ensure non-equivalency
l2.Memo = []byte{}
sw.l = l2
tg.must(sw.writeAllSafe(true))
tg.mustNotExist(mpath)
tg.mustExist(lpath)
tg.mustExist(vpath)
tg.mustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
diskl = tg.readLock()
if diskl != expectedLock {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
}
// repeat op to ensure good behavior when vendor dir already exists
sw.l = nil
tg.must(sw.writeAllSafe(true))
tg.mustNotExist(mpath)
tg.mustExist(lpath)
tg.mustExist(vpath)
tg.mustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
diskl = tg.readLock()
if diskl != expectedLock {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
}
// TODO test txn rollback cases. maybe we can force errors with chmodding?
}