From ea8055cb6b0b5fb81b06e91020dd490ee0f418cf Mon Sep 17 00:00:00 2001 From: felix9 Date: Thu, 22 Oct 2026 23:45:04 +0800 Subject: [PATCH] test: isolate the home directory on Windows freshHome() only set HOME, but Windows resolves os.homedir() from USERPROFILE, so every isolated test still read the real ~/.evolver settings (a stale Proxy URL) and bypassed its fake Proxy. Three tests also hand-rolled the same HOME-only isolation. Set HOME, USERPROFILE, HOMEDRIVE and HOMEPATH together and restore them all, and route the hand-rolled blocks through freshHome(). Windows: 151 pass / 0 fail / 6 skipped (was 134/17/6). --- test/apply.test.js | 10 ++++------ test/diagnostics.test.js | 9 ++++----- test/support/host.js | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/test/apply.test.js b/test/apply.test.js index d9a3a47..28c5c40 100644 --- a/test/apply.test.js +++ b/test/apply.test.js @@ -104,8 +104,7 @@ test('every turn looks the Hub up with its own prompt, without repeating assets' }); }); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - const home = process.env.HOME; - process.env.HOME = mkdtempSync(join(tmpdir(), 'evolver-prime-home-')); + const restoreHome = freshHome('evolver-prime-home-'); try { const { ctx, listeners } = fakeContext(); @@ -129,7 +128,7 @@ test('every turn looks the Hub up with its own prompt, without repeating assets' assert.match(second[0].content[0].text, /^1\. Chunk the download\.$/m); assert.doesNotMatch(second[0].content[0].text, /sha256:abc/); } finally { - process.env.HOME = home; + restoreHome(); server.close(); } }); @@ -262,8 +261,7 @@ test('a search slower than the wait budget injects itself instead of holding the }); }); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - const home = process.env.HOME; - process.env.HOME = mkdtempSync(join(tmpdir(), 'evolver-slow-home-')); + const restoreHome = freshHome('evolver-slow-home-'); try { const { ctx, listeners } = fakeContext(); @@ -276,7 +274,7 @@ test('a search slower than the wait budget injects itself instead of holding the assert.match(injected[0].content[0].text, /evolver_asset_reuse_result for sha256:slow\./); assert.match(injected[0].content[0].text, /^1\. Arrived late\.$/m); } finally { - process.env.HOME = home; + restoreHome(); server.close(); } }); diff --git a/test/diagnostics.test.js b/test/diagnostics.test.js index 7b94331..e97b875 100644 --- a/test/diagnostics.test.js +++ b/test/diagnostics.test.js @@ -1,12 +1,12 @@ import assert from 'node:assert/strict'; -import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; +import { mkdirSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { test } from 'node:test'; import { createRecallLog, createReportLog, pluginDiagnostics } from '../src/diagnostics.js'; import { hubGene } from '../src/prime.js'; import { evolverTools } from '../src/tools.js'; +import { freshHome } from './support/host.js'; const PROMPT = 'add a retry to the uploader'; const steps = (count, text = 'step') => Array.from({ length: count }, (_, index) => `${text} ${index}.`); @@ -105,8 +105,7 @@ test('evolver_status reports an unreachable Proxy instead of failing, and still }); test('diagnostics flag a running Proxy older than the minimum even when PATH has a newer evolver', async () => { - const home = process.env.HOME; - process.env.HOME = mkdtempSync(join(tmpdir(), 'evolver-diagnostics-home-')); + const restoreHome = freshHome('evolver-diagnostics-home-'); try { mkdirSync(join(process.env.HOME, '.evolver')); writeFileSync(join(process.env.HOME, '.evolver', 'settings.json'), JSON.stringify({ @@ -135,6 +134,6 @@ test('diagnostics flag a running Proxy older than the minimum even when PATH has assert.equal(report.runtime.platform, process.platform); assert.deepEqual(report.reuse_reports, { delivered: [], rejected: [] }); } finally { - process.env.HOME = home; + restoreHome(); } }); diff --git a/test/support/host.js b/test/support/host.js index f1a9c0c..201f7f4 100644 --- a/test/support/host.js +++ b/test/support/host.js @@ -137,7 +137,19 @@ export async function startProxy(answer, { delayMs = () => 0 } = {}) { } export function freshHome(prefix) { - const home = process.env.HOME; - process.env.HOME = mkdtempSync(join(tmpdir(), prefix)); - return () => { process.env.HOME = home; }; + const keys = ['HOME', 'USERPROFILE', 'HOMEDRIVE', 'HOMEPATH']; + const previous = Object.fromEntries(keys.map((key) => [key, process.env[key]])); + const home = mkdtempSync(join(tmpdir(), prefix)); + process.env.HOME = home; + if (process.platform === 'win32') { + process.env.USERPROFILE = home; + process.env.HOMEDRIVE = home.slice(0, 2); + process.env.HOMEPATH = home.slice(2); + } + return () => { + for (const key of keys) { + if (previous[key] === undefined) delete process.env[key]; + else process.env[key] = previous[key]; + } + }; }