Skip to content

Commit 47fe8ce

Browse files
authored
refactor: update multiple packages's pyproject.toml to PEP-621 (#253)
1 parent 47e5a91 commit 47fe8ce

13 files changed

Lines changed: 2036 additions & 1125 deletions

File tree

.github/workflows/bkapi-client-core.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
python-version: ["3.6", "3.7"]
22-
os: [ubuntu-20.04, macos-latest, windows-latest]
22+
os: [ubuntu-20.04]
2323

2424
steps:
2525
- uses: actions/checkout@v3
@@ -28,15 +28,13 @@ jobs:
2828
uses: actions/setup-python@v4
2929
with:
3030
python-version: ${{ matrix.python-version }}
31-
cache: 'pip'
32-
cache-dependency-path: 'sdks/bkapi-client-core/requirements_tox.txt'
3331

34-
- name: Install dependencies
35-
run: |
36-
python -m pip install --upgrade pip
37-
python -m pip install . 'tox-gh-actions==2.12.0' -r requirements_tox.txt
38-
working-directory: sdks/bkapi-client-core
32+
- name: Setup uv
33+
uses: astral-sh/setup-uv@v5
34+
35+
- name: Install Nox
36+
run: uv pip install --system nox
3937

40-
- name: Test with tox
41-
run: tox
38+
- name: Run tests on ${{ matrix.os }}
4239
working-directory: sdks/bkapi-client-core
40+
run: nox --non-interactive --error-on-missing-interpreters --session "tests(python='${{ matrix.python-version }}')" -- --full-trace

sdks/bkapi-client-core/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ build: ALL
6767
publish: build
6868
poetry publish -r pypi
6969

70+
.PHONY: nox
71+
nox:
72+
nox
73+
7074
.PHONY: tox
71-
tox:
72-
tox
75+
tox: nox

sdks/bkapi-client-core/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616
- 对 IDE 开发友好,SDK 支持常见 IDE 智能提示及补全;
1717
- 兼容 Python2 及 Python3 的类型补全;
1818

19+
## 如何使用 Nox 运行测试
20+
21+
测试分两步,先导出依赖,再执行测试:
22+
23+
```bash
24+
# 使用 Python 3.11 导出 requirements.txt(包含 dev 依赖)
25+
# 因为 pyproject.toml 已经更新为 PEP-621 版本,所以必须使用比较新的 Python 版本来导出
26+
nox -s export_deps
27+
28+
# 安装依赖并运行测试(依赖安装会以 requirements.txt 作为 constraints)
29+
nox -s tests
30+
```
31+
32+
若依赖或锁文件有变更,请先重新执行 `nox -s export_deps`
33+
1934
## SDK 使用样例
2035

2136
### 1. 使用 Client

sdks/bkapi-client-core/noxfile.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from pathlib import Path
2+
3+
import nox
4+
5+
nox.options.default_venv_backend = "venv"
6+
7+
ROOT = Path(__file__).parent.resolve()
8+
ALL_PYTHON = ["3.6", "3.7"]
9+
REQUESTS_VERSIONS = ["2.20", "2.26"]
10+
REQUIREMENTS_PATH = ROOT / "requirements.txt"
11+
12+
13+
def _ensure_requirements_exported(session) -> Path:
14+
if REQUIREMENTS_PATH.exists():
15+
return REQUIREMENTS_PATH
16+
17+
session.error("requirements.txt not found. Run `nox -s export_deps` first.")
18+
return REQUIREMENTS_PATH
19+
20+
21+
def install_with_constraints(session, *args: str, **kwargs) -> None:
22+
"""
23+
Install packages constrained by exported requirements.txt.
24+
25+
This function is a wrapper for nox.sessions.Session.install. It
26+
invokes pip to install packages inside of the session's virtualenv.
27+
Additionally, pip is passed a constraints file generated from
28+
Poetry's lock file, to ensure that the packages are pinned to the
29+
versions specified in poetry.lock.
30+
"""
31+
requirements = _ensure_requirements_exported(session)
32+
session.install(
33+
f"--constraint={requirements}",
34+
"--use-deprecated=legacy-resolver",
35+
*args,
36+
**kwargs,
37+
)
38+
39+
40+
@nox.session(python="3.11", reuse_venv=True)
41+
def export_deps(session):
42+
"""Export dependencies from Poetry to requirements.txt. When running tests later, the
43+
requirements.txt file will be used as a constraints file to ensure that the same version
44+
of each dependency is used.
45+
"""
46+
session.run("python", "-m", "ensurepip", "--upgrade")
47+
session.install("poetry")
48+
session.run("poetry", "self", "add", "poetry-plugin-export@latest")
49+
session.run(
50+
"poetry",
51+
"export",
52+
"--with=dev",
53+
"--format=requirements.txt",
54+
"--without-hashes",
55+
f"--output={REQUIREMENTS_PATH}",
56+
)
57+
58+
59+
@nox.session(reuse_venv=True)
60+
@nox.parametrize("python", ALL_PYTHON)
61+
def tests(session):
62+
session.run("python", "-m", "ensurepip", "--upgrade")
63+
session.run("python", "-m", "pip", "install", "--upgrade", "pip<24", "setuptools<68", "wheel")
64+
# Install main dependencies
65+
install_with_constraints(
66+
session,
67+
"-v",
68+
"requests",
69+
"curlify",
70+
"typing-extensions",
71+
"six",
72+
)
73+
# Install test dependencies
74+
install_with_constraints(
75+
session,
76+
"pytest",
77+
"pytest-django",
78+
"pytest-mock",
79+
"requests-mock",
80+
"Faker",
81+
"django",
82+
"prometheus-client",
83+
)
84+
85+
for version in REQUESTS_VERSIONS:
86+
session.install(f"requests=={version}")
87+
session.run("pytest", "tests", *session.posargs)

0 commit comments

Comments
 (0)