Skip to content

Commit

Permalink
injected config
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Nov 16, 2023
1 parent 38842bc commit f19044d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 36 deletions.
3 changes: 1 addition & 2 deletions packages/cli/src/cmds/runNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)!;
Expand Down
22 changes: 14 additions & 8 deletions packages/cli/src/cmds/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -72,7 +78,6 @@ export const testEffect = (envName: string, additionalArgs?: object) => {
};

export async function testCmd(envName: string, additionalArgs?: object): Promise<boolean> {
await cacheConfig();
const globalConfig = await importAsyncConfig();
const env = globalConfig.environments.find(({ name }) => name === envName)!;
process.env.MOON_TEST_ENV = envName;
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/internal/vitestSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { importJsonConfig } from "../lib/configReader";
import { beforeAll } from "vitest";

beforeAll(() => {
globalThis.config = importJsonConfig();
});
31 changes: 7 additions & 24 deletions packages/cli/src/lib/configReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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!;
Expand All @@ -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<MoonwallConfig> {
if (cachedConfig) {
return cachedConfig;
export async function importAsyncConfig() {
if (globalThis.config) {
return replaceEnvVars(globalThis.config);
}

const configPath = process.env.MOON_CONFIG_PATH!;
Expand All @@ -114,8 +98,7 @@ export async function importAsyncConfig(): Promise<MoonwallConfig> {
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}`);
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/lib/globalContextEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 9 additions & 0 deletions test/suites/basic/test_basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
});
},
});

0 comments on commit f19044d

Please sign in to comment.