Skip to content

Commit 477488e

Browse files
authored
Merge pull request #2950 from mroutis/tests-repo
tests: use dir_helpers on repo
2 parents 825e9fc + 7dedd7f commit 477488e

File tree

3 files changed

+67
-100
lines changed

3 files changed

+67
-100
lines changed

tests/func/test_destroy.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/func/test_repo.py

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
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
35
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
4538
)
46-
result = self._check("new_branch", "buzz", False, [["buzz"]])
47-
self.assertEqual([str(i) for i in result[0].deps], ["bar"])
4839

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"}
4957

50-
class TestIgnore(TestDvcGit):
51-
def _stage_name(self, file):
52-
return file + Stage.STAGE_FILE_SUFFIX
5358

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)
5762

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"})
6064

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+
}
6270

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")
6672

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"}

tests/unit/test_repo.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import os
22

3-
from tests.basic_env import TestDvc
43

5-
6-
class TestIsDvcInternal(TestDvc):
7-
def test_return_false_on_non_dvc_file(self):
8-
path = os.path.join("path", "to-non-.dvc", "file")
9-
self.assertFalse(self.dvc.is_dvc_internal(path))
10-
11-
def test_return_true_on_dvc_internal_file(self):
12-
path = os.path.join("path", "to", ".dvc", "file")
13-
self.assertTrue(path)
4+
def test_is_dvc_internal(dvc):
5+
assert dvc.is_dvc_internal(os.path.join("path", "to", ".dvc", "file"))
6+
assert not dvc.is_dvc_internal(os.path.join("path", "to-non-.dvc", "file"))

0 commit comments

Comments
 (0)