Skip to content

Commit

Permalink
fix: Issue with report with Linux file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles authored Jun 2, 2024
1 parent 5a8d15d commit c29f356
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
41 changes: 21 additions & 20 deletions src/StaticSettingsTests/VirtualizedRunHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ public void Built_on_windows_run_on_linux_wsl()
Assert.Equal("/mnt/c/build/project_dir/src/file.cs", helper.GetMappedBuildPath(@"C:\build\project_dir\src\file.cs"));
}

[Fact]
public void Built_on_linux_wsl_run_on_windows()
{
VirtualizedRunHelper.Env = new TestEnv(
_ => _ switch
{
@"C:\build\project_dir" => true,
@"C:\build\project_dir\src\file.cs" => true,
_ => false
},
@"C:\build\project_dir\subdir", '\\');

var helper = new VirtualizedRunHelper("/mnt/c/build/project_dir", string.Empty);

Assert.True(helper.AppearsToBeLocalVirtualizedRun);
Assert.True(helper.Initialized);

Assert.Equal(@"C:\build\project_dir\src\file.cs", helper.GetMappedBuildPath("/mnt/c/build/project_dir/src/file.cs"));
}

[Fact]
public void Built_on_windows_run_on_linux_ci()
{
Expand Down Expand Up @@ -96,26 +116,7 @@ class TestEnv(Func<string, bool> exists, string currentDirectory, char directory
public string CurrentDirectory { get; } = currentDirectory;
public char DirectorySeparatorChar { get; } = directorySeparatorChar;

public char AltDirectorySeparatorChar
{
get
{
if (DirectorySeparatorChar == '/')
{
return '\\';
}

return '/';
}
}

public bool PathExists(string path) =>
exists(path) ||
exists(path.Replace('\\', '/')) ||
exists(path.Replace('/', '\\'));

// Make sure Path.Combine behaves as on Unix, even when executed on Windows
public string CombinePaths(string path1, string path2) =>
Path.Combine(path1.TrimEnd('\\', '/') + '/', path2);
exists(path);
}
}
2 changes: 0 additions & 2 deletions src/Verify/IEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ interface IEnvironment
{
string CurrentDirectory { get; }
char DirectorySeparatorChar { get; }
char AltDirectorySeparatorChar { get; }
bool PathExists(string path);
string CombinePaths(string path1, string path2);
}
2 changes: 0 additions & 2 deletions src/Verify/PhysicalEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ class PhysicalEnvironment : IEnvironment

public string CurrentDirectory => Environment.CurrentDirectory;
public char DirectorySeparatorChar => Path.DirectorySeparatorChar;
public char AltDirectorySeparatorChar => Path.AltDirectorySeparatorChar;
public bool PathExists(string path) => File.Exists(path) || Directory.Exists(path);
public string CombinePaths(string path1, string path2) => Path.Combine(path1, path2);
}
16 changes: 10 additions & 6 deletions src/Verify/VirtualizedRunHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ public string GetMappedBuildPath(string path)
return path;
}

var mappedPathRelative = path[originalCodeBaseRootAbsolute.Length..]
.Replace('\\', '/');
var mappedPathRelative = path[originalCodeBaseRootAbsolute.Length..];

var mappedPath = Env.CombinePaths(mappedCodeBaseRootAbsolute!, mappedPathRelative);
var mappedPath = CombinePaths(mappedCodeBaseRootAbsolute!, mappedPathRelative);

if (Env.PathExists(mappedPath))
{
Expand Down Expand Up @@ -128,7 +127,7 @@ static bool TryGetRelative(
var currentDir = Env.CurrentDirectory;
do
{
var testMappedPath = Env.CombinePaths(currentDir, buildTimePathRelative);
var testMappedPath = CombinePaths(currentDir, buildTimePathRelative);
if (Env.PathExists(testMappedPath))
{
baseRootAbsolute = buildTimePath[..^buildTimePathRelative.Length];
Expand Down Expand Up @@ -176,7 +175,7 @@ static bool TryFindByCrossSectionOfBuildRunPath(

codeBaseRootAbsolute = originalCodeBaseRoot[..^mappedCodeBaseRootRelative.Length];

var testMappedPath = Env.CombinePaths(
var testMappedPath = CombinePaths(
mappedCodeBaseRootAbsolute,
originalCodeBaseRoot[codeBaseRootAbsolute.Length..]
.Replace('\\', '/'));
Expand Down Expand Up @@ -218,7 +217,7 @@ static string GetBuildTimePathRelative(string? originalCodeBaseRoot, string buil

static bool AppearsBuiltOnCurrentPlatform(string buildTimePath) =>
buildTimePath.Contains(Env.DirectorySeparatorChar) &&
!buildTimePath.Contains(Env.AltDirectorySeparatorChar);
!buildTimePath.Contains(OtherDirectorySeparatorChar);

static bool TryRemoveDirFromStartOfPath(ref string path)
{
Expand Down Expand Up @@ -255,4 +254,9 @@ static string TryRemoveDirFromEndOfPath(string path)

return path;
}

private static string CombinePaths(string path1, string path2) =>
$"{path1.TrimEnd(separators)}{Env.DirectorySeparatorChar}{path2.Replace(OtherDirectorySeparatorChar, Env.DirectorySeparatorChar)}";

private static char OtherDirectorySeparatorChar => Env.DirectorySeparatorChar == '/' ? '\\' : '/';
}

0 comments on commit c29f356

Please sign in to comment.