Skip to content

Commit d2f8a3d

Browse files
committed
Fewer allocs
1 parent bcbb1db commit d2f8a3d

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/ReTestItems.jl

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -789,49 +789,53 @@ function nestedrelpath(path::T, startdir::AbstractString) where {T <: AbstractSt
789789
end
790790

791791
# is `dir` the root of a subproject inside the current project?
792-
function _is_subproject(dir, current_projectfile)
793-
projectfile = _project_file(dir)
794-
isnothing(projectfile) && return false
795-
796-
projectfile = abspath(projectfile)
797-
projectfile == current_projectfile && return false
798-
# a `test/Project.toml` is special and doesn't indicate a subproject
799-
current_project_dir = dirname(current_projectfile)
800-
rel_projectfile = nestedrelpath(projectfile, current_project_dir)
801-
rel_projectfile == joinpath("test", "Project.toml") && return false
802-
return true
792+
# all three paths are assumed to be absolute paths
793+
let test_project = joinpath("test", "Project.toml")
794+
global function _is_subproject(dir, current_projectfile, current_project_dir)
795+
projectfile = _project_file(dir)
796+
isnothing(projectfile) && return false
797+
798+
projectfile == current_projectfile && return false
799+
# a `test/Project.toml` is special and doesn't indicate a subproject
800+
rel_projectfile = nestedrelpath(projectfile, current_project_dir)
801+
rel_projectfile == test_project && return false
802+
return true
803+
end
803804
end
804805

805806
# called on results of `readdir(root)`
806-
_is_hidden(name::AbstractString) = ncodeunits(name) > 1 && name[1] == '.'
807+
_is_hidden(name::AbstractString) = ncodeunits(name) > 1 && codeunits(name)[1] == UInt8('.')
807808

808809
# Traverses the directory tree starting at `project_root` and grows `root_node` with
809810
# `DirNode`s and `FileNode`s for each directory and test file found. Filters out non-eligible
810811
# paths.
811812
function walkdir_task(walkdir_channel::Channel{Tuple{String,FileNode}}, project_root::String, root_node, ti_filter, paths, projectfile, report, verbose_results)
813+
@assert isabspath(project_root)
814+
@assert isabspath(projectfile)
812815
dir_nodes = Dict{String, DirNode}()
813816
subproject_root = nothing # don't recurse into directories with their own Project.toml.
817+
abspaths = map(abspath, paths)
814818
try
815819
# Since test items don't store paths to their test setups, we need to traverse the
816820
# whole project, not just the requested paths.
817821
stack = [project_root]
818822
while !isempty(stack)
819823
root = pop!(stack)
824+
if subproject_root !== nothing && startswith(root, subproject_root)
825+
@debugv 1 "Skipping files in `$root` in subproject `$subproject_root`"
826+
continue
827+
elseif _is_subproject(root, projectfile, project_root)
828+
subproject_root = root
829+
continue
830+
end
820831
rel_root = nestedrelpath(root, project_root)
821832
dir_node = DirNode(rel_root; report, verbose=verbose_results)
822833
dir_nodes[rel_root] = dir_node
823834
push!(get(dir_nodes, dirname(rel_root), root_node), dir_node)
824-
for file in readdir(root)
835+
for file in readdir(root) # TODO: Use https://github.com/JuliaLang/julia/pull/55358 once it lands
825836
_is_hidden(file) && continue # skip hidden files/directories
826837
full_path = joinpath(root, file)
827838
if isdir(full_path)
828-
if subproject_root !== nothing && startswith(full_path, subproject_root)
829-
@debugv 1 "Skipping files in `$root` in subproject `$subproject_root`"
830-
continue
831-
elseif _is_subproject(root, projectfile)
832-
subproject_root = root
833-
continue
834-
end
835839
push!(stack, full_path)
836840
else
837841
# We filter here, rather than the testitem level, to make sure we don't
@@ -841,7 +845,7 @@ function walkdir_task(walkdir_channel::Channel{Tuple{String,FileNode}}, project_
841845
# even if they're not in a requested path, e.g. they are a level up in the
842846
# directory tree. The testsetup-file suffix is hopefully specific enough
843847
# to ReTestItems that this doesn't lead to `include`ing unexpected files.
844-
if !(is_testsetup_file(full_path) || (is_test_file(full_path) && is_requested(full_path, paths)))
848+
if !(is_testsetup_file(full_path) || (is_test_file(full_path) && is_requested(full_path, abspaths)))
845849
continue
846850
end
847851
rel_full_path = nestedrelpath(full_path, project_root)
@@ -962,9 +966,9 @@ end
962966

963967
# Is filepath one of the paths the user requested?
964968
is_requested(filepath, paths::Tuple{}) = true # no paths means no restrictions
965-
function is_requested(filepath, paths::Tuple)
966-
return any(paths) do p
967-
startswith(filepath, abspath(p))
969+
function is_requested(filepath, abspaths::Tuple)
970+
return any(abspaths) do p
971+
startswith(filepath, p)
968972
end
969973
end
970974

test/internals.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ end
7575
@assert isfile(monorepo_proj)
7676
for pkg in ("B", "C", "D")
7777
path = joinpath(monorepo, "monorepo_packages", pkg)
78-
@test _is_subproject(path, monorepo_proj)
78+
@test _is_subproject(path, monorepo_proj, monorepo)
7979
end
8080
for dir in ("src", "test")
8181
path = joinpath(monorepo, dir)
82-
@test !_is_subproject(path, monorepo_proj)
82+
@test !_is_subproject(path, monorepo_proj, monorepo)
8383
end
8484
# Test "test/Project.toml" does cause "test/" to be subproject
8585
tpf = joinpath(test_pkg_dir, "TestProjectFile.jl")
@@ -88,7 +88,7 @@ end
8888
@assert isfile(joinpath(tpf, "test", "Project.toml"))
8989
for dir in ("src", "test")
9090
path = joinpath(tpf, dir)
91-
@test !_is_subproject(path, tpf_proj)
91+
@test !_is_subproject(path, tpf_proj, tpf)
9292
end
9393
end
9494

0 commit comments

Comments
 (0)