Skip to content

Commit

Permalink
Adds tests for: mv function JuliaLang#11172
Browse files Browse the repository at this point in the history
  • Loading branch information
peter1000 committed May 13, 2015
1 parent fec7ef1 commit 43eb785
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 34 deletions.
10 changes: 5 additions & 5 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ end


# The following use Unix command line facilites
# check for https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
function checkfor_mv_cp_cptree(src::AbstractString, dst::AbstractString, txt::AbstractString;
remove_destination::Bool=false)
if ispath(dst)
if remove_destination
# check for https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
# Check for issue when: (src == dst) or when one is a link to the other
# https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
if Base.samefile(src, dst)
abs_src = islink(src) ? abspath(readlink(src)) : abspath(src)
abs_dst = islink(dst) ? abspath(readlink(dst)) : abspath(dst)
throw(ArgumentError(string("'src' and 'dst' refer to the same file/dir.",
"This is not supported.\n ",
"`src` referce to: $(abs_src)\n ",
"`dst` referce to: $(abs_dst)\n")))
"`src` refers to: $(abs_src)\n ",
"`dst` refers to: $(abs_dst)\n")))
end
rm(dst; recursive=true)
else
Expand Down Expand Up @@ -124,7 +124,7 @@ function cp(src::AbstractString, dst::AbstractString; remove_destination::Bool=f
end

function mv(src::AbstractString, dst::AbstractString; remove_destination::Bool=false)
checkfor_mv_cp_cptree(src, dst, "copying"; remove_destination=remove_destination)
checkfor_mv_cp_cptree(src, dst, "moving"; remove_destination=remove_destination)
FS.rename(src, dst)
end

Expand Down
2 changes: 1 addition & 1 deletion base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ filesize(path...) = stat(path...).size
mtime(path...) = stat(path...).mtime
ctime(path...) = stat(path...).ctime

# samefile can be used for files and directories: 11145#issuecomment-99511194
# samefile can be used for files and directories: #11145#issuecomment-99511194
samefile(a::StatStruct, b::StatStruct) = a.device==b.device && a.inode==b.inode
function samefile(a::AbstractString, b::AbstractString)
if ispath(a) && ispath(b)
Expand Down
152 changes: 124 additions & 28 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -529,25 +529,35 @@ if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
# cp ----------------------------------------------------
mktempdir() do tmpdir
# Setup new copies for the test
test_src_paths, test_new_paths = setup_dirs(tmpdir)
for (s, d) in zip(test_src_paths, test_new_paths)
maindir1 = joinpath(tmpdir, "maindir1")
maindir2 = joinpath(tmpdir, "maindir2")
mkdir(maindir1)
mkdir(maindir2)
test_src_paths1, test_new_paths1 = setup_dirs(maindir1)
test_src_paths2, test_new_paths2 = setup_dirs(maindir2)
for (s, d) in zip(test_src_paths1, test_new_paths1)
cp_follow_symlinks_false_check(s, d)
end
for (s, d) in zip(test_src_paths2, test_new_paths2)
cp_follow_symlinks_false_check(s, d)
end
# Test require `remove_destination=true`
for s in test_src_paths
for d in test_new_paths
for s in test_src_paths1
for d in test_new_paths2
@test_throws ArgumentError cp(s, d; remove_destination=false)
@test_throws ArgumentError cp(s, d; remove_destination=false, follow_symlinks=true)
end
end
# Test remove the existing path first and copy
for (s, d) in zip(test_src_paths, test_new_paths)
# need to use here the test_src_paths2:
# otherwise ArgumentError: 'src' and 'dst' refer to the same file/dir.
for (s, d) in zip(test_src_paths2, test_new_paths1)
cp_follow_symlinks_false_check(s, d; remove_destination=true)
end
# Test remove the existing path first and copy an empty dir
emptydir = joinpath(tmpdir, "emptydir")
emptydir = joinpath(maindir1, "emptydir")
mkdir(emptydir)
for d in test_new_paths
for d in test_new_paths1
cp(emptydir, d; remove_destination=true, follow_symlinks=false)
# Expect no link because a dir is copied (follow_symlinks=false does not effect this)
@test isdir(d) && !islink(d)
Expand All @@ -558,18 +568,29 @@ if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
# mv ----------------------------------------------------
mktempdir() do tmpdir
# Setup new copies for the test
test_src_paths, test_new_paths = setup_dirs(tmpdir)
for (s, d) in zip(test_src_paths, test_new_paths)
maindir1 = joinpath(tmpdir, "maindir1")
maindir2 = joinpath(tmpdir, "maindir2")
mkdir(maindir1)
mkdir(maindir2)
test_src_paths1, test_new_paths1 = setup_dirs(maindir1)
test_src_paths2, test_new_paths2 = setup_dirs(maindir2)
for (s, d) in zip(test_src_paths1, test_new_paths1)
cp_follow_symlinks_false_check(s, d; remove_destination=true)
end
for (s, d) in zip(test_src_paths2, test_new_paths2)
cp_follow_symlinks_false_check(s, d; remove_destination=true)
end

# Test require `remove_destination=true`
for s in test_src_paths
for d in test_new_paths
for s in test_src_paths1
for d in test_new_paths2
@test_throws ArgumentError mv(s, d; remove_destination=false)
end
end
# Test remove the existing path first and move
for (s, d) in zip(test_src_paths, test_new_paths)
# need to use here the test_src_paths2:
# otherwise ArgumentError: 'src' and 'dst' refer to the same file/dir.This is not supported.
for (s, d) in zip(test_src_paths2, test_new_paths1)
d_mv = joinpath(dirname(d), "$(basename(d))_mv")
mv_check(s, d, d_mv; remove_destination=true)
end
Expand Down Expand Up @@ -614,6 +635,40 @@ if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
mv(d, d_mv; remove_destination=true)
end
end

# issue ----------------------------------------------------
# Check for issue when: (src == dst) or when one is a link to the other
# https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
mktempdir() do tmpdir
test_src_paths1, test_new_paths1 = setup_dirs(tmpdir)
dirs = [joinpath(tmpdir, "src"), joinpath(tmpdir, "abs_dirlink"), joinpath(tmpdir, "rel_dirlink")]
for src in dirs
for dst in dirs
# cptree
@test_throws ArgumentError Base.cptree(src,dst; remove_destination=true, follow_symlinks=false)
@test_throws ArgumentError Base.cptree(src,dst; remove_destination=true, follow_symlinks=true)
# cp
@test_throws ArgumentError cp(src,dst; remove_destination=true, follow_symlinks=false)
@test_throws ArgumentError cp(src,dst; remove_destination=true, follow_symlinks=true)
# mv
@test_throws ArgumentError mv(src,dst; remove_destination=true)
end
end
end
# None existing src
mktempdir() do tmpdir
none_existing_src = joinpath(tmpdir, "none_existing_src")
dst = joinpath(tmpdir, "dst")
@test !ispath(none_existing_src)
# cptree
@test_throws ArgumentError Base.cptree(none_existing_src,dst; remove_destination=true, follow_symlinks=false)
@test_throws ArgumentError Base.cptree(none_existing_src,dst; remove_destination=true, follow_symlinks=true)
# cp
@test_throws Base.UVError cp(none_existing_src,dst; remove_destination=true, follow_symlinks=false)
@test_throws Base.UVError cp(none_existing_src,dst; remove_destination=true, follow_symlinks=true)
# mv
@test_throws Base.UVError mv(none_existing_src,dst; remove_destination=true)
end
end

# issue #10506 #10434
Expand Down Expand Up @@ -682,28 +737,38 @@ end
# cp ----------------------------------------------------
mktempdir() do tmpdir
# Setup new copies for the test
test_src_paths, test_new_paths, file_txt = setup_files(tmpdir)
for (s, d) in zip(test_src_paths, test_new_paths)
cp_follow_symlinks_false_check(s, d, file_txt)
maindir1 = joinpath(tmpdir, "maindir1")
maindir2 = joinpath(tmpdir, "maindir2")
mkdir(maindir1)
mkdir(maindir2)
test_src_paths1, test_new_paths1, file_txt1 = setup_files(maindir1)
test_src_paths2, test_new_paths2, file_txt2 = setup_files(maindir2)
for (s, d) in zip(test_src_paths1, test_new_paths1)
cp_follow_symlinks_false_check(s, d, file_txt1)
end
for (s, d) in zip(test_src_paths2, test_new_paths2)
cp_follow_symlinks_false_check(s, d, file_txt2)
end
# Test require `remove_destination=true`
for s in test_src_paths
for d in test_new_paths
for s in test_src_paths1
for d in test_new_paths2
@test_throws ArgumentError cp(s, d; remove_destination=false)
@test_throws ArgumentError cp(s, d; remove_destination=false, follow_symlinks=true)
end
end
# Test remove the existing path first and copy: follow_symlinks=false
for (s, d) in zip(test_src_paths, test_new_paths)
cp_follow_symlinks_false_check(s, d, file_txt; remove_destination=true)
# Test remove the existing path first and copy
# need to use here the test_src_paths2:
# otherwise ArgumentError: 'src' and 'dst' refer to the same file/dir.This is not supported.
for (s, d) in zip(test_src_paths2, test_new_paths1)
cp_follow_symlinks_false_check(s, d, file_txt2; remove_destination=true)
end
# Test remove the existing path first and copy an other file
otherfile = joinpath(tmpdir, "otherfile.txt")
otherfile_content = "This is otherfile.txt with unicode - 这是一个文件"
open(otherfile, "w") do f
write(f, otherfile_content)
end
for d in test_new_paths
for d in test_new_paths1
cp(otherfile, d; remove_destination=true, follow_symlinks=false)
# Expect no link because a file is copied (follow_symlinks=false does not effect this)
@test isfile(d) && !islink(d)
Expand All @@ -714,20 +779,30 @@ end
# mv ----------------------------------------------------
mktempdir() do tmpdir
# Setup new copies for the test
test_src_paths, test_new_paths, file_txt = setup_files(tmpdir)
for (s, d) in zip(test_src_paths, test_new_paths)
cp_follow_symlinks_false_check(s, d, file_txt; remove_destination=true)
maindir1 = joinpath(tmpdir, "maindir1")
maindir2 = joinpath(tmpdir, "maindir2")
mkdir(maindir1)
mkdir(maindir2)
test_src_paths1, test_new_paths1, file_txt1 = setup_files(maindir1)
test_src_paths2, test_new_paths2, file_txt2 = setup_files(maindir2)
for (s, d) in zip(test_src_paths1, test_new_paths1)
cp_follow_symlinks_false_check(s, d, file_txt1)
end
for (s, d) in zip(test_src_paths2, test_new_paths2)
cp_follow_symlinks_false_check(s, d, file_txt2)
end
# Test require `remove_destination=true`
for s in test_src_paths
for d in test_new_paths
for s in test_src_paths1
for d in test_new_paths2
@test_throws ArgumentError mv(s, d; remove_destination=false)
end
end
# Test remove the existing path first and move
for (s, d) in zip(test_src_paths, test_new_paths)
# need to use here the test_src_paths2:
# otherwise ArgumentError: 'src' and 'dst' refer to the same file/dir.This is not supported.
for (s, d) in zip(test_src_paths2, test_new_paths1)
d_mv = joinpath(dirname(d), "$(basename(d))_mv")
mv_check(s, d, d_mv, file_txt; remove_destination=true)
mv_check(s, d, d_mv, file_txt2; remove_destination=true)
end
end

Expand Down Expand Up @@ -796,8 +871,29 @@ end
mv(d, d_mv; remove_destination=true)
end
end
# issue ----------------------------------------------------
# Check for issue when: (src == dst) or when one is a link to the other
# https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
mktempdir() do tmpdir
test_src_paths, test_new_paths, file_txt = setup_files(tmpdir)
files = [joinpath(tmpdir, "srcfile.txt"), joinpath(tmpdir, "abs_filelink"), joinpath(tmpdir, "rel_filelink")]
for src in files
for dst in files
# cptree
@test_throws ArgumentError Base.cptree(src,dst; remove_destination=true, follow_symlinks=false)
@test_throws ArgumentError Base.cptree(src,dst; remove_destination=true, follow_symlinks=true)
# cp
@test_throws ArgumentError cp(src,dst; remove_destination=true, follow_symlinks=false)
@test_throws ArgumentError cp(src,dst; remove_destination=true, follow_symlinks=true)
# mv
@test_throws ArgumentError mv(src,dst; remove_destination=true)
end
end
end
# None existing src: not needed here as it is done above with the directories.
end


###################
# FILE* interface #
###################
Expand Down

0 comments on commit 43eb785

Please sign in to comment.