test: use node:test in test-cli-syntax.bad

PR-URL: https://github.com/nodejs/node/pull/54513
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Aviv Keller 2024-08-22 20:53:46 -04:00 committed by James M Snell
parent 22ea302978
commit 5de919b652

View File

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const assert = require('assert');
const { exec } = require('child_process'); const { exec } = require('child_process');
const { test } = require('node:test');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
const node = process.execPath; const node = process.execPath;
@ -23,26 +23,39 @@ const syntaxErrorRE = /^SyntaxError: \b/m;
'syntax/bad_syntax', 'syntax/bad_syntax',
'syntax/bad_syntax_shebang.js', 'syntax/bad_syntax_shebang.js',
'syntax/bad_syntax_shebang', 'syntax/bad_syntax_shebang',
].forEach(function(file) { ].forEach((file) => {
file = fixtures.path(file); const path = fixtures.path(file);
// Loop each possible option, `-c` or `--check` // Loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(args) { syntaxArgs.forEach((args) => {
const _args = args.concat(file); test(`Checking syntax for ${file} with ${args.join(' ')}`, async (t) => {
const cmd = [node, ..._args].join(' '); const _args = args.concat(path);
exec(cmd, common.mustCall((err, stdout, stderr) => { const cmd = [node, ..._args].join(' ');
assert.strictEqual(err instanceof Error, true);
assert.strictEqual(err.code, 1,
`code ${err.code} !== 1 for error:\n\n${err}`);
// No stdout should be produced try {
assert.strictEqual(stdout, ''); const { stdout, stderr } = await execPromise(cmd);
// Stderr should have a syntax error message // No stdout should be produced
assert.match(stderr, syntaxErrorRE); t.assert.strictEqual(stdout, '');
// stderr should include the filename // Stderr should have a syntax error message
assert(stderr.startsWith(file), `${stderr} starts with ${file}`); t.assert.match(stderr, syntaxErrorRE);
}));
// stderr should include the filename
t.assert.ok(stderr.startsWith(path));
} catch (err) {
t.assert.strictEqual(err.code, 1);
}
});
}); });
}); });
// Helper function to promisify exec
function execPromise(cmd) {
const { promise, resolve, reject } = Promise.withResolvers();
exec(cmd, (err, stdout, stderr) => {
if (err) return reject({ ...err, stdout, stderr });
resolve({ stdout, stderr });
});
return promise;
}