UNPKG

1.14 kBJavaScriptView Raw
1'use strict';
2
3const assert = require('assert');
4const path = require('path');
5const fs = require('fs');
6const { exec } = require('child_process');
7
8const testsPath = path.resolve(__dirname, './test-modules');
9const tests = fs.readdirSync(testsPath);
10
11tests.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
28function 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