Skip to content

Commit 42096c5

Browse files
committed
test: migrate test_ignore to new helpers
1 parent c6c67ac commit 42096c5

File tree

1 file changed

+68
-139
lines changed

1 file changed

+68
-139
lines changed

tests/func/test_ignore.py

Lines changed: 68 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,115 @@
1-
import itertools
1+
# encoding: utf-8
2+
from __future__ import unicode_literals
23
import os
34
import shutil
4-
55
import pytest
66

77
from dvc.exceptions import DvcIgnoreInCollectedDirError
8-
from dvc.ignore import DvcIgnore
9-
from dvc.ignore import DvcIgnoreDirs
10-
from dvc.ignore import DvcIgnoreFilter
11-
from dvc.ignore import DvcIgnorePatterns
8+
from dvc.ignore import DvcIgnore, DvcIgnoreDirs, DvcIgnorePatterns
129
from dvc.scm.tree import WorkingTree
13-
from dvc.utils.compat import cast_bytes
10+
from dvc.utils import walk_files
11+
from dvc.utils.compat import fspath
1412
from dvc.utils.fs import get_mtime_and_size
15-
from tests.basic_env import TestDvc
16-
from tests.utils import to_posixpath
17-
18-
19-
class TestDvcIgnore(TestDvc):
20-
def setUp(self):
21-
super(TestDvcIgnore, self).setUp()
22-
23-
def _get_all_paths(self):
24-
25-
paths = []
26-
for root, dirs, files in self.dvc.tree.walk(
27-
self.dvc.root_dir, dvcignore=self.dvc.dvcignore
28-
):
29-
for dname in dirs:
30-
paths.append(os.path.join(root, dname))
31-
32-
for fname in files:
33-
paths.append(os.path.join(root, fname))
3413

35-
return paths
3614

37-
def test_ignore_in_child_dir(self):
38-
ignore_file = os.path.join(self.dvc.root_dir, DvcIgnore.DVCIGNORE_FILE)
39-
with open(ignore_file, "w") as fobj:
40-
fobj.write("data_dir/data")
15+
def test_ignore_in_child_dir(tmp_dir, dvc):
16+
tmp_dir.gen({"dir": {"ignored": "text", "other": "text2"}})
17+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/ignored")
4118

42-
forbidden_path = os.path.join(self.dvc.root_dir, self.DATA)
43-
all_paths = self._get_all_paths()
19+
assert set(walk_files("dir", dvc.dvcignore)) == {"dir/other"}
4420

45-
self.assertNotIn(forbidden_path, all_paths)
4621

47-
def test_ignore_in_child_dir_unicode(self):
48-
ignore_file = os.path.join(self.dvc.root_dir, DvcIgnore.DVCIGNORE_FILE)
49-
with open(ignore_file, "wb") as fobj:
50-
fobj.write(cast_bytes(self.UNICODE, "utf-8"))
22+
def test_ignore_in_parent_dir(tmp_dir, dvc, monkeypatch):
23+
tmp_dir.gen({"dir": {"ignored": "text", "other": "text2"}})
24+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/ignored")
5125

52-
forbidden_path = os.path.join(self.dvc.root_dir, self.UNICODE)
53-
all_paths = self._get_all_paths()
26+
monkeypatch.chdir("dir")
27+
assert set(walk_files(".", dvc.dvcignore)) == {"./other"}
5428

55-
self.assertNotIn(forbidden_path, all_paths)
5629

57-
def test_ignore_in_parent_dir(self):
58-
ignore_file = os.path.join(self.dvc.root_dir, DvcIgnore.DVCIGNORE_FILE)
59-
with open(ignore_file, "w") as fobj:
60-
fobj.write("data_dir/data")
30+
def test_ignore_unicode(tmp_dir, dvc):
31+
tmp_dir.gen({"dir": {"тест": "проверка", "other": "text"}})
32+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/тест")
6133

62-
os.chdir(self.DATA_DIR)
34+
assert set(walk_files("dir", dvc.dvcignore)) == {"dir/other"}
6335

64-
forbidden_path = os.path.join(self.dvc.root_dir, self.DATA)
65-
all_paths = self._get_all_paths()
6636

