Support for test files

This commit is contained in:
Thomas Rory Gummerson committed 2019-06-20 11:02:01 +02:00
1 parent 6807c284b2
commit 1167915557
8 files changed
+127 -10

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
node_modules
package-lock.json
+50 -9
View File
@@ -3,16 +3,57 @@
'use strict';
const { resolve } = require('path');
const { spawnSync } = require('child_process');
const readline = () => {
const { stdout } = spawnSync(
'node',
[ resolve('readline.js') ],
{ stdio: [ 'inherit', 'pipe', 'ignore' ] });
return String(stdout);
const parseArgs = require('minimist');
const chalk = require('chalk');
const read = require('./testreader');
const run = require('./runner');
const args = parseArgs(process.argv.slice(2), {
alias: { t: 'test' }
});
const readTests = () => {
if (args.test) {
const testfiles = Array.isArray(args.test)
? args.test
: [ args.test ];
return Promise.all(testfiles.map(read))
.then(x => x.flat());
}
return Promise.resolve([]);
};
global.readline = readline;
const main = tests => {
for (const file of args._) {
if (tests.length === 0) {
// eslint-disable-next-line global-require
global.readline = require('./readline');
// eslint-disable-next-line global-require
require(resolve(file));
} else {
let n = 0;
for (const { input, output: expected } of tests) {
n++;
const output = run(input.join('\n'), file).trim();
if (output !== expected.join('\n')) {
console.error(
// eslint-disable-next-line indent
`${chalk.red(`Test #${n} failed`)}
Input:
${input.join('\n ')}
Expected output:
${expected.join('\n ')}
Actual output:
${output.split('\n').join('\n ')}`);
}
}
}
}
};
require(resolve(process.argv[2]));
readTests().then(main);
// require(resolve(process.argv[2]));
+5 -1
View File
@@ -24,5 +24,9 @@
"bugs": {
"url": "https://github.com/trgwii/CodinGame-CLI/issues"
},
"homepage": "https://github.com/trgwii/CodinGame-CLI#readme"
"homepage": "https://github.com/trgwii/CodinGame-CLI#readme",
"dependencies": {
"chalk": "^2.4.2",
"minimist": "^1.2.0"
}
}
View File
File renamed without changes.
+14
View File
@@ -0,0 +1,14 @@
'use strict';
const { join } = require('path');
const { spawnSync } = require('child_process');
const readline = () => {
const { stdout } = spawnSync(
'node',
[ join(__dirname, 'child.js') ],
{ stdio: [ 'inherit', 'pipe', 'ignore' ] });
return String(stdout);
};
module.exports = readline;
+17
View File
@@ -0,0 +1,17 @@
'use strict';
const { join } = require('path');
const { spawnSync } = require('child_process');
const run = (input, path) => {
const { stdout } = spawnSync(
'node',
[
'-r', join(__dirname, 'setup.js'),
path
],
{ input });
return String(stdout);
};
module.exports = run;
+5
View File
@@ -0,0 +1,5 @@
'use strict';
const { join } = require('path');
global.readline = require(join(__dirname, '..', 'readline'));
+34
View File
@@ -0,0 +1,34 @@
'use strict';
const { createReadStream } = require('fs');
const { createInterface } = require('readline');
const read = async path => {
const rl = createInterface(createReadStream(path));
const tests = [
{ input: [], output: [] }
];
let [ current ] = tests;
let mode = '';
for await (const line of rl) {
if (line.trim().toLowerCase() === 'input:') {
if (mode === 'output') {
const next = { input: [], output: [] };
tests.push(next);
current = next;
}
mode = 'input';
continue;
}
if (line.trim().toLowerCase() === 'output:') {
mode = 'output';
continue;
}
if (line.length > 0 && mode.length > 0 && !line.startsWith('#')) {
current[mode].push(line);
}
}
return tests;
};
module.exports = read;