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.
This commit is contained in:
yetone
2026-11-10 11:52:34 +08:00
parent 545ed66e74
commit 2025e8ac89
2 changed files with 16 additions and 2 deletions
+5
View File
@@ -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 })
})
+11 -2
View File
@@ -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',