@@ -312,3 +312,83 @@ impl SourceMapExtension for SourceMap {
312312 }
313313 }
314314}
315+
316+ fn map_path_prefix ( mapping : & FilePathMapping , path : & str ) -> String {
317+ // It's important that we convert to a string here because that's what
318+ // later stages do too (e.g. in the backend), and comparing `Path` values
319+ // won't catch some differences at the string level, e.g. "abc" and "abc/"
320+ // compare as equal.
321+ mapping. map_prefix ( path. into ( ) ) . 0 . to_string_lossy ( ) . to_string ( )
322+ }
323+
324+ #[ cfg( unix) ]
325+ #[ test]
326+ fn path_prefix_remapping ( ) {
327+ // Relative to relative
328+ {
329+ let mapping = & FilePathMapping :: new ( vec ! [ ( "abc/def" . into( ) , "foo" . into( ) ) ] ) ;
330+
331+ assert_eq ! ( map_path_prefix( mapping, "abc/def/src/main.rs" ) , "foo/src/main.rs" ) ;
332+ assert_eq ! ( map_path_prefix( mapping, "abc/def" ) , "foo" ) ;
333+ }
334+
335+ // Relative to absolute
336+ {
337+ let mapping = & FilePathMapping :: new ( vec ! [ ( "abc/def" . into( ) , "/foo" . into( ) ) ] ) ;
338+
339+ assert_eq ! ( map_path_prefix( mapping, "abc/def/src/main.rs" ) , "/foo/src/main.rs" ) ;
340+ assert_eq ! ( map_path_prefix( mapping, "abc/def" ) , "/foo" ) ;
341+ }
342+
343+ // Absolute to relative
344+ {
345+ let mapping = & FilePathMapping :: new ( vec ! [ ( "/abc/def" . into( ) , "foo" . into( ) ) ] ) ;
346+
347+ assert_eq ! ( map_path_prefix( mapping, "/abc/def/src/main.rs" ) , "foo/src/main.rs" ) ;
348+ assert_eq ! ( map_path_prefix( mapping, "/abc/def" ) , "foo" ) ;
349+ }
350+
351+ // Absolute to absolute
352+ {
353+ let mapping = & FilePathMapping :: new ( vec ! [ ( "/abc/def" . into( ) , "/foo" . into( ) ) ] ) ;
354+
355+ assert_eq ! ( map_path_prefix( mapping, "/abc/def/src/main.rs" ) , "/foo/src/main.rs" ) ;
356+ assert_eq ! ( map_path_prefix( mapping, "/abc/def" ) , "/foo" ) ;
357+ }
358+ }
359+
360+ #[ cfg( windows) ]
361+ #[ test]
362+ fn path_prefix_remapping_from_relative2 ( ) {
363+ // Relative to relative
364+ {
365+ let mapping = & FilePathMapping :: new ( vec ! [ ( "abc\\ def" . into( ) , "foo" . into( ) ) ] ) ;
366+
367+ assert_eq ! ( map_path_prefix( mapping, "abc\\ def\\ src\\ main.rs" ) , "foo\\ src\\ main.rs" ) ;
368+ assert_eq ! ( map_path_prefix( mapping, "abc\\ def" ) , "foo" ) ;
369+ }
370+
371+ // Relative to absolute
372+ {
373+ let mapping = & FilePathMapping :: new ( vec ! [ ( "abc\\ def" . into( ) , "X:\\ foo" . into( ) ) ] ) ;
374+
375+ assert_eq ! ( map_path_prefix( mapping, "abc\\ def\\ src\\ main.rs" ) , "X:\\ foo\\ src\\ main.rs" ) ;
376+ assert_eq ! ( map_path_prefix( mapping, "abc\\ def" ) , "X:\\ foo" ) ;
377+ }
378+
379+ // Absolute to relative
380+ {
381+ let mapping = & FilePathMapping :: new ( vec ! [ ( "X:\\ abc\\ def" . into( ) , "foo" . into( ) ) ] ) ;
382+
383+ assert_eq ! ( map_path_prefix( mapping, "X:\\ abc\\ def\\ src\\ main.rs" ) , "foo\\ src\\ main.rs" ) ;
384+ assert_eq ! ( map_path_prefix( mapping, "X:\\ abc\\ def" ) , "foo" ) ;
385+ }
386+
387+ // Absolute to absolute
388+ {
389+ let mapping = & FilePathMapping :: new ( vec ! [ ( "X:\\ abc\\ def" . into( ) , "X:\\ foo" . into( ) ) ] ) ;
390+
391+ assert_eq ! ( map_path_prefix( mapping, "X:\\ abc\\ def\\ src\\ main.rs" ) , "X:\\ foo\\ src\\ main.rs" ) ;
392+ assert_eq ! ( map_path_prefix( mapping, "X:\\ abc\\ def" ) , "X:\\ foo" ) ;
393+ }
394+ }
0 commit comments