mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
migrate test_move.py to pytest
This commit is contained in:
committed by
skshetry
parent
bda1388bff
commit
ecd371f8ae
+87
-156
@@ -4,198 +4,129 @@ import textwrap
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from dvc.cli import main
|
from dvc.cli import main
|
||||||
from dvc.dvcfile import DVC_FILE_SUFFIX
|
|
||||||
from dvc.exceptions import DvcException, MoveNotDataSourceError
|
from dvc.exceptions import DvcException, MoveNotDataSourceError
|
||||||
from dvc.utils.serialize import load_yaml
|
|
||||||
from tests.basic_env import TestDvc, TestDvcGit
|
|
||||||
from tests.func.test_repro import TestRepro
|
|
||||||
from tests.utils import cd
|
|
||||||
|
|
||||||
|
|
||||||
class TestMove(TestDvc):
|
def test_move(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("foo", "foo")
|
||||||
dst = self.FOO + "1"
|
dvc.move("foo", "foo1")
|
||||||
self.dvc.add(self.FOO)
|
|
||||||
self.dvc.move(self.FOO, dst)
|
|
||||||
|
|
||||||
self.assertFalse(os.path.isfile(self.FOO))
|
assert not (tmp_dir / "foo").is_file()
|
||||||
self.assertTrue(os.path.isfile(dst))
|
assert (tmp_dir / "foo1").is_file()
|
||||||
|
|
||||||
|
|
||||||
class TestMoveNonExistentFile(TestDvc):
|
def test_move_non_existent_file(dvc):
|
||||||
def test(self):
|
with pytest.raises(DvcException):
|
||||||
with self.assertRaises(DvcException):
|
dvc.move("non_existent_file", "dst")
|
||||||
self.dvc.move("non_existent_file", "dst")
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveDirectory(TestDvc):
|
def test_move_directory(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("data", {"foo": "foo", "bar": "bar"})
|
||||||
dst = "dst"
|
dvc.move("data", "dst")
|
||||||
stages = self.dvc.add(self.DATA_DIR)
|
assert not (tmp_dir / "data").is_dir()
|
||||||
self.assertEqual(len(stages), 1)
|
assert (tmp_dir / "dst").is_dir()
|
||||||
self.assertTrue(stages[0] is not None)
|
|
||||||
self.dvc.move(self.DATA_DIR, dst)
|
|
||||||
self.assertFalse(os.path.exists(self.DATA_DIR))
|
|
||||||
self.assertTrue(os.path.exists(dst))
|
|
||||||
|
|
||||||
|
|
||||||
class TestCmdMove(TestDvc):
|
def test_cmd_move(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("foo", "foo")
|
||||||
stages = self.dvc.add(self.FOO)
|
assert main(["move", "foo", "foo" + "1"]) == 0
|
||||||
self.assertEqual(len(stages), 1)
|
assert main(["move", "non-existing-file", "dst"]) != 0
|
||||||
self.assertTrue(stages[0] is not None)
|
|
||||||
|
|
||||||
ret = main(["move", self.FOO, self.FOO + "1"])
|
|
||||||
self.assertEqual(ret, 0)
|
|
||||||
|
|
||||||
ret = main(["move", "non-existing-file", "dst"])
|
|
||||||
self.assertNotEqual(ret, 0)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveNotDataSource(TestRepro):
|
def test_move_not_data_source(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("foo", "foo")
|
||||||
from dvc.repo import Repo as DvcRepo
|
dvc.run(
|
||||||
|
cmd="cp foo file1",
|
||||||
|
outs=["file1"],
|
||||||
|
deps=["foo"],
|
||||||
|
single_stage=True,
|
||||||
|
)
|
||||||
|
|
||||||
self.dvc = DvcRepo(self._root_dir)
|
with pytest.raises(MoveNotDataSourceError):
|
||||||
with self.assertRaises(MoveNotDataSourceError):
|
dvc.move("file1", "dst")
|
||||||
self.dvc.move(self.file1, "dst")
|
|
||||||
|
|
||||||
ret = main(["move", self.file1, "dst"])
|
assert main(["move", "file1", "dst"]) != 0
|
||||||
self.assertNotEqual(ret, 0)
|
assert (tmp_dir / "file1").exists()
|
||||||
|
|
||||||
|
|
||||||
class TestMoveFileWithExtension(TestDvc):
|
def test_move_file_with_extension(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("file.csv", "1,2,3\n")
|
||||||
with open(
|
|
||||||
os.path.join(self.dvc.root_dir, "file.csv"), "w", encoding="utf-8"
|
|
||||||
) as fd:
|
|
||||||
fd.write("1,2,3\n")
|
|
||||||
|
|
||||||
self.dvc.add("file.csv")
|
assert main(["move", "file.csv", "other_name.csv"]) == 0
|
||||||
|
assert not (tmp_dir / "file.csv").exists()
|
||||||
self.assertTrue(os.path.exists("file.csv"))
|
assert not (tmp_dir / "file.csv.dvc").exists()
|
||||||
self.assertTrue(os.path.exists("file.csv.dvc"))
|
assert (tmp_dir / "other_name.csv").exists()
|
||||||
|
assert (tmp_dir / "other_name.csv.dvc").exists()
|
||||||
ret = main(["move", "file.csv", "other_name.csv"])
|
|
||||||
self.assertEqual(ret, 0)
|
|
||||||
|
|
||||||
self.assertFalse(os.path.exists("file.csv"))
|
|
||||||
self.assertFalse(os.path.exists("file.csv.dvc"))
|
|
||||||
self.assertTrue(os.path.exists("other_name.csv"))
|
|
||||||
self.assertTrue(os.path.exists("other_name.csv.dvc"))
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveFileToDirectory(TestDvc):
|
def test_move_file_to_directory(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("foo", "foo")
|
||||||
foo_dvc_file = self.FOO + DVC_FILE_SUFFIX
|
tmp_dir.gen({"data": {"bar": "bar"}})
|
||||||
ret = main(["add", self.FOO])
|
|
||||||
self.assertEqual(ret, 0)
|
|
||||||
self.assertTrue(os.path.exists(foo_dvc_file))
|
|
||||||
|
|
||||||
new_foo_path = os.path.join(self.DATA_DIR, self.FOO)
|
assert main(["move", "foo", os.path.join("data", "foo")]) == 0
|
||||||
new_foo_dvc_path = new_foo_path + DVC_FILE_SUFFIX
|
assert not (tmp_dir / "foo").exists()
|
||||||
ret = main(["move", self.FOO, new_foo_path])
|
assert not (tmp_dir / "foo.dvc").exists()
|
||||||
self.assertEqual(ret, 0)
|
assert (tmp_dir / "data" / "foo").exists()
|
||||||
|
assert (tmp_dir / "data" / "foo.dvc").exists()
|
||||||
self.assertFalse(os.path.exists(self.FOO))
|
|
||||||
self.assertFalse(os.path.exists(foo_dvc_file))
|
|
||||||
self.assertTrue(os.path.exists(new_foo_path))
|
|
||||||
self.assertTrue(os.path.exists(new_foo_dvc_path))
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveFileToDirectoryWithoutSpecifiedTargetName(TestDvc):
|
def test_move_file_to_directory_without_specified_target_name(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.dvc_gen("foo", "foo")
|
||||||
foo_stage_file_path = self.FOO + DVC_FILE_SUFFIX
|
tmp_dir.gen({"data": {"bar": "bar"}})
|
||||||
ret = main(["add", self.FOO])
|
|
||||||
self.assertEqual(ret, 0)
|
|
||||||
self.assertTrue(os.path.exists(foo_stage_file_path))
|
|
||||||
|
|
||||||
target_foo_path = os.path.join(self.DATA_DIR, self.FOO)
|
assert main(["move", "foo", "data"]) == 0
|
||||||
target_foo_stage_file_path = target_foo_path + DVC_FILE_SUFFIX
|
assert not (tmp_dir / "foo").exists()
|
||||||
|
assert not (tmp_dir / "foo.dvc").exists()
|
||||||
|
assert (tmp_dir / "data" / "foo").exists()
|
||||||
|
assert (tmp_dir / "data" / "foo.dvc").exists()
|
||||||
|
|
||||||
ret = main(["move", self.FOO, self.DATA_DIR])
|
new_stage = (tmp_dir / "data" / "foo.dvc").load_yaml()
|
||||||
self.assertEqual(ret, 0)
|
assert new_stage["outs"][0]["path"] == "foo"
|
||||||
|
|
||||||
self.assertFalse(os.path.exists(self.FOO))
|
|
||||||
self.assertFalse(os.path.exists(foo_stage_file_path))
|
|
||||||
|
|
||||||
self.assertTrue(os.path.exists(target_foo_path))
|
|
||||||
self.assertTrue(os.path.exists(target_foo_stage_file_path))
|
|
||||||
|
|
||||||
new_stage = load_yaml(target_foo_stage_file_path)
|
|
||||||
self.assertEqual(self.FOO, new_stage["outs"][0]["path"])
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveDirectoryShouldNotOverwriteExisting(TestDvcGit):
|
def test_move_directory_should_not_overwrite_existing(tmp_dir, dvc, scm):
|
||||||
def test(self):
|
tmp_dir.dvc_gen({"data": {"foo": "foo"}})
|
||||||
dir_name = "dir"
|
new_dir = tmp_dir / "dir"
|
||||||
orig_listdir = set(os.listdir(self.DATA_DIR))
|
new_dir.mkdir()
|
||||||
|
|
||||||
self.dvc.add(self.DATA_DIR)
|
dvc.move("data", "dir")
|
||||||
|
assert not (tmp_dir / "data").exists()
|
||||||
os.mkdir(dir_name)
|
assert not (tmp_dir / "data.dvc").exists()
|
||||||
new_dir_name = os.path.join(dir_name, self.DATA_DIR)
|
assert set(new_dir.iterdir()) == {
|
||||||
|
new_dir / ".gitignore",
|
||||||
self.dvc.move(self.DATA_DIR, dir_name)
|
new_dir / "data.dvc",
|
||||||
|
new_dir / "data",
|
||||||
data_dir_stage = self.DATA_DIR + DVC_FILE_SUFFIX
|
}
|
||||||
self.assertFalse(os.path.exists(self.DATA_DIR))
|
assert set((new_dir / "data").iterdir()) == {new_dir / "data" / "foo"}
|
||||||
self.assertFalse(os.path.exists(data_dir_stage))
|
|
||||||
|
|
||||||
self.assertTrue(os.path.exists(dir_name))
|
|
||||||
self.assertEqual(
|
|
||||||
set(os.listdir(dir_name)),
|
|
||||||
{".gitignore", data_dir_stage, self.DATA_DIR},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertTrue(os.path.exists(new_dir_name))
|
|
||||||
self.assertTrue(os.path.isfile(new_dir_name + DVC_FILE_SUFFIX))
|
|
||||||
self.assertEqual(set(os.listdir(new_dir_name)), orig_listdir)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveFileBetweenDirectories(TestDvc):
|
def test_move_file_between_directories(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.gen({"data": {"foo": "foo"}})
|
||||||
data_stage_file = self.DATA + DVC_FILE_SUFFIX
|
dvc.add(os.path.join("data", "foo"))
|
||||||
ret = main(["add", self.DATA])
|
|
||||||
self.assertEqual(ret, 0)
|
|
||||||
self.assertTrue(os.path.exists(data_stage_file))
|
|
||||||
|
|
||||||
new_data_dir = "data_dir2"
|
(tmp_dir / "data2").mkdir()
|
||||||
os.makedirs(new_data_dir)
|
|
||||||
|
|
||||||
ret = main(["move", self.DATA, new_data_dir])
|
assert main(["move", os.path.join("data", "foo"), "data2"]) == 0
|
||||||
self.assertEqual(ret, 0)
|
assert not (tmp_dir / "data" / "foo").exists()
|
||||||
|
assert not (tmp_dir / "data" / "foo.dvc").exists()
|
||||||
|
assert (tmp_dir / "data2" / "foo").exists()
|
||||||
|
assert (tmp_dir / "data2" / "foo.dvc").exists()
|
||||||
|
|
||||||
new_data_path = os.path.join(new_data_dir, os.path.basename(self.DATA))
|
d = (tmp_dir / "data2" / "foo.dvc").load_yaml()
|
||||||
new_data_stage_file = new_data_path + DVC_FILE_SUFFIX
|
assert d["outs"][0]["path"] == "foo"
|
||||||
|
|
||||||
self.assertFalse(os.path.exists(self.DATA))
|
|
||||||
self.assertFalse(os.path.exists(data_stage_file))
|
|
||||||
|
|
||||||
self.assertTrue(os.path.exists(new_data_path))
|
|
||||||
self.assertTrue(os.path.exists(new_data_stage_file))
|
|
||||||
|
|
||||||
new_stage_file = load_yaml(new_data_stage_file)
|
|
||||||
self.assertEqual(
|
|
||||||
os.path.basename(self.DATA), new_stage_file["outs"][0]["path"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMoveFileInsideDirectory(TestDvc):
|
def test_move_file_inside_directory(tmp_dir, dvc):
|
||||||
def test(self):
|
tmp_dir.gen({"data": {"foo": "foo"}})
|
||||||
ret = main(["add", self.DATA])
|
file = tmp_dir / "data" / "foo"
|
||||||
self.assertEqual(ret, 0)
|
dvc.add(file.fs_path)
|
||||||
|
|
||||||
with cd(self.DATA_DIR):
|
with (tmp_dir / "data").chdir():
|
||||||
ret = main(["move", os.path.basename(self.DATA), "data.txt"])
|
assert main(["move", "foo", "data.txt"]) == 0
|
||||||
self.assertEqual(ret, 0)
|
|
||||||
|
|
||||||
self.assertFalse(os.path.exists(self.DATA))
|
assert not file.exists()
|
||||||
|
assert (tmp_dir / "data" / "data.txt").exists()
|
||||||
data_fullpath = os.path.join(self.DATA_DIR, "data.txt")
|
assert (tmp_dir / "data" / "data.txt.dvc").exists()
|
||||||
dvc_fullpath = os.path.join(self.DATA_DIR, "data.txt.dvc")
|
|
||||||
self.assertTrue(os.path.exists(data_fullpath))
|
|
||||||
self.assertTrue(os.path.exists(dvc_fullpath))
|
|
||||||
|
|
||||||
|
|
||||||
def test_move_should_save_stage_info(tmp_dir, dvc):
|
def test_move_should_save_stage_info(tmp_dir, dvc):
|
||||||
|
|||||||
Reference in New Issue
Block a user