Skip to content

Commit 3dd463f

Browse files
committed
More sync tests and throw argument error son mismatch types or missing src.
1 parent 16efb93 commit 3dd463f

File tree

2 files changed

+134
-75
lines changed

2 files changed

+134
-75
lines changed

src/path.jl

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -530,38 +530,61 @@ function sync(src::AbstractPath, dst::AbstractPath; kwargs...)
530530
end
531531

532532
function sync(f::Function, src::AbstractPath, dst::AbstractPath; delete=false, overwrite=true)
533-
# Create an index of all of the source files
534-
src_paths = collect(walkpath(src))
535-
index = Dict(
536-
Tuple(setdiff(p.segments, src.segments)) => i for (i, p) in enumerate(src_paths)
537-
)
533+
# Throw an error if the source path doesn't exist at all
534+
exists(src) || throw(ArgumentError("$src does not exist"))
535+
536+
# If the top level source is just a file then try to just sync that
537+
# without calling walkpath
538+
if isfile(src)
539+
# If the destination exists then we should make sure it is a file and check
540+
# if we should copy the source over.
541+
if exists(dst)
542+
isfile(dst) || throw(ArgumentError("$dst is not a file"))
543+
if overwrite && f(src, dst)
544+
cp(src, dst; force=true)
545+
end
546+
else
547+
cp(src, dst)
548+
end
549+
else
550+
isdir(src) || throw(ArgumentError("$src is neither a file or directory."))
551+
if exists(dst) && !isdir(dst)
552+
throw(ArgumentError("$dst is not a directory while $src is"))
553+
end
538554

539-
if exists(dst)
540-
for p in walkpath(dst)
541-
k = Tuple(setdiff(p.segments, dst.segments))
555+
# Create an index of all of the source files
556+
src_paths = collect(walkpath(src))
557+
index = Dict(
558+
Tuple(setdiff(p.segments, src.segments)) => i for (i, p) in enumerate(src_paths)
559+
)
542560

543-
if haskey(index, k)
544-
src_path = src_paths[index[k]]
545-
if overwrite && f(src_path, p)
546-
cp(src_path, p; force=true)
547-
end
561+
if exists(dst)
562+
for p in walkpath(dst)
563+
k = Tuple(setdiff(p.segments, dst.segments))
564+
565+
if haskey(index, k)
566+
src_path = src_paths[index[k]]
567+
if overwrite && f(src_path, p)
568+
cp(src_path, p; force=true)
569+
end
548570

549-
delete!(index, k)
550-
elseif delete
551-
rm(p; recursive=true)
571+
delete!(index, k)
572+
elseif delete
573+
rm(p; recursive=true)
574+
end
552575
end
553-
end
554576

555-
# Finally, copy over files that don't exist at the destination
556-
# But we need to iterate through it in a way that respects the original
557-
# walkpath order otherwise we may end up trying to copy a file before its parents.
558-
index_pairs = collect(pairs(index))
559-
index_pairs = index_pairs[sortperm(last.(index_pairs))]
560-
for (seg, i) in index_pairs
561-
cp(src_paths[i], Path(dst, tuple(dst.segments..., seg...)); force=true)
577+
# Finally, copy over files that don't exist at the destination
578+
# But we need to iterate through it in a way that respects the original
579+
# walkpath order otherwise we may end up trying to copy a file before its parents.
580+
index_pairs = collect(pairs(index))
581+
index_pairs = index_pairs[sortperm(last.(index_pairs))]
582+
for (seg, i) in index_pairs
583+
cp(src_paths[i], Path(dst, tuple(dst.segments..., seg...)); force=true)
584+
end
585+
else
586+
cp(src, dst)
562587
end
563-
else
564-
cp(src, dst)
565588
end
566589
end
567590

src/test.jl

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -612,57 +612,93 @@ module TestPaths
612612

613613
function test_sync(ps::PathSet)
614614
@testset "sync" begin
615-
# Base cp case
616-
sync(ps.foo, ps.qux / "foo")
617-
@test exists(ps.qux / "foo" / "baz.txt")
615+
@testset "empty destination" begin
616+
sync(ps.foo, ps.qux / "foo")
617+
@test exists(ps.qux / "foo" / "baz.txt")
618618

619-
# Test that the copied baz file has a newer modified time
620-
baz_t = modified(ps.qux / "foo" / "baz.txt")
621-
@test modified(ps.baz) < baz_t
622-
623-
# Don't cp unchanged files when a new file is added
624-
# NOTE: sleep before we make a new file, so it's clear that the
625-
# modified time has changed.
626-
sleep(1)
627-
write(ps.foo / "test.txt", "New File")
628-
sync(ps.foo, ps.qux / "foo")
629-
@test exists(ps.qux / "foo" / "test.txt")
630-
@test read(ps.qux / "foo" / "test.txt", String) == "New File"
631-
@test modified(ps.qux / "foo" / "baz.txt") == baz_t
632-
@test modified(ps.qux / "foo" / "test.txt") > baz_t
633-
634-
# Test not deleting a file on sync
635-
rm(ps.foo / "test.txt")
636-
sync(ps.foo, ps.qux / "foo")
637-
@test exists(ps.qux / "foo" / "test.txt")
638-
639-
# Test passing delete flag
640-
sync(ps.foo, ps.qux / "foo"; delete=true)
641-
@test !exists(ps.qux / "foo" / "test.txt")
642-
rm(ps.qux / "foo"; recursive=true)
619+
# Test that the copied baz file has a newer modified time
620+
baz_t = modified(ps.qux / "foo" / "baz.txt")
621+
@test modified(ps.baz) < baz_t
622+
end
623+
624+
@testset "empty source" begin
625+
@test_throws ArgumentError sync(ps.root / "quux", ps.foo)
626+
end
643627

