diff --git a/google/auth/_cloud_sdk.py b/google/auth/_cloud_sdk.py index 7cd41434a..a94411949 100644 --- a/google/auth/_cloud_sdk.py +++ b/google/auth/_cloud_sdk.py @@ -23,9 +23,7 @@ # The ~/.config subdirectory containing gcloud credentials. -_CONFIG_DIRECTORY_GCLOUD = "gcloud" -# The ~/.config subdirectory containing gcloud credentials for bq. -_CONFIG_DIRECTORY_BQ = "bq" +_CONFIG_DIRECTORY = "gcloud" # 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 @@ -44,14 +42,11 @@ ) -def get_config_path(config_directory=_CONFIG_DIRECTORY_GCLOUD): - """Returns the absolute path of the given configuration directory. - - Args: - config_directory: The absolute path of the configuration directory. +def get_config_path(): + """Returns the absolute path the the Cloud SDK's configuration directory. Returns: - str: The config path. + str: The Cloud SDK config path. """ # If the path is explicitly set, return that. try: @@ -59,38 +54,20 @@ def get_config_path(config_directory=_CONFIG_DIRECTORY_GCLOUD): except KeyError: pass - # Non-windows systems store this at ~/.config/. + # Non-windows systems store this at ~/.config/gcloud if os.name != "nt": - return os.path.join(os.path.expanduser("~"), ".config", config_directory) - # Windows systems store config at %APPDATA%\. + return os.path.join(os.path.expanduser("~"), ".config", _CONFIG_DIRECTORY) + # Windows systems store config at %APPDATA%\gcloud else: try: return os.path.join( - os.environ[_WINDOWS_CONFIG_ROOT_ENV_VAR], config_directory + os.environ[_WINDOWS_CONFIG_ROOT_ENV_VAR], _CONFIG_DIRECTORY ) except KeyError: # This should never happen unless someone is really # messing with things, but we'll cover the case anyway. drive = os.environ.get("SystemDrive", "C:") - return os.path.join(drive, "\\", config_directory) - - -def get_gcloud_config_path(): - """Returns the absolute path of Cloud CLI's configuration directory. - - Returns: - str: The Cloud CLI config path. - """ - return get_config_path(config_directory=_CONFIG_DIRECTORY_GCLOUD) - - -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) + return os.path.join(drive, "\\", _CONFIG_DIRECTORY) def get_application_default_credentials_path(): @@ -101,7 +78,7 @@ def get_application_default_credentials_path(): Returns: str: The full path to application default credentials. """ - config_path = get_gcloud_config_path() + config_path = get_config_path() return os.path.join(config_path, _CREDENTIALS_FILENAME) diff --git a/tests/test__cloud_sdk.py b/tests/test__cloud_sdk.py index 4eaf1b18f..e45c65bd9 100644 --- a/tests/test__cloud_sdk.py +++ b/tests/test__cloud_sdk.py @@ -112,79 +112,40 @@ def test_get_application_default_credentials_path(get_config_dir): ) -def test_get_gcloud_config_path_env_var(monkeypatch): +def test_get_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_gcloud_config_path() + config_path = _cloud_sdk.get_config_path() assert config_path == config_path_sentinel @mock.patch("os.path.expanduser") -def test_get_gcloud_config_path_unix(expanduser): +def test_get_config_path_unix(expanduser): expanduser.side_effect = lambda path: path - config_path = _cloud_sdk.get_gcloud_config_path() + config_path = _cloud_sdk.get_config_path() - assert os.path.split(config_path) == ( - "~/.config", - _cloud_sdk._CONFIG_DIRECTORY_GCLOUD, - ) - - -@mock.patch("os.name", new="nt") -def test_get_gcloud_config_path_windows(monkeypatch): - appdata = "appdata" - monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata) - - config_path = _cloud_sdk.get_gcloud_config_path() - - assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY_GCLOUD) - - -@mock.patch("os.name", new="nt") -def test_get_gcloud_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_gcloud_config_path() - - assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY_GCLOUD) - - -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) + assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY) @mock.patch("os.name", new="nt") -def test_get_bq_config_path_windows(monkeypatch): +def test_get_config_path_windows(monkeypatch): appdata = "appdata" monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata) - config_path = _cloud_sdk.get_bq_config_path() + config_path = _cloud_sdk.get_config_path() - assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY_BQ) + assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY) @mock.patch("os.name", new="nt") -def test_get_bq_config_path_no_appdata(monkeypatch): +def test_get_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() + config_path = _cloud_sdk.get_config_path() - assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY_BQ) + assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY) @mock.patch("os.name", new="nt")