From d2cd2e5f94b488d49a7f15da5030473bf34bf9b5 Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:53:46 -0400 Subject: [PATCH 1/3] test: use `node:test` in `test-cli-syntax.bad` --- test/sequential/test-cli-syntax-bad.js | 58 ++++++++++++++++---------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/test/sequential/test-cli-syntax-bad.js b/test/sequential/test-cli-syntax-bad.js index 5c87bf11419af6..47d8e04c26acfc 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -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; @@ -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 }); + } + }); + }); +} From f58aa1f1aaab82113b4673549fccea2c1727e561 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Tue, 3 Sep 2024 20:12:56 -0400 Subject: [PATCH 2/3] Update test/sequential/test-cli-syntax-bad.js Co-authored-by: James M Snell --- test/sequential/test-cli-syntax-bad.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/sequential/test-cli-syntax-bad.js b/test/sequential/test-cli-syntax-bad.js index 47d8e04c26acfc..44bb8877b0ba55 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -52,13 +52,10 @@ const syntaxErrorRE = /^SyntaxError: \b/m; // 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 }); - } - }); + const { promise, resolve, reject } = Promise.withResolvers(); + exec(cmb, (err, stdout, stderr) => { + if (err) return reject({...err, stdout, stderr}); + resolve({ stdout, stderr }); }); + return promise; } From a33839df762c0fb904f1027526b6557fcc88beb9 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Thu, 5 Sep 2024 17:00:07 -0400 Subject: [PATCH 3/3] Update test-cli-syntax-bad.js --- test/sequential/test-cli-syntax-bad.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/sequential/test-cli-syntax-bad.js b/test/sequential/test-cli-syntax-bad.js index 44bb8877b0ba55..c16de3b50133ec 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -53,8 +53,8 @@ const syntaxErrorRE = /^SyntaxError: \b/m; // Helper function to promisify exec function execPromise(cmd) { const { promise, resolve, reject } = Promise.withResolvers(); - exec(cmb, (err, stdout, stderr) => { - if (err) return reject({...err, stdout, stderr}); + exec(cmd, (err, stdout, stderr) => { + if (err) return reject({ ...err, stdout, stderr }); resolve({ stdout, stderr }); }); return promise;