Skip to content

Commit 468ea97

Browse files
a6b8claude
andcommitted
fix(tests): prevent overwriting real ~/.flowmcp config
Tests were writing directly to ~/.flowmcp/.env and config.json without backup/restore, destroying user data on every test run. Now all 3 files use the correct merge+restore pattern. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6e7c92e commit 468ea97

3 files changed

Lines changed: 67 additions & 17 deletions

File tree

tests/unit/group-commands.test.mjs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals'
2-
import { writeFile, mkdir, rm, access } from 'node:fs/promises'
2+
import { writeFile, mkdir, rm, readFile } from 'node:fs/promises'
33
import { join } from 'node:path'
44
import { tmpdir, homedir } from 'node:os'
55

@@ -13,15 +13,24 @@ const LOCAL_CONFIG_PATH = join( LOCAL_CONFIG_DIR, 'config.json' )
1313

1414
const GLOBAL_CONFIG_DIR = join( homedir(), '.flowmcp' )
1515
const GLOBAL_CONFIG_PATH = join( GLOBAL_CONFIG_DIR, 'config.json' )
16-
let globalConfigExistedBefore = false
16+
let originalGlobalConfig = null
17+
let globalConfigExisted = false
1718

1819
beforeAll( async () => {
1920
try {
20-
await access( GLOBAL_CONFIG_PATH )
21-
globalConfigExistedBefore = true
21+
originalGlobalConfig = await readFile( GLOBAL_CONFIG_PATH, 'utf-8' )
22+
globalConfigExisted = true
2223
} catch {
23-
globalConfigExistedBefore = false
24-
await mkdir( GLOBAL_CONFIG_DIR, { recursive: true } )
24+
globalConfigExisted = false
25+
}
26+
27+
await mkdir( GLOBAL_CONFIG_DIR, { recursive: true } )
28+
29+
if( globalConfigExisted && originalGlobalConfig ) {
30+
const parsed = JSON.parse( originalGlobalConfig )
31+
parsed[ 'envPath' ] = parsed[ 'envPath' ] || VALID_GLOBAL_CONFIG[ 'envPath' ]
32+
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( parsed, null, 4 ), 'utf-8' )
33+
} else {
2534
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( VALID_GLOBAL_CONFIG, null, 4 ), 'utf-8' )
2635
}
2736

@@ -30,7 +39,11 @@ beforeAll( async () => {
3039
} )
3140

3241
afterAll( async () => {
33-
await rm( TEST_CWD, { recursive: true, force: true } )
42+
if( globalConfigExisted && originalGlobalConfig ) {
43+
await writeFile( GLOBAL_CONFIG_PATH, originalGlobalConfig, 'utf-8' )
44+
}
45+
46+
await rm( TEST_CWD, { recursive: true, force: true } ).catch( () => {} )
3447
} )
3548

3649

tests/unit/prompt-commands.test.mjs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const PROMPTS_DIR = join( LOCAL_CONFIG_DIR, 'prompts' )
1414

1515
const GLOBAL_CONFIG_DIR = join( homedir(), '.flowmcp' )
1616
const GLOBAL_CONFIG_PATH = join( GLOBAL_CONFIG_DIR, 'config.json' )
17-
let globalConfigExistedBefore = false
17+
let originalGlobalConfig = null
18+
let globalConfigExisted = false
1819

