|
1 | | -from dvc.ignore import DvcIgnore |
2 | | -from dvc.main import main |
| 1 | +import os |
| 2 | + |
| 3 | +from dvc.scm.git.tree import GitTree |
| 4 | +from dvc.cache import Cache |
3 | 5 | from dvc.repo import Repo |
4 | | -from dvc.scm.git import GitTree |
5 | | -from dvc.scm.tree import WorkingTree |
6 | | -from dvc.stage import Stage |
7 | | -from tests.basic_env import TestDvcGit |
8 | | - |
9 | | - |
10 | | -class TestCollect(TestDvcGit): |
11 | | - def setUp(self): |
12 | | - super(TestCollect, self).setUp() |
13 | | - self.dvc.add(self.FOO) |
14 | | - self.dvc.run( |
15 | | - deps=[self.FOO], |
16 | | - outs=[self.BAR], |
17 | | - cmd="python code.py {} {}".format(self.FOO, self.BAR), |
18 | | - ) |
19 | | - self.dvc.scm.add([".gitignore", self.FOO + ".dvc", self.BAR + ".dvc"]) |
20 | | - self.dvc.scm.commit("foo.dvc and bar.dvc") |
21 | | - self.dvc.scm.checkout("new_branch", True) |
22 | | - self.dvc.run( |
23 | | - deps=[self.BAR], |
24 | | - outs=["buzz"], |
25 | | - cmd="python code.py {} {}".format(self.BAR, "buzz"), |
26 | | - ) |
27 | | - self.dvc.scm.add([".gitignore", "buzz.dvc"]) |
28 | | - self.dvc.scm.commit("add buzz") |
29 | | - self.dvc.scm.checkout("master") |
30 | | - |
31 | | - def _check(self, branch, target, with_deps, expected): |
32 | | - if branch: |
33 | | - self.dvc.tree = GitTree(self.dvc.scm.repo, branch) |
34 | | - else: |
35 | | - self.dvc.tree = WorkingTree() |
36 | | - result = self.dvc.collect(target + ".dvc", with_deps=with_deps) |
37 | | - self.assertEqual([[str(j) for j in i.outs] for i in result], expected) |
38 | | - return result |
39 | | - |
40 | | - def test(self): |
41 | | - self._check("", self.BAR, True, [[self.FOO], [self.BAR]]) |
42 | | - self._check("master", self.BAR, True, [[self.FOO], [self.BAR]]) |
43 | | - self._check( |
44 | | - "new_branch", "buzz", True, [[self.FOO], [self.BAR], ["buzz"]] |
| 6 | +from dvc.system import System |
| 7 | +from dvc.utils.compat import fspath |
| 8 | + |
| 9 | + |
| 10 | +def test_destroy(tmp_dir, dvc): |
| 11 | + dvc.config.set("cache", "type", "symlink") |
| 12 | + dvc.cache = Cache(dvc) |
| 13 | + |
| 14 | + tmp_dir.dvc_gen("file", "text") |
| 15 | + tmp_dir.dvc_gen({"dir": {"file": "lorem", "subdir/file": "ipsum"}}) |
| 16 | + |
| 17 | + dvc.destroy() |
| 18 | + |
| 19 | + # Remove all the files related to DVC |
| 20 | + for path in [".dvc", "file.dvc", "dir.dvc"]: |
| 21 | + assert not (tmp_dir / path).exists() |
| 22 | + |
| 23 | + # Leave the rest of the files |
| 24 | + for path in ["file", "dir/file", "dir/subdir/file"]: |
| 25 | + assert (tmp_dir / path).is_file() |
| 26 | + |
| 27 | + # Make sure that data was unprotected after `destroy` |
| 28 | + for path in ["file", "dir", "dir/file", "dir/subdir", "dir/subdir/file"]: |
| 29 | + assert not System.is_symlink(fspath(tmp_dir / path)) |
| 30 | + |
| 31 | + |
| 32 | +def test_collect(tmp_dir, scm, dvc, run_copy): |
| 33 | + def collect_outs(*args, **kwargs): |
| 34 | + return set( |
| 35 | + str(out.path_info) |
| 36 | + for stage in dvc.collect(*args, **kwargs) |
| 37 | + for out in stage.outs |
45 | 38 | ) |
46 | | - result = self._check("new_branch", "buzz", False, [["buzz"]]) |
47 | | - self.assertEqual([str(i) for i in result[0].deps], ["bar"]) |
48 | 39 |
|
| 40 | + tmp_dir.dvc_gen("foo", "foo") |
| 41 | + run_copy("foo", "bar") |
| 42 | + scm.add([".gitignore", "foo.dvc", "bar.dvc"]) |
| 43 | + scm.commit("Add foo and bar") |
| 44 | + |
| 45 | + scm.checkout("new-branch", create_new=True) |
| 46 | + |
| 47 | + run_copy("bar", "buzz") |
| 48 | + scm.add([".gitignore", "buzz.dvc"]) |
| 49 | + scm.commit("Add buzz") |
| 50 | + |
| 51 | + assert collect_outs("bar.dvc", with_deps=True) == {"foo", "bar"} |
| 52 | + |
| 53 | + dvc.tree = GitTree(scm.repo, "new-branch") |
| 54 | + |
| 55 | + assert collect_outs("buzz.dvc", with_deps=True) == {"foo", "bar", "buzz"} |
| 56 | + assert collect_outs("buzz.dvc", with_deps=False) == {"buzz"} |
49 | 57 |
|
50 | | -class TestIgnore(TestDvcGit): |
51 | | - def _stage_name(self, file): |
52 | | - return file + Stage.STAGE_FILE_SUFFIX |
53 | 58 |
|
54 | | - def test_should_not_gather_stage_files_from_ignored_dir(self): |
55 | | - ret = main(["add", self.FOO, self.BAR, self.DATA, self.DATA_SUB]) |
56 | | - self.assertEqual(0, ret) |
| 59 | +def test_stages(tmp_dir, dvc): |
| 60 | + def stages(): |
| 61 | + return set(stage.relpath for stage in Repo(fspath(tmp_dir)).stages) |
57 | 62 |
|
58 | | - stages = self.dvc.stages |
59 | | - self.assertEqual(4, len(stages)) |
| 63 | + tmp_dir.dvc_gen({"file": "a", "dir/file": "b", "dir/subdir/file": "c"}) |
60 | 64 |
|
61 | | - self.create(DvcIgnore.DVCIGNORE_FILE, self.DATA_DIR) |
| 65 | + assert stages() == { |
| 66 | + "file.dvc", |
| 67 | + os.path.join("dir", "file.dvc"), |
| 68 | + os.path.join("dir", "subdir", "file.dvc"), |
| 69 | + } |
62 | 70 |
|
63 | | - self.dvc = Repo(self.dvc.root_dir) |
64 | | - stages = self.dvc.stages |
65 | | - self.assertEqual(2, len(stages)) |
| 71 | + tmp_dir.gen(".dvcignore", "dir") |
66 | 72 |
|
67 | | - stagenames = [s.relpath for s in stages] |
68 | | - self.assertIn(self._stage_name(self.FOO), stagenames) |
69 | | - self.assertIn(self._stage_name(self.BAR), stagenames) |
70 | | - self.assertNotIn(self._stage_name(self.DATA), stagenames) |
71 | | - self.assertNotIn(self._stage_name(self.DATA_SUB), stagenames) |
| 73 | + assert stages() == {"file.dvc"} |
0 commit comments