Skip to content

Commit 302b016

Browse files
authored
Merge pull request #2913 from Suor/migrate-test-ignore
test: migrate test_ignore
2 parents 3796c41 + 6c12a2e commit 302b016

File tree

2 files changed

+79
-141
lines changed

2 files changed

+79
-141
lines changed

tests/dir_helpers.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from funcy.py3 import lmap, retry
77

88
from dvc.utils import makedirs
9-
from dvc.utils.compat import basestring, is_py2, pathlib, fspath, fspath_py35
9+
from dvc.utils.compat import basestring, pathlib, fspath, fspath_py35, bytes
1010

1111

1212
__all__ = ["tmp_dir", "scm", "dvc", "repo_template", "run_copy", "erepo_dir"]
@@ -55,13 +55,16 @@ def _gen(self, struct, prefix=None):
5555
path = (prefix or self) / name
5656

5757
if isinstance(contents, dict):
58-
self._gen(contents, prefix=path)
58+
if not contents:
59+
makedirs(path, exist_ok=True)
60+
else:
61+
self._gen(contents, prefix=path)
5962
else:
6063
makedirs(path.parent, exist_ok=True)
61-
if is_py2 and isinstance(contents, str):
64+
if isinstance(contents, bytes):
6265
path.write_bytes(contents)
6366
else:
64-
path.write_text(contents)
67+
path.write_text(contents, encoding="utf-8")
6568

6669
def dvc_gen(self, struct, text="", commit=None):
6770
paths = self.gen(struct, text)

tests/func/test_ignore.py

Lines changed: 72 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,121 @@
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-
1813

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))
14+
from tests.utils import to_posixpath
3115

32-
for fname in files:
33-
paths.append(os.path.join(root, fname))
3416

35-
return paths
17+
def test_ignore(tmp_dir, dvc, monkeypatch):
18+
tmp_dir.gen({"dir": {"ignored": "text", "other": "text2"}})
19+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/ignored")
3620

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")
21+
assert _files_set("dir", dvc.dvcignore) == {"dir/other"}
4122

42-
forbidden_path = os.path.join(self.dvc.root_dir, self.DATA)
43-
all_paths = self._get_all_paths()
23+
monkeypatch.chdir("dir")
24+
assert _files_set(".", dvc.dvcignore) == {"./other"}
4425

45-
self.assertNotIn(forbidden_path, all_paths)
4626

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"))
27+
def test_ignore_unicode(tmp_dir, dvc):
28+
tmp_dir.gen({"dir": {"other": "text"}})
29+
# Path() doesn't handle unicode paths in Windows/Python 2
30+
# I don't know to debug it further, waiting till Python 2 EOL
31+
with open("dir/тест", "wb") as fd:
32+
fd.write("проверка".encode("utf-8"))
5133

52-
forbidden_path = os.path.join(self.dvc.root_dir, self.UNICODE)
53-
all_paths = self._get_all_paths()
34+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/тест")
5435

55-
self.assertNotIn(forbidden_path, all_paths)
36+
assert _files_set("dir", dvc.dvcignore) == {"dir/other"}
5637

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

62-
os.chdir(self.DATA_DIR)
39+
def test_rename_ignored_file(tmp_dir, dvc):
40+
tmp_dir.gen({"dir": {"ignored": "...", "other": "text"}})
6341

64-
forbidden_path = os.path.join(self.dvc.root_dir, self.DATA)
65-
all_paths = self._get_all_paths()
42+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "ignored*")
43+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
6644

67-
self.assertNotIn(forbidden_path, all_paths)
45+
shutil.move("dir/ignored", "dir/ignored_new")
46+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
6847

48+
assert new_mtime == mtime and new_size == size
6949

70-
def test_metadata_unchanged_when_moving_ignored_file(dvc_repo, repo_dir):
71-
new_data_path = repo_dir.DATA_SUB + "_new"
7250

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-
)
51+
def test_rename_file(tmp_dir, dvc):
52+
tmp_dir.gen({"dir": {"foo": "foo", "bar": "bar"}})
53+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
8054

