Skip to content

Commit

Permalink
Make type-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrobinson251 committed Sep 11, 2024
1 parent 3409203 commit ee74779
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 7 additions & 5 deletions src/ReTestItems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 5 additions & 7 deletions test/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ee74779

Please sign in to comment.