Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use node:test in test-cli-syntax-bad #54513

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions test/sequential/test-cli-syntax-bad.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const common = require('../common');
const assert = require('assert');
require('../common');
const { exec } = require('child_process');
const { test } = require('node:test');
const fixtures = require('../common/fixtures');

const node = process.execPath;
Expand All @@ -23,26 +23,42 @@ const syntaxErrorRE = /^SyntaxError: \b/m;
'syntax/bad_syntax',
'syntax/bad_syntax_shebang.js',
'syntax/bad_syntax_shebang',
].forEach(function(file) {
file = fixtures.path(file);
].forEach((file) => {
const path = fixtures.path(file);

// Loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(args) {
const _args = args.concat(file);
const cmd = [node, ..._args].join(' ');
exec(cmd, common.mustCall((err, stdout, stderr) => {
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
assert.strictEqual(stdout, '');

// Stderr should have a syntax error message
assert.match(stderr, syntaxErrorRE);

// stderr should include the filename
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
}));
syntaxArgs.forEach((args) => {
test(`Checking syntax for ${file} with ${args.join(' ')}`, async (t) => {
const _args = args.concat(path);
const cmd = [node, ..._args].join(' ');

try {
const { stdout, stderr } = await execPromise(cmd);

// 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);
}
});
});
});

// Helper function to promisify exec
function execPromise(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout, stderr) => {
if (err) {
reject({ ...err, stdout, stderr });
} else {
resolve({ stdout, stderr });
}
});
});
RedYetiDev marked this conversation as resolved.
Show resolved Hide resolved
}
Loading