This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestdata_test.go
118 lines (95 loc) · 2.69 KB
/
testdata_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
// Copyright 2017 Tom Thorogood. All rights reserved.
// Use of this source code is governed by a Modified
// BSD License that can be found in the LICENSE file.
package bindata
import (
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
"time"
chacha20rand "github.com/tmthrgd/go-rand"
"github.com/zach-klippenstein/goregen"
)
type testStat struct{ f *testFile }
func (s testStat) Name() string {
return path.Base(s.f.path)
}
func (s testStat) Size() int64 { return s.f.size }
func (s testStat) Mode() os.FileMode { return s.f.mode }
func (s testStat) ModTime() time.Time { return s.f.time }
func (s testStat) IsDir() bool { return false }
func (s testStat) Sys() interface{} { return nil }
type testFile struct {
path string
seed [chacha20rand.SeedSize]byte
size int64
time time.Time
mode os.FileMode
}
func (f *testFile) Name() string { return f.path }
func (f *testFile) Path() string { return f.path }
func (f *testFile) AbsolutePath() string {
return path.Join("/path/to/test/data", f.path)
}
func (f *testFile) Open() (io.ReadCloser, error) {
r, err := chacha20rand.New(f.seed[:])
if err != nil {
return nil, err
}
return ioutil.NopCloser(io.LimitReader(r, f.size)), nil
}
func (f *testFile) Stat() (os.FileInfo, error) {
return testStat{f}, nil
}
var (
numTestFiles = flag.Uint("testfiles", 25, "the number of random test files to add")
maxFileSize = flag.Uint("filesize", 512, "the maximum size of random test files")
)
var testFiles Files
func setupTestFiles() {
mrand := rand.New(rand.NewSource(int64(0)))
dg, err := regen.NewGenerator("[a-zA-Z0-9_. -][a-zA-Z0-9/_. -]{15,31}", ®en.GeneratorArgs{
RngSource: mrand,
})
if err != nil {
panic(err)
}
var dir string
testFiles = make(Files, 0, *numTestFiles+1)
for i := uint(0); i < *numTestFiles; i++ {
rand := rand.New(rand.NewSource(int64(i + 1)))
// regen seems biased towards short names when using
// [a-zA-Z0-9_. -]{1,72}
regex := fmt.Sprintf("[a-zA-Z0-9_. -]{%d}", 1+rand.Intn(72))
fg, err := regen.NewGenerator(regex, ®en.GeneratorArgs{
RngSource: rand,
})
if err != nil {
panic(err)
}
f := &testFile{
path: path.Join(dir, fg.Generate()),
size: int64(rand.Intn(int(*maxFileSize))),
time: time.Unix(rand.Int63(), rand.Int63()),
mode: os.FileMode(rand.Intn(int(os.ModePerm))),
}
rand.Read(f.seed[:])
testFiles = append(testFiles, f)
switch mrand.Intn(5) {
case 0:
dir = dg.Generate()
case 1:
dir = path.Join(dir, dg.Generate())
}
}
// This test file is designed to trigger the fix for
// https://github.com/tmthrgd/go-bindata/issues/8.
testFiles = append(testFiles, &testFile{
path: path.Join(dir, "issue-8.bin"),
size: 15 << 10,
})
}