forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_test.go
389 lines (332 loc) · 8.77 KB
/
script_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/rogpeppe/go-internal/goproxytest"
"github.com/rogpeppe/go-internal/gotooltest"
"github.com/rogpeppe/go-internal/testscript"
)
var (
proxyURL string
)
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(gobinMain{m}, map[string]func() int{
"gopherjs": main1,
}))
}
type gobinMain struct {
m *testing.M
}
func (m gobinMain) Run() int {
// Start the Go proxy server running for all tests.
srv, err := goproxytest.NewServer("testdata/mod", "")
if err != nil {
fmt.Fprintf(os.Stderr, "cannot start proxy: %v", err)
return 1
}
proxyURL = srv.URL
return m.m.Run()
}
func TestScripts(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
p := testscript.Params{
Dir: "testdata",
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"modified": buildModified(),
"changed": buildChanged(),
"cpr": cmdCpr,
"highport": highport,
"httpget": httpget,
"sleep": sleep,
},
Setup: func(e *testscript.Env) error {
e.Vars = append(e.Vars,
"NODE_PATH="+os.Getenv("NODE_PATH"),
"GOPROXY="+proxyURL,
"SELF="+wd,
)
return nil
},
}
if err := gotooltest.Setup(&p); err != nil {
t.Fatal(err)
}
testscript.Run(t, p)
}
// sleep for the specified duration
func sleep(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("sleep does not understand negation")
}
if len(args) != 1 {
ts.Fatalf("Usage: sleep duration")
}
d, err := time.ParseDuration(args[0])
if err != nil {
ts.Fatalf("failed to parse duration %q: %v", args[0], err)
}
time.Sleep(d)
}
// Usage:
//
// httpget url outputFile
//
func httpget(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 2 {
ts.Fatalf("Usage: httpget url outputFile")
}
url := args[0]
ofPath := ts.MkAbs(args[1])
resp, err := http.Get(url)
if err != nil {
ts.Fatalf("httpget %v failed: %v", url, err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if !neg {
ts.Fatalf("httpget %v return status code %v", url, resp.StatusCode)
}
}
of, err := os.Create(ofPath)
if err != nil {
ts.Fatalf("failed to create output file %v: %v", ofPath, err)
}
if _, err := io.Copy(of, resp.Body); err != nil {
ts.Fatalf("failed to write response to output file %v: %v", ofPath, err)
}
if err := of.Close(); err != nil {
ts.Fatalf("failed to close output file %v: %v", ofPath, err)
}
}
// Sets the environment variable named by the single argument key to an
// available high port.
func highport(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("highport does not understand negation")
}
if len(args) != 1 {
ts.Fatalf("highport takes exactly one argument; the name of the environment variable key to set")
}
key := args[0]
l, err := net.Listen("tcp", ":0")
if err != nil {
ts.Fatalf("could not get a free high port: %v", err)
}
defer func() {
if err := l.Close(); err != nil {
ts.Fatalf("failed to free up high port: %v", err)
}
}()
_, port, err := net.SplitHostPort(l.Addr().String())
if err != nil {
ts.Fatalf("could not extract port from %q: %v", l.Addr().String(), err)
}
ts.Setenv(key, port)
}
// buildModified returns a new instance of a testscript command that determines
// whether the single file argument has been modified since the command was
// last called on that file. Strictly speaking it is only safe to test files
// beneath $WORK because this represents the truly isolated area of a
// testscript run. This is an inherently racey operation in the presence of
// background tasks, hence we don't worry about synchronising.
//
// The first time of calling modified for any given file path is defined to
// return 0, i.e. false
func buildModified() func(ts *testscript.TestScript, neg bool, args []string) {
var lock sync.Mutex
cache := make(map[string]os.FileInfo)
return func(ts *testscript.TestScript, neg bool, args []string) {
lock.Lock()
defer lock.Unlock()
var poorUsage bool
switch len(args) {
case 2:
if args[0] != "-clear" {
poorUsage = true
}
case 1:
if args[0] == "-clear" {
poorUsage = true
}
default:
poorUsage = true
}
if poorUsage {
ts.Fatalf("usage: modified [-clear] file")
}
if args[0] == "-clear" {
delete(cache, args[1])
return
}
fp := ts.MkAbs(args[0])
nfi, err := os.Stat(fp)
if err != nil {
ts.Fatalf("failed to stat %v: %v", fp, err)
}
if nfi.IsDir() {
ts.Fatalf("%v is a directory, not a file", fp)
}
fi, ok := cache[fp]
cache[fp] = nfi
if !ok {
if !neg {
ts.Fatalf("%v has not been modified; first time of checking", fp)
}
return
}
switch {
case nfi.ModTime().Before(fi.ModTime()):
ts.Fatalf("file %v now has an earlier modification time (%v -> %v)", fp, fi.ModTime(), nfi.ModTime())
case nfi.ModTime().Equal(fi.ModTime()):
if neg {
ts.Fatalf("%v has not been modified", fp)
}
return
default:
if neg {
ts.Fatalf("%v has been modified (%v -> %v)", fp, fi.ModTime(), nfi.ModTime())
}
cache[fp] = nfi
}
}
}
// buildChanged returns a new instance of a testscript command that determines
// whether the single file argument has been changed since the command was
// last called on that file. Strictly speaking it is only safe to test files
// beneath $WORK because this represents the truly isolated area of a
// testscript run. This is an inherently racey operation in the presence of
// background tasks, hence we don't worry about synchronising.
//
// The first time of calling changed for any given file path is defined to
// return 0, i.e. false
func buildChanged() func(ts *testscript.TestScript, neg bool, args []string) {
var lock sync.Mutex
cache := make(map[string][]byte)
return func(ts *testscript.TestScript, neg bool, args []string) {
lock.Lock()
defer lock.Unlock()
var poorUsage bool
switch len(args) {
case 2:
if args[0] != "-clear" {
poorUsage = true
}
case 1:
if args[0] == "-clear" {
poorUsage = true
}
default:
poorUsage = true
}
if poorUsage {
ts.Fatalf("usage: changed [-clear] file")
}
if args[0] == "-clear" {
delete(cache, args[1])
return
}
fp := ts.MkAbs(args[0])
f, err := os.Open(fp)
if err != nil {
ts.Fatalf("failed to open %v: %v", fp, err)
}
defer f.Close()
nhash := sha256.New()
if _, err := io.Copy(nhash, f); err != nil {
ts.Fatalf("failed to hash %v: %v", fp, err)
}
nsum := nhash.Sum(nil)
sum, ok := cache[fp]
cache[fp] = nsum
if !ok {
if !neg {
ts.Fatalf("%v has not been changed; first time of checking", fp)
}
return
}
eq := bytes.Equal(nsum, sum)
if eq && !neg {
ts.Fatalf("file %v not changed", fp)
}
if !eq && neg {
ts.Fatalf("file %v changed", fp)
}
}
}
// cmdCpr implements a recursive copy of go files. Takes two arguments: source
// directory and target directory. The source directory must exist. The target
// directory will be created if it does not exist.
func cmdCpr(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("cpr does not understand negation")
}
if len(args) != 2 {
ts.Fatalf("cpr takes two arguments: got %v", len(args))
}
src, dst := ts.MkAbs(args[0]), ts.MkAbs(args[1])
sfi, err := os.Stat(src)
if err != nil {
ts.Fatalf("source %v must exist: %v", src, err)
}
if !sfi.IsDir() {
ts.Fatalf("source %v must be a directory", src)
}
if err := os.MkdirAll(dst, 0755); err != nil {
ts.Fatalf("error trying to ensure target directory exists")
}
err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dstPath := path
dstPath = strings.TrimPrefix(dstPath, src)
dstPath = strings.TrimPrefix(dstPath, string(os.PathSeparator))
// root
if dstPath == "" {
return nil
}
dstPath = filepath.Join(dst, dstPath)
name := info.Name()
if info.IsDir() {
switch name[0] {
case '_', '.':
return filepath.SkipDir
}
if err := os.MkdirAll(dstPath, info.Mode()); err != nil {
return fmt.Errorf("failed to mkdir %v: %v", dstPath, err)
}
return nil
}
if !strings.HasSuffix(name, ".go") || name[0] == '_' || name[0] == '.' {
return nil
}
srcf, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open source file %v: %v", path, err)
}
dstf, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, info.Mode())
if err != nil {
return fmt.Errorf("failed to create target file %v: %v", dstPath, err)
}
if _, err := io.Copy(dstf, srcf); err != nil {
return fmt.Errorf("failed to copy from %v to %v: %v", path, dstPath, err)
}
return nil
})
if err != nil {
ts.Fatalf("failed to recursively copy: %v", err)
}
}