1920
const VALID_PROMPT_CONTENT = `# Standard Token Analysis
2021
@@ -49,11 +50,19 @@ Call \`searchSymbol\` to find the asset.
4950

5051
beforeAll( async () => {
5152
try {
52-
await access( GLOBAL_CONFIG_PATH )
53-
globalConfigExistedBefore = true
53+
originalGlobalConfig = await readFile( GLOBAL_CONFIG_PATH, 'utf-8' )
54+
globalConfigExisted = true
5455
} catch {
55-
globalConfigExistedBefore = false
56-
await mkdir( GLOBAL_CONFIG_DIR, { recursive: true } )
56+
globalConfigExisted = false
57+
}
58+
59+
await mkdir( GLOBAL_CONFIG_DIR, { recursive: true } )
60+
61+
if( globalConfigExisted && originalGlobalConfig ) {
62+
const parsed = JSON.parse( originalGlobalConfig )
63+
parsed[ 'envPath' ] = parsed[ 'envPath' ] || VALID_GLOBAL_CONFIG[ 'envPath' ]
64+
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( parsed, null, 4 ), 'utf-8' )
65+
} else {
5766
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( VALID_GLOBAL_CONFIG, null, 4 ), 'utf-8' )
5867
}
5968

@@ -65,7 +74,11 @@ beforeAll( async () => {
6574

6675

6776
afterAll( async () => {
68-
await rm( TEST_CWD, { recursive: true, force: true } )
77+
if( globalConfigExisted && originalGlobalConfig ) {
78+
await writeFile( GLOBAL_CONFIG_PATH, originalGlobalConfig, 'utf-8' )
79+
}
80+
81+
await rm( TEST_CWD, { recursive: true, force: true } ).catch( () => {} )
6982
} )
7083

7184

tests/unit/validate-group-path.test.mjs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ const GLOBAL_SCHEMAS_DIR = join( GLOBAL_CONFIG_DIR, 'schemas' )
1212
const TEST_CWD = join( tmpdir(), 'flowmcp-cli-validate-group-path' )
1313
const LOCAL_CONFIG_DIR = join( TEST_CWD, '.flowmcp' )
1414
const LOCAL_CONFIG_PATH = join( LOCAL_CONFIG_DIR, 'config.json' )
15-
const ENV_PATH = join( GLOBAL_CONFIG_DIR, '.env' )
15+
const ENV_PATH = join( GLOBAL_CONFIG_DIR, '.env.vgpath' )
1616
const SOURCE_NAME = 'valsrc'
1717
const SOURCE_DIR = join( GLOBAL_SCHEMAS_DIR, SOURCE_NAME )
1818

1919
let originalGlobalConfig = null
2020
let globalConfigExisted = false
21+
let originalEnvContent = null
22+
let envExisted = false
2123

2224
const VALID_SCHEMA_CONTENT = `export const main = {
2325
namespace: 'valsrc',
@@ -93,8 +95,24 @@ beforeAll( async () => {
9395
globalConfigExisted = false
9496
}
9597

98+
try {
99+
originalEnvContent = await readFile( ENV_PATH, 'utf-8' )
100+
envExisted = true
101+
} catch {
102+
envExisted = false
103+
}
104+
96105
await mkdir( GLOBAL_CONFIG_DIR, { recursive: true } )
97-
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( VALID_GLOBAL_CONFIG, null, 4 ), 'utf-8' )
106+
107+
if( globalConfigExisted && originalGlobalConfig ) {
108+
const parsed = JSON.parse( originalGlobalConfig )
109+
parsed[ 'sources' ] = parsed[ 'sources' ] || {}
110+
parsed[ 'sources' ][ SOURCE_NAME ] = VALID_GLOBAL_CONFIG[ 'sources' ][ SOURCE_NAME ]
111+
parsed[ 'envPath' ] = ENV_PATH
112+
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( parsed, null, 4 ), 'utf-8' )
113+
} else {
114+
await writeFile( GLOBAL_CONFIG_PATH, JSON.stringify( VALID_GLOBAL_CONFIG, null, 4 ), 'utf-8' )
115+
}
98116

99117
await mkdir( SOURCE_DIR, { recursive: true } )
100118
await writeFile( join( SOURCE_DIR, 'check.mjs' ), VALID_SCHEMA_CONTENT, 'utf-8' )
@@ -111,8 +129,14 @@ afterAll( async () => {
111129
await writeFile( GLOBAL_CONFIG_PATH, originalGlobalConfig, 'utf-8' )
112130
}
113131

114-
await rm( SOURCE_DIR, { recursive: true, force: true } )
115-
await rm( TEST_CWD, { recursive: true, force: true } )
132+
if( envExisted && originalEnvContent ) {
133+
await writeFile( ENV_PATH, originalEnvContent, 'utf-8' )
134+
} else {
135+
await rm( ENV_PATH, { force: true } ).catch( () => {} )
136+
}
137+
138+
await rm( SOURCE_DIR, { recursive: true, force: true } ).catch( () => {} )
139+
await rm( TEST_CWD, { recursive: true, force: true } ).catch( () => {} )
116140
} )
117141

118142

0 commit comments

Comments
 (0)