5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
- import type * as path from 'path' ;
8
+ import * as path from 'path' ;
9
9
import {
10
10
TestPathPatterns ,
11
11
TestPathPatternsExecutor ,
12
12
type TestPathPatternsExecutorOptions ,
13
13
} from '../TestPathPatterns' ;
14
14
15
15
const mockSep : jest . Mock < ( ) => string > = jest . fn ( ) ;
16
+ const mockIsAbsolute : jest . Mock < ( p : string ) => boolean > = jest . fn ( ) ;
17
+ const mockRelative : jest . Mock < ( from : string , to : string ) => string > = jest . fn ( ) ;
16
18
jest . mock ( 'path' , ( ) => {
19
+ const actualPath = jest . requireActual ( 'path' ) ;
17
20
return {
18
- ...jest . requireActual ( 'path' ) ,
21
+ ...actualPath ,
22
+ isAbsolute ( p ) {
23
+ return mockIsAbsolute ( p ) || actualPath . isAbsolute ( p ) ;
24
+ } ,
25
+ relative ( from , to ) {
26
+ return mockRelative ( from , to ) || actualPath . relative ( from , to ) ;
27
+ } ,
19
28
get sep ( ) {
20
- return mockSep ( ) || '/' ;
29
+ return mockSep ( ) || actualPath . sep ;
21
30
} ,
22
31
} as typeof path ;
23
32
} ) ;
33
+ const forcePosix = ( ) => {
34
+ mockSep . mockReturnValue ( path . posix . sep ) ;
35
+ mockIsAbsolute . mockImplementation ( path . posix . isAbsolute ) ;
36
+ mockRelative . mockImplementation ( path . posix . relative ) ;
37
+ } ;
38
+ const forceWindows = ( ) => {
39
+ mockSep . mockReturnValue ( path . win32 . sep ) ;
40
+ mockIsAbsolute . mockImplementation ( path . win32 . isAbsolute ) ;
41
+ mockRelative . mockImplementation ( path . win32 . relative ) ;
42
+ } ;
24
43
beforeEach ( ( ) => {
25
44
jest . resetAllMocks ( ) ;
45
+ forcePosix ( ) ;
26
46
} ) ;
27
47
28
48
const config = { rootDir : '' } ;
@@ -124,6 +144,22 @@ describe('TestPathPatternsExecutor', () => {
124
144
expect ( testPathPatterns . isMatch ( '/a/b/c' ) ) . toBe ( true ) ;
125
145
} ) ;
126
146
147
+ it ( 'returns true for explicit relative path for Windows with ./' , ( ) => {
148
+ forceWindows ( ) ;
149
+ const testPathPatterns = makeExecutor ( [ './b/c' ] , {
150
+ rootDir : 'C:\\a' ,
151
+ } ) ;
152
+ expect ( testPathPatterns . isMatch ( 'C:\\a\\b\\c' ) ) . toBe ( true ) ;
153
+ } ) ;
154
+
155
+ it ( 'returns true for explicit relative path for Windows with .\\' , ( ) => {
156
+ forceWindows ( ) ;
157
+ const testPathPatterns = makeExecutor ( [ '.\\b\\c' ] , {
158
+ rootDir : 'C:\\a' ,
159
+ } ) ;
160
+ expect ( testPathPatterns . isMatch ( 'C:\\a\\b\\c' ) ) . toBe ( true ) ;
161
+ } ) ;
162
+
127
163
it ( 'returns true for partial file match' , ( ) => {
128
164
const testPathPatterns = makeExecutor ( [ 'aaa' ] , config ) ;
129
165
expect ( testPathPatterns . isMatch ( '/foo/..aaa..' ) ) . toBe ( true ) ;
@@ -158,12 +194,21 @@ describe('TestPathPatternsExecutor', () => {
158
194
} ) ;
159
195
160
196
it ( 'matches absolute paths regardless of rootDir' , ( ) => {
197
+ forcePosix ( ) ;
161
198
const testPathPatterns = makeExecutor ( [ '/a/b' ] , {
162
199
rootDir : '/foo/bar' ,
163
200
} ) ;
164
201
expect ( testPathPatterns . isMatch ( '/a/b' ) ) . toBe ( true ) ;
165
202
} ) ;
166
203
204
+ it ( 'matches absolute paths for Windows' , ( ) => {
205
+ forceWindows ( ) ;
206
+ const testPathPatterns = makeExecutor ( [ 'C:\\a\\b' ] , {
207
+ rootDir : 'C:\\foo\\bar' ,
208
+ } ) ;
209
+ expect ( testPathPatterns . isMatch ( 'C:\\a\\b' ) ) . toBe ( true ) ;
210
+ } ) ;
211
+
167
212
it ( 'returns true if match any paths' , ( ) => {
168
213
const testPathPatterns = makeExecutor ( [ 'a/b' , 'c/d' ] , config ) ;
169
214
@@ -175,15 +220,15 @@ describe('TestPathPatternsExecutor', () => {
175
220
} ) ;
176
221
177
222
it ( 'does not normalize Windows paths on POSIX' , ( ) => {
178
- mockSep . mockReturnValue ( '/' ) ;
223
+ forcePosix ( ) ;
179
224
const testPathPatterns = makeExecutor ( [ 'a\\z' , 'a\\\\z' ] , config ) ;
180
225
expect ( testPathPatterns . isMatch ( '/foo/a/z' ) ) . toBe ( false ) ;
181
226
} ) ;
182
227
183
228
it ( 'normalizes paths for Windows' , ( ) => {
184
- mockSep . mockReturnValue ( '\\' ) ;
229
+ forceWindows ( ) ;
185
230
const testPathPatterns = makeExecutor ( [ 'a/b' ] , config ) ;
186
- expect ( testPathPatterns . isMatch ( '\\foo\\a\\b' ) ) . toBe ( true ) ;
231
+ expect ( testPathPatterns . isMatch ( 'C: \\foo\\a\\b' ) ) . toBe ( true ) ;
187
232
} ) ;
188
233
} ) ;
189
234
} ) ;
0 commit comments