fix(test): isolate discovery tests from global env var contamination

The discovery.test.ts was using process.env.CLAUDE_PLUGINS_HOME to set
the plugins directory, but this global state could be affected by other
tests running in parallel, causing flaky failures with errors like:
  Expected: "oh-my-openagent"
  Received: "demo"

Changes:
- Added pluginsHomeOverride option to PluginLoaderOptions type
- Modified discoverInstalledPlugins to accept optional pluginsHomeOverride
- Modified loadInstalledPlugins to accept optional pluginsBaseDir
- Updated all 3 discovery tests to use pluginsHomeOverride instead of
  relying on global process.env.CLAUDE_PLUGINS_HOME

This makes the tests properly isolated and deterministic regardless of
test execution order or parallelization.

Fixes CI failure on dev branch.
This commit is contained in:
YeonGyu-Kim
2026-04-05 11:34:24 +09:00
parent 3517017ee1
commit 23582ea9a5
3 changed files with 16 additions and 8 deletions
@@ -59,7 +59,7 @@ describe("discoverInstalledPlugins", () => {
) )
//#when //#when
const discovered = discoverInstalledPlugins() const discovered = discoverInstalledPlugins({ pluginsHomeOverride: pluginsHome })
//#then //#then
expect(discovered.errors).toHaveLength(0) expect(discovered.errors).toHaveLength(0)
@@ -94,7 +94,7 @@ describe("discoverInstalledPlugins", () => {
) )
//#when //#when
const discovered = discoverInstalledPlugins() const discovered = discoverInstalledPlugins({ pluginsHomeOverride: pluginsHome })
//#then //#then
expect(discovered.errors).toHaveLength(0) expect(discovered.errors).toHaveLength(0)
@@ -129,7 +129,7 @@ describe("discoverInstalledPlugins", () => {
) )
//#when //#when
const discovered = discoverInstalledPlugins() const discovered = discoverInstalledPlugins({ pluginsHomeOverride: pluginsHome })
//#then //#then
expect(discovered.errors).toHaveLength(0) expect(discovered.errors).toHaveLength(0)
@@ -23,12 +23,12 @@ function getPluginsBaseDir(): string {
return join(homedir(), ".claude", "plugins") return join(homedir(), ".claude", "plugins")
} }
function getInstalledPluginsPath(): string { function getInstalledPluginsPath(pluginsBaseDir?: string): string {
return join(getPluginsBaseDir(), "installed_plugins.json") return join(pluginsBaseDir ?? getPluginsBaseDir(), "installed_plugins.json")
} }
function loadInstalledPlugins(): InstalledPluginsDatabase | null { function loadInstalledPlugins(pluginsBaseDir?: string): InstalledPluginsDatabase | null {
const dbPath = getInstalledPluginsPath() const dbPath = getInstalledPluginsPath(pluginsBaseDir)
if (!existsSync(dbPath)) { if (!existsSync(dbPath)) {
return null return null
} }
@@ -163,7 +163,9 @@ function extractPluginEntries(
} }
export function discoverInstalledPlugins(options?: PluginLoaderOptions): PluginLoadResult { export function discoverInstalledPlugins(options?: PluginLoaderOptions): PluginLoadResult {
const db = loadInstalledPlugins() // Allow overriding the plugins base directory for testing
const pluginsBaseDir = options?.pluginsHomeOverride ?? getPluginsBaseDir()
const db = loadInstalledPlugins(pluginsBaseDir)
const settings = loadClaudeSettings() const settings = loadClaudeSettings()
const plugins: LoadedPlugin[] = [] const plugins: LoadedPlugin[] = []
const errors: PluginLoadError[] = [] const errors: PluginLoadError[] = []
@@ -221,6 +221,12 @@ export interface ClaudeSettings {
* Plugin loader options * Plugin loader options
*/ */
export interface PluginLoaderOptions { export interface PluginLoaderOptions {
/**
* Override the plugins home directory for testing.
* If not provided, uses CLAUDE_PLUGINS_HOME env var or ~/.claude/plugins
*/
pluginsHomeOverride?: string
/** /**
* Override enabled plugins from oh-my-opencode config. * Override enabled plugins from oh-my-opencode config.
* Key format: "pluginName@marketplace" (e.g., "shell-scripting@claude-code-workflows") * Key format: "pluginName@marketplace" (e.g., "shell-scripting@claude-code-workflows")