644-
# Test a condtion where the index could reorder the walkpath order.
645-
tmp_src = ps.root / "tmp-src"
646-
mkdir(tmp_src)
647-
src_file = tmp_src / "file1"
648-
write(src_file, "Hello World!")
649-
650-
src_folder = tmp_src / "folder1"
651-
mkdir(src_folder)
652-
src_folder_file = src_folder / "file2"
653-
write(src_folder_file, "") # empty file
654-
655-
src_folder2 = src_folder / "folder2" # nested folders
656-
mkdir(src_folder2)
657-
src_folder2_file = src_folder2 / "file3"
658-
write(src_folder2_file, "Test")
659-
660-
tmp_dst = ps.root / "tmp_dst"
661-
mkdir(tmp_dst)
662-
sync(tmp_src, tmp_dst)
663-
@test exists(tmp_dst / "folder1" / "folder2" / "file3")
664-
rm(tmp_src; recursive=true)
665-
rm(tmp_dst; recursive=true)
628+
@testset "new source" begin
629+
# Don't cp unchanged files when a new file is added
630+
# NOTE: sleep before we make a new file, so it's clear that the
631+
# modified time has changed.
632+
baz_t = modified(ps.qux / "foo" / "baz.txt")
633+
sleep(1)
634+
write(ps.foo / "test.txt", "New src")
635+
sync(ps.foo, ps.qux / "foo")
636+
@test exists(ps.qux / "foo" / "test.txt")
637+
@test read(ps.qux / "foo" / "test.txt", String) == "New src"
638+
@test modified(ps.qux / "foo" / "baz.txt") == baz_t
639+
@test modified(ps.qux / "foo" / "test.txt") > baz_t
640+
end
641+
642+
@testset "new destination" begin
643+
# Newer file of the same size is likely the result of an upload which
644+
# will always have a newer last modified time.
645+
test_t = modified(ps.foo / "test.txt")
646+
sleep(1)
647+
write(ps.qux / "foo" / "test.txt", "New dst")
648+
@test modified(ps.qux / "foo" / "test.txt") > test_t
649+
sync(ps.foo, ps.qux / "foo")
650+
@test read(ps.qux / "foo" / "test.txt", String) == "New dst"
651+
@test modified(ps.qux / "foo" / "test.txt") > test_t
652+
end
653+
654+
@testset "no delete" begin
655+
# Test not deleting a file on sync
656+
rm(ps.foo / "test.txt")
657+
sync(ps.foo, ps.qux / "foo")
658+
@test exists(ps.qux / "foo" / "test.txt")
659+
end
660+
661+
@testset "delete" begin
662+
# Test passing delete flag
663+
sync(ps.foo, ps.qux / "foo"; delete=true)
664+
@test !exists(ps.qux / "foo" / "test.txt")
665+
rm(ps.qux / "foo"; recursive=true)
666+
end
667+
668+
@testset "mixed types" begin
669+
@testset "directory -> file" begin
670+
@test_throws ArgumentError sync(ps.foo, ps.quux)
671+
end
672+
673+
@testset "file -> directory" begin
674+
@test_throws ArgumentError sync(ps.quux, ps.foo)
675+
end
676+
end
677+
678+
@testset "walkpath order" begin
679+
# Test a condtion where the index could reorder the walkpath order.
680+
tmp_src = ps.root / "tmp-src"
681+
mkdir(tmp_src)
682+
src_file = tmp_src / "file1"
683+
write(src_file, "Hello World!")
684+
685+
src_folder = tmp_src / "folder1"
686+
mkdir(src_folder)
687+
src_folder_file = src_folder / "file2"
688+
write(src_folder_file, "") # empty file
689+
690+
src_folder2 = src_folder / "folder2" # nested folders
691+
mkdir(src_folder2)
692+
src_folder2_file = src_folder2 / "file3"
693+
write(src_folder2_file, "Test")
694+
695+
tmp_dst = ps.root / "tmp_dst"
696+
mkdir(tmp_dst)
697+
sync(tmp_src, tmp_dst)
698+
@test exists(tmp_dst / "folder1" / "folder2" / "file3")
699+
rm(tmp_src; recursive=true)
700+
rm(tmp_dst; recursive=true)
701+
end
666702
end
667703
end
668704

0 commit comments

Comments
 (0)