Skip to content

Commit ab52516

Browse files
Make type-stable
1 parent da77644 commit ab52516

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
@@ -640,17 +640,19 @@ function is_testsetup_file(filepath)
640640
)
641641
end
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
655657
end
656658

test/internals.jl

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

407405
end # internals.jl testset

0 commit comments

Comments
 (0)