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 @@ -633,17 +633,19 @@ function is_testsetup_file(filepath)
633633 )
634634end
635635
636- # like `relpath` but assumes `path` is nested under `startdir`, else just returns `path`
637- function nestedrelpath (path, startdir)
638- path == startdir && return " ."
636+ # Like `relpath` but assumes `path` is nested under `startdir`, else just returns `path`.
637+ # Always returns a `SubString` to be type-stable.
638+ function nestedrelpath (path:: T , startdir:: AbstractString ) where {T <: AbstractString }
639+ path == startdir && return SubString {T} (" ." )
639640 relp = chopprefix (path, startdir)
641+ relp == path && return relp
640642 sep = Base. Filesystem. path_separator
641643 if endswith (startdir, sep)
642644 return relp
643645 elseif startswith (relp, sep)
644646 return chopprefix (relp, sep)
645- else
646- return path
647+ else # `startdir` was a prefix of `path` but not a directory
648+ return SubString {T} ( path)
647649 end
648650end
649651
Original file line number Diff line number Diff line change @@ -394,13 +394,11 @@ end
394394 @test nestedrelpath (path, " test/dir/other" ) == " test/dir/foo_test.jl"
395395 @test nestedrelpath (path, " test/dir/other/bar_test.jl" ) == " test/dir/foo_test.jl"
396396
397- @static if isdefined (Base, Symbol (" @allocations" )) # added in Julia v1.9
398- @test 2 >= @allocations (nestedrelpath (path, " test" ))
399- @test 2 >= @allocations (nestedrelpath (path, " test/dir" ))
400- @test 1 >= @allocations (nestedrelpath (path, " test/dir/foo_test.jl" ))
401- @test 1 >= @allocations (nestedrelpath (path, " test/dir/other" ))
402- @test 1 >= @allocations (nestedrelpath (path, " test/dir/other/bar_test.jl" ))
403- end
397+ # leading '/' doesn't get ignored or stripped
398+ @test nestedrelpath (" /a/b/c" , " /a/b" ) == " c"
399+ @test nestedrelpath (" /a/b/c" , " a/b" ) == " /a/b/c"
400+ @test nestedrelpath (" /a/b" , " /a/b/c" ) == " /a/b"
401+ @test nestedrelpath (" /a/b" , " c" ) == " /a/b"
404402end
405403
406404end # internals.jl testset
You can’t perform that action at this time.
0 commit comments