Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

feat!: replace yargs-unparser in CLI respawn paths (#6125)

Josh Goldberg ✨1d2bb2efea72
diff --git a/bin/mocha.js b/bin/mocha.js
index a1b86afd..4bb39e24 100755
--- a/bin/mocha.js
+++ b/bin/mocha.js
@@ -11,7 +11,7 @@
import d from "debug";
import { spawn } from "node:child_process";
import os from "node:os";
-import unparse from "yargs-unparser";
+import { unparseMochaArgs } from "../lib/cli/unparse-args.js";
import { main } from "../lib/cli/cli.js";
import { loadOptions } from "../lib/cli/options.cjs";
@@ -20,7 +20,6 @@ import {
isNodeFlag,
impliesNoTimeouts,
} from "../lib/cli/node-flags.cjs";
-import { aliases } from "../lib/cli/run-option-metadata.cjs";
import { fileURLToPath } from "node:url";
const debug = d.debug("mocha:cli:mocha");
@@ -99,7 +98,7 @@ if (mochaArgs["node-option"] || Object.keys(nodeArgs).length || hasInspect) {
const args = [].concat(
nodeArgv,
mochaPath,
- unparse(mochaArgs, { alias: aliases }),
+ unparseMochaArgs(mochaArgs),
);
debug(
diff --git a/lib/cli/node-flags.cjs b/lib/cli/node-flags.cjs
index d3d55aaa..a7f9cecb 100644
--- a/lib/cli/node-flags.cjs
+++ b/lib/cli/node-flags.cjs
@@ -8,7 +8,7 @@
const nodeFlags = process.allowedNodeEnvironmentFlags;
const { isMochaFlag } = require("./run-option-metadata.cjs");
-const unparse = require("yargs-unparser");
+const { unparseNodeFlags } = require("./unparse-args.js");
/**
* These flags are considered "debug" flags.
@@ -66,26 +66,6 @@ function impliesNoTimeouts(flag) {
return debugFlags.has(flag);
}
-/**
- * All non-strictly-boolean arguments to node--those with values--must specify those values using `=`, e.g., `--inspect=0.0.0.0`.
- * Unparse these arguments using `yargs-unparser` (which would result in `--inspect 0.0.0.0`), then supply `=` where we have values.
- * There's probably an easier or more robust way to do this; fixes welcome
- * @param {Object} opts - Arguments object
- * @returns {string[]} Unparsed arguments using `=` to specify values
- * @private
- */
-function unparseNodeFlags(opts) {
- var args = unparse(opts);
- return args.length
- ? args
- .join(" ")
- .split(/\b/)
- .map((arg) => (arg === " " ? "=" : arg))
- .join("")
- .split(" ")
- : [];
-}
-
exports.isNodeFlag = isNodeFlag;
exports.impliesNoTimeouts = impliesNoTimeouts;
exports.unparseNodeFlags = unparseNodeFlags;
diff --git a/lib/cli/unparse-args.js b/lib/cli/unparse-args.js
new file mode 100644
index 00000000..b11dff4b
--- /dev/null
+++ b/lib/cli/unparse-args.js
@@ -0,0 +1,52 @@
+const toArray = (value) => (Array.isArray(value) ? value : [value]);
+
+const unparseOption = (name, value) => {
+ if (value === false) {
+ return [`--no-${name}`];
+ }
+
+ if (value === true) {
+ return [`--${name}`];
+ }
+
+ if (value == null) {
+ return [];
+ }
+
+ if (Array.isArray(value)) {
+ return value.flatMap((item) => unparseOption(name, item));
+ }
+
+ if (typeof value === "object") {
+ return Object.entries(value).flatMap(([key, item]) =>
+ unparseOption(`${name}.${key}`, item),
+ );
+ }
+
+ return [`--${name}`, String(value)];
+};
+
+const unparseMochaArgs = (opts) => {
+ const positionals = opts._ == null ? [] : toArray(opts._).map(String);
+ const options = Object.entries(opts).flatMap(([name, value]) =>
+ name === "_" ? [] : unparseOption(name, value),
+ );
+
+ return positionals.concat(options);
+};
+
+const unparseNodeFlags = (opts) => {
+ return Object.entries(opts).flatMap(([name, value]) => {
+ if (value === true) {
+ return [`--${name}`];
+ }
+
+ if (value === false || value == null) {
+ return [];
+ }
+
+ return toArray(value).map((item) => `--${name}=${item}`);
+ });
+};
+
+export { unparseMochaArgs, unparseNodeFlags };
diff --git a/package-lock.json b/package-lock.json
index cd77d144..faac6925 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -25,8 +25,7 @@
"strip-json-comments": "^5.0.3",
"supports-color": "^8.1.1",
"workerpool": "^10.0.0",
- "yargs": "^17.7.2",
- "yargs-unparser": "^2.0.0"
+ "yargs": "^17.7.2"
},
"bin": {
"mocha": "bin/mocha.js"
@@ -4350,6 +4349,7 @@
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz",
"integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==",
+ "dev": true,
"bin": {
"flat": "cli.js"
}
@@ -9558,50 +9558,6 @@
"node": ">=12"
}
},
- "node_modules/yargs-unparser": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz",
- "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==",
- "dependencies": {
- "camelcase": "^6.0.0",
- "decamelize": "^4.0.0",
- "flat": "^5.0.2",
- "is-plain-obj": "^2.1.0"
- },
- "engines": {
- "node": ">=10"
- }
- },
- "node_modules/yargs-unparser/node_modules/camelcase": {
- "version": "6.3.0",
- "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz",
- "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==",
- "engines": {
- "node": ">=10"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/yargs-unparser/node_modules/decamelize": {
- "version": "4.0.0",
- "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz",
- "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==",
- "engines": {
- "node": ">=10"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/yargs-unparser/node_modules/is-plain-obj": {
- "version": "2.1.0",
- "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz",
- "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==",
- "engines": {
- "node": ">=8"
- }
- },
"node_modules/yargs/node_modules/cliui": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",
diff --git a/package.json b/package.json
index 15551803..9208649e 100644
--- a/package.json
+++ b/package.json
@@ -103,8 +103,7 @@
"strip-json-comments": "^5.0.3",
"supports-color": "^8.1.1",
"workerpool": "^10.0.0",
- "yargs": "^17.7.2",
- "yargs-unparser": "^2.0.0"
+ "yargs": "^17.7.2"
},
"devDependencies": {
"@eslint/js": "^10.0.0",
diff --git a/test/node-unit/cli/unparse-args.spec.js b/test/node-unit/cli/unparse-args.spec.js
new file mode 100644
index 00000000..6e9e7955
--- /dev/null
+++ b/test/node-unit/cli/unparse-args.spec.js
@@ -0,0 +1,101 @@
+import {
+ unparseMochaArgs,
+ unparseNodeFlags,
+} from "../../../lib/cli/unparse-args.js";
+
+describe("unparse-args", function () {
+ describe("unparseNodeFlags()", function () {
+ it("unparses Node and V8 flags using equals for values", function () {
+ expect(
+ unparseNodeFlags({
+ "trace-warnings": true,
+ "max-old-space-size": "4096",
+ inspect: "0.0.0.0:9229",
+ }),
+ "to equal",
+ [
+ "--trace-warnings",
+ "--max-old-space-size=4096",
+ "--inspect=0.0.0.0:9229",
+ ],
+ );
+ });
+
+ it("omits false and nullish Node and V8 flag values", function () {
+ expect(
+ unparseNodeFlags({
+ "trace-warnings": false,
+ conditions: undefined,
+ "zero-fill-buffers": null,
+ }),
+ "to equal",
+ [],
+ );
+ });
+ });
+
+ describe("unparseMochaArgs()", function () {
+ it("unparses positionals, booleans, scalars, and repeated arrays", function () {
+ expect(
+ unparseMochaArgs({
+ _: ["test/a.spec.js", "test/b.spec.js"],
+ require: ["tsx", "./setup.js"],
+ reporter: "json",
+ timeout: "0",
+ color: false,
+ bail: true,
+ }),
+ "to equal",
+ [
+ "test/a.spec.js",
+ "test/b.spec.js",
+ "--require",
+ "tsx",
+ "--require",
+ "./setup.js",
+ "--reporter",
+ "json",
+ "--timeout",
+ "0",
+ "--no-color",
+ "--bail",
+ ],
+ );
+ });
+
+ it("emits positionals first regardless of object insertion order", function () {
+ expect(
+ unparseMochaArgs({
+ reporter: "json",
+ _: ["test/a.spec.js"],
+ }),
+ "to equal",
+ ["test/a.spec.js", "--reporter", "json"],
+ );
+ });
+
+ it("omits absent positionals", function () {
+ expect(
+ unparseMochaArgs({
+ _: undefined,
+ reporter: "json",
+ }),
+ "to equal",
+ ["--reporter", "json"],
+ );
+ });
+
+ it("unparses reporter-option object values in yargs-unparser-compatible dotted form", function () {
+ expect(
+ unparseMochaArgs({
+ "reporter-option": {
+ foo: "bar",
+ baz: true,
+ },
+ }),
+ "to equal",
+ ["--reporter-option.foo", "bar", "--reporter-option.baz"],
+ );
+ });
+ });
+});