Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 4921d44

Browse files
PicardParisJon Wayne Parrott
authored andcommitted
Fix retrieval of default project ID on Windows (#179)
1 parent d020426 commit 4921d44

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

google/auth/_cloud_sdk.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
# The name of the file in the Cloud SDK config that contains default
3434
# credentials.
3535
_CREDENTIALS_FILENAME = 'application_default_credentials.json'
36+
# The name of the Cloud SDK shell script
37+
_CLOUD_SDK_POSIX_COMMAND = 'gcloud'
38+
_CLOUD_SDK_WINDOWS_COMMAND = 'gcloud.cmd'
3639
# The command to get the Cloud SDK configuration
37-
_CLOUD_SDK_CONFIG_COMMAND = (
38-
'gcloud', 'config', 'config-helper', '--format', 'json')
40+
_CLOUD_SDK_CONFIG_COMMAND = ('config', 'config-helper', '--format', 'json')
3941

4042

4143
def get_config_path():
@@ -114,10 +116,14 @@ def get_project_id():
114116
Returns:
115117
Optional[str]: The project ID.
116118
"""
119+
if os.name == 'nt':
120+
command = _CLOUD_SDK_WINDOWS_COMMAND
121+
else:
122+
command = _CLOUD_SDK_POSIX_COMMAND
117123

118124
try:
119125
output = subprocess.check_output(
120-
_CLOUD_SDK_CONFIG_COMMAND,
126+
(command,) + _CLOUD_SDK_CONFIG_COMMAND,
121127
stderr=subprocess.STDOUT)
122128
except (subprocess.CalledProcessError, OSError, IOError):
123129
return None

tests/test__cloud_sdk.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ def test_get_project_id_call_error(check_output):
6565
assert check_output.called
6666

6767

68+
@mock.patch('os.name', new='nt')
69+
def test_get_project_id_windows():
70+
check_output_patch = mock.patch(
71+
'subprocess.check_output', autospec=True,
72+
return_value=CLOUD_SDK_CONFIG_FILE_DATA)
73+
74+
with check_output_patch as check_output:
75+
project_id = _cloud_sdk.get_project_id()
76+
77+
assert project_id == 'example-project'
78+
assert check_output.called
79+
# Make sure the executable is `gcloud.cmd`.
80+
args = check_output.call_args[0]
81+
command = args[0]
82+
executable = command[0]
83+
assert executable == 'gcloud.cmd'
84+
85+
6886
@mock.patch(
6987
'google.auth._cloud_sdk.get_config_path', autospec=True)
7088
def test_get_application_default_credentials_path(get_config_dir):

0 commit comments

Comments
 (0)