67-
self.assertNotIn(forbidden_path, all_paths)
37+
def test_rename_ignored_file(tmp_dir, dvc):
38+
tmp_dir.gen({"dir": {"ignored": "...", "other": "text"}})
6839

40+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "ignored*")
41+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
6942

70-
def test_metadata_unchanged_when_moving_ignored_file(dvc_repo, repo_dir):
71-
new_data_path = repo_dir.DATA_SUB + "_new"
43+
shutil.move("dir/ignored", "dir/ignored_new")
44+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
7245

73-
ignore_file = os.path.join(dvc_repo.root_dir, DvcIgnore.DVCIGNORE_FILE)
74-
repo_dir.create(
75-
ignore_file,
76-
"\n".join(
77-
[to_posixpath(repo_dir.DATA_SUB), to_posixpath(new_data_path)]
78-
),
79-
)
46+
assert new_mtime == mtime and new_size == size
8047

81-
mtime_sig, size = get_mtime_and_size(repo_dir.DATA_DIR, dvc_repo.dvcignore)
8248

83-
shutil.move(repo_dir.DATA_SUB, new_data_path)
49+
def test_rename_file(tmp_dir, dvc):
50+
tmp_dir.gen({"dir": {"foo": "foo", "bar": "bar"}})
51+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
8452

85-
new_mtime_sig, new_size = get_mtime_and_size(
86-
repo_dir.DATA_DIR, dvc_repo.dvcignore
87-
)
53+
shutil.move("dir/foo", "dir/foo_new")
54+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
8855

89-
assert new_mtime_sig == mtime_sig
90-
assert new_size == size
56+
assert new_mtime != mtime and new_size == size
9157

9258

93-
def test_mtime_changed_when_moving_non_ignored_file(dvc_repo, repo_dir):
94-
new_data_path = repo_dir.DATA_SUB + "_new"
95-
mtime, size = get_mtime_and_size(repo_dir.DATA_DIR, dvc_repo.dvcignore)
59+
def test_remove_ignored_file(tmp_dir, dvc):
60+
tmp_dir.gen({"dir": {"ignored": "...", "other": "text"}})
61+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/ignored")
9662

97-
shutil.move(repo_dir.DATA_SUB, new_data_path)
98-
new_mtime, new_size = get_mtime_and_size(
99-
repo_dir.DATA_DIR, dvc_repo.dvcignore
100-
)
63+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
10164

102-
assert new_mtime != mtime
103-
assert new_size == size
65+
os.remove("dir/ignored")
66+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
10467

68+
assert new_mtime == mtime and new_size == size
10569

106-
def test_metadata_unchanged_on_ignored_file_deletion(dvc_repo, repo_dir):
107-
ignore_file = os.path.join(dvc_repo.root_dir, DvcIgnore.DVCIGNORE_FILE)
108-
repo_dir.create(ignore_file, to_posixpath(repo_dir.DATA_SUB))
10970

110-
mtime, size = get_mtime_and_size(repo_dir.DATA_DIR, dvc_repo.dvcignore)
71+
def test_remove_file(tmp_dir, dvc):
72+
tmp_dir.gen({"dir": {"foo": "foo", "bar": "bar"}})
73+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
11174

112-
os.remove(repo_dir.DATA_SUB)
113-
new_mtime, new_size = get_mtime_and_size(
114-
repo_dir.DATA_DIR, dvc_repo.dvcignore
115-
)
75+
os.remove("dir/foo")
76+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
11677

117-
assert new_mtime == mtime
118-
assert new_size == size
78+
assert new_mtime != mtime and new_size != size
11979

12080

121-
def test_metadata_changed_on_non_ignored_file_deletion(dvc_repo, repo_dir):
122-
mtime, size = get_mtime_and_size(repo_dir.DATA_DIR, dvc_repo.dvcignore)
81+
def test_dvcignore_in_out_dir(tmp_dir, dvc):
82+
tmp_dir.gen({"dir": {"foo": "foo", DvcIgnore.DVCIGNORE_FILE: ""}})
12383

124-
os.remove(repo_dir.DATA_SUB)
125-
new_mtime_sig, new_size = get_mtime_and_size(
126-
repo_dir.DATA_DIR, dvc_repo.dvcignore
127-
)
84+
with pytest.raises(DvcIgnoreInCollectedDirError):
85+
dvc.add("dir")
12886

