22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Runtime . CompilerServices ;
5+ using System . Text . RegularExpressions ;
56
67namespace Docfx . Tests ;
78
89internal class PathHelper
910{
1011 public static string GetSolutionFolder ( [ CallerFilePath ] string callerFilePath = "" )
1112 {
12- if ( callerFilePath . StartsWith ( "/_/" ) )
13- {
14- // PathMap is rewritten on CI environment (`ContinuousIntegrationBuild=true`).
15- // So try to get workspace folder from GitHub Action environment variable.
16- var workspace = Environment . GetEnvironmentVariable ( "GITHUB_WORKSPACE" ) ;
17- if ( workspace != null )
18- return workspace ;
19- }
13+ callerFilePath = NormalizeCallerFilePath ( callerFilePath ) ;
2014
2115 if ( ! File . Exists ( callerFilePath ) )
2216 {
2317 // CallerFilePath is resolved at build timing.
2418 // If build/test is executed on separated machine. It failed to find file.
25- throw new FileNotFoundException ( $ "File is not found. callerFilePath : { callerFilePath } ") ;
19+ throw new FileNotFoundException ( $ "File is not found. path : { callerFilePath } ") ;
2620 }
2721
2822 return FindSolutionFolder ( callerFilePath , "docfx" ) ;
@@ -58,7 +52,7 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
5852 var dir = GetTestDataDirectory ( callerFilePath ) ;
5953
6054 var resultPath = Path . Combine ( dir , path ) ;
61- if ( ! File . Exists ( callerFilePath ) && ! Directory . Exists ( callerFilePath ) )
55+ if ( ! File . Exists ( resultPath ) && ! Directory . Exists ( resultPath ) )
6256 {
6357 throw new FileNotFoundException ( $ "Specified TestData file/directory is not found. path: { resultPath } ") ;
6458 }
@@ -71,18 +65,13 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
7165 /// </summary>
7266 public static string GetTestDataDirectory ( [ CallerFilePath ] string callerFilePath = "" )
7367 {
74- if ( callerFilePath . StartsWith ( "/_/" ) )
75- {
76- var workspace = Environment . GetEnvironmentVariable ( "GITHUB_WORKSPACE" ) ;
77- if ( workspace != null )
78- callerFilePath = callerFilePath . Replace ( "/_/" , workspace ) ;
79- }
68+ callerFilePath = NormalizeCallerFilePath ( callerFilePath ) ;
8069
8170 if ( ! File . Exists ( callerFilePath ) )
8271 {
8372 // CallerFilePath is resolved at build timing.
8473 // If build/test is executed on separated machine. It failed to find file.
85- throw new FileNotFoundException ( $ "File is not found. callerFilePath : { callerFilePath } ") ;
74+ throw new FileNotFoundException ( $ "File is not found. path : { callerFilePath } ") ;
8675 }
8776
8877 // Find closest `TestData` directory.
@@ -105,4 +94,30 @@ public static string GetTestDataDirectory([CallerFilePath] string callerFilePath
10594
10695 return dir . FullName ;
10796 }
97+
98+ private static string NormalizeCallerFilePath ( string callerFilePath )
99+ {
100+ // PathMap is rewritten on CI environment (`ContinuousIntegrationBuild=true`).
101+ // So try to get workspace folder from GitHub Action environment variable.
102+ if ( callerFilePath . StartsWith ( "/_/" ) )
103+ {
104+ var workspace = Environment . GetEnvironmentVariable ( "GITHUB_WORKSPACE" ) ;
105+ if ( workspace != null )
106+ return workspace ;
107+ }
108+
109+ // Rewrite path when test runnign on WSL environment that are executed by Visual Studio Remote Testing.
110+ if ( Environment . GetEnvironmentVariable ( "WSLENV" ) != null && callerFilePath . Contains ( '\\ ' ) )
111+ {
112+ var match = Regex . Match ( callerFilePath , @"^([a-zA-Z]):\\(.+)$" ) ;
113+ if ( match . Success )
114+ {
115+ var driveLetter = match . Groups [ 1 ] . Value . ToLowerInvariant ( ) ;
116+ var path = match . Groups [ 2 ] . Value . Replace ( '\\ ' , '/' ) ;
117+ return $ "/mnt/{ driveLetter } /{ path } ";
118+ }
119+ }
120+
121+ return callerFilePath ;
122+ }
108123}
0 commit comments