81-
mtime_sig, size = get_mtime_and_size(repo_dir.DATA_DIR, dvc_repo.dvcignore)
55+
shutil.move("dir/foo", "dir/foo_new")
56+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
8257

83-
shutil.move(repo_dir.DATA_SUB, new_data_path)
58+
assert new_mtime != mtime and new_size == size
8459

85-
new_mtime_sig, new_size = get_mtime_and_size(
86-
repo_dir.DATA_DIR, dvc_repo.dvcignore
87-
)
8860

89-
assert new_mtime_sig == mtime_sig
90-
assert new_size == size
61+
def test_remove_ignored_file(tmp_dir, dvc):
62+
tmp_dir.gen({"dir": {"ignored": "...", "other": "text"}})
63+
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/ignored")
9164

65+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
9266

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)
67+
os.remove("dir/ignored")
68+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
9669

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-
)
70+
assert new_mtime == mtime and new_size == size
10171

102-
assert new_mtime != mtime
103-
assert new_size == size
10472

73+
def test_remove_file(tmp_dir, dvc):
74+
tmp_dir.gen({"dir": {"foo": "foo", "bar": "bar"}})
75+
mtime, size = get_mtime_and_size("dir", dvc.dvcignore)
10576

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))
77+
os.remove("dir/foo")
78+
new_mtime, new_size = get_mtime_and_size("dir", dvc.dvcignore)
10979

110-
mtime, size = get_mtime_and_size(repo_dir.DATA_DIR, dvc_repo.dvcignore)
80+
assert new_mtime != mtime and new_size != size
11181

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

117-
assert new_mtime == mtime
118-
assert new_size == size
83+
def test_dvcignore_in_out_dir(tmp_dir, dvc):
84+
tmp_dir.gen({"dir": {"foo": "foo", DvcIgnore.DVCIGNORE_FILE: ""}})
11985

86+
with pytest.raises(DvcIgnoreInCollectedDirError):
87+
dvc.add("dir")
12088

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

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-
)
90+
# @efiop why do we need to parametrize this?
91+
@pytest.mark.parametrize("dname", ["dir", "dir/subdir"])
92+
def test_ignore_collecting_dvcignores(tmp_dir, dvc, dname):
93+
tmp_dir.gen({"dir": {"subdir": {}}})
12894

129-
assert new_mtime_sig != mtime
130-
assert new_size != size
95+
top_ignore_file = (tmp_dir / dname).with_name(DvcIgnore.DVCIGNORE_FILE)
96+
top_ignore_file.write_text(os.path.basename(dname))
13197

98+
ignore_file = tmp_dir / dname / DvcIgnore.DVCIGNORE_FILE
99+
ignore_file.write_text("foo")
132100

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, "")
101+
assert dvc.dvcignore.ignores == {
102+
DvcIgnoreDirs([".git", ".hg", ".dvc"]),
103+
DvcIgnorePatterns(fspath(top_ignore_file), WorkingTree(dvc.root_dir)),
104+
}
136105

137-
with pytest.raises(DvcIgnoreInCollectedDirError):
138-
dvc_repo.add(repo_dir.DATA_DIR)
139106

107+
def test_ignore_on_branch(tmp_dir, scm, dvc):
108+
tmp_dir.scm_gen({"foo": "foo", "bar": "bar"}, commit="add files")
140109

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))
110+
scm.checkout("branch", create_new=True)
111+
tmp_dir.scm_gen(DvcIgnore.DVCIGNORE_FILE, "foo", commit="add ignore")
147112

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)
113+
scm.checkout("master")
114+
assert _files_set(".", dvc.dvcignore) == {"./foo", "./bar"}
152115

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-
}
116+
dvc.tree = scm.get_tree("branch")
117+
assert _files_set(".", dvc.dvcignore) == {"./bar"}
159118

160119

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"}
120+
def _files_set(root, dvcignore):
121+
return {to_posixpath(f) for f in walk_files(root, dvcignore)}

0 commit comments

Comments
 (0)