-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_external_repo.py
246 lines (186 loc) · 7.48 KB
/
test_external_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import os
from unittest.mock import ANY
from scmrepo.git import Git
from dvc.data.stage import stage
from dvc.data.transfer import transfer
from dvc.external_repo import CLONES, external_repo
from dvc.utils import relpath
from dvc.utils.fs import makedirs, remove
from tests.unit.fs.test_repo import make_subrepo
from tests.utils import clean_staging
def test_external_repo(erepo_dir, mocker):
with erepo_dir.chdir():
with erepo_dir.branch("branch", new=True):
erepo_dir.dvc_gen("file", "branch", commit="create file on branch")
erepo_dir.dvc_gen("file", "master", commit="create file on master")
url = os.fspath(erepo_dir)
clone_spy = mocker.spy(Git, "clone")
with external_repo(url) as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "master"
with external_repo(url, rev="branch") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "branch"
assert clone_spy.call_count == 1
def test_source_change(erepo_dir):
url = os.fspath(erepo_dir)
with external_repo(url) as repo:
old_rev = repo.scm.get_rev()
erepo_dir.scm_gen("file", "text", commit="a change")
with external_repo(url) as repo:
new_rev = repo.scm.get_rev()
assert old_rev != new_rev
def test_cache_reused(erepo_dir, mocker, local_cloud):
import dvc.fs.utils
erepo_dir.add_remote(config=local_cloud.config)
with erepo_dir.chdir():
erepo_dir.dvc_gen("file", "text", commit="add file")
erepo_dir.dvc.push()
download_spy = mocker.spy(dvc.fs.utils, "transfer")
# Use URL to prevent any fishy optimizations
url = f"file://{erepo_dir}"
with external_repo(url) as repo:
repo.fetch()
assert download_spy.mock.call_count == 1
# Should not download second time
erepo_dir.scm.branch("branch")
with external_repo(url, "branch") as repo:
repo.fetch()
assert download_spy.mock.call_count == 1
def test_known_sha(erepo_dir):
erepo_dir.scm.commit("init")
url = f"file://{erepo_dir}"
with external_repo(url) as repo:
rev = repo.scm.get_rev()
prev_rev = repo.scm.resolve_rev("HEAD^")
# Hits cache
with external_repo(url, rev) as repo:
pass
# No clone, no pull, copies a repo, checks out the known sha
with external_repo(url, prev_rev) as repo:
pass
def test_pull_subdir_file(tmp_dir, erepo_dir):
with erepo_dir.chdir():
subdir = erepo_dir / "subdir"
subdir.mkdir()
(subdir / "file").write_text("contents")
erepo_dir.dvc_add(subdir / "file", commit="create file")
dest = tmp_dir / "file"
with external_repo(os.fspath(erepo_dir)) as repo:
repo.repo_fs.get(
"subdir/file",
os.fspath(dest),
)
assert dest.is_file()
assert dest.read_text() == "contents"
def test_relative_remote(erepo_dir, tmp_dir):
# these steps reproduce the script on this issue:
# https://github.com/iterative/dvc/issues/2756
with erepo_dir.chdir():
erepo_dir.dvc_gen("file", "contents", commit="create file")
upstream_dir = tmp_dir
upstream_url = relpath(upstream_dir, erepo_dir)
erepo_dir.add_remote(url=upstream_url)
erepo_dir.dvc.push()
(erepo_dir / "file").unlink()
remove(erepo_dir.dvc.odb.local.cache_dir)
url = os.fspath(erepo_dir)
with external_repo(url) as repo:
assert os.path.isabs(repo.config["remote"]["upstream"]["url"])
assert os.path.isdir(repo.config["remote"]["upstream"]["url"])
with repo.open_by_relpath("file") as fd:
assert fd.read() == "contents"
def test_shallow_clone_branch(erepo_dir, mocker):
with erepo_dir.chdir():
with erepo_dir.branch("branch", new=True):
erepo_dir.dvc_gen("file", "branch", commit="create file on branch")
erepo_dir.dvc_gen("file", "master", commit="create file on master")
url = os.fspath(erepo_dir)
clone_spy = mocker.spy(Git, "clone")
with external_repo(url, rev="branch") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "branch"
clone_spy.assert_called_with(
url, ANY, shallow_branch="branch", progress=ANY
)
path, _ = CLONES[url]
CLONES[url] = (path, True)
mock_fetch = mocker.patch.object(Git, "fetch")
with external_repo(url) as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "master"
mock_fetch.assert_called_with(unshallow=True)
def test_shallow_clone_tag(erepo_dir, mocker):
with erepo_dir.chdir():
erepo_dir.dvc_gen("file", "foo", commit="init")
erepo_dir.scm.tag("v1")
erepo_dir.dvc_gen("file", "bar", commit="update file")
url = os.fspath(erepo_dir)
clone_spy = mocker.spy(Git, "clone")
with external_repo(url, rev="v1") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "foo"
clone_spy.assert_called_with(url, ANY, shallow_branch="v1", progress=ANY)
path, _ = CLONES[url]
CLONES[url] = (path, True)
mock_fetch = mocker.patch.object(Git, "fetch")
with external_repo(url, rev="master") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "bar"
mock_fetch.assert_called_with(unshallow=True)
def test_subrepos_are_ignored(tmp_dir, erepo_dir):
subrepo = erepo_dir / "dir" / "subrepo"
make_subrepo(subrepo, erepo_dir.scm)
with erepo_dir.chdir():
erepo_dir.dvc_gen("dir/foo", "foo", commit="foo")
erepo_dir.scm_gen("dir/bar", "bar", commit="bar")
with subrepo.chdir():
subrepo.dvc_gen({"file": "file"}, commit="add files on subrepo")
with external_repo(os.fspath(erepo_dir)) as repo:
repo.repo_fs.get(
"dir",
os.fspath(tmp_dir / "out"),
)
expected_files = {"foo": "foo", "bar": "bar", ".gitignore": "/foo\n"}
assert (tmp_dir / "out").read_text() == expected_files
# clear cache to test saving to cache
cache_dir = tmp_dir / repo.odb.local.cache_dir
remove(cache_dir)
clean_staging()
makedirs(cache_dir)
staging, _, obj = stage(
repo.odb.local,
"dir",
repo.repo_fs,
"md5",
dvcignore=repo.dvcignore,
)
transfer(
staging,
repo.odb.local,
{obj.hash_info},
shallow=False,
hardlink=True,
)
assert set(cache_dir.glob("??/*")) == {
cache_dir / "e1" / "d9e8eae5374860ae025ec84cfd85c7",
cache_dir / "e1" / "d9e8eae5374860ae025ec84cfd85c7.dir",
cache_dir / "37" / "b51d194a7513e45b56f6524f2d51f2",
cache_dir / "94" / "7d2b84e5aa88170e80dff467a5bfb6",
cache_dir / "ac" / "bd18db4cc2f85cedef654fccc4a4d8",
}
def test_subrepos_are_ignored_for_git_tracked_dirs(tmp_dir, erepo_dir):
subrepo = erepo_dir / "dir" / "subrepo"
make_subrepo(subrepo, erepo_dir.scm)
with erepo_dir.chdir():
scm_files = {"foo": "foo", "bar": "bar", "subdir": {"lorem": "lorem"}}
erepo_dir.scm_gen({"dir": scm_files}, commit="add scm dir")
with subrepo.chdir():
subrepo.dvc_gen({"file": "file"}, commit="add files on subrepo")
with external_repo(os.fspath(erepo_dir)) as repo:
repo.repo_fs.get(
"dir",
os.fspath(tmp_dir / "out"),
)
# subrepo files should not be here
assert (tmp_dir / "out").read_text() == scm_files