Skip to content

Commit

Permalink
Fix inconsistent trees from getindex and merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrChainsaw committed Jan 20, 2021
1 parent 43d7baa commit 6e1ee98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/patterns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function _glob_map(yes, no, t::FileTree, p, ps...)
if isempty(cs)
return no(t)
else
return FileTree(t, children=cs)
return FileTree(t, children=cs) |> setparent
end
else
return no(t)
Expand Down Expand Up @@ -73,7 +73,7 @@ function _regex_map(yes, no, t::FileTree, regex::Regex, toplevel=true)
if isempty(cs)
return no(t)
else
return FileTree(t; children=cs)
return FileTree(t; children=cs) |> setparent
end
end

Expand Down
8 changes: 6 additions & 2 deletions src/tree-ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ end
# path use `combine` to overwrite multiple files which map to the same path
function regex_rewrite_tree(tree, from_path, to_path, combine)
newtree = maketree(name(tree)=>[])
# Exclude the root from the matching because
# 1) the public API where the pattern is relative to the root and
# 2) attach wants a path relative to the root
rootoffset = length(canonical_path(path(tree)))+2
for x in Leaves(tree)
newname = replace(canonical_path(path(x)), from_path => to_path)
newname = replace(canonical_path(path(x))[rootoffset:end], from_path => to_path)
dir = dirname(newname)
dir = isempty(dir) ? "." : dir
newtree = attach(newtree,
Expand Down Expand Up @@ -84,7 +88,7 @@ function normdots(x::FileTree; combine=_merge_error)
z=normdots(y; combine=combine)
name(z) == "." ? children(z) : [z]
end |> Iterators.flatten |> collect
FileTree(x; children=_combine(c2, combine))
FileTree(x; children=_combine(c2, combine)) |> setparent
end

normdots(x::File; kw...) = x
Expand Down
25 changes: 24 additions & 1 deletion test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ using Distributed
└─ c/"""
end

# Helper function to recursively check that all children of ft have ft as their parent
function isconsistent(ft)
ok = true
for c in children(ft)
ok = parent(c) === ft && isconsistent(c)
end
return ok
end

@testset "indexing" begin
@test name(t) == "."
@test t["a"]["b"]["a"] isa File
Expand All @@ -38,6 +47,9 @@ end
maketree("c" => ["b","a"]))

@test_throws ErrorException t["c"]

@test isconsistent(t[r"a|c"])
@test isconsistent(t[glob"a/*/*"])
end

@testset "filter" begin
Expand Down Expand Up @@ -66,18 +78,25 @@ import FileTrees: attach
File(nothing, "data.csv", (yr, mo)) )
end
end

@test isconsistent(t1)

data1 = reducevalues(vcat, t1) |> exec

t4 = mv(t1, r"^(.*)/(.*)/data.csv$", s"\1/\2.csv")

@test isconsistent(t4)

t5 = maketree([])
for yr = 1:9
for mo = 1:6
t5 = attach(t5, string("0", yr),
t5 = attach(t5, joinpath(string("0", yr)),
File(nothing, string("0", mo) * ".csv", (yr, mo)))
end
end

@test isconsistent(t5)

@test isequal(t4, t5)

data2 = reducevalues(vcat, t4) |> exec
Expand All @@ -88,10 +107,14 @@ import FileTrees: attach
reducevalues(vcat, subtree)
end

@test isconsistent(t7)

@test reducevalues(hcat, t7; dirs=true) == [(j, i) for i=1:6, j=1:9]

t6 = cp(t1, r"^(.*)/(.*)/data.csv$", s"\1/\2.csv")

@test isconsistent(t6)

@test isequal(t6, merge(t1, t5))
end

Expand Down

0 comments on commit 6e1ee98

Please sign in to comment.