diff --git a/changelog.d/unreleased/2951.fixed.md b/changelog.d/unreleased/2951.fixed.md new file mode 100644 index 0000000000..3ec9306675 --- /dev/null +++ b/changelog.d/unreleased/2951.fixed.md @@ -0,0 +1,16 @@ +--- +category: fixed +issues: + - 2951 +affected: + - src/CodeIndex/Cli/DbPathResolver.cs + - tests/CodeIndex.Tests/DbPathResolverTests.cs +--- + +## English + +- **DbPathResolver query tests now isolate active workspace state (#2951)** — internal query data-dir resolution tests can inject active-workspace state explicitly, so ambient user config no longer redirects the temporary project roots under test. + +## 日本語 + +- **DbPathResolver の query test が active workspace state を隔離するようになりました (#2951)** — internal query data-dir resolution test が active-workspace state を明示的に注入できるようになり、ambient なユーザー設定でテスト用の一時 project root が別 DB に向かなくなりました。 diff --git a/src/CodeIndex/Cli/DbPathResolver.cs b/src/CodeIndex/Cli/DbPathResolver.cs index 92ccc6531e..1124e54045 100644 --- a/src/CodeIndex/Cli/DbPathResolver.cs +++ b/src/CodeIndex/Cli/DbPathResolver.cs @@ -60,7 +60,12 @@ internal static DbPathResolution ResolveDataDir(string workspacePath, string? ex return BuildDataDirResolution(Path.Combine(fullWorkspacePath, ".cdidx"), DataDirSourceWorkspace); } - internal static DbPathResolution ResolveDataDirForQuery(string workspacePath, string? explicitDataDir, string? environmentDataDir, string? xdgDataHome) + internal static DbPathResolution ResolveDataDirForQuery( + string workspacePath, + string? explicitDataDir, + string? environmentDataDir, + string? xdgDataHome, + Func? activeWorkspaceLoader = null) { var fullWorkspacePath = Path.GetFullPath(workspacePath); if (!string.IsNullOrWhiteSpace(explicitDataDir)) @@ -69,7 +74,7 @@ internal static DbPathResolution ResolveDataDirForQuery(string workspacePath, st if (!string.IsNullOrWhiteSpace(environmentDataDir)) return BuildDataDirResolution(environmentDataDir, DataDirSourceEnv); - var active = ActiveWorkspace.Load(); + var active = (activeWorkspaceLoader ?? ActiveWorkspace.Load)(); if (active != null) return new DbPathResolution(active.DbPath, Path.GetDirectoryName(active.DbPath), DataDirSourceActiveWorkspace); diff --git a/tests/CodeIndex.Tests/DbPathResolverTests.cs b/tests/CodeIndex.Tests/DbPathResolverTests.cs index 7e324c1629..69ac383f52 100644 --- a/tests/CodeIndex.Tests/DbPathResolverTests.cs +++ b/tests/CodeIndex.Tests/DbPathResolverTests.cs @@ -89,7 +89,12 @@ public void ResolveDataDirForQuery_WithXdgPrefersAncestorWorkspaceDataDir() var indexedRootResolution = DbPathResolver.ResolveDataDir(projectRoot, explicitDataDir: null, environmentDataDir: null, xdgDataHome: xdgDir); Directory.CreateDirectory(indexedRootResolution.DataDir!); - var resolved = DbPathResolver.ResolveDataDirForQuery(child, explicitDataDir: null, environmentDataDir: null, xdgDataHome: xdgDir); + var resolved = DbPathResolver.ResolveDataDirForQuery( + child, + explicitDataDir: null, + environmentDataDir: null, + xdgDataHome: xdgDir, + activeWorkspaceLoader: () => null); Assert.Equal(indexedRootResolution.DbPath, resolved.DbPath); Assert.Equal(indexedRootResolution.DataDir, resolved.DataDir); @@ -113,7 +118,12 @@ public void ResolveDataDirForQuery_PrefersOutermostAncestorCdidx() Directory.CreateDirectory(Path.Combine(projectRoot, ".cdidx")); Directory.CreateDirectory(Path.Combine(projectRoot, "src", ".cdidx")); - var resolved = DbPathResolver.ResolveDataDirForQuery(child, explicitDataDir: null, environmentDataDir: null, xdgDataHome: null); + var resolved = DbPathResolver.ResolveDataDirForQuery( + child, + explicitDataDir: null, + environmentDataDir: null, + xdgDataHome: null, + activeWorkspaceLoader: () => null); Assert.Equal(Path.Combine(projectRoot, ".cdidx", "codeindex.db"), resolved.DbPath); Assert.Equal(DbPathResolver.DataDirSourceWorkspace, resolved.DataDirSource); @@ -124,6 +134,36 @@ public void ResolveDataDirForQuery_PrefersOutermostAncestorCdidx() } } + [Fact] + public void ResolveDataDirForQuery_UsesInjectedActiveWorkspaceBeforeAncestorCdidx() + { + var projectRoot = TestProjectHelper.CreateTempProject("cdidx_query_active_workspace_project"); + var activeRoot = TestProjectHelper.CreateTempProject("cdidx_query_active_workspace_state"); + var activeDb = Path.Combine(activeRoot, ".cdidx", "codeindex.db"); + try + { + var child = Path.Combine(projectRoot, "src", "App"); + Directory.CreateDirectory(child); + Directory.CreateDirectory(Path.Combine(projectRoot, ".cdidx")); + + var resolved = DbPathResolver.ResolveDataDirForQuery( + child, + explicitDataDir: null, + environmentDataDir: null, + xdgDataHome: null, + activeWorkspaceLoader: () => new ActiveWorkspaceState("test", activeRoot, activeDb)); + + Assert.Equal(Path.GetFullPath(activeDb), resolved.DbPath); + Assert.Equal(Path.GetDirectoryName(Path.GetFullPath(activeDb)), resolved.DataDir); + Assert.Equal(DbPathResolver.DataDirSourceActiveWorkspace, resolved.DataDirSource); + } + finally + { + TestProjectHelper.DeleteDirectory(projectRoot); + TestProjectHelper.DeleteDirectory(activeRoot); + } + } + [Fact] public void ResolveDataDirForQuery_FallsBackToCurrentDirectoryWhenNoAncestorCdidxExists() { @@ -133,7 +173,12 @@ public void ResolveDataDirForQuery_FallsBackToCurrentDirectoryWhenNoAncestorCdid var child = Path.Combine(projectRoot, "src", "App"); Directory.CreateDirectory(child); - var resolved = DbPathResolver.ResolveDataDirForQuery(child, explicitDataDir: null, environmentDataDir: null, xdgDataHome: null); + var resolved = DbPathResolver.ResolveDataDirForQuery( + child, + explicitDataDir: null, + environmentDataDir: null, + xdgDataHome: null, + activeWorkspaceLoader: () => null); Assert.Equal(Path.Combine(child, ".cdidx", "codeindex.db"), resolved.DbPath); Assert.Equal(DbPathResolver.DataDirSourceWorkspace, resolved.DataDirSource);