Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get_bq_config_path() to _cloud_sdk.py #1358

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions google/auth/_cloud_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

# The ~/.config subdirectory containing gcloud credentials.
_CONFIG_DIRECTORY = "gcloud"
# The ~/.config subdirectory containing gcloud credentials for bq.
_CONFIG_DIRECTORY_BQ = "bq"
# Windows systems store config at %APPDATA%\gcloud
_WINDOWS_CONFIG_ROOT_ENV_VAR = "APPDATA"
# The name of the file in the Cloud SDK config that contains default
Expand All @@ -42,8 +44,11 @@
)


def get_config_path():
"""Returns the absolute path the the Cloud SDK's configuration directory.
def get_config_path(config_directory=_CONFIG_DIRECTORY):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should wrap this function with a gcloud_get_config_path so the intent is clearly documented.

Would it be a large change to modify existing uses of get_config_path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, not too much overhead to modify existing uses of get_config_path

"""Returns the absolute path of the given configuration directory.

Args:
config_directory: The absolute path of the configuration directory.

Returns:
str: The Cloud SDK config path.
Expand All @@ -70,6 +75,15 @@ def get_config_path():
return os.path.join(drive, "\\", _CONFIG_DIRECTORY)


def get_bq_config_path():
"""Returns the absolute path of bq's configuration directory.

Returns:
str: The bq config path.
"""
return get_config_path(config_directory=_CONFIG_DIRECTORY_BQ)


def get_application_default_credentials_path():
"""Gets the path to the application default credentials file.

Expand Down
36 changes: 36 additions & 0 deletions tests/test__cloud_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,42 @@ def test_get_config_path_no_appdata(monkeypatch):
assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)


def test_get_bq_config_path_env_var(monkeypatch):
config_path_sentinel = "config_path"
monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
config_path = _cloud_sdk.get_bq_config_path()
assert config_path == config_path_sentinel


@mock.patch("os.path.expanduser")
def test_get_bq_config_path_unix(expanduser):
expanduser.side_effect = lambda path: path

config_path = _cloud_sdk.get_bq_config_path()

assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY_BQ)


@mock.patch("os.name", new="nt")
def test_get_bq_config_path_windows(monkeypatch):
appdata = "appdata"
monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)

config_path = _cloud_sdk.get_bq_config_path()

assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY_BQ)


@mock.patch("os.name", new="nt")
def test_get_bq_config_path_no_appdata(monkeypatch):
monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
monkeypatch.setenv("SystemDrive", "G:")

config_path = _cloud_sdk.get_bq_config_path()

assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY_BQ)


@mock.patch("os.name", new="nt")
@mock.patch("subprocess.check_output", autospec=True)
def test_get_auth_access_token_windows(check_output):
Expand Down