File tree Expand file tree Collapse file tree 2 files changed +12
-12
lines changed Expand file tree Collapse file tree 2 files changed +12
-12
lines changed Original file line number Diff line number Diff line change @@ -640,17 +640,19 @@ function is_testsetup_file(filepath)
640640 )
641641end
642642
643- # like `relpath` but assumes `path` is nested under `startdir`, else just returns `path`
644- function nestedrelpath (path, startdir)
645- path == startdir && return " ."
643+ # Like `relpath` but assumes `path` is nested under `startdir`, else just returns `path`.
644+ # Always returns a `SubString` to be type-stable.
645+ function nestedrelpath (path:: T , startdir:: AbstractString ) where {T <: AbstractString }
646+ path == startdir && return SubString {T} (" ." )
646647 relp = chopprefix (path, startdir)
648+ relp == path && return relp
647649 sep = Base. Filesystem. path_separator
648650 if endswith (startdir, sep)
649651 return relp
650652 elseif startswith (relp, sep)
651653 return chopprefix (relp, sep)
652- else
653- return path
654+ else # `startdir` was a prefix of `path` but not a directory
655+ return SubString {T} ( path)
654656 end
655657end
656658
Original file line number Diff line number Diff line change @@ -395,13 +395,11 @@ end
395395 @test nestedrelpath (path, " test/dir/other" ) == " test/dir/foo_test.jl"
396396 @test nestedrelpath (path, " test/dir/other/bar_test.jl" ) == " test/dir/foo_test.jl"
397397
398- @static if isdefined (Base, Symbol (" @allocations" )) # added in Julia v1.9
399- @test 2 >= @allocations (nestedrelpath (path, " test" ))
400- @test 2 >= @allocations (nestedrelpath (path, " test/dir" ))
401- @test 1 >= @allocations (nestedrelpath (path, " test/dir/foo_test.jl" ))
402- @test 1 >= @allocations (nestedrelpath (path, " test/dir/other" ))
403- @test 1 >= @allocations (nestedrelpath (path, " test/dir/other/bar_test.jl" ))
404- end
398+ # leading '/' doesn't get ignored or stripped
399+ @test nestedrelpath (" /a/b/c" , " /a/b" ) == " c"
400+ @test nestedrelpath (" /a/b/c" , " a/b" ) == " /a/b/c"
401+ @test nestedrelpath (" /a/b" , " /a/b/c" ) == " /a/b"
402+ @test nestedrelpath (" /a/b" , " c" ) == " /a/b"
405403end
406404
407405end # internals.jl testset
You can’t perform that action at this time.
0 commit comments