2018-12-09 09:44:44 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const { spawnSync } = require('child_process');
|
|
|
|
|
|
|
|
const node = process.execPath;
|
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Test both sets of arguments that check syntax
|
2018-12-09 09:44:44 -08:00
|
|
|
const syntaxArgs = [
|
2018-08-28 17:28:46 +02:00
|
|
|
'-c',
|
2021-03-26 08:51:08 -07:00
|
|
|
'--check',
|
2018-12-09 09:44:44 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
// Match on the name of the `Error` but not the message as it is different
|
|
|
|
// depending on the JavaScript engine.
|
2018-08-28 17:28:46 +02:00
|
|
|
const syntaxErrorRE = /^SyntaxError: Unexpected identifier\b/m;
|
2018-12-09 09:44:44 -08:00
|
|
|
|
|
|
|
// Should throw if code piped from stdin with --check has bad syntax
|
|
|
|
// loop each possible option, `-c` or `--check`
|
2018-08-28 17:28:46 +02:00
|
|
|
syntaxArgs.forEach(function(arg) {
|
2018-12-09 09:44:44 -08:00
|
|
|
const stdin = 'var foo bar;';
|
2018-08-28 17:28:46 +02:00
|
|
|
const c = spawnSync(node, [arg], { encoding: 'utf8', input: stdin });
|
2018-12-09 09:44:44 -08:00
|
|
|
|
|
|
|
// stderr should include '[stdin]' as the filename
|
|
|
|
assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
|
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
// No stdout should be produced
|
|
|
|
assert.strictEqual(c.stdout, '');
|
|
|
|
|
|
|
|
// stderr should have a syntax error message
|
2021-08-29 10:14:22 +02:00
|
|
|
assert.match(c.stderr, syntaxErrorRE);
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
assert.strictEqual(c.status, 1);
|
|
|
|
});
|
|
|
|
|
2019-04-15 10:29:56 -07:00
|
|
|
// Check --input-type=module
|
2018-08-28 17:28:46 +02:00
|
|
|
syntaxArgs.forEach(function(arg) {
|
|
|
|
const stdin = 'export var p = 5; var foo bar;';
|
|
|
|
const c = spawnSync(
|
|
|
|
node,
|
2019-10-11 17:57:13 -04:00
|
|
|
['--input-type=module', '--no-warnings', arg],
|
2018-08-28 17:28:46 +02:00
|
|
|
{ encoding: 'utf8', input: stdin }
|
|
|
|
);
|
|
|
|
|
|
|
|
// stderr should include '[stdin]' as the filename
|
|
|
|
assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
|
|
|
|
|
|
|
|
// No stdout should be produced
|
2018-12-09 09:44:44 -08:00
|
|
|
assert.strictEqual(c.stdout, '');
|
|
|
|
|
|
|
|
// stderr should have a syntax error message
|
2021-08-29 10:14:22 +02:00
|
|
|
assert.match(c.stderr, syntaxErrorRE);
|
2018-12-09 09:44:44 -08:00
|
|
|
|
|
|
|
assert.strictEqual(c.status, 1);
|
|
|
|
});
|