Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
// just a little pre-run script to set up the fixtures.
2
// zz-finish cleans it up
3
4
var mkdirp = require("mkdirp")
5
var path = require("path")
6
var i = 0
7
var tap = require("tap")
8
var fs = require("fs")
9
var rimraf = require("rimraf")
10
11
var files =
12
[ "a/.abcdef/x/y/z/a"
13
, "a/abcdef/g/h"
14
, "a/abcfed/g/h"
15
, "a/b/c/d"
16
, "a/bc/e/f"
17
, "a/c/d/c/b"
18
, "a/cb/e/f"
19
]
20
21
var symlinkTo = path.resolve(__dirname, "a/symlink/a/b/c")
22
var symlinkFrom = "../.."
23
24
files = files.map(function (f) {
25
return path.resolve(__dirname, f)
26
})
27
28
tap.test("remove fixtures", function (t) {
29
rimraf(path.resolve(__dirname, "a"), function (er) {
30
t.ifError(er, "remove fixtures")
31
t.end()
32
})
33
})
34
35
files.forEach(function (f) {
36
tap.test(f, function (t) {
37
var d = path.dirname(f)
38
mkdirp(d, 0755, function (er) {
39
if (er) {
40
t.fail(er)
41
return t.bailout()
42
}
43
fs.writeFile(f, "i like tests", function (er) {
44
t.ifError(er, "make file")
45
t.end()
46
})
47
})
48
})
49
})
50
51
if (process.platform !== "win32") {
52
tap.test("symlinky", function (t) {
53
var d = path.dirname(symlinkTo)
54
console.error("mkdirp", d)
55
mkdirp(d, 0755, function (er) {
56
t.ifError(er)
57
fs.symlink(symlinkFrom, symlinkTo, "dir", function (er) {
58
t.ifError(er, "make symlink")
59
t.end()
60
})
61
})
62
})
63
}
64
65
;["foo","bar","baz","asdf","quux","qwer","rewq"].forEach(function (w) {
66
w = "/tmp/glob-test/" + w
67
tap.test("create " + w, function (t) {
68
mkdirp(w, function (er) {
69
if (er)
70
throw er
71
t.pass(w)
72
t.end()
73
})
74
})
75
})
76
77
78
// generate the bash pattern test-fixtures if possible
79
if (process.platform === "win32" || !process.env.TEST_REGEN) {
80
console.error("Windows, or TEST_REGEN unset. Using cached fixtures.")
81
return
82
}
83
84
var spawn = require("child_process").spawn;
85
var globs =
86
// put more patterns here.
87
// anything that would be directly in / should be in /tmp/glob-test
88
["test/a/*/+(c|g)/./d"
89
,"test/a/**/[cg]/../[cg]"
90
,"test/a/{b,c,d,e,f}/**/g"
91
,"test/a/b/**"
92
,"test/**/g"
93
,"test/a/abc{fed,def}/g/h"
94
,"test/a/abc{fed/g,def}/**/"
95
,"test/a/abc{fed/g,def}/**///**/"
96
,"test/**/a/**/"
97
,"test/+(a|b|c)/a{/,bc*}/**"
98
,"test/*/*/*/f"
99
,"test/**/f"
100
,"test/a/symlink/a/b/c/a/b/c/a/b/c//a/b/c////a/b/c/**/b/c/**"
101
,"{./*/*,/tmp/glob-test/*}"
102
,"{/tmp/glob-test/*,*}" // evil owl face! how you taunt me!
103
,"test/a/!(symlink)/**"
104
]
105
var bashOutput = {}
106
var fs = require("fs")
107
108
globs.forEach(function (pattern) {
109
tap.test("generate fixture " + pattern, function (t) {
110
var cmd = "shopt -s globstar && " +
111
"shopt -s extglob && " +
112
"shopt -s nullglob && " +
113
// "shopt >&2; " +
114
"eval \'for i in " + pattern + "; do echo $i; done\'"
115
var cp = spawn("bash", ["-c", cmd], { cwd: path.dirname(__dirname) })
116
var out = []
117
cp.stdout.on("data", function (c) {
118
out.push(c)
119
})
120
cp.stderr.pipe(process.stderr)
121
cp.on("close", function (code) {
122
out = flatten(out)
123
if (!out)
124
out = []
125
else
126
out = cleanResults(out.split(/\r*\n/))
127
128
bashOutput[pattern] = out
129
t.notOk(code, "bash test should finish nicely")
130
t.end()
131
})
132
})
133
})
134
135
tap.test("save fixtures", function (t) {
136
var fname = path.resolve(__dirname, "bash-results.json")
137
var data = JSON.stringify(bashOutput, null, 2) + "\n"
138
fs.writeFile(fname, data, function (er) {
139
t.ifError(er)
140
t.end()
141
})
142
})
143
144
function cleanResults (m) {
145
// normalize discrepancies in ordering, duplication,
146
// and ending slashes.
147
return m.map(function (m) {
148
return m.replace(/\/+/g, "/").replace(/\/$/, "")
149
}).sort(alphasort).reduce(function (set, f) {
150
if (f !== set[set.length - 1]) set.push(f)
151
return set
152
}, []).sort(alphasort).map(function (f) {
153
// de-windows
154
return (process.platform !== 'win32') ? f
155
: f.replace(/^[a-zA-Z]:\\\\/, '/').replace(/\\/g, '/')
156
})
157
}
158
159
function flatten (chunks) {
160
var s = 0
161
chunks.forEach(function (c) { s += c.length })
162
var out = new Buffer(s)
163
s = 0
164
chunks.forEach(function (c) {
165
c.copy(out, s)
166
s += c.length
167
})
168
169
return out.toString().trim()
170
}
171
172
function alphasort (a, b) {
173
a = a.toLowerCase()
174
b = b.toLowerCase()
175
return a > b ? 1 : a < b ? -1 : 0
176
}
177
178