Skip to content

Commit 2f7b126

Browse files
committed
Add Windows cache path support (%LOCALAPPDATA%) and platform test coverage
1 parent 802ba7c commit 2f7b126

3 files changed

Lines changed: 108 additions & 11 deletions

File tree

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,11 @@ disk for **24 hours** to avoid repeating dozens of API calls on every run.
399399

400400
### Cache location
401401

402-
| OS | Path |
403-
| ----- | ----------------------------------------------------------------------- |
404-
| macOS | `~/Library/Caches/github-code-search/` |
405-
| Linux | `$XDG_CACHE_HOME/github-code-search/` or `~/.cache/github-code-search/` |
402+
| OS | Path |
403+
| ------- | ----------------------------------------------------------------------- |
404+
| macOS | `~/Library/Caches/github-code-search/` |
405+
| Linux | `$XDG_CACHE_HOME/github-code-search/` or `~/.cache/github-code-search/` |
406+
| Windows | `%LOCALAPPDATA%\github-code-search\` |
406407

407408
You can also override the cache directory with the `GITHUB_CODE_SEARCH_CACHE_DIR`
408409
environment variable.
@@ -419,11 +420,14 @@ github-code-search "useFeatureFlag" --org fulll \
419420
### Purging the cache
420421

421422
```bash
422-
# macOS
423-
rm -rf ~/Library/Caches/github-code-search
423+
# macOS / Linux
424+
rm -rf ~/Library/Caches/github-code-search # macOS
425+
rm -rf "${XDG_CACHE_HOME:-$HOME/.cache}/github-code-search" # Linux
426+
```
424427

425-
# Linux
426-
rm -rf "${XDG_CACHE_HOME:-$HOME/.cache}/github-code-search"
428+
```powershell
429+
# Windows (PowerShell)
430+
Remove-Item -Recurse -Force "$env:LOCALAPPDATA\github-code-search"
427431
```
428432

429433
## Known limitations

src/cache.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,90 @@ describe("getCacheDir", () => {
4141
delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR;
4242
expect(getCacheDir()).toContain("github-code-search");
4343
});
44+
45+
it("uses LOCALAPPDATA on win32 when the env var is set", () => {
46+
delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR;
47+
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
48+
const originalLocalAppData = process.env.LOCALAPPDATA;
49+
try {
50+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
51+
process.env.LOCALAPPDATA = "C:\\Users\\user\\AppData\\Local";
52+
const dir = getCacheDir();
53+
// path.join uses the host OS separator in tests (macOS: /), so we just
54+
// assert both components are present rather than hard-coding a separator.
55+
expect(dir).toContain("AppData");
56+
expect(dir).toContain("Local");
57+
expect(dir).toContain("github-code-search");
58+
} finally {
59+
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform);
60+
if (originalLocalAppData !== undefined) process.env.LOCALAPPDATA = originalLocalAppData;
61+
else delete process.env.LOCALAPPDATA;
62+
}
63+
});
64+
65+
it("falls back to ~/AppData/Local on win32 when LOCALAPPDATA is not set", () => {
66+
delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR;
67+
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
68+
const originalLocalAppData = process.env.LOCALAPPDATA;
69+
try {
70+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
71+
delete process.env.LOCALAPPDATA;
72+
const dir = getCacheDir();
73+
expect(dir).toContain("AppData");
74+
expect(dir).toContain("Local");
75+
expect(dir).toContain("github-code-search");
76+
} finally {
77+
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform);
78+
if (originalLocalAppData !== undefined) process.env.LOCALAPPDATA = originalLocalAppData;
79+
}
80+
});
81+
82+
it("uses XDG_CACHE_HOME on linux when set", () => {
83+
delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR;
84+
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
85+
const originalXdg = process.env.XDG_CACHE_HOME;
86+
try {
87+
Object.defineProperty(process, "platform", { value: "linux", configurable: true });
88+
process.env.XDG_CACHE_HOME = "/custom/xdg/cache";
89+
const dir = getCacheDir();
90+
expect(dir).toContain("custom");
91+
expect(dir).toContain("xdg");
92+
expect(dir).toContain("github-code-search");
93+
} finally {
94+
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform);
95+
if (originalXdg !== undefined) process.env.XDG_CACHE_HOME = originalXdg;
96+
else delete process.env.XDG_CACHE_HOME;
97+
}
98+
});
99+
100+
it("falls back to ~/.cache on linux when XDG_CACHE_HOME is not set", () => {
101+
delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR;
102+
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
103+
const originalXdg = process.env.XDG_CACHE_HOME;
104+
try {
105+
Object.defineProperty(process, "platform", { value: "linux", configurable: true });
106+
delete process.env.XDG_CACHE_HOME;
107+
const dir = getCacheDir();
108+
expect(dir).toContain(".cache");
109+
expect(dir).toContain("github-code-search");
110+
} finally {
111+
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform);
112+
if (originalXdg !== undefined) process.env.XDG_CACHE_HOME = originalXdg;
113+
}
114+
});
115+
116+
it("falls back to ~/.github-code-search/cache on unknown platforms", () => {
117+
delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR;
118+
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
119+
try {
120+
Object.defineProperty(process, "platform", { value: "freebsd", configurable: true });
121+
const dir = getCacheDir();
122+
expect(dir).toContain(".github-code-search");
123+
expect(dir).toContain("cache");
124+
} finally {
125+
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform);
126+
}
127+
});
44128
});
45129

46130
// ─── getCacheKey ─────────────────────────────────────────────────────────────

src/cache.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ export const CACHE_TTL_MS = 24 * 60 * 60 * 1_000; // 24 hours
1212

1313
/**
1414
* Returns the OS-appropriate cache directory for the application:
15-
* - macOS → ~/Library/Caches/github-code-search
16-
* - Linux → $XDG_CACHE_HOME/github-code-search (fallback: ~/.cache/github-code-search)
17-
* - Other → ~/.github-code-search/cache
15+
* - macOS → ~/Library/Caches/github-code-search
16+
* - Linux → $XDG_CACHE_HOME/github-code-search (fallback: ~/.cache/github-code-search)
17+
* - Windows → %LOCALAPPDATA%\github-code-search (fallback: ~/AppData/Local/github-code-search)
18+
* - Other → ~/.github-code-search/cache
1819
*
1920
* Override with `GITHUB_CODE_SEARCH_CACHE_DIR` env var (useful in tests and CI).
2021
*/
@@ -31,6 +32,14 @@ export function getCacheDir(): string {
3132
const base = xdg && xdg.trim() !== "" ? xdg : join(homedir(), ".cache");
3233
return join(base, "github-code-search");
3334
}
35+
if (platform === "win32") {
36+
const localAppData = process.env.LOCALAPPDATA;
37+
const base =
38+
localAppData && localAppData.trim() !== ""
39+
? localAppData
40+
: join(homedir(), "AppData", "Local");
41+
return join(base, "github-code-search");
42+
}
3443
return join(homedir(), ".github-code-search", "cache");
3544
}
3645

0 commit comments

Comments
 (0)