Skip to content

Commit efc6e25

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

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
HOME = os.getenv('HOME') if os.getenv('HOME') else os.getenv('HOMEPATH')
3+
HOME = os.getenv('HOME') or os.getenv('HOMEPATH') or os.path.expanduser("~")
44
INI_ACCESS_KEY_ID = "access_key_id"
55
INI_ACCESS_KEY_IDSECRET = "access_key_secret"
66
INI_TYPE = "type"

tests/test_util.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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
68

@@ -52,3 +54,30 @@ def test_get_new_request(test):
5254
test_sign_string(self)
5355
test_compose_url(self)
5456
test_get_new_request(self)
57+
58+
def test_home(self):
59+
def get_home():
60+
"""mock auth_constant.HOME"""
61+
return os.getenv('HOME') or os.getenv('HOMEPATH') or os.path.expanduser("~")
62+
63+
@patch.dict(os.environ, {'HOME': '/mock/home/linux'})
64+
def test_home_exists():
65+
"""case1:HOME exists"""
66+
assert get_home() == '/mock/home/linux'
67+
68+
@patch.dict(os.environ, {'HOME': ''})
69+
@patch.dict(os.environ, {'HOMEPATH': ''})
70+
def test_home_empty():
71+
"""case2:HOME exists but empty"""
72+
with patch('os.path.expanduser', return_value='/fallback'):
73+
assert get_home() == '/fallback'
74+
75+
def test_real_system():
76+
"""case3:test real system"""
77+
assert auth_constant.HOME
78+
assert os.path.exists(auth_constant.HOME)
79+
assert auth_constant.HOME == os.path.expanduser('~')
80+
81+
test_home_exists()
82+
test_home_empty()
83+
test_real_system()

0 commit comments

Comments
 (0)