2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . Runtime . CompilerServices ;
5
+ using System . Text . RegularExpressions ;
5
6
6
7
namespace Docfx . Tests ;
7
8
8
9
internal class PathHelper
9
10
{
10
11
public static string GetSolutionFolder ( [ CallerFilePath ] string callerFilePath = "" )
11
12
{
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 ) ;
20
14
21
15
if ( ! File . Exists ( callerFilePath ) )
22
16
{
23
17
// CallerFilePath is resolved at build timing.
24
18
// 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 : { path } ") ;
26
20
}
27
21
28
22
return FindSolutionFolder ( callerFilePath , "docfx" ) ;
@@ -58,31 +52,26 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
58
52
var dir = GetTestDataDirectory ( callerFilePath ) ;
59
53
60
54
var resultPath = Path . Combine ( dir , path ) ;
61
- if ( ! File . Exists ( callerFilePath ) && ! Directory . Exists ( callerFilePath ) )
55
+ if ( ! File . Exists ( resultPath ) && ! Directory . Exists ( resultPath ) )
62
56
{
63
57
throw new FileNotFoundException ( $ "Specified TestData file/directory is not found. path: { resultPath } ") ;
64
58
}
65
59
66
- return Path . GetFullPath ( resultPath ) ;
60
+ return Path . GetFullPath ( path ) ;
67
61
}
68
62
69
63
/// <summary>
70
64
/// Find TestData from callerFilePath.
71
65
/// </summary>
72
66
public static string GetTestDataDirectory ( [ CallerFilePath ] string callerFilePath = "" )
73
67
{
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 ) ;
80
69
81
70
if ( ! File . Exists ( callerFilePath ) )
82
71
{
83
72
// CallerFilePath is resolved at build timing.
84
73
// 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 } ") ;
86
75
}
87
76
88
77
// Find closest `TestData` directory.
@@ -105,4 +94,30 @@ public static string GetTestDataDirectory([CallerFilePath] string callerFilePath
105
94
106
95
return dir . FullName ;
107
96
}
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
+ }
108
123
}
0 commit comments