-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Map local build path during WSL run (#729)
- Loading branch information
1 parent
ced30c2
commit 3aefe4c
Showing
12 changed files
with
217 additions
and
15 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
class VirtualizedRunHelper | ||
{ | ||
// e.g. WSL or docker run (https://github.com/VerifyTests/Verify#unit-testing-inside-virtualized-environment) | ||
readonly bool appearsToBeLocalVirtualizedRun; | ||
readonly string originalCodeBaseRootAbsolute = string.Empty; | ||
readonly string mappedCodeBaseRootAbsolute = string.Empty; | ||
static readonly char[] Separators = { '\\', '/' }; | ||
|
||
public VirtualizedRunHelper(Assembly userAssembly) | ||
{ | ||
var originalCodeBaseRoot = AttributeReader.TryGetSolutionDirectory(userAssembly, false, out var solutionDir) | ||
? solutionDir | ||
: AttributeReader.GetProjectDirectory(userAssembly); | ||
var appearsToBeBuiltOnDifferentPlatform = | ||
!string.IsNullOrEmpty(originalCodeBaseRoot) && | ||
!originalCodeBaseRoot.Contains(Path.DirectorySeparatorChar) && | ||
originalCodeBaseRoot.Contains("\\"); | ||
|
||
if (!appearsToBeBuiltOnDifferentPlatform) | ||
{ | ||
return; | ||
} | ||
|
||
string currentDir = Environment.CurrentDirectory; | ||
string currentDirRelativeToAppRoot = currentDir.TrimStart(Separators); | ||
|
||
// WSL paths mount to /mnt/<drive>/... | ||
// docker testing mounts to /mnt/approot/... | ||
if (!TryRemoveDirFromStartOfPath(ref currentDirRelativeToAppRoot) || | ||
!TryRemoveDirFromStartOfPath(ref currentDirRelativeToAppRoot)) | ||
{ | ||
return; | ||
} | ||
|
||
//remove the drive info from the code root | ||
var mappedCodeBaseRootRelative = originalCodeBaseRoot.Replace('\\', '/'); | ||
if (!TryRemoveDirFromStartOfPath(ref mappedCodeBaseRootRelative)) | ||
{ | ||
return; | ||
} | ||
|
||
// Move through the code base dir and try to see if it seems to be basePath for currentDir | ||
while (!currentDirRelativeToAppRoot.StartsWith(mappedCodeBaseRootRelative, StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
//no more dirs in code base path - no match found - bail out. | ||
if (!TryRemoveDirFromStartOfPath(ref mappedCodeBaseRootRelative)) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
mappedCodeBaseRootAbsolute = currentDir[..^currentDirRelativeToAppRoot.Length]; | ||
|
||
// the start of paths to be mapped | ||
originalCodeBaseRootAbsolute = originalCodeBaseRoot[..^mappedCodeBaseRootRelative.Length]; | ||
|
||
var testMappedPath = Path.Combine( | ||
mappedCodeBaseRootAbsolute, | ||
originalCodeBaseRoot[originalCodeBaseRootAbsolute.Length..].Replace('\\', '/')); | ||
|
||
if (PathExists(testMappedPath)) | ||
{ | ||
appearsToBeLocalVirtualizedRun = true; | ||
} | ||
} | ||
|
||
public string? GetMappedBuildPath(string? path) | ||
{ | ||
if (path == null || | ||
string.IsNullOrEmpty(path) || | ||
!appearsToBeLocalVirtualizedRun) | ||
{ | ||
return path; | ||
} | ||
|
||
if (!path.StartsWith(originalCodeBaseRootAbsolute, StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
return path; | ||
} | ||
|
||
var mappedPathRelative = path[originalCodeBaseRootAbsolute.Length..].Replace('\\', '/'); | ||
|
||
var mappedPath = Path.Combine(mappedCodeBaseRootAbsolute, mappedPathRelative); | ||
|
||
if (PathExists(mappedPath)) | ||
{ | ||
return mappedPath; | ||
} | ||
|
||
return path; | ||
} | ||
|
||
static bool TryRemoveDirFromStartOfPath(ref string path) | ||
{ | ||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
return false; | ||
} | ||
|
||
path = path.TrimStart(Separators); | ||
|
||
int nextSeparatorIdx = path.IndexOfAny(Separators); | ||
if (nextSeparatorIdx <= 0 || nextSeparatorIdx == path.Length - 1) | ||
{ | ||
return false; | ||
} | ||
|
||
path = path[(nextSeparatorIdx + 1)..]; | ||
|
||
return !string.IsNullOrWhiteSpace(path); | ||
} | ||
|
||
static bool PathExists(string path) => | ||
File.Exists(path) || Directory.Exists(path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"version": "1", | ||
"environments": [ | ||
{ | ||
"name": "WSL-Ubuntu", | ||
"type": "wsl", | ||
"wslDistribution": "Ubuntu" | ||
}, | ||
{ | ||
"name": "docker dotnet 7.0", | ||
"type": "docker", | ||
"dockerImage": "mcr.microsoft.com/dotnet/sdk:7.0" | ||
} | ||
] | ||
} |