2018-12-09 09:44:44 -08:00
|
|
|
'use strict';
|
|
|
|
|
2024-09-29 22:44:52 +02:00
|
|
|
const common = require('../common');
|
2018-12-09 09:44:44 -08:00
|
|
|
const { exec } = require('child_process');
|
2024-08-22 20:53:46 -04:00
|
|
|
const { test } = require('node:test');
|
2018-12-09 09:44:44 -08:00
|
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
|
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 = [
|
2024-09-29 22:44:52 +02:00
|
|
|
'-c',
|
|
|
|
'--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.
|
|
|
|
const syntaxErrorRE = /^SyntaxError: \b/m;
|
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Test bad syntax with and without shebang
|
2018-12-09 09:44:44 -08:00
|
|
|
[
|
|
|
|
'syntax/bad_syntax.js',
|
|
|
|
'syntax/bad_syntax',
|
|
|
|
'syntax/bad_syntax_shebang.js',
|
2021-03-26 08:51:08 -07:00
|
|
|
'syntax/bad_syntax_shebang',
|
2024-08-22 20:53:46 -04:00
|
|
|
].forEach((file) => {
|
|
|
|
const path = fixtures.path(file);
|
2018-12-09 09:44:44 -08:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Loop each possible option, `-c` or `--check`
|
2024-09-29 22:44:52 +02:00
|
|
|
syntaxArgs.forEach((flag) => {
|
|
|
|
test(`Checking syntax for ${file} with ${flag}`, async (t) => {
|
2024-08-22 20:53:46 -04:00
|
|
|
try {
|
2024-09-29 22:44:52 +02:00
|
|
|
const { stdout, stderr } = await execNode(flag, path);
|
2024-08-22 20:53:46 -04:00
|
|
|
|
|
|
|
// No stdout should be produced
|
|
|
|
t.assert.strictEqual(stdout, '');
|
|
|
|
|
|
|
|
// Stderr should have a syntax error message
|
|
|
|
t.assert.match(stderr, syntaxErrorRE);
|
|
|
|
|
|
|
|
// stderr should include the filename
|
|
|
|
t.assert.ok(stderr.startsWith(path));
|
|
|
|
} catch (err) {
|
|
|
|
t.assert.strictEqual(err.code, 1);
|
|
|
|
}
|
|
|
|
});
|
2018-12-09 09:44:44 -08:00
|
|
|
});
|
|
|
|
});
|
2024-08-22 20:53:46 -04:00
|
|
|
|
|
|
|
// Helper function to promisify exec
|
2024-09-29 22:44:52 +02:00
|
|
|
function execNode(flag, path) {
|
2024-08-22 20:53:46 -04:00
|
|
|
const { promise, resolve, reject } = Promise.withResolvers();
|
2024-09-29 22:44:52 +02:00
|
|
|
exec(...common.escapePOSIXShell`"${process.execPath}" ${flag} "${path}"`, (err, stdout, stderr) => {
|
2024-08-22 20:53:46 -04:00
|
|
|
if (err) return reject({ ...err, stdout, stderr });
|
|
|
|
resolve({ stdout, stderr });
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
}
|