1 | 'use strict';
|
2 |
|
3 | const assert = require('assert');
|
4 | const path = require('path');
|
5 | const fs = require('fs');
|
6 | const { exec } = require('child_process');
|
7 |
|
8 | const testsPath = path.resolve(__dirname, './test-modules');
|
9 | const tests = fs.readdirSync(testsPath);
|
10 |
|
11 | tests.forEach(test => {
|
12 | describe(test, function() {
|
13 | const modulePath = path.resolve(testsPath, test);
|
14 | const packageJson = require(path.resolve(modulePath, 'package.json'));
|
15 | const moduleCliPath = path.resolve(modulePath, packageJson.bin);
|
16 | const specs = require(path.resolve(modulePath, 'specs.js'));
|
17 |
|
18 | specs.forEach(spec => {
|
19 | const { input, output, description, stdin } = spec;
|
20 |
|
21 | it(description || input, function() {
|
22 | return execCli(moduleCliPath, input, stdin).then(result => assert.equal(result, output));
|
23 | });
|
24 | });
|
25 | });
|
26 | });
|
27 |
|
28 | function execCli(moduleCliPath, args, stdin = '') {
|
29 | return new Promise((resolve, reject) => {
|
30 | const cmd = `${stdin} node ${moduleCliPath} ${args}`;
|
31 | exec(cmd, (err, stdout, stderr) => {
|
32 | if (err || stderr) {
|
33 | return reject(err || stderr);
|
34 | }
|
35 |
|
36 | resolve(stdout.replace(/ +$/gm, '').replace(/\n$/, ''));
|
37 | }).stdin.end();
|
38 | });
|
39 | } |
\ | No newline at end of file |