From f19044d4fdcc1a11649562dd1af1f7c11e87910b Mon Sep 17 00:00:00 2001 From: timbrinded <79199034+timbrinded@users.noreply.github.com> Date: Thu, 16 Nov 2023 20:41:54 +0000 Subject: [PATCH] injected config --- packages/cli/src/cmds/runNetwork.ts | 3 +- packages/cli/src/cmds/runTests.ts | 22 +++++++++------ packages/cli/src/internal/vitestSetup.ts | 6 ++++ packages/cli/src/lib/configReader.ts | 31 +++++---------------- packages/cli/src/lib/globalContextEffect.ts | 1 - test/package.json | 2 +- test/suites/basic/test_basic.ts | 9 ++++++ 7 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 packages/cli/src/internal/vitestSetup.ts diff --git a/packages/cli/src/cmds/runNetwork.ts b/packages/cli/src/cmds/runNetwork.ts index 232ff04e..10301bdf 100644 --- a/packages/cli/src/cmds/runNetwork.ts +++ b/packages/cli/src/cmds/runNetwork.ts @@ -9,7 +9,7 @@ import WebSocket from "ws"; import { parse } from "yaml"; import { clearNodeLogs, reportLogLocation } from "../internal/cmdFunctions/tempLogs"; import { commonChecks } from "../internal/launcherCommon"; -import { cacheConfig, importAsyncConfig, loadEnvVars } from "../lib/configReader"; +import { importAsyncConfig, loadEnvVars } from "../lib/configReader"; import { MoonwallContext, runNetworkOnly } from "../lib/globalContext"; import { executeTests } from "./runTests"; import { sendIpcMessage } from "../internal/foundations/zombieHelpers"; @@ -19,7 +19,6 @@ inquirer.registerPrompt("press-to-continue", PressToContinuePrompt); let lastSelected = 0; export async function runNetworkCmd(args) { - await cacheConfig(); process.env.MOON_TEST_ENV = args.envName; const globalConfig = await importAsyncConfig(); const env = globalConfig.environments.find(({ name }) => name === args.envName)!; diff --git a/packages/cli/src/cmds/runTests.ts b/packages/cli/src/cmds/runTests.ts index 70828363..539b8b57 100644 --- a/packages/cli/src/cmds/runTests.ts +++ b/packages/cli/src/cmds/runTests.ts @@ -6,7 +6,7 @@ import type { UserConfig, Vitest } from "vitest"; import { startVitest } from "vitest/node"; import { clearNodeLogs } from "../internal/cmdFunctions/tempLogs"; import { commonChecks } from "../internal/launcherCommon"; -import { cacheConfig, importAsyncConfig, loadEnvVars } from "../lib/configReader"; +import { importAsyncConfig, loadEnvVars } from "../lib/configReader"; import * as Err from "../errors"; import { MoonwallContext, @@ -18,15 +18,21 @@ import { runNetworkOnly as legacyRunNetworkOnly, contextCreator, } from "../lib/globalContext"; +import { fileURLToPath } from "url"; + +let __dirname; + +if (typeof import.meta.url !== "undefined") { + // ESM environment + __dirname = path.dirname(fileURLToPath(import.meta.url)); +} else { + // CJS environment + // eslint-disable-next-line no-self-assign + __dirname = __dirname; +} export const testEffect = (envName: string, additionalArgs?: object) => { return Effect.gen(function* (_) { - yield* _( - Effect.tryPromise({ - try: () => cacheConfig(), - catch: () => new Err.ConfigError(), - }) - ); const globalConfig = yield* _( Effect.tryPromise({ try: () => importAsyncConfig(), @@ -72,7 +78,6 @@ export const testEffect = (envName: string, additionalArgs?: object) => { }; export async function testCmd(envName: string, additionalArgs?: object): Promise { - await cacheConfig(); const globalConfig = await importAsyncConfig(); const env = globalConfig.environments.find(({ name }) => name === envName)!; process.env.MOON_TEST_ENV = envName; @@ -161,6 +166,7 @@ export const executeTestEffect = (env: Environment, additionalArgs?: object) => const baseOptions = { watch: false, globals: true, + setupFiles: [path.join(__dirname, "internal/vitestSetup.js")], reporters: env.reporters ? env.reporters : ["default"], outputFile: env.reportFile, testTimeout: env.timeout || globalConfig.defaultTestTimeout, diff --git a/packages/cli/src/internal/vitestSetup.ts b/packages/cli/src/internal/vitestSetup.ts new file mode 100644 index 00000000..b5ad3b60 --- /dev/null +++ b/packages/cli/src/internal/vitestSetup.ts @@ -0,0 +1,6 @@ +import { importJsonConfig } from "../lib/configReader"; +import { beforeAll } from "vitest"; + +beforeAll(() => { + globalThis.config = importJsonConfig(); +}); diff --git a/packages/cli/src/lib/configReader.ts b/packages/cli/src/lib/configReader.ts index b5a2dc85..9368dc92 100644 --- a/packages/cli/src/lib/configReader.ts +++ b/packages/cli/src/lib/configReader.ts @@ -5,8 +5,6 @@ import { readFileSync } from "fs"; import JSONC from "jsonc-parser"; import path, { extname } from "path"; -let cachedConfig: MoonwallConfig | undefined; - async function parseConfig(filePath: string) { let result: any; @@ -70,22 +68,9 @@ export function isEthereumDevConfig(): boolean { return env.foundation.type == "dev" && !env.foundation.launchSpec[0].disableDefaultEthProviders; } -export async function cacheConfig() { - const configPath = process.env.MOON_CONFIG_PATH!; - const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath); - try { - const config = parseConfigSync(filePath); - const replacedConfig = replaceEnvVars(config); - cachedConfig = replacedConfig as MoonwallConfig; - } catch (e) { - console.error(e); - throw new Error(`Error import config at ${filePath}`); - } -} - export function importJsonConfig(): MoonwallConfig { - if (cachedConfig) { - return cachedConfig; + if (globalThis.config) { + return replaceEnvVars(globalThis.config); } const configPath = process.env.MOON_CONFIG_PATH!; @@ -94,17 +79,16 @@ export function importJsonConfig(): MoonwallConfig { try { const config = parseConfigSync(filePath); const replacedConfig = replaceEnvVars(config); - cachedConfig = replacedConfig as MoonwallConfig; - return cachedConfig; + return replacedConfig as MoonwallConfig; } catch (e) { console.error(e); throw new Error(`Error import config at ${filePath}`); } } -export async function importAsyncConfig(): Promise { - if (cachedConfig) { - return cachedConfig; +export async function importAsyncConfig() { + if (globalThis.config) { + return replaceEnvVars(globalThis.config); } const configPath = process.env.MOON_CONFIG_PATH!; @@ -114,8 +98,7 @@ export async function importAsyncConfig(): Promise { const config = await parseConfig(filePath); const replacedConfig = replaceEnvVars(config); - cachedConfig = replacedConfig as MoonwallConfig; - return cachedConfig; + return replacedConfig as MoonwallConfig; } catch (e) { console.error(e); throw new Error(`Error import config at ${filePath}`); diff --git a/packages/cli/src/lib/globalContextEffect.ts b/packages/cli/src/lib/globalContextEffect.ts index 7f933a33..3d6a4d1c 100644 --- a/packages/cli/src/lib/globalContextEffect.ts +++ b/packages/cli/src/lib/globalContextEffect.ts @@ -302,7 +302,6 @@ export class MoonwallContext { this.ipcServer = server; process.env.MOON_IPC_SOCKET = socketPath; - process.once("exit", onProcessExit); process.once("SIGINT", onProcessExit); this.zombieNetwork = network; diff --git a/test/package.json b/test/package.json index 76c9c6ea..201e5f74 100644 --- a/test/package.json +++ b/test/package.json @@ -9,7 +9,7 @@ "clean": "rm -rf node_modules", "test-dev": "bun moonwall test 'dev_test dev_seq dev_mult'", "test": "bun moonwall test 'basic chopsticks'", - "node_test": "node --no-warnings --loader tsx --test suites/node_test/*.ts", + "node_test": "node --loader tsx --test suites/node_test/*.ts", "bun_test": "bun test suites/bun_test/*" }, "devDependencies": { diff --git a/test/suites/basic/test_basic.ts b/test/suites/basic/test_basic.ts index 7cefccee..6ab7f499 100644 --- a/test/suites/basic/test_basic.ts +++ b/test/suites/basic/test_basic.ts @@ -46,5 +46,14 @@ describeSuite({ }).to.throw("ERROR THROWN"); }, }); + + it({ + id: "T05", + title: "This test case can read globals", + test: function () { + log("Printing Global config", globalThis.config); + expect(globalThis.config).to.be.an("object"); + }, + }); }, });