attempted haskell support

This commit is contained in:
Thomas Rory Gummerson committed 2019-06-20 12:57:01 +02:00
1 parent 63200e056f
commit 91f1c28eb7
3 files changed
+85 -45

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
'use strict';
const chalk = require('chalk');
const fatal = (file, code, stderr, error) => {
console.error(
chalk.red(
file + ': ' +
(code
? 'exited with code: ' + code
: 'failed!') + '\n' +
(stderr
? '\n Error output:\n ' + stderr.split('\n').join('\n ')
: '')));
if (error) {
console.error(chalk.red('Error: ' + error.message));
}
};
const indent = (n, code) => {
const level = ' '.repeat(n);
const lines = Array.isArray(code) ? code : (code || '').split('\n');
return level + lines.join('\n' + level);
};
const failed = (n, file, input, expected, actual, stderr) => {
console.error(chalk.red(file + ': Test #' + n + ' failed!'));
if (input) {
console.error(chalk.gray(' Input:\n') + indent(4, input));
}
if (expected) {
console.error(chalk.gray(' Expected output:\n') + indent(4, expected));
}
console.error(chalk.gray(' Actual output:\n') + indent(4, actual));
if (stderr) {
console.error(chalk.gray(' Error output:\n') + indent(4, stderr));
}
};
module.exports = { fatal, failed };
+11 -15
View File
@@ -3,10 +3,10 @@
'use strict';
const parseArgs = require('minimist');
const chalk = require('chalk');
const read = require('./testreader');
const run = require('./runner');
const { fatal, failed } = require('./errors');
const args = parseArgs(process.argv.slice(2), {
alias: { t: 'test' }
@@ -27,26 +27,22 @@ const readTests = () => {
const main = tests => {
for (const file of args._) {
if (tests.length === 0) {
run(file);
const [ , stderr, code, error ] = run(file);
if (code > 0 || error) {
fatal(file, code, stderr, error);
}
} else {
let n = 0;
for (const { input, output: expected } of tests) {
n++;
const [ output, error ] = run(file, input.join('\n'));
const [ output, stderr, code, error ] =
run(file, input.join('\n'));
if (error) {
fatal(file, code, stderr, error);
}
const troutput = output.trim();
if (troutput !== expected.join('\n')) {
console.error(
// eslint-disable-next-line indent
`${chalk.red(`${file}: Test #${n} failed`)}
Input:
${input.join('\n ')}
Expected output:
${expected.join('\n ')}
Actual output:
${troutput.split('\n').join('\n ') +
(error
? '\n Error output:\n ' + error.split('\n').join('\n ')
: '')}`);
failed(n, file, input, expected, troutput, stderr);
}
}
}
+34 -30
View File
@@ -1,38 +1,42 @@
'use strict';
const { platform } = require('os');
const { join, extname } = require('path');
const { join, extname, basename } = require('path');
const { spawnSync } = require('child_process');
const spawn = (command, args, input) => {
const { stdout, stderr, status, error } = spawnSync(
command,
args,
input ? { input } : { stdio: 'inherit' });
return [
stdout ? String(stdout) : stdout,
stderr ? String(stderr) : stderr,
status,
error
];
};
const runners = {
js: (path, input) => {
const { stdout, stderr } = spawnSync(
'node',
[
'-r', join(__dirname, 'setup.js'),
path
],
input
? { input }
: { stdio: 'inherit' });
return [ String(stdout), String(stderr) ];
},
ts: (path, input) => {
const { stdout, stderr } = spawnSync(
platform() === 'win32'
? 'cmd'
: 'ts-node',
[
...platform() === 'win32'
? [ '/c', 'ts-node' ]
: [],
'-r', join(__dirname, 'setup.js'),
path
],
input
? { input }
: { stdio: 'inherit' });
return [ String(stdout), String(stderr) ];
js: (path, input) =>
spawn('node', [
'-r', join(__dirname, 'setup.js'), path
], input),
ts: (path, input) =>
spawn(platform() === 'win32' ? 'cmd' : 'ts-node', [
...platform() === 'win32'
? [ '/c', 'ts-node' ]
: [],
'-r', join(__dirname, 'setup.js'), path
], input),
hs: (path, input) => {
const [ cout, cerr, code ] = spawn(
'ghc',
[ path ]);
if (code > 0) {
return [ cout, cerr ];
}
return spawn(basename(path, '.hs'), [], input);
}
};
@@ -41,7 +45,7 @@ const run = (path, input = null) => {
if (type in runners) {
return runners[type](path, input);
}
throw new TypeError('Unknown file type: ' + type);
throw new TypeError('Unknown file type:', type);
};
module.exports = run;