Skip to content

Commit 6fa85d8

Browse files
Fix GoldenFile.GetSnapshotPath under ContinuousIntegrationBuild (#61)
Azure Pipelines' build step applies /p:ContinuousIntegrationBuild=true to every project (src/**/*.csproj), which makes Roslyn remap the [CallerFilePath] literal compiled into GoldenFile to a deterministic placeholder like "/_/src/Dax.Template.Tests/...". That path never exists on disk, so walking up from it to find the test project's .csproj always bottomed out at null and threw. Fall back to AppContext.BaseDirectory (the real runtime output directory, unaffected by compile-time path mapping) when the caller-path walk doesn't resolve.
1 parent 7466db2 commit 6fa85d8

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/Dax.Template.Tests/Infrastructure/GoldenFile.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,29 @@ private static string GetSnapshotPath(string name, string extension, string call
6363
// callerFilePath points at the calling test's source file, which lives somewhere under the test
6464
// project. Walk up to the directory that holds the .csproj so snapshots are committed next to the
6565
// source and located independently of the build output directory or the caller's subfolder depth.
66-
var dir = Path.GetDirectoryName(callerFilePath);
67-
while (dir != null && Directory.GetFiles(dir, "*.csproj").Length == 0)
68-
{
69-
dir = Path.GetDirectoryName(dir);
70-
}
66+
// Under a ContinuousIntegrationBuild (e.g. the Azure Pipelines build step, which applies
67+
// /p:ContinuousIntegrationBuild=true to every project), Roslyn's deterministic source-path mapping
68+
// rewrites the [CallerFilePath] literal to a placeholder like "/_/src/...", which never exists on
69+
// disk. AppContext.BaseDirectory is the test assembly's real runtime output directory and is
70+
// unaffected by that compile-time remapping, so fall back to it when the caller path doesn't resolve.
71+
var dir = FindProjectDirectory(Path.GetDirectoryName(callerFilePath))
72+
?? FindProjectDirectory(AppContext.BaseDirectory);
7173
if (dir == null)
7274
{
7375
throw new InvalidOperationException(
74-
$"Could not locate the test project directory (.csproj) from caller path '{callerFilePath}'.");
76+
$"Could not locate the test project directory (.csproj) from caller path '{callerFilePath}' or base directory '{AppContext.BaseDirectory}'.");
7577
}
7678
return Path.Combine(dir, "_data", "Golden", name + "." + extension);
7779
}
80+
81+
private static string? FindProjectDirectory(string? startDirectory)
82+
{
83+
var dir = startDirectory;
84+
while (dir != null && Directory.Exists(dir) && Directory.GetFiles(dir, "*.csproj").Length == 0)
85+
{
86+
dir = Path.GetDirectoryName(dir);
87+
}
88+
return dir != null && Directory.Exists(dir) && Directory.GetFiles(dir, "*.csproj").Length > 0 ? dir : null;
89+
}
7890
}
7991
}

0 commit comments

Comments
 (0)