Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog.d/unreleased/2951.fixed.md
Original file line number Diff line number Diff line change
@@ -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 に向かなくなりました。
9 changes: 7 additions & 2 deletions src/CodeIndex/Cli/DbPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActiveWorkspaceState?>? activeWorkspaceLoader = null)
{
var fullWorkspacePath = Path.GetFullPath(workspacePath);
if (!string.IsNullOrWhiteSpace(explicitDataDir))
Expand All @@ -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);

Expand Down
51 changes: 48 additions & 3 deletions tests/CodeIndex.Tests/DbPathResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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()
{
Expand All @@ -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);
Expand Down
Loading