Skip to content

Commit 272e6ef

Browse files
test: locate MigrationChecks.targets via directory walk for path robustness
1 parent 830d7bf commit 272e6ef

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

src/ElectronNET.IntegrationTests/Tests/MigrationChecksTargetsTests.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,42 @@ namespace ElectronNET.IntegrationTests.Tests;
99
/// </summary>
1010
public class MigrationChecksTargetsTests
1111
{
12-
// AppContext.BaseDirectory resolves to:
13-
// src\ElectronNET.IntegrationTests\bin\Debug\net10.0\win-x64\
14-
// Five levels up => src\
15-
private static readonly string TargetsFilePath = Path.GetFullPath(
16-
Path.Combine(
17-
AppContext.BaseDirectory,
18-
"..", "..", "..", "..", "..",
19-
"ElectronNET", "build", "ElectronNET.MigrationChecks.targets"));
12+
private static readonly string TargetsFilePath = FindTargetsFile();
13+
14+
/// <summary>
15+
/// Walks up the directory tree from <see cref="AppContext.BaseDirectory"/> until it finds
16+
/// the migration checks targets file. This is robust against varying output paths
17+
/// (with or without RID subfolder, debug/release, etc.).
18+
/// </summary>
19+
private static string FindTargetsFile()
20+
{
21+
const string RelativeFromRepoRoot =
22+
"src/ElectronNET/build/ElectronNET.MigrationChecks.targets";
23+
const string RelativeFromSrc =
24+
"ElectronNET/build/ElectronNET.MigrationChecks.targets";
25+
26+
var dir = new DirectoryInfo(AppContext.BaseDirectory);
27+
while (dir != null)
28+
{
29+
var fromRepoRoot = Path.Combine(dir.FullName, RelativeFromRepoRoot);
30+
if (File.Exists(fromRepoRoot))
31+
{
32+
return Path.GetFullPath(fromRepoRoot);
33+
}
34+
35+
var fromSrc = Path.Combine(dir.FullName, RelativeFromSrc);
36+
if (File.Exists(fromSrc))
37+
{
38+
return Path.GetFullPath(fromSrc);
39+
}
40+
41+
dir = dir.Parent;
42+
}
43+
44+
throw new FileNotFoundException(
45+
"Could not locate ElectronNET.MigrationChecks.targets by walking up from " +
46+
$"'{AppContext.BaseDirectory}'.");
47+
}
2048

2149
// -----------------------------------------------------------------------
2250
// Content-level test (RED before fix, GREEN after fix on ALL platforms)

0 commit comments

Comments
 (0)