From 8b8a70c08466f08e243e0524588d5da0daf43480 Mon Sep 17 00:00:00 2001 From: Laurent Christophe Date: Fri, 10 Mar 2023 19:55:46 +0100 Subject: [PATCH] fix: use command-options.cwd to load jest config We should use that configuration instead of the cwd of the current process. That enables to spawn jest commands from another directory than the `rootDir` of jest. --- .eslintrc.yaml | 2 +- components/command/node/index.mjs | 6 +++--- components/command/node/jest.mjs | 21 +++++++------------ components/command/node/jest.test.mjs | 9 ++++---- components/command/node/mocha.mjs | 8 +++---- components/command/node/mocha.test.mjs | 9 ++++---- components/command/node/node-recursive.mjs | 6 +++--- .../command/node/node-recursive.test.mjs | 7 ++++--- components/command/node/node.mjs | 6 +++--- components/command/node/node.test.mjs | 7 ++++--- 10 files changed, 39 insertions(+), 42 deletions(-) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index e6b916af4..e142567fe 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -52,7 +52,7 @@ rules: # complexity # computed-property-spacing: [error, never] >> Prettier consistent-return: error - consistent-this: [error, self] + consistent-this: [error, that] # constructor-super: error >> Class are disabled by no-restricted-syntax curly: [error, all] # default-case >> SwitchStatement disabled by no-restricted-syntax diff --git a/components/command/node/index.mjs b/components/command/node/index.mjs index 2a841988a..d5a8ae990 100644 --- a/components/command/node/index.mjs +++ b/components/command/node/index.mjs @@ -111,7 +111,7 @@ export const compileConfigurationCommandAsync = async (configuration, env) => { const { "recursive-process-recording": recursive, recorder, - agent: { directory }, + agent: { directory: self }, command: { tokens }, "command-options": options, } = configuration; @@ -125,13 +125,13 @@ export const compileConfigurationCommandAsync = async (configuration, env) => { (recorder_recursive === null || recorder_recursive === recursive) && name === recorder, ); - const [exec, ...argv] = await hookCommandAsync(tokens, directory); + const [exec, ...argv] = await hookCommandAsync(tokens, self, options.cwd); return { exec, argv, options: { ...options, - env: hookEnvironment(env, directory), + env: hookEnvironment(env, self, options.cwd), }, }; }; diff --git a/components/command/node/jest.mjs b/components/command/node/jest.mjs index 13282d6e2..771f64231 100644 --- a/components/command/node/jest.mjs +++ b/components/command/node/jest.mjs @@ -1,10 +1,6 @@ -import { cwd } from "node:process"; import { coalesce } from "../../util/index.mjs"; -import { - convertFileUrlToPath, - convertPathToFileUrl, -} from "../../path/index.mjs"; -import { toAbsoluteUrl, toDirectoryUrl } from "../../url/index.mjs"; +import { convertFileUrlToPath } from "../../path/index.mjs"; +import { toAbsoluteUrl } from "../../url/index.mjs"; import { escapeNodeOption } from "./escape.mjs"; import { sniffTokens, splitTokens } from "./package.mjs"; import { hookJestArgvAsync } from "./jest-argv.mjs"; @@ -14,26 +10,23 @@ export const recursive = null; export const doesSupport = (tokens) => sniffTokens(tokens, "jest"); -export const hookCommandAsync = async (tokens, base) => { +export const hookCommandAsync = async (tokens, self, base) => { const { exec, argv } = splitTokens(tokens); return [ ...exec, - ...(await hookJestArgvAsync( - argv, - toDirectoryUrl(convertPathToFileUrl(cwd())), - )), + ...(await hookJestArgvAsync(argv, base)), "--setupFilesAfterEnv", - convertFileUrlToPath(toAbsoluteUrl("lib/node/recorder.mjs", base)), + convertFileUrlToPath(toAbsoluteUrl("lib/node/recorder.mjs", self)), ]; }; -export const hookEnvironment = (env, base) => ({ +export const hookEnvironment = (env, self, _base) => ({ ...env, NODE_OPTIONS: `${coalesce( env, "NODE_OPTIONS", "", )} --experimental-vm-modules --experimental-loader=${escapeNodeOption( - toAbsoluteUrl("lib/node/loader-esm.mjs", base), + toAbsoluteUrl("lib/node/loader-esm.mjs", self), )}`, }); diff --git a/components/command/node/jest.test.mjs b/components/command/node/jest.test.mjs index 2e0384366..15dde31db 100644 --- a/components/command/node/jest.test.mjs +++ b/components/command/node/jest.test.mjs @@ -8,14 +8,15 @@ const { JSON: { stringify: stringifyJSON }, } = globalThis; +const self = "file:///A:/self/"; const base = "file:///A:/base/"; const recorder_path = convertFileUrlToPath( - "file:///A:/base/lib/node/recorder.mjs", + "file:///A:/self/lib/node/recorder.mjs", ); const transformer_path = convertFileUrlToPath( toAbsoluteUrl("lib/node/transformer-jest.mjs", self_directory), ); -const loader_url = "file:///A:/base/lib/node/loader-esm.mjs"; +const loader_url = "file:///A:/self/lib/node/loader-esm.mjs"; ////////////////// // mocha --argv // @@ -23,7 +24,7 @@ const loader_url = "file:///A:/base/lib/node/loader-esm.mjs"; assertEqual(doesSupport(["jest", "--argv"]), true); -assertDeepEqual(await hookCommandAsync(["jest", "--argv"], base), [ +assertDeepEqual(await hookCommandAsync(["jest", "--argv"], self, base), [ "jest", "--argv", "--transform", @@ -44,7 +45,7 @@ assertDeepEqual(await hookCommandAsync(["jest", "--argv"], base), [ ///////////////////// assertDeepEqual( - hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, base), + hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, self, base), { FOO: "bar", NODE_OPTIONS: `options --experimental-vm-modules --experimental-loader=${loader_url}`, diff --git a/components/command/node/mocha.mjs b/components/command/node/mocha.mjs index 5000a9c0b..a8fd498ba 100644 --- a/components/command/node/mocha.mjs +++ b/components/command/node/mocha.mjs @@ -9,23 +9,23 @@ export const recursive = null; export const doesSupport = (tokens) => sniffTokens(tokens, "mocha"); -export const hookCommandAsync = (tokens, base) => { +export const hookCommandAsync = (tokens, self, _base) => { const { exec, argv } = splitTokens(tokens); return [ ...exec, "--require", - convertFileUrlToPath(toAbsoluteUrl("lib/node/mocha-hook.mjs", base)), + convertFileUrlToPath(toAbsoluteUrl("lib/node/mocha-hook.mjs", self)), ...argv, ]; }; -export const hookEnvironment = (env, base) => ({ +export const hookEnvironment = (env, self, _base) => ({ ...env, NODE_OPTIONS: `${coalesce( env, "NODE_OPTIONS", "", )} --experimental-loader=${escapeNodeOption( - toAbsoluteUrl("lib/node/recorder.mjs", base), + toAbsoluteUrl("lib/node/recorder.mjs", self), )}`, }); diff --git a/components/command/node/mocha.test.mjs b/components/command/node/mocha.test.mjs index 8d997df50..af842c222 100644 --- a/components/command/node/mocha.test.mjs +++ b/components/command/node/mocha.test.mjs @@ -2,16 +2,17 @@ import { assertDeepEqual, assertEqual } from "../../__fixture__.mjs"; import { fileURLToPath } from "node:url"; import { doesSupport, hookCommandAsync, hookEnvironment } from "./mocha.mjs"; +const self = "file:///A:/self/"; const base = "file:///A:/base/"; -const hook_path = fileURLToPath("file:///A:/base/lib/node/mocha-hook.mjs"); -const recorder_url = "file:///A:/base/lib/node/recorder.mjs"; +const hook_path = fileURLToPath("file:///A:/self/lib/node/mocha-hook.mjs"); +const recorder_url = "file:///A:/self/lib/node/recorder.mjs"; ////////////////// // mocha --argv // ////////////////// assertEqual(doesSupport(["mocha", "--argv"]), true); -assertDeepEqual(await hookCommandAsync(["mocha", "--argv"], base), [ +assertDeepEqual(await hookCommandAsync(["mocha", "--argv"], self, base), [ "mocha", "--require", hook_path, @@ -23,7 +24,7 @@ assertDeepEqual(await hookCommandAsync(["mocha", "--argv"], base), [ ///////////////////// assertDeepEqual( - hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, base), + hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, self, base), { FOO: "bar", NODE_OPTIONS: `options --experimental-loader=${recorder_url}`, diff --git a/components/command/node/node-recursive.mjs b/components/command/node/node-recursive.mjs index c534f4ecd..5bd96b82b 100644 --- a/components/command/node/node-recursive.mjs +++ b/components/command/node/node-recursive.mjs @@ -8,15 +8,15 @@ export const generateNodeRecorder = (recorder) => ({ name: recorder, recursive: true, doesSupport, - hookCommandAsync: (tokens, _base) => tokens, - hookEnvironment: (env, base) => ({ + hookCommandAsync: (tokens, _self, _base) => tokens, + hookEnvironment: (env, self, _base) => ({ ...env, NODE_OPTIONS: `${coalesce( env, "NODE_OPTIONS", "", )} --experimental-loader=${escapeNodeOption( - toAbsoluteUrl(`lib/node/recorder.mjs`, base), + toAbsoluteUrl(`lib/node/recorder.mjs`, self), )}`, }), }); diff --git a/components/command/node/node-recursive.test.mjs b/components/command/node/node-recursive.test.mjs index b6e1c5df7..2229363dd 100644 --- a/components/command/node/node-recursive.test.mjs +++ b/components/command/node/node-recursive.test.mjs @@ -5,7 +5,8 @@ const { name, recursive, doesSupport, hookCommandAsync, hookEnvironment } = generateNodeRecorder("process"); const base = "file:///A:/base/"; -const recorder_url = "file:///A:/base/lib/node/recorder.mjs"; +const self = "file:///A:/self/"; +const recorder_url = "file:///A:/self/lib/node/recorder.mjs"; assertEqual(name, "process"); @@ -13,10 +14,10 @@ assertEqual(recursive, true); assertEqual(doesSupport(["token"]), true); -assertDeepEqual(await hookCommandAsync(["token"], base), ["token"]); +assertDeepEqual(await hookCommandAsync(["token"], self, base), ["token"]); assertDeepEqual( - hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, base), + hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, self, base), { FOO: "bar", NODE_OPTIONS: `options --experimental-loader=${recorder_url}`, diff --git a/components/command/node/node.mjs b/components/command/node/node.mjs index 4635952df..baf25c7b4 100644 --- a/components/command/node/node.mjs +++ b/components/command/node/node.mjs @@ -26,14 +26,14 @@ export const generateNodeRecorder = (recorder) => ({ doesSupport, recursive: false, name: recorder, - hookCommandAsync: (tokens, base) => { + hookCommandAsync: (tokens, self, _base) => { const { exec, argv } = splitNodeCommand(tokens); return [ ...exec, "--experimental-loader", - toAbsoluteUrl(`lib/node/recorder.mjs`, base), + toAbsoluteUrl(`lib/node/recorder.mjs`, self), ...argv, ]; }, - hookEnvironment: (env, _base) => env, + hookEnvironment: (env, _self, _base) => env, }); diff --git a/components/command/node/node.test.mjs b/components/command/node/node.test.mjs index c13373a88..110314c1c 100644 --- a/components/command/node/node.test.mjs +++ b/components/command/node/node.test.mjs @@ -4,18 +4,19 @@ import { generateNodeRecorder } from "./node.mjs"; const { name, recursive, doesSupport, hookCommandAsync, hookEnvironment } = generateNodeRecorder("process"); +const self = "file:///A:/self/"; const base = "file:///A:/base/"; -const recorder_url = "file:///A:/base/lib/node/recorder.mjs"; +const recorder_url = "file:///A:/self/lib/node/recorder.mjs"; assertEqual(name, "process"); assertEqual(recursive, false); assertEqual(doesSupport(["node.ext", "main.mjs"]), true); -assertDeepEqual(await hookCommandAsync(["node.ext", "main.mjs"], base), [ +assertDeepEqual(await hookCommandAsync(["node.ext", "main.mjs"], self, base), [ "node.ext", "--experimental-loader", recorder_url, "main.mjs", ]); -assertDeepEqual(hookEnvironment({ FOO: "bar" }, base), { FOO: "bar" }); +assertDeepEqual(hookEnvironment({ FOO: "bar" }, self, base), { FOO: "bar" });