129-
assert new_mtime_sig != mtime
130-
assert new_size != size
13187

88+
# @efiop why do we need to parametrize this?
89+
@pytest.mark.parametrize("dname", ["dir", "dir/subdir"])
90+
def test_ignore_collecting_dvcignores(tmp_dir, dvc, dname):
91+
tmp_dir.gen({"dir": {"subdir": {}}})
13292

133-
def test_should_raise_on_dvcignore_in_out_dir(dvc_repo, repo_dir):
134-
ignore_file = os.path.join(repo_dir.DATA_DIR, DvcIgnore.DVCIGNORE_FILE)
135-
repo_dir.create(ignore_file, "")
93+
top_ignore_file = (tmp_dir / dname).with_name(DvcIgnore.DVCIGNORE_FILE)
94+
top_ignore_file.write_text(os.path.basename(dname))
13695

137-
with pytest.raises(DvcIgnoreInCollectedDirError):
138-
dvc_repo.add(repo_dir.DATA_DIR)
96+
ignore_file = tmp_dir / dname / DvcIgnore.DVCIGNORE_FILE
97+
ignore_file.write_text("foo")
13998

99+
assert dvc.dvcignore.ignores == {
100+
DvcIgnoreDirs([".git", ".hg", ".dvc"]),
101+
DvcIgnorePatterns(fspath(top_ignore_file), WorkingTree(dvc.root_dir)),
102+
}
140103

141-
@pytest.mark.parametrize("dname", [TestDvc.DATA_DIR, TestDvc.DATA_SUB_DIR])
142-
def test_ignore_collecting_dvcignores(repo_dir, dname):
143-
top_ignore_file = os.path.join(
144-
repo_dir.root_dir, os.path.dirname(dname), DvcIgnore.DVCIGNORE_FILE
145-
)
146-
repo_dir.create(top_ignore_file, os.path.basename(dname))
147104

148-
ignore_file = os.path.join(
149-
repo_dir.root_dir, dname, DvcIgnore.DVCIGNORE_FILE
150-
)
151-
repo_dir.create(ignore_file, repo_dir.FOO)
105+
def test_ignore_on_branch(tmp_dir, scm, dvc):
106+
tmp_dir.scm_gen({"foo": "foo", "bar": "bar"}, commit="add files")
152107

153-
assert DvcIgnoreFilter(
154-
repo_dir.root_dir, WorkingTree(repo_dir.root_dir)
155-
).ignores == {
156-
DvcIgnoreDirs([".git", ".hg", ".dvc"]),
157-
DvcIgnorePatterns(top_ignore_file, WorkingTree(repo_dir.root_dir)),
158-
}
108+
scm.checkout("branch", create_new=True)
109+
tmp_dir.scm_gen(DvcIgnore.DVCIGNORE_FILE, "foo", commit="add ignore")
159110

111+
scm.checkout("master")
112+
assert set(walk_files(".", dvc.dvcignore)) == {"./foo", "./bar"}
160113

161-
def test_ignore_on_branch(git, dvc_repo, repo_dir):
162-
dvc_repo.add(repo_dir.DATA_DIR)
163-
dvc_repo.scm.commit("add data dir")
164-
165-
branch_name = "branch_one"
166-
dvc_repo.scm.checkout(branch_name, create_new=True)
167-
168-
repo_dir.create(DvcIgnore.DVCIGNORE_FILE, to_posixpath(repo_dir.DATA_SUB))
169-
dvc_repo.scm.add([DvcIgnore.DVCIGNORE_FILE])
170-
git.index.commit("add ignore")
171-
172-
dvc_repo.scm.checkout("master")
173-
174-
git_tree = dvc_repo.scm.get_tree(branch_name)
175-
branch_data_files = set(
176-
itertools.chain.from_iterable(
177-
[
178-
files
179-
for _, _, files in dvc_repo.tree.walk(
180-
repo_dir.DATA_DIR,
181-
dvcignore=DvcIgnoreFilter(repo_dir.root_dir, git_tree),
182-
)
183-
]
184-
)
185-
)
186-
assert branch_data_files == {"data"}
114+
dvc.tree = scm.get_tree("branch")
115+
assert set(walk_files(".", dvc.dvcignore)) == {"./bar"}

0 commit comments

Comments
 (0)