Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_move(tmpfolder):
# Given a file or directory exists,
tmpfolder.join("a-file.txt").write("text")
tmpfolder.join("a-folder").ensure_dir()
tmpfolder.join("a-folder/another-file.txt").write("text")
# When it is moved,
tmpfolder.join("a-dir").ensure_dir()
utils.move("a-file.txt", target="a-dir")
utils.move("a-folder", target="a-dir")
# Then the original path should not exist
assert not tmpfolder.join("a-file.txt").check()
assert not tmpfolder.join("a-folder").check()
# And the new path should exist
assert tmpfolder.join("a-dir/a-file.txt").check()
assert tmpfolder.join("a-dir/a-folder/another-file.txt").check()
def test_move_log(tmpfolder, caplog):
caplog.set_level(logging.INFO)
fname1 = uniqstr() # Use a unique name to get easily identifiable logs
fname2 = uniqstr()
dname = uniqstr()
# Given a file or directory exists,
tmpfolder.join(fname1).write("text")
tmpfolder.join(fname2).write("text")
# When it is moved without log kwarg,
tmpfolder.join(dname).ensure_dir()
utils.move(fname1, target=dname)
# Then no log should be created.
logs = caplog.text
assert not re.search("move.+{}.+to.+{}".format(fname1, dname), logs)
# When it is moved with log kwarg,
utils.move(fname2, target=dname, log=True)
# Then log should be created.
logs = caplog.text
assert re.search("move.+{}.+to.+{}".format(fname2, dname), logs)
def test_move(tmpfolder):
# Given a file or directory exists,
tmpfolder.join("a-file.txt").write("text")
tmpfolder.join("a-folder").ensure_dir()
tmpfolder.join("a-folder/another-file.txt").write("text")
# When it is moved,
tmpfolder.join("a-dir").ensure_dir()
utils.move("a-file.txt", target="a-dir")
utils.move("a-folder", target="a-dir")
# Then the original path should not exist
assert not tmpfolder.join("a-file.txt").check()
assert not tmpfolder.join("a-folder").check()
# And the new path should exist
assert tmpfolder.join("a-dir/a-file.txt").check()
assert tmpfolder.join("a-dir/a-folder/another-file.txt").check()
def test_move_non_dir_target(tmpfolder):
# Given a file exists,
tmpfolder.join("a-file.txt").write("text")
assert not tmpfolder.join("another-file.txt").check()
# When it is moved,
utils.move("a-file.txt", target="another-file.txt")
# Then the original path should not exist
assert not tmpfolder.join("a-file.txt").check()
# And the new path should exist
assert tmpfolder.join("another-file.txt").read() == "text"
# Given a dir exists,
tmpfolder.join("a-dir").ensure_dir()
tmpfolder.join("a-dir/a-file.txt").write("text")
assert not tmpfolder.join("another-dir/a-file.txt").check()
# When it is moved to a path that do not exist yet,
utils.move("a-dir", target="another-dir")
# Then the dir should be renamed.
assert not tmpfolder.join("a-dir").check()
assert tmpfolder.join("another-dir/a-file.txt").read() == "text"
# Given a file exists,
tmpfolder.join("a-file.txt").write("text")
assert not tmpfolder.join("another-file.txt").check()
# When it is moved,
utils.move("a-file.txt", target="another-file.txt")
# Then the original path should not exist
assert not tmpfolder.join("a-file.txt").check()
# And the new path should exist
assert tmpfolder.join("another-file.txt").read() == "text"
# Given a dir exists,
tmpfolder.join("a-dir").ensure_dir()
tmpfolder.join("a-dir/a-file.txt").write("text")
assert not tmpfolder.join("another-dir/a-file.txt").check()
# When it is moved to a path that do not exist yet,
utils.move("a-dir", target="another-dir")
# Then the dir should be renamed.
assert not tmpfolder.join("a-dir").check()
assert tmpfolder.join("another-dir/a-file.txt").read() == "text"
def test_pretend_move(tmpfolder):
# Given a file or directory exists,
tmpfolder.join("a-file.txt").write("text")
tmpfolder.join("another-file.txt").write("text")
# When it is moved without pretend kwarg,
tmpfolder.join("a-dir").ensure_dir()
utils.move("a-file.txt", target="a-dir")
# Then the src should be moved
assert tmpfolder.join("a-dir/a-file.txt").check()
# When it is moved with pretend kwarg,
utils.move("another-file.txt", target="a-dir", pretend=True)
# Then the src should not be moved
assert not tmpfolder.join("a-dir/another-file.txt").check()
assert tmpfolder.join("another-file.txt").check()
def test_move_log(tmpfolder, caplog):
caplog.set_level(logging.INFO)
fname1 = uniqstr() # Use a unique name to get easily identifiable logs
fname2 = uniqstr()
dname = uniqstr()
# Given a file or directory exists,
tmpfolder.join(fname1).write("text")
tmpfolder.join(fname2).write("text")
# When it is moved without log kwarg,
tmpfolder.join(dname).ensure_dir()
utils.move(fname1, target=dname)
# Then no log should be created.
logs = caplog.text
assert not re.search("move.+{}.+to.+{}".format(fname1, dname), logs)
# When it is moved with log kwarg,
utils.move(fname2, target=dname, log=True)
# Then log should be created.
logs = caplog.text
assert re.search("move.+{}.+to.+{}".format(fname2, dname), logs)
def test_move_multiple_args(tmpfolder):
# Given several files exist,
tmpfolder.join("a-file.txt").write("text")
tmpfolder.join("another-file.txt").write("text")
assert not tmpfolder.join("a-dir/a-file.txt").check()
assert not tmpfolder.join("a-dir/another-file.txt").check()
# When they are moved together,
tmpfolder.join("a-dir").ensure_dir()
utils.move("a-file.txt", "another-file.txt", target="a-dir")
# Then the original paths should not exist
assert not tmpfolder.join("a-file.txt").check()
assert not tmpfolder.join("another-file.txt").check()
# And the new paths should exist
assert tmpfolder.join("a-dir/a-file.txt").read() == "text"
assert tmpfolder.join("a-dir/another-file.txt").read() == "text"
def test_pretend_move(tmpfolder):
# Given a file or directory exists,
tmpfolder.join("a-file.txt").write("text")
tmpfolder.join("another-file.txt").write("text")
# When it is moved without pretend kwarg,
tmpfolder.join("a-dir").ensure_dir()
utils.move("a-file.txt", target="a-dir")
# Then the src should be moved
assert tmpfolder.join("a-dir/a-file.txt").check()
# When it is moved with pretend kwarg,
utils.move("another-file.txt", target="a-dir", pretend=True)
# Then the src should not be moved
assert not tmpfolder.join("a-dir/another-file.txt").check()
assert tmpfolder.join("another-file.txt").check()