Skip to content

Commit ee74779

Browse files
Make type-stable
1 parent 3409203 commit ee74779

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/ReTestItems.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,17 +633,19 @@ function is_testsetup_file(filepath)
633633
)
634634
end
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
648650
end
649651

test/internals.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff 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"
404402
end
405403

406404
end # internals.jl testset

0 commit comments

Comments
 (0)