Skip to content

Commit 603da1c

Browse files
dberenbaumdberenbaum
authored andcommitted
api: update remote config tests
1 parent 377d0c9 commit 603da1c

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

dvc/api/data.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ def _wrap_exceptions(repo, url):
2525
raise PathMissingError(exc.path, url) from exc
2626

2727

28-
def get_url(path, repo=None, rev=None, **kwargs):
28+
def get_url(
29+
path: str,
30+
repo: Optional[str] = None,
31+
rev: Optional[str] = None,
32+
remote: Optional[str] = None,
33+
config: Optional[dict[str, Any]] = None,
34+
remote_config: Optional[dict[str, Any]] = None,
35+
):
2936
"""
3037
Returns the URL to the storage location of a data file or directory tracked
3138
in a DVC repo. For Git repos, HEAD is used unless a rev argument is
@@ -35,11 +42,45 @@ def get_url(path, repo=None, rev=None, **kwargs):
3542
3643
NOTE: This function does not check for the actual existence of the file or
3744
directory in the remote storage.
45+
46+
Args:
47+
path (str): location and file name of the target, relative to the root
48+
of `repo`.
49+
repo (str, optional): location of the DVC project or Git Repo.
50+
Defaults to the current DVC project (found by walking up from the
51+
current working directory tree).
52+
It can be a URL or a file system path.
53+
Both HTTP and SSH protocols are supported for online Git repos
54+
(e.g. [user@]server:project.git).
55+
rev (str, optional): Any `Git revision`_ such as a branch or tag name,
56+
a commit hash or a dvc experiment name.
57+
Defaults to HEAD.
58+
If `repo` is not a Git repo, this option is ignored.
59+
remote (str, optional): Name of the `DVC remote`_ used to form the
60+
returned URL string.
61+
Defaults to the `default remote`_ of `repo`.
62+
For local projects, the cache is tried before the default remote.
63+
config(dict, optional): config to be passed to the DVC repository.
64+
Defaults to None.
65+
remote_config(dict, optional): remote config to be passed to the DVC
66+
repository.
67+
Defaults to None.
68+
69+
Returns:
70+
str: URL to the file or directory.
3871
"""
3972
from dvc.config import NoRemoteError
4073
from dvc_data.index import StorageKeyError
4174

42-
with Repo.open(repo, rev=rev, subrepos=True, uninitialized=True, **kwargs) as _repo:
75+
with Repo.open(
76+
repo,
77+
rev=rev,
78+
subrepos=True,
79+
uninitialized=True,
80+
remote=remote,
81+
config=config,
82+
remote_config=remote_config,
83+
) as _repo:
4384
index, entry = _repo.get_data_index_entry(path)
4485
with reraise(
4586
(StorageKeyError, ValueError),

tests/func/api/test_data.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@ def test_get_url_requires_dvc(tmp_dir, scm):
3131
api.get_url("foo", repo=f"file://{tmp_dir.as_posix()}")
3232

3333

34+
def test_get_url_from_remote(tmp_dir, erepo_dir, cloud, local_cloud):
35+
erepo_dir.add_remote(config=cloud.config, name="other")
36+
erepo_dir.add_remote(config=local_cloud.config, default=True)
37+
with erepo_dir.chdir():
38+
erepo_dir.dvc_gen("foo", "foo", commit="add foo")
39+
40+
# Using file url to force clone to tmp repo
41+
repo_url = f"file://{erepo_dir.as_posix()}"
42+
expected_rel_path = os.path.join(
43+
"files", "md5", "ac/bd18db4cc2f85cedef654fccc4a4d8"
44+
)
45+
46+
# Test default remote
47+
assert api.get_url("foo", repo=repo_url) == (local_cloud / expected_rel_path).url
48+
49+
# Test remote arg
50+
assert (
51+
api.get_url("foo", repo=repo_url, remote="other")
52+
== (cloud / expected_rel_path).url
53+
)
54+
55+
# Test config arg
56+
assert (
57+
api.get_url("foo", repo=repo_url, config={"core": {"remote": "other"}})
58+
== (cloud / expected_rel_path).url
59+
)
60+
61+
# Test remote_config arg
62+
assert (
63+
api.get_url("foo", repo=repo_url, remote_config={"url": cloud.url})
64+
== (cloud / expected_rel_path).url
65+
)
66+
67+
3468
def test_open_external(tmp_dir, erepo_dir, cloud):
3569
erepo_dir.add_remote(config=cloud.config)
3670

@@ -253,3 +287,12 @@ def test_read_from_remote(tmp_dir, erepo_dir, cloud, local_cloud):
253287
)
254288
== "foo content"
255289
)
290+
291+
assert (
292+
api.read(
293+
os.path.join("dir", "foo"),
294+
repo=f"file://{erepo_dir.as_posix()}",
295+
remote_config={"url": cloud.url},
296+
)
297+
== "foo content"
298+
)

0 commit comments

Comments
 (0)