-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhooks.js
More file actions
163 lines (142 loc) · 3.92 KB
/
hooks.js
File metadata and controls
163 lines (142 loc) · 3.92 KB
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
// just a little pre-run script to set up the fixtures.
// zz-finish cleans it up
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
function promisify(func) {
return function(...args) {
return new Promise(function(resolve, reject) {
func(...args, function(...callbackArgs) {
if(callbackArgs[0]) {
reject(callbackArgs[0]);
} else {
resolve(...callbackArgs.slice(1));
}
});
});
};
}
const fsPromises = {
writeFile: promisify(fs.writeFile),
symlink: promisify(fs.symlink)
};
function cleanResults (m) {
// normalize discrepancies in ordering, duplication,
// and ending slashes.
return m.sort(alphasort);
}
function flatten (chunks) {
let s = 0;
chunks.forEach(function (c) { s += c.length });
const out = Buffer.alloc(s);
s = 0;
chunks.forEach(function (c) {
c.copy(out, s);
s += c.length;
});
return out.toString().trim();
}
function alphasort (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a > b ? 1 : a < b ? -1 : 0;
}
beforeAll(async () => {
const fixtureDir = path.resolve(__dirname, 'fixtures');
let files = [
'a/.abcdef/x/y/z/a',
'a/abcdef/g/h',
'a/abcfed/g/h',
'a/b/c/d',
'a/bc/e/f',
'a/c/d/c/b',
'a/cb/e/f',
'a/x/.y/b',
'a/z/.y/b'
];
const symlinkTo = path.resolve(fixtureDir, 'a/symlink/a/b/c');
const symlinkFrom = '../../../b';
files = files.map(f => path.resolve(fixtureDir, f));
await new Promise((resolve, reject) => rimraf(fixtureDir, resolve));
for(let f of files) {
f = path.resolve(fixtureDir, f);
const d = path.dirname(f);
await mkdirp(d, '0755');
await fsPromises.writeFile(f, 'i like tests');
}
if (process.platform !== 'win32') {
const d = path.dirname(symlinkTo);
await mkdirp(d, '0755');
await fsPromises.symlink(symlinkFrom, symlinkTo, 'dir');
}
// generate the bash pattern test-fixtures if possible
if (process.platform === 'win32' || !process.env.TEST_REGEN) {
//console.info('Windows, or TEST_REGEN unset. Using cached fixtures.');
return;
}
const spawn = require('child_process').spawn;
const globs = [
// put more patterns here.
// anything that would be directly in / should be in /tmp/glob-test
'a/{b,c,d,e,f}/**/g',
'a/b/**',
'**/g',
'a/abc{fed,def}/g/h',
'a/abc{fed/g,def}/**/',
'a/abc{fed/g,def}/**///**/',
'**/a/**/',
'+(a|b|c)/a{/,bc*}/**',
'*/*/*/f',
'**/f',
'a/!(symlink)/**',
'a/symlink/a/**/*'
];
const bashOutput = {};
try {
for(const pattern of globs) {
const opts = [
'-O', 'globstar',
'-O', 'extglob',
'-O', 'nullglob',
'-c',
'for i in ' + pattern + '; do echo $i; done'
];
const cp = spawn('bash', opts, { cwd: fixtureDir });
let out = [];
cp.stdout.on('data', function (c) {
out.push(c);
});
cp.stderr.pipe(process.stderr);
await new Promise((resolve, reject) => {
cp.on('close', (code) => {
if(code !== 0) {
reject();
}
out = flatten(out);
if (!out) {
out = [];
} else {
out = cleanResults(out.split(/\r*\n/));
}
bashOutput[pattern] = out;
resolve();
});
});
}
} catch(e) {
// Something went wrong when using bash, bash-results.json should not be overriden.
console.error('Unable to regenerate bash-results.json');
return;
}
const fname = path.resolve(__dirname, 'bash-results.json');
const data = JSON.stringify(bashOutput, null, 2) + '\n';
await fsPromises.writeFile(fname, data);
});
afterAll(async () => {
await new Promise((resolve, reject) => rimraf(path.resolve(__dirname, 'fixtures'), resolve));
});
const origCwd = process.cwd();
afterEach(async () => {
process.chdir(origCwd);
});