-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtest_repo.py
168 lines (125 loc) · 4.4 KB
/
test_repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import os
import shutil
import pytest
from dvc.exceptions import OutputDuplicationError
from dvc.repo import NotDvcRepoError, Repo, locked
from dvc_data.hashfile.hash_info import HashInfo
def test_is_dvc_internal(dvc):
assert dvc.is_dvc_internal(os.path.join("path", "to", ".dvc", "file"))
assert not dvc.is_dvc_internal(os.path.join("path", "to-non-.dvc", "file"))
@pytest.mark.parametrize(
"path",
[
os.path.join("dir", "subdir", "file"),
os.path.join("dir", "subdir"),
"dir",
],
)
def test_find_outs_by_path(tmp_dir, dvc, path):
(stage,) = tmp_dir.dvc_gen(
{"dir": {"subdir": {"file": "file"}, "other": "other"}}
)
outs = dvc.find_outs_by_path(path, strict=False)
assert len(outs) == 1
assert outs[0].fs_path == stage.outs[0].fs_path
def test_find_outs_by_path_does_graph_checks(tmp_dir, dvc):
tmp_dir.dvc_gen("foo", "foo")
shutil.copyfile("foo.dvc", "foo-2.dvc")
dvc._reset()
with pytest.raises(OutputDuplicationError):
dvc.find_outs_by_path("foo")
@pytest.mark.parametrize(
"path",
[os.path.join("dir", "subdir", "file"), os.path.join("dir", "subdir")],
)
def test_used_objs(tmp_dir, dvc, path):
tmp_dir.dvc_gen({"dir": {"subdir": {"file": "file"}, "other": "other"}})
expected = {
HashInfo("md5", "70922d6bf66eb073053a82f77d58c536.dir"),
HashInfo("md5", "8c7dd922ad47494fc02c388e12c00eac"),
}
used = set()
for _, obj_ids in dvc.used_objs([path]).items():
used.update(obj_ids)
assert used == expected
def test_used_objs_ignore_revs(tmp_dir, scm, dvc):
def _get_used_values(dvc):
used_obj_ids = list(dvc.used_objs(all_commits=True).values())[0]
return {o.value for o in used_obj_ids}
obj_ids = {}
revs = {}
for i in range(3):
o = tmp_dir.dvc_gen("foo", str(i), commit=str(i))
obj_ids[i] = o[0].outs[0].hash_info.value
revs[i] = scm.get_rev()
assert set(obj_ids.values()) == _get_used_values(dvc)
with dvc.fs.open(dvc.ignore_revs_file, "w") as f:
f.write(revs[0])
expected = {obj_ids[1], obj_ids[2]}
assert expected == _get_used_values(dvc)
with dvc.fs.open(dvc.ignore_revs_file, "w") as f:
f.write(f"{revs[0]}\n{revs[1]}")
expected = {obj_ids[2]}
assert expected == _get_used_values(dvc)
def test_locked(mocker):
repo = mocker.MagicMock()
repo._lock_depth = 0
repo.method = locked(repo.method)
args = ()
kwargs = {}
repo.method(repo, args, kwargs)
assert repo.method_calls == [
mocker.call._reset(),
mocker.call.method(repo, args, kwargs),
mocker.call._reset(),
]
def test_skip_graph_checks(tmp_dir, dvc, mocker, run_copy):
# See https://github.com/iterative/dvc/issues/2671 for more info
from dvc.repo.index import Index
mock_build_graph = mocker.spy(Index.graph, "fget")
# sanity check
tmp_dir.gen("foo", "foo text")
dvc.add("foo")
run_copy("foo", "bar", single_stage=True)
assert mock_build_graph.called
# check that our hack can be enabled
mock_build_graph.reset_mock()
dvc._skip_graph_checks = True
tmp_dir.gen("baz", "baz text")
dvc.add("baz")
run_copy("baz", "qux", single_stage=True)
assert not mock_build_graph.called
# check that our hack can be disabled
mock_build_graph.reset_mock()
dvc._skip_graph_checks = False
tmp_dir.gen("quux", "quux text")
dvc.add("quux")
run_copy("quux", "quuz", single_stage=True)
assert mock_build_graph.called
def test_branch_config(tmp_dir, scm):
tmp_dir.scm_gen("foo", "foo", commit="init")
# sanity check
with pytest.raises(NotDvcRepoError):
Repo().close()
scm.checkout("branch", create_new=True)
dvc = Repo.init()
with dvc.config.edit() as conf:
conf["remote"]["branch"] = {"url": "/some/path"}
dvc.close()
scm.add([os.path.join(".dvc", "config")])
scm.commit("init dvc")
scm.checkout("master")
with pytest.raises(NotDvcRepoError):
Repo(rev="master").close()
dvc = Repo(rev="branch")
try:
assert dvc.config["remote"]["branch"]["url"] == "/some/path"
finally:
dvc.close()
def test_dynamic_cache_initalization(tmp_dir, scm):
dvc = Repo.init()
with dvc.config.edit() as conf:
conf["cache"]["ssh"] = "foo"
conf["remote"]["foo"] = {"url": "remote://bar/baz"}
dvc.close()
Repo(str(tmp_dir)).close()