Skip to content

Commit e3d1815

Browse files
authored
Use hatch over tox (#262)
1 parent 25895b9 commit e3d1815

File tree

6 files changed

+133
-176
lines changed

6 files changed

+133
-176
lines changed

.github/workflows/check.yml

+52-49
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: check
22
on:
33
workflow_dispatch:
44
push:
5-
branches: "main"
5+
branches: ["main"]
66
tags-ignore: ["**"]
77
pull_request:
88
schedule:
@@ -36,45 +36,47 @@ jobs:
3636
uses: actions/setup-python@v5
3737
with:
3838
python-version: "3.12"
39-
- name: Install tox
40-
run: python -m pip install tox
39+
- name: Pick environment to run
40+
run: |
41+
import codecs; import os
42+
py = "${{ matrix.py }}"
43+
py = "test.{}".format(py if py.startswith("pypy") else f"py{py}")
44+
print(f"Picked {py}")
45+
with codecs.open(os.environ["GITHUB_ENV"], mode="a", encoding="utf-8") as file_handler:
46+
file_handler.write("FORCE_COLOR=1\n")
47+
file_handler.write(f"ENV={py}\n")
48+
shell: python
49+
- name: Install hatch
50+
run: python -m pip install hatch
4151
- uses: actions/checkout@v4
4252
with:
4353
fetch-depth: 0
4454
- name: Setup python for test ${{ matrix.py }}
4555
uses: actions/setup-python@v5
4656
with:
4757
python-version: ${{ matrix.py }}
48-
- name: Pick environment to run
58+
- name: Setup test environment
4959
run: |
50-
import codecs
51-
import os
52-
import platform
53-
import sys
54-
cpy = platform.python_implementation() == "CPython"
55-
base =("{}{}{}" if cpy else "{}{}").format("py" if cpy else "pypy", *sys.version_info[0:2])
56-
env = "TOXENV={}\n".format(base)
57-
print("Picked:\n{}for{}".format(env, sys.version))
58-
with codecs.open(os.environ["GITHUB_ENV"], "a", "utf-8") as file_handler:
59-
file_handler.write(env)
60-
shell: python
61-
- name: Setup test suite
62-
run: tox -vv --notest
60+
hatch -v env create ${ENV}
61+
hatch run ${ENV}:pip freeze
62+
shell: bash
6363
- name: Run test suite
64-
run: tox --skip-pkg-install
64+
run: hatch -v run ${ENV}:run
6565
env:
6666
PYTEST_ADDOPTS: "-vv --durations=20"
6767
CI_RUN: "yes"
68+
shell: bash
6869
- name: Rename coverage report file
6970
run: |
7071
import os; import sys;
71-
os.rename(f".tox/.coverage.{os.environ['TOXENV']}", f".tox/.coverage.{os.environ['TOXENV']}-{sys.platform}")
72+
os.rename(f"report{os.sep}.coverage.${{ matrix.py }}", f"report{os.sep}.coverage.${{ matrix.py }}-{sys.platform}")
7273
shell: python
7374
- name: Upload coverage data
74-
uses: actions/upload-artifact@v3
75+
uses: actions/upload-artifact@v4
7576
with:
76-
name: coverage-data
77-
path: ".tox/.coverage.*"
77+
name: coverage-${{ matrix.os }}-${{ matrix.py }}
78+
path: "report/.coverage.*"
79+
retention-days: 3
7880

7981
coverage:
8082
name: Combine coverage
@@ -87,43 +89,42 @@ jobs:
8789
- uses: actions/setup-python@v5
8890
with:
8991
python-version: "3.12"
90-
- name: Install tox
91-
run: python -m pip install tox
92+
- name: Let us have colors
93+
run: echo "FORCE_COLOR=true" >> "$GITHUB_ENV"
94+
- name: Install hatch
95+
run: python -m pip install hatch
9296
- name: Setup coverage tool
93-
run: tox -e coverage --notest
94-
- name: Install package builder
95-
run: python -m pip install build
96-
- name: Build package
97-
run: pyproject-build --wheel .
97+
run: |
98+
hatch -v env create coverage
99+
hatch run coverage:pip freeze
98100
- name: Download coverage data
99-
uses: actions/download-artifact@v3
101+
uses: actions/download-artifact@v4
100102
with:
101-
name: coverage-data
102-
path: .tox
103+
path: report
104+
pattern: coverage-*
105+
merge-multiple: true
103106
- name: Combine and report coverage
104-
run: tox -e coverage
107+
run: hatch run coverage:run
105108
- name: Upload HTML report
106-
uses: actions/upload-artifact@v3
109+
uses: actions/upload-artifact@v4
107110
with:
108111
name: html-report
109-
path: .tox/htmlcov
112+
path: report/html
110113

111114
check:
112-
name: ${{ matrix.tox_env }} - ${{ matrix.os }}
115+
name: ${{ matrix.env.name }} - ${{ matrix.os }}
113116
runs-on: ${{ matrix.os }}
114117
strategy:
115118
fail-fast: false
116119
matrix:
117120
os:
118121
- ubuntu-22.04
119122
- windows-2022
120-
tox_env:
121-
- dev
122-
- type
123-
- docs
124-
- readme
125-
exclude:
126-
- { os: windows-latest, tox_env: readme }
123+
env:
124+
- {"name": "default", "target": "show"}
125+
- {"name": "type", "target": "run"}
126+
- {"name": "docs", "target": "build"}
127+
- {"name": "readme", "target": "run"}
127128
steps:
128129
- uses: actions/checkout@v4
129130
with:
@@ -132,9 +133,11 @@ jobs:
132133
uses: actions/setup-python@v5
133134
with:
134135
python-version: "3.12"
135-
- name: Install tox
136-
run: python -m pip install tox
137-
- name: Setup test suite
138-
run: tox -vv --notest -e ${{ matrix.tox_env }}
139-
- name: Run test suite
140-
run: tox --skip-pkg-install -e ${{ matrix.tox_env }}
136+
- name: Install hatch
137+
run: python -m pip install hatch
138+
- name: Setup ${{ matrix.env.name }}
139+
run: |
140+
hatch -v env create ${{ matrix.env.name }}
141+
hatch run ${{ matrix.env.name }}:pip freeze
142+
- name: Run ${{ matrix.env.name }}
143+
run: hatch -v run ${{ matrix.env.name }}:${{ matrix.env.target }}

.gitignore

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
/.idea/
2-
/.vscode/
3-
41
*.pyc
52
*.egg-info
6-
tmp/
7-
build/
8-
dist/
9-
.tox/
3+
/dist
4+
/.tox
105
/src/platformdirs/version.py
6+
/report
7+
/docs/build

.pre-commit-config.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ repos:
2121
- id: ruff-format
2222
- id: ruff
2323
args: [ "--fix", "--unsafe-fixes", "--exit-non-zero-on-fix" ]
24-
- repo: https://github.com/tox-dev/tox-ini-fmt
25-
rev: "1.3.1"
26-
hooks:
27-
- id: tox-ini-fmt
28-
args: ["-p", "fix"]
2924
- repo: https://github.com/tox-dev/pyproject-fmt
3025
rev: "1.7.0"
3126
hooks:

CONTRIBUTING.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This project uses [hatch](https://hatch.pypa.io) development, therefore consult that documentation for more in-depth how
2+
to. To see a list of available environments use `hatch env show`, for example to run the test suite under Python 3.12
3+
can type in a shell `hatch run test.py3.12:run`.

pyproject.toml

+74
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ optional-dependencies.test = [
5757
"pytest-cov>=4.1",
5858
"pytest-mock>=3.12",
5959
]
60+
optional-dependencies.type = [
61+
"mypy>=1.8",
62+
]
6063
urls.Documentation = "https://platformdirs.readthedocs.io"
6164
urls.Homepage = "https://github.com/platformdirs/platformdirs"
6265
urls.Source = "https://github.com/platformdirs/platformdirs"
@@ -67,6 +70,77 @@ build.hooks.vcs.version-file = "src/platformdirs/version.py"
6770
build.targets.sdist.include = ["/src", "/tests", "/tox.ini"]
6871
version.source = "vcs"
6972

73+
[tool.hatch.envs.default]
74+
description = "Development environment"
75+
features = ["test", "docs", "type"]
76+
scripts = { show = ["python -m pip list --format=columns", 'python -c "import sys; print(sys.executable)"'] }
77+
78+
[tool.hatch.envs.test]
79+
template = "test"
80+
# dev-mode = false # cannot enable this until https://github.com/pypa/hatch/issues/1237
81+
description = "Run the test suite"
82+
matrix = [{ python = ["3.12", "3.11", "3.10", "3.9", "3.8", "pypy3.9"] }]
83+
features = ["test"]
84+
env-vars = { COVERAGE_FILE = "report/.coverage.{matrix:python}", COVERAGE_PROCESS_START = "pyproject.toml", _COVERAGE_SRC = "src/platformdirs" }
85+
[tool.hatch.envs.test.scripts]
86+
run = ["""
87+
pytest --junitxml report/junit.{matrix:python}.xml --cov src/platformdirs --cov tests \
88+
--cov-config=pyproject.toml --no-cov-on-fail --cov-report term-missing:skip-covered --cov-context=test \
89+
--cov-report html:report/html{matrix:python} --cov-report xml:report/coverage{matrix:python}.xml \
90+
tests
91+
"""]
92+
93+
[tool.hatch.envs.coverage]
94+
template = "coverage"
95+
description = "combine coverage files and generate diff"
96+
dependencies = ["covdefaults>=2.3", "coverage[toml]>=7.3.2", "diff-cover>=8.0.1"]
97+
env-vars = { COVERAGE_FILE = "report/.coverage" }
98+
[tool.hatch.envs.coverage.scripts]
99+
run = [
100+
"coverage combine report",
101+
"coverage report --skip-covered --show-missing",
102+
"coverage xml -o report/coverage.xml",
103+
"coverage html -d report/html",
104+
"diff-cover --compare-branch {env:DIFF_AGAINST:origin/main} report/coverage.xml",
105+
]
106+
107+
[tool.hatch.envs.type]
108+
template = "type"
109+
description = "Run the type checker"
110+
python = "3.12"
111+
dev-mode = false
112+
features = ["type", "test"]
113+
scripts = { run = ["mypy --strict src", "mypy --strict tests"] }
114+
115+
[tool.hatch.envs.fix]
116+
template = "fix"
117+
description = "Run the pre-commit tool to lint and autofix issues"
118+
python = "3.12"
119+
detached = true
120+
dependencies = ["pre-commit>=3.6"]
121+
scripts = { "run" = ["pre-commit run --all-files --show-diff-on-failure"] }
122+
123+
[tool.hatch.envs.docs]
124+
template = "docs"
125+
description = "Build documentation using Sphinx"
126+
python = "3.12"
127+
dev-mode = false
128+
features = ["docs"]
129+
[tool.hatch.envs.docs.scripts]
130+
build = [
131+
"""python -c "import glob; import subprocess; subprocess.call(['proselint'] + glob.glob('docs/*.rst'))" """,
132+
"""python -c "from shutil import rmtree; rmtree('docs/build', ignore_errors=True)" """,
133+
"sphinx-build -d docs/build/tree docs docs/build --color -b html",
134+
"""python -c "from pathlib import Path; p=(Path('docs')/'build'/'index.html').absolute().as_uri(); print('Documentation built under '+p)" """,
135+
]
136+
137+
[tool.hatch.envs.readme]
138+
template = "readme"
139+
description = "check that the long description is valid"
140+
python = "3.12"
141+
dependencies = ["build[virtualenv]>=1.0.3", "twine>=4.0.2", "check-wheel-contents>=0.6.0"]
142+
scripts = { "run" = ["python -m build -o dist", "twine check dist/*.whl dist/*.tar.gz", "check-wheel-contents dist"] }
143+
70144
[tool.ruff]
71145
select = ["ALL"]
72146
line-length = 120

tox.ini

-115
This file was deleted.

0 commit comments

Comments
 (0)