Skip to content

Commit 0500881

Browse files
fix(config): read POSIX fallback on Windows
1 parent 7066cc8 commit 0500881

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/hooks/caveman-config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ const VALID_MODES = [
2424
'wenyan-lite', 'wenyan', 'wenyan-full', 'wenyan-ultra',
2525
'commit', 'review', 'compress'
2626
];
27+
const CONFIG_FILE_NAME = 'config.json';
28+
const WINDOWS_PLATFORM = 'win32';
2729

2830
function getConfigDir() {
2931
if (process.env.XDG_CONFIG_HOME) {
3032
return path.join(process.env.XDG_CONFIG_HOME, 'caveman');
3133
}
32-
if (process.platform === 'win32') {
34+
if (process.platform === WINDOWS_PLATFORM) {
3335
return path.join(
3436
process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'),
3537
'caveman'
@@ -39,7 +41,11 @@ function getConfigDir() {
3941
}
4042

4143
function getConfigPath() {
42-
return path.join(getConfigDir(), 'config.json');
44+
return path.join(getConfigDir(), CONFIG_FILE_NAME);
45+
}
46+
47+
function getPosixHomeConfigPath() {
48+
return path.join(os.homedir(), '.config', 'caveman', CONFIG_FILE_NAME);
4349
}
4450

4551
// Walk up from `start` looking for a repo-local caveman config. Returns the
@@ -110,6 +116,10 @@ function getDefaultMode(startDir) {
110116
// 3. User config file
111117
const userMode = readModeFromConfigFile(getConfigPath());
112118
if (userMode) return userMode;
119+
if (process.platform === WINDOWS_PLATFORM && !process.env.XDG_CONFIG_HOME) {
120+
const posixHomeMode = readModeFromConfigFile(getPosixHomeConfigPath());
121+
if (posixHomeMode) return posixHomeMode;
122+
}
113123

114124
// 4. Default
115125
return 'full';

tests/test_repo_local_config.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ const assert = require('assert');
1717
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'caveman-userhome-'));
1818
process.env.XDG_CONFIG_HOME = tmpHome;
1919
delete process.env.CAVEMAN_DEFAULT_MODE;
20+
const APPDATA_SEGMENT = /AppData/;
21+
const CONFIG_FILE_NAME = 'config.json';
22+
const WINDOWS_PLATFORM = 'win32';
2023

21-
const { getDefaultMode, findRepoConfigPath } = require('../src/hooks/caveman-config');
24+
const { getDefaultMode, findRepoConfigPath, getConfigPath } = require('../src/hooks/caveman-config');
2225

2326
let passed = 0;
2427
let failed = 0;
@@ -123,6 +126,33 @@ test('falls through to user config when repo config absent', (tmp) => {
123126
}
124127
});
125128

129+
test('win32 without APPDATA falls back to ~/.config/caveman/config.json', (tmp) => {
130+
const origXdg = process.env.XDG_CONFIG_HOME;
131+
const origAppData = process.env.APPDATA;
132+
const origPlatform = Object.getOwnPropertyDescriptor(process, 'platform');
133+
const origHomedir = os.homedir;
134+
const posixConfigDir = path.join(tmp, '.config', 'caveman');
135+
try {
136+
delete process.env.XDG_CONFIG_HOME;
137+
delete process.env.APPDATA;
138+
Object.defineProperty(process, 'platform', { value: WINDOWS_PLATFORM });
139+
os.homedir = () => tmp;
140+
fs.mkdirSync(posixConfigDir, { recursive: true });
141+
fs.writeFileSync(path.join(posixConfigDir, CONFIG_FILE_NAME),
142+
JSON.stringify({ defaultMode: 'lite' }));
143+
144+
assert.match(getConfigPath(), APPDATA_SEGMENT);
145+
assert.strictEqual(getDefaultMode(), 'lite');
146+
} finally {
147+
if (origXdg === undefined) delete process.env.XDG_CONFIG_HOME;
148+
else process.env.XDG_CONFIG_HOME = origXdg;
149+
if (origAppData === undefined) delete process.env.APPDATA;
150+
else process.env.APPDATA = origAppData;
151+
Object.defineProperty(process, 'platform', origPlatform);
152+
os.homedir = origHomedir;
153+
}
154+
});
155+
126156
test('invalid mode in repo config falls through to default', (tmp) => {
127157
fs.writeFileSync(path.join(tmp, '.caveman.json'),
128158
JSON.stringify({ defaultMode: 'definitely-not-a-mode' }));

0 commit comments

Comments
 (0)