From 2025e8ac8911d4e32344254e5d2365adb440f103 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 10 Nov 2026 11:52:34 +0800 Subject: [PATCH] tests: mail-watch puts MAGPIE_CONFIG_DIR back; page-usage-board's children drop it bun test runs every package in one process. mail-watch's test set MAGPIE_CONFIG_DIR to its temp dir at module level and never restored it, so page-usage-board's spawned service inherited it through ...process.env. MAGPIE_CONFIG_DIR wins over the cwd probe, and "config dir: the host cwd carrying magpie markers wins" read /tmp/mail-watch-test-*/usage.jsonl on CI, which kept every publish run red after #844. mail-watch's afterAll now restores the variable. page-usage-board's test spawns build their env without an inherited MAGPIE_CONFIG_DIR or USAGE_JSONL. Run: - On main (545ed66), mote: `bun test packages/mail-watch packages/page-usage-board` failed this one test (79 pass, 1 fail). - With the fix, WSL Ubuntu 24.04: full-repo `HOME=$(mktemp -d) bun test`, as in CI, gave 7835 pass and 0 fail. Not tried: the full repo on macOS. mote's suite lock was queued for hours. Supersedes #921, which chdir'd the child and left the env as it was. --- packages/mail-watch/mail-watch.test.mjs | 5 +++++ packages/page-usage-board/test/server.test.mjs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/mail-watch/mail-watch.test.mjs b/packages/mail-watch/mail-watch.test.mjs index d561e0f..c0b601f 100644 --- a/packages/mail-watch/mail-watch.test.mjs +++ b/packages/mail-watch/mail-watch.test.mjs @@ -11,12 +11,17 @@ import net from "node:net" import { execFileSync } from "node:child_process" const dir = mkdtempSync(join(tmpdir(), "mail-watch-test-")) +// bun test runs every package in one process: put the variable back when done, +// or other packages' children inherit this temp dir as magpie's config dir +const prevConfigDir = process.env.MAGPIE_CONFIG_DIR process.env.MAGPIE_CONFIG_DIR = dir mkdirSync(join(dir, "magpie"), { recursive: true }) // the temp dir serves every describe block; it goes when the file's tests // are all done, not when the first block's are afterAll(() => { + if (prevConfigDir === undefined) delete process.env.MAGPIE_CONFIG_DIR + else process.env.MAGPIE_CONFIG_DIR = prevConfigDir rmSync(dir, { recursive: true, force: true }) }) diff --git a/packages/page-usage-board/test/server.test.mjs b/packages/page-usage-board/test/server.test.mjs index a2bcce8..d9457a8 100644 --- a/packages/page-usage-board/test/server.test.mjs +++ b/packages/page-usage-board/test/server.test.mjs @@ -27,8 +27,17 @@ let port; let publicChild; let publicPort; +// 所有包共用一个 bun test 进程:别的包的测试会设 MAGPIE_CONFIG_DIR / USAGE_JSONL +// (mail-watch 设 MAGPIE_CONFIG_DIR),继承下来会盖过本包要测的探测顺序。 +function hostEnv() { + const env = { ...process.env }; + delete env.MAGPIE_CONFIG_DIR; + delete env.USAGE_JSONL; + return env; +} + const baseEnv = (dir = os.tmpdir()) => ({ - ...process.env, + ...hostEnv(), PAGE_KEY: TEST_KEY, USAGE_JSONL: path.join(dir, 'usage.jsonl'), GATEWAY: 'http://127.0.0.1:9', @@ -358,7 +367,7 @@ test('config dir: the host cwd carrying magpie markers wins (portable installs)' const c = spawn(RUNTIME, [path.join(root, 'server.mjs')], { cwd: realDir, env: { - ...process.env, + ...hostEnv(), PAGE_KEY: TEST_KEY, GATEWAY: 'http://127.0.0.1:9', ATTRIBUTION: 'local',