Skip to content

Commit 5adb714

Browse files
committed
gdrive: add tests for the gdrive_user_credentials_file relative path
1 parent 6deecbe commit 5adb714

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

dvc/remote/gdrive.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,12 @@ def _validate_config(self):
179179
)
180180
)
181181

182+
# Helper to determine where will it read credentials from.
183+
# Mostly useful for tests, exception messages, etc
184+
# Returns either env variable name if it's set or actual path to the
185+
# credentials file
182186
@cached_property
183-
def _credentials_location(self):
187+
def credentials_location(self):
184188
if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA):
185189
return GDriveRemote.GDRIVE_CREDENTIALS_DATA
186190
if os.path.exists(self._gdrive_user_credentials_path):
@@ -274,7 +278,7 @@ def _drive(self):
274278
# a lot of different errors - broken credentials file, refresh token
275279
# expired, flow failed, etc.
276280
except Exception as exc:
277-
raise GDriveAuthError(self._credentials_location) from exc
281+
raise GDriveAuthError(self.credentials_location) from exc
278282
finally:
279283
if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA):
280284
os.remove(self._gdrive_user_credentials_path)

tests/func/remote/test_gdrive.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import posixpath
3+
4+
import configobj
5+
6+
from dvc.compat import fspath
7+
from dvc.main import main
8+
from dvc.remote import GDriveRemote
9+
from dvc.repo import Repo
10+
11+
12+
def test_relative_user_credentials_file_config_setting(tmp_dir, dvc):
13+
# CI sets it to test GDrive, here we want to test the work with file system
14+
# based, regular credentials
15+
if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA):
16+
del os.environ[GDriveRemote.GDRIVE_CREDENTIALS_DATA]
17+
18+
credentials = os.path.join("secrets", "credentials.json")
19+
20+
# GDriveRemote.credentials_location helper checks for file existence,
21+
# create the file
22+
tmp_dir.gen(credentials, "{'token': 'test'}")
23+
24+
remote_name = "gdrive"
25+
assert (
26+
main(["remote", "add", "-d", remote_name, "gdrive://root/test"]) == 0
27+
)
28+
assert (
29+
main(
30+
[
31+
"remote",
32+
"modify",
33+
remote_name,
34+
"gdrive_user_credentials_file",
35+
credentials,
36+
]
37+
)
38+
== 0
39+
)
40+
41+
# We need to load repo again to test updates to the config
42+
str_path = fspath(tmp_dir)
43+
repo = Repo(str_path)
44+
45+
# Check that in config we got the path relative to the config file itself
46+
# Also, we store posix path even on Windows
47+
config = configobj.ConfigObj(repo.config.files["repo"])
48+
assert config['remote "{}"'.format(remote_name)][
49+
"gdrive_user_credentials_file"
50+
] == posixpath.join("..", "secrets", "credentials.json")
51+
52+
# Check that in the remote itself we got an absolute path
53+
remote = repo.cloud.get_remote(remote_name)
54+
assert os.path.normpath(remote.credentials_location) == os.path.join(
55+
str_path, credentials
56+
)

0 commit comments

Comments
 (0)