Skip to content

Commit

Permalink
Replace relpath with cheaper function
Browse files Browse the repository at this point in the history
Since we call `relpath` for every directory and file
in the directory tree before any tests start, we want
this to be as cheap as possible (to minimise latency).
Hopefully this new function is valid on Windows, but
I've no machine to test that :/
  • Loading branch information
nickrobinson251 committed Sep 11, 2024
1 parent f9cf56c commit 3409203
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/ReTestItems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,20 @@ 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 "."
relp = chopprefix(path, startdir)
sep = Base.Filesystem.path_separator
if endswith(startdir, sep)
return relp
elseif startswith(relp, sep)
return chopprefix(relp, sep)
else
return path
end
end

# is `dir` the root of a subproject inside the current project?
function _is_subproject(dir, current_projectfile)
projectfile = _project_file(dir)
Expand All @@ -642,7 +656,7 @@ function _is_subproject(dir, current_projectfile)
projectfile == current_projectfile && return false
# a `test/Project.toml` is special and doesn't indicate a subproject
current_project_dir = dirname(current_projectfile)
rel_projectfile = relpath(projectfile, current_project_dir)
rel_projectfile = nestedrelpath(projectfile, current_project_dir)
rel_projectfile == joinpath("test", "Project.toml") && return false
return true
end
Expand Down Expand Up @@ -675,7 +689,7 @@ function include_testfiles!(project_name, projectfile, paths, ti_filter::TestIte
subproject_root = root
continue
end
rpath = relpath(root, project_root)
rpath = nestedrelpath(root, project_root)
startswith(rpath, hidden_re) && continue # skip hidden directories
dir_node = DirNode(rpath; report, verbose=verbose_results)
dir_nodes[rpath] = dir_node
Expand All @@ -693,7 +707,7 @@ function include_testfiles!(project_name, projectfile, paths, ti_filter::TestIte
if !(is_testsetup_file(filepath) || (is_test_file(filepath) && is_requested(filepath, paths)))
continue
end
fpath = relpath(filepath, project_root)
fpath = nestedrelpath(filepath, project_root)
file_node = FileNode(fpath, ti_filter; report, verbose=verbose_results)
testitem_names = Set{String}() # to enforce that names in the same file are unique
push!(dir_node, file_node)
Expand Down Expand Up @@ -745,7 +759,7 @@ function _throw_duplicate_ids(testitems)
seen = Dict{String,String}()
for ti in testitems
id = ti.id
source = string(relpath(ti.file, ti.project_root), ":", ti.line)
source = string(nestedrelpath(ti.file, ti.project_root), ":", ti.line)
name = string(repr(ti.name), " at ", source)
if haskey(seen, id)
name1 = seen[id]
Expand Down
4 changes: 2 additions & 2 deletions src/junit_xml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ mutable struct JUnitTestSuite # File
counts::JUnitCounts
testcases::Vector{JUnitTestCase}
end
JUnitTestSuite(name::String) = JUnitTestSuite(name, JUnitCounts(), JUnitTestCase[])
JUnitTestSuite(name::AbstractString) = JUnitTestSuite(name, JUnitCounts(), JUnitTestCase[])

mutable struct JUnitTestSuites
const name::String
counts::JUnitCounts
testsuites::Vector{JUnitTestSuite}
end

JUnitTestSuites(name::String) = JUnitTestSuites(name, JUnitCounts(), JUnitTestSuite[])
JUnitTestSuites(name::AbstractString) = JUnitTestSuites(name, JUnitCounts(), JUnitTestSuite[])

function junit_record!(suites1::JUnitTestSuites, suites2::JUnitTestSuites)
update!(suites1.counts, suites2.counts)
Expand Down
25 changes: 25 additions & 0 deletions test/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,29 @@ end
end
end

@testset "nestedrelpath" begin
using ReTestItems: nestedrelpath
@assert Base.Filesystem.path_separator == "/"
path = "test/dir/foo_test.jl"
@test nestedrelpath(path, "test") == relpath(path, "test") == "dir/foo_test.jl"
@test nestedrelpath(path, "test/") == relpath(path, "test/") == "dir/foo_test.jl"
@test nestedrelpath(path, "test/dir") == relpath(path, "test/dir") == "foo_test.jl"
@test nestedrelpath(path, "test/dir/") == relpath(path, "test/dir/") == "foo_test.jl"
@test nestedrelpath(path, "test/dir/foo_test.jl") == relpath(path, "test/dir/foo_test.jl") == "."

# unlike `relpath`: if `startdir` is not a prefix of `path`, the assumption is violated,
# and `path` is just returned as-is
@test nestedrelpath(path, "test/dir/foo_") == "test/dir/foo_test.jl"
@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
end

end # internals.jl testset

0 comments on commit 3409203

Please sign in to comment.