Skip to content

Commit f90ea91

Browse files
committed
fix: resolve home path in all environments
1 parent d0e0a63 commit f90ea91

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

.github/workflows/testPython.yml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ permissions:
1010
id-token: write
1111

1212
jobs:
13-
build:
13+
build-linux:
1414
runs-on: ubuntu-22.04
1515
strategy:
1616
matrix:
@@ -49,4 +49,45 @@ jobs:
4949
- name: Upload Coverage Report
5050
uses: codecov/codecov-action@v4
5151
with:
52-
token: ${{ secrets.CODECOV_TOKEN }} # required
52+
token: ${{ secrets.CODECOV_TOKEN }} # required
53+
54+
build-win:
55+
runs-on: windows-2019
56+
strategy:
57+
matrix:
58+
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
59+
fail-fast: false
60+
steps:
61+
- uses: actions/checkout@v4
62+
- name: Set up Python ${{ matrix.python-version }}
63+
uses: actions/setup-python@v5
64+
with:
65+
python-version: ${{ matrix.python-version }}
66+
- name: Install dependencies
67+
run: pip install alibabacloud-tea coverage pytest alibabacloud_credentials_api APScheduler aiofiles
68+
- name: Setup OIDC
69+
run: npm install @actions/[email protected] @actions/http-client
70+
- name: Get Id Token
71+
uses: actions/github-script@v6
72+
id: idtoken
73+
with:
74+
script: |
75+
const coreDemo = require('@actions/core');
76+
const idToken = await coreDemo.getIDToken('sts.aliyuncs.com');
77+
const fsx = require('fs/promises');
78+
await fsx.writeFile('D:\\oidc_token', idToken);
79+
- name: Test with unittest
80+
run: |
81+
coverage run -m unittest discover
82+
env:
83+
SUB_ALIBABA_CLOUD_ACCESS_KEY: ${{ secrets.SUB_ALIBABA_CLOUD_ACCESS_KEY }}
84+
SUB_ALIBABA_CLOUD_SECRET_KEY: ${{ secrets.SUB_ALIBABA_CLOUD_SECRET_KEY }}
85+
SUB_ALIBABA_CLOUD_ROLE_ARN: ${{ secrets.ALIBABA_CLOUD_ROLE_ARN }}
86+
ALIBABA_CLOUD_ROLE_ARN: ${{ secrets.OIDC_ROLE_ARN }}
87+
ALIBABA_CLOUD_ROLE_SESSION_NAME: ${{ secrets.ALIBABA_CLOUD_ROLE_SESSION_NAME }}
88+
ALIBABA_CLOUD_OIDC_TOKEN_FILE: "D:\\oidc_token"
89+
ALIBABA_CLOUD_OIDC_PROVIDER_ARN: ${{ secrets.OIDC_PROVIDER_ARN }}
90+
- name: Upload Coverage Report
91+
uses: codecov/codecov-action@v4
92+
with:
93+
token: ${{ secrets.CODECOV_TOKEN }} # required

alibabacloud_credentials/utils/auth_constant.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import os
1+
from alibabacloud_credentials.utils import auth_util
2+
3+
HOME = auth_util.get_home()
24

3-
HOME = os.getenv('HOME') if os.getenv('HOME') else os.getenv('HOMEPATH')
45
INI_ACCESS_KEY_ID = "access_key_id"
56
INI_ACCESS_KEY_IDSECRET = "access_key_secret"
67
INI_TYPE = "type"

alibabacloud_credentials/utils/auth_util.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import platform
3+
import re
24

35
client_type = os.environ.get('ALIBABA_CLOUD_PROFILE', 'default')
46

@@ -35,3 +37,19 @@ def get_private_key(file_path):
3537
with open(file_path, encoding='utf-8') as f:
3638
key = f.read()
3739
return key
40+
41+
42+
def get_home():
43+
if platform.system() == 'Windows':
44+
home = os.getenv('HOME')
45+
home_path = os.getenv('HOMEPATH')
46+
home_drive = os.getenv('HOMEDRIVE')
47+
if home:
48+
return home
49+
elif home_path:
50+
has_drive_in_home_path = bool(re.match(r'^[A-Za-z]:', home_path))
51+
return home_path if has_drive_in_home_path else os.path.join(home_drive or '', home_path)
52+
else:
53+
return os.path.expanduser("~")
54+
else:
55+
return os.getenv('HOME') or os.getenv('HOMEPATH') or os.path.expanduser("~")

tests/test_util.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
from alibabacloud_credentials.utils import auth_util, parameter_helper
1+
from alibabacloud_credentials.utils import auth_util, parameter_helper, auth_constant
22

3+
import os
34
import unittest
5+
from unittest.mock import patch
46
import re
57
from . import txt_file
8+
import platform
69

710

811
class TestUtil(unittest.TestCase):
@@ -52,3 +55,46 @@ def test_get_new_request(test):
5255
test_sign_string(self)
5356
test_compose_url(self)
5457
test_get_new_request(self)
58+
59+
def test_home(self):
60+
@patch.dict(os.environ, {'HOME': '/mock/home/linux'})
61+
def test_home_exists():
62+
"""case1:HOME exists"""
63+
assert auth_util.get_home() == '/mock/home/linux'
64+
65+
@patch.dict(os.environ, {'HOME': ''})
66+
@patch.dict(os.environ, {'HOMEPATH': ''})
67+
def test_home_empty():
68+
"""case2:HOME exists but empty"""
69+
with patch('os.path.expanduser', return_value='/fallback'):
70+
assert auth_util.get_home() == '/fallback'
71+
72+
@patch.dict(os.environ, {'HOME': ''})
73+
@patch.dict(os.environ, {'HOMEPATH': '\\Users\\mockuser'})
74+
@patch.dict(os.environ, {'HOMEDRIVE': 'C:'})
75+
def test_home_path_and_drive_windows():
76+
"""case3:Windows HOMEPATH exists and HOMEDRIVE exists"""
77+
if platform.system() == 'Windows':
78+
assert auth_util.get_home() == 'C:\\Users\\mockuser'
79+
else:
80+
assert auth_util.get_home() == '\\Users\\mockuser'
81+
82+
@patch.dict(os.environ, {'HOME': ''})
83+
@patch.dict(os.environ, {'HOMEPATH': 'D:\\Users\\mockuser'})
84+
@patch.dict(os.environ, {'HOMEDRIVE': 'C:'})
85+
def test_home_path_windows():
86+
"""case4:Windows HOMEPATH exists"""
87+
assert auth_util.get_home() == 'D:\\Users\\mockuser'
88+
89+
def test_real_system():
90+
"""case5:test real system"""
91+
assert auth_constant.HOME
92+
assert os.path.exists(auth_constant.HOME)
93+
assert auth_constant.HOME == os.path.expanduser('~')
94+
assert auth_util.get_home() == os.path.expanduser('~')
95+
96+
test_home_exists()
97+
test_home_empty()
98+
test_home_path_and_drive_windows()
99+
test_home_path_windows()
100+
test_real_system()

0 commit comments

Comments
 (0)