MCPcopy Create free account
hub / github.com/EvoMap/evolver / withTempHome

Function withTempHome

test/nodeIdResolution.test.js:23–64  ·  view source on GitHub ↗
(run)

Source from the content-addressed store, hash-verified

21}
22
23function withTempHome(run) {
24 const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'evomap-nodeid-'));
25 const originalHome = process.env.HOME;
26 const originalUserProfile = process.env.USERPROFILE;
27 process.env.HOME = tmpHome;
28 process.env.USERPROFILE = tmpHome;
29
30 // Also hide the project-local .evomap_node_id fallback so a pre-existing
31 // file from a real run (where evolver was exercised in this checkout)
32 // does not short-circuit _loadPersistedNodeId() and bypass the tmpHome
33 // persist path under test. We stash it and restore in finally.
34 const LOCAL_NODE_ID_FILE = path.resolve(__dirname, '..', '.evomap_node_id');
35 const LOCAL_STASH = LOCAL_NODE_ID_FILE + '.test-stash';
36 let stashed = false;
37 try {
38 if (fs.existsSync(LOCAL_NODE_ID_FILE)) {
39 fs.renameSync(LOCAL_NODE_ID_FILE, LOCAL_STASH);
40 stashed = true;
41 }
42 } catch { /* best effort */ }
43
44 try {
45 return run(tmpHome);
46 } finally {
47 if (originalHome === undefined) delete process.env.HOME;
48 else process.env.HOME = originalHome;
49 if (originalUserProfile === undefined) delete process.env.USERPROFILE;
50 else process.env.USERPROFILE = originalUserProfile;
51 try { fs.rmSync(tmpHome, { recursive: true, force: true }); } catch {}
52 if (stashed) {
53 try {
54 // Clean any spurious node_id written during the test so restore wins.
55 if (fs.existsSync(LOCAL_NODE_ID_FILE)) fs.rmSync(LOCAL_NODE_ID_FILE);
56 fs.renameSync(LOCAL_STASH, LOCAL_NODE_ID_FILE);
57 } catch { /* best effort */ }
58 } else {
59 // No pre-existing file, but test may have written one via the
60 // LOCAL_NODE_ID_FILE fallback. Clean it up so we leave no trace.
61 try { if (fs.existsSync(LOCAL_NODE_ID_FILE)) fs.rmSync(LOCAL_NODE_ID_FILE); } catch {}
62 }
63 }
64}
65
66describe('getNodeId resolution', () => {
67 let savedEnv;

Callers 1

Calls 1

runFunction · 0.50

Tested by

no test coverage detected