/
index.js
119 lines (105 loc) · 3.09 KB
/
index.js
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
const path = require('path');
const assert = require('assert');
const sander = require('sander');
const buble = require('buble');
const { exec } = require('child_process');
const { normaliseOutput, runTestSuiteWithSamples } = require('../utils.js');
const cwd = process.cwd();
sander.rimrafSync(__dirname, 'node_modules');
sander.copydirSync(__dirname, 'node_modules_rename_me').to(__dirname, 'node_modules');
runTestSuiteWithSamples(
'cli',
path.resolve(__dirname, 'samples'),
(dir, config) => {
(config.skip ? it.skip : config.solo ? it.only : it)(
path.basename(dir) + ': ' + config.description,
done => {
process.chdir(config.cwd || dir);
const command = 'node ' + path.resolve(__dirname, '../../bin') + path.sep + config.command;
exec(command, {}, (err, code, stderr) => {
if (err) {
if (config.error) {
const shouldContinue = config.error(err);
if (!shouldContinue) return done();
} else {
throw err;
}
}
if ('stderr' in config) {
const shouldContinue = config.stderr(stderr.trim());
if (!shouldContinue) return done();
} else if (stderr) {
console.error(stderr);
}
let unintendedError;
if (config.execute) {
try {
if (config.buble) {
code = buble.transform(code, {
transforms: { modules: false }
}).code;
}
const fn = new Function('require', 'module', 'exports', 'assert', code);
const module = {
exports: {}
};
fn(require, module, module.exports, assert);
if (config.error) {
unintendedError = new Error('Expected an error while executing output');
}
if (config.exports) {
config.exports(module.exports);
}
} catch (err) {
if (config.error) {
config.error(err);
} else {
unintendedError = err;
}
}
if (config.show || unintendedError) {
console.log(code + '\n\n\n');
}
if (config.solo) console.groupEnd();
unintendedError ? done(unintendedError) : done();
} else if (config.result) {
try {
config.result(code);
done();
} catch (err) {
done(err);
}
} else if (config.test) {
try {
config.test();
done();
} catch (err) {
done(err);
}
} else if (sander.existsSync('_expected') && sander.statSync('_expected').isDirectory()) {
let error = null;
sander.readdirSync('_expected').forEach(child => {
const expected = sander.readFileSync(path.join('_expected', child)).toString();
const actual = sander.readFileSync(path.join('_actual', child)).toString();
try {
assert.equal(normaliseOutput(actual), normaliseOutput(expected));
} catch (err) {
error = err;
}
});
done(error);
} else {
const expected = sander.readFileSync('_expected.js').toString();
try {
assert.equal(normaliseOutput(code), normaliseOutput(expected));
done();
} catch (err) {
done(err);
}
}
});
}
);
},
() => process.chdir(cwd)
);