-
Notifications
You must be signed in to change notification settings - Fork 25
/
test-utils.js
executable file
·160 lines (113 loc) · 3.91 KB
/
test-utils.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
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const cli = require('cli');
const process = require('process');
const { it } = require('mocha');
const {
compose,
concat,
curry,
forEach,
map,
reduce,
} = require('./support');
/* -------- Internal -------- */
const filenameRe = base => new RegExp(`${base}_([a-z]).js`);
const readDir = d => fs.readdirSync(d);
const readFile = f => fs.readFileSync(f).toString('utf-8');
const writeFile = curry((f, data) => fs.writeFileSync(f, data));
const mkdir = d => fs.mkdirSync(d);
const mconcat = zero => reduce(concat, zero);
const evalFiles = compose(eval, mconcat(''), map(readFile)); // eslint-disable-line no-eval
const listToMap = xs => xs.reduce((m, [k, v]) => m.set(k, v), new Map());
/* -------- Utils -------- */
const readExercises = (ch, sections = ['exercise', 'solution', 'validation']) => {
const folder = path.join(__dirname, ch);
const partition = reduce((pts, f) => {
forEach((section) => {
const re = filenameRe(section);
if (re.test(f)) {
const spec = re.exec(f)[1];
pts.get(section).set(spec, path.join(folder, f));
}
}, sections);
return pts;
}, listToMap(map(s => [s, new Map()], sections)));
return partition(readDir(folder));
};
const defineSpecs = curry((input, exercises) => {
const support = path.join(__dirname, 'support.js');
forEach(([num, exercise]) => {
it(`exercise_${num}`, () => {
const validation = exercises.get('validation').get(num);
evalFiles([support, exercise, validation]);
});
}, [...exercises.get(input)]);
});
const runSolutions = compose(defineSpecs('solution'), readExercises);
const runExercises = compose(defineSpecs('exercise'), readExercises);
/* -------- Cli -------- */
if (process.argv.length > 2 && process.argv[1] === __filename) {
const usage = `Usage: test-utils new
Available options:
-h, --help Show this help text
-c, --chapter New chapter to create`;
const spec = curry((runner, chapter) => `const { describe } = require('mocha');
const { ${runner} } = require('../test-utils');
describe('Exercises Chapter ${chapter}', () => {
${runner}('ch${chapter}');
});`);
const addTestScript = curry((pkg, chapter) => {
const { scripts } = pkg;
return {
...pkg,
scripts: {
...scripts,
[`ch${chapter}`]: `mocha test/ch${chapter}.js`,
},
};
});
const failWith = curry((ctx, msg) => {
ctx.fatal([msg, usage].join('\n\n'));
});
// NOTE Override the `getUsage` method of the cli which is... not satisfactory.
cli.getUsage = () => { console.error(usage); cli.exit(0); }; // eslint-disable-line no-console
cli.parse({
chapter: ['c', 'New chapter to create', 'int'],
});
cli.main(function main(args, options) {
cli.failWith = failWith(this);
if (args.length !== 1 || args[0] !== 'new') {
cli.failWith(`Unknown command: ${args.join(' ')}`);
}
if (!options.chapter) {
cli.failWith('Missing required Chapter');
}
if (readDir(__dirname).includes(`ch${options.chapter}`)) {
cli.failWith(`Chapter ${options.chapter} already exists`);
}
mkdir(path.join(__dirname, `ch${options.chapter}`));
writeFile(
path.join(__dirname, 'test', `ch${options.chapter}.js`),
spec('runExercises', options.chapter),
);
writeFile(
path.join(__dirname, 'test', `ch${options.chapter}.solutions.js`),
spec('runSolutions', options.chapter),
);
const pkg = addTestScript(require('./package.json'), options.chapter); // eslint-disable-line global-require
writeFile(
path.join(__dirname, 'package.json'),
JSON.stringify(pkg, null, 2),
);
console.error(`Bootstrapped Chapter ${options.chapter}.`); // eslint-disable-line no-console
});
}
/* -------- Exports -------- */
module.exports = {
readExercises,
defineSpecs,
runSolutions,
runExercises,
};