Skip to content

Commit fa7a3bb

Browse files
authored
feat: add get_bq_config_path() to _cloud_sdk.py (#1358)
feat: add get_bq_config_path() to _cloud_sdk.py
1 parent af0eb18 commit fa7a3bb

3 files changed

Lines changed: 83 additions & 21 deletions

File tree

packages/google-auth/google/auth/_cloud_sdk.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424

2525
# The ~/.config subdirectory containing gcloud credentials.
26-
_CONFIG_DIRECTORY = "gcloud"
26+
_CONFIG_DIRECTORY_GCLOUD = "gcloud"
27+
# The ~/.config subdirectory containing gcloud credentials for bq.
28+
_CONFIG_DIRECTORY_BQ = "bq"
2729
# Windows systems store config at %APPDATA%\gcloud
2830
_WINDOWS_CONFIG_ROOT_ENV_VAR = "APPDATA"
2931
# The name of the file in the Cloud SDK config that contains default
@@ -42,32 +44,53 @@
4244
)
4345

4446

45-
def get_config_path():
46-
"""Returns the absolute path the the Cloud SDK's configuration directory.
47+
def get_config_path(config_directory=_CONFIG_DIRECTORY_GCLOUD):
48+
"""Returns the absolute path of the given configuration directory.
49+
50+
Args:
51+
config_directory: The absolute path of the configuration directory.
4752
4853
Returns:
49-
str: The Cloud SDK config path.
54+
str: The config path.
5055
"""
5156
# If the path is explicitly set, return that.
5257
try:
5358
return os.environ[environment_vars.CLOUD_SDK_CONFIG_DIR]
5459
except KeyError:
5560
pass
5661

57-
# Non-windows systems store this at ~/.config/gcloud
62+
# Non-windows systems store this at ~/.config/<config_directory>.
5863
if os.name != "nt":
59-
return os.path.join(os.path.expanduser("~"), ".config", _CONFIG_DIRECTORY)
60-
# Windows systems store config at %APPDATA%\gcloud
64+
return os.path.join(os.path.expanduser("~"), ".config", config_directory)
65+
# Windows systems store config at %APPDATA%\<config_directory>.
6166
else:
6267
try:
6368
return os.path.join(
64-
os.environ[_WINDOWS_CONFIG_ROOT_ENV_VAR], _CONFIG_DIRECTORY
69+
os.environ[_WINDOWS_CONFIG_ROOT_ENV_VAR], config_directory
6570
)
6671
except KeyError:
6772
# This should never happen unless someone is really
6873
# messing with things, but we'll cover the case anyway.
6974
drive = os.environ.get("SystemDrive", "C:")
70-
return os.path.join(drive, "\\", _CONFIG_DIRECTORY)
75+
return os.path.join(drive, "\\", config_directory)
76+
77+
78+
def get_gcloud_config_path():
79+
"""Returns the absolute path of Cloud CLI's configuration directory.
80+
81+
Returns:
82+
str: The Cloud CLI config path.
83+
"""
84+
return get_config_path(config_directory=_CONFIG_DIRECTORY_GCLOUD)
85+
86+
87+
def get_bq_config_path():
88+
"""Returns the absolute path of bq's configuration directory.
89+
90+
Returns:
91+
str: The bq config path.
92+
"""
93+
return get_config_path(config_directory=_CONFIG_DIRECTORY_BQ)
7194

7295

7396
def get_application_default_credentials_path():
@@ -78,7 +101,7 @@ def get_application_default_credentials_path():
78101
Returns:
79102
str: The full path to application default credentials.
80103
"""
81-
config_path = get_config_path()
104+
config_path = get_gcloud_config_path()
82105
return os.path.join(config_path, _CREDENTIALS_FILENAME)
83106

84107

0 Bytes
Binary file not shown.

packages/google-auth/tests/test__cloud_sdk.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,40 +112,79 @@ def test_get_application_default_credentials_path(get_config_dir):
112112
)
113113

114114

115-
def test_get_config_path_env_var(monkeypatch):
115+
def test_get_gcloud_config_path_env_var(monkeypatch):
116116
config_path_sentinel = "config_path"
117117
monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
118-
config_path = _cloud_sdk.get_config_path()
118+
config_path = _cloud_sdk.get_gcloud_config_path()
119119
assert config_path == config_path_sentinel
120120

121121

122122
@mock.patch("os.path.expanduser")
123-
def test_get_config_path_unix(expanduser):
123+
def test_get_gcloud_config_path_unix(expanduser):
124124
expanduser.side_effect = lambda path: path
125125

126-
config_path = _cloud_sdk.get_config_path()
126+
config_path = _cloud_sdk.get_gcloud_config_path()
127127

128-
assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY)
128+
assert os.path.split(config_path) == (
129+
"~/.config",
130+
_cloud_sdk._CONFIG_DIRECTORY_GCLOUD,
131+
)
132+
133+
134+
@mock.patch("os.name", new="nt")
135+
def test_get_gcloud_config_path_windows(monkeypatch):
136+
appdata = "appdata"
137+
monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
138+
139+
config_path = _cloud_sdk.get_gcloud_config_path()
140+
141+
assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY_GCLOUD)
142+
143+
144+
@mock.patch("os.name", new="nt")
145+
def test_get_gcloud_config_path_no_appdata(monkeypatch):
146+
monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
147+
monkeypatch.setenv("SystemDrive", "G:")
148+
149+
config_path = _cloud_sdk.get_gcloud_config_path()
150+
151+
assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY_GCLOUD)
152+
153+
154+
def test_get_bq_config_path_env_var(monkeypatch):
155+
config_path_sentinel = "config_path"
156+
monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
157+
config_path = _cloud_sdk.get_bq_config_path()
158+
assert config_path == config_path_sentinel
159+
160+
161+
@mock.patch("os.path.expanduser")
162+
def test_get_bq_config_path_unix(expanduser):
163+
expanduser.side_effect = lambda path: path
164+
165+
config_path = _cloud_sdk.get_bq_config_path()
166+
167+
assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY_BQ)
129168

130169

131170
@mock.patch("os.name", new="nt")
132-
def test_get_config_path_windows(monkeypatch):
171+
def test_get_bq_config_path_windows(monkeypatch):
133172
appdata = "appdata"
134173
monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
135174

136-
config_path = _cloud_sdk.get_config_path()
175+
config_path = _cloud_sdk.get_bq_config_path()
137176

138-
assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY)
177+
assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY_BQ)
139178

140179

141180
@mock.patch("os.name", new="nt")
142-
def test_get_config_path_no_appdata(monkeypatch):
181+
def test_get_bq_config_path_no_appdata(monkeypatch):
143182
monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
144183
monkeypatch.setenv("SystemDrive", "G:")
145184

146-
config_path = _cloud_sdk.get_config_path()
185+
config_path = _cloud_sdk.get_bq_config_path()
147186

148-
assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)
187+
assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY_BQ)
149188

150189

151190
@mock.patch("os.name", new="nt")

0 commit comments

Comments
 (0)