diff --git a/src/ReTestItems.jl b/src/ReTestItems.jl index 00342ebe..a6ff0beb 100644 --- a/src/ReTestItems.jl +++ b/src/ReTestItems.jl @@ -633,17 +633,19 @@ function is_testsetup_file(filepath) ) end -# like `relpath` but assumes `path` is nested under `startdir`, else just returns `path` -function nestedrelpath(path, startdir) - path == startdir && return "." +# Like `relpath` but assumes `path` is nested under `startdir`, else just returns `path`. +# Always returns a `SubString` to be type-stable. +function nestedrelpath(path::T, startdir::AbstractString) where {T <: AbstractString} + path == startdir && return SubString{T}(".") relp = chopprefix(path, startdir) + relp == path && return relp sep = Base.Filesystem.path_separator if endswith(startdir, sep) return relp elseif startswith(relp, sep) return chopprefix(relp, sep) - else - return path + else # `startdir` was a prefix of `path` but not a directory + return SubString{T}(path) end end diff --git a/test/internals.jl b/test/internals.jl index 0b1e7b89..ccfc4d38 100644 --- a/test/internals.jl +++ b/test/internals.jl @@ -394,13 +394,11 @@ end @test nestedrelpath(path, "test/dir/other") == "test/dir/foo_test.jl" @test nestedrelpath(path, "test/dir/other/bar_test.jl") == "test/dir/foo_test.jl" - @static if isdefined(Base, Symbol("@allocations")) # added in Julia v1.9 - @test 2 >= @allocations(nestedrelpath(path, "test")) - @test 2 >= @allocations(nestedrelpath(path, "test/dir")) - @test 1 >= @allocations(nestedrelpath(path, "test/dir/foo_test.jl")) - @test 1 >= @allocations(nestedrelpath(path, "test/dir/other")) - @test 1 >= @allocations(nestedrelpath(path, "test/dir/other/bar_test.jl")) - end + # leading '/' doesn't get ignored or stripped + @test nestedrelpath("/a/b/c", "/a/b") == "c" + @test nestedrelpath("/a/b/c", "a/b") == "/a/b/c" + @test nestedrelpath("/a/b", "/a/b/c") == "/a/b" + @test nestedrelpath("/a/b", "c") == "/a/b" end end # internals.jl testset