Skip to content

Commit ec9dfe3

Browse files
committed
build: update pyproject.toml to support hatch test command
1 parent b7cc49b commit ec9dfe3

File tree

16 files changed

+66
-57
lines changed

16 files changed

+66
-57
lines changed

.github/workflows/release.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ jobs:
3535
shell: bash
3636
run: |
3737
echo "CACHE_KEY=$( \
38-
.github/workflows/get-key.py --hash tool.hatch.envs.default.dependencies \
38+
yq eval -r .tool.hatch.envs.default.dependencies -oy pyproject.toml \
39+
| sha1sum \
40+
| cut -f 1 -d ' ' \
3941
)" | tee -a "${GITHUB_ENV}"
4042
4143
- name: Prepare Cache

.github/workflows/run-tests.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ jobs:
4040

4141
- name: Get Cache Key
4242
run: |
43-
[[ '${{ matrix.python-version }}' == '3.10' ]] && pip install tomli
4443
echo "CACHE_KEY=$( \
45-
.github/workflows/get-key.py --json --hash tool.hatch.envs.test.overrides.matrix.pytest.dependencies \
44+
yq eval -r .tool.hatch.envs.hatch-test.overrides.matrix.pytest.dependencies -oy pyproject.toml \
45+
| sha1sum \
46+
| cut -f 1 -d ' ' \
4647
)" | tee -a "${GITHUB_ENV}"
4748
shell: bash
4849

@@ -58,8 +59,5 @@ jobs:
5859
uses: pypa/hatch@install
5960

6061
- name: Run tests
61-
# NOTE Run tests per environment to avoid `default` environment activation
62-
# when `hatch run test` running.
6362
run: |
64-
hatch run test.pytest.7x:test
65-
hatch run test.pytest.8x:test
63+
hatch test -i pytest=pytest.7x,pytest.8x

ChangeLog.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ to `Semantic Versioning`_.
2626
Unreleased_
2727
===========
2828

29-
Added
30-
-----
29+
Changed
30+
-------
3131

32+
- Set the :option:`pm-patterns-base-dir` default to :file:`tests/data/expected` to align with
33+
the common :file:`tests/` directory layout.
3234
- Improve documentation.
35+
- Update the :file:`pyproject.toml` to suit the :program:`hatch test` command.
3336

3437

3538
2.0.2_ -- 2024-08-01

REUSE.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SPDX-FileCopyrightText = "2024 Alex Turbov <zaufi@pm.me>"
2525
SPDX-License-Identifier = "CC0-1.0"
2626

2727
[[annotations]]
28-
path = "test/data/**"
28+
path = "tests/data/**"
2929
precedence = "aggregate"
3030
SPDX-FileCopyrightText = "2024 Alex Turbov <zaufi@pm.me>"
3131
SPDX-License-Identifier = "CC0-1.0"

doc/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Marker
6969
def system_specific_test(capfd, expected_out):
7070
...
7171
stdout, _ = capfd.readouterr()
72-
# Get content from `<base-dir>/.../system_specific-Linux.out`
72+
# Get content from `<base-dir>/.../system_specific_test-Linux.out`
7373
assert expected_out == stdout
7474
7575
@pytest.mark.expect_suffix(

doc/configuration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The following options can be set in the `Pytest configuration file`_.
4040

4141
.. option:: pm-patterns-base-dir
4242

43-
:Default: :file:`test/data/expected`
43+
:Default: :file:`tests/data/expected`
4444

4545
Base directory used for storing pattern files.
4646
The directory must be relative to the project's root.

doc/getting-started.rst

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ or with ``diff`` mode highlighted via ``Pygments``:
2121
The plugin provides the fixtures :py:data:`expected_out` and :py:data:`expected_err`.
2222
Usage is straightforward as shown below:
2323

24-
.. literalinclude:: ../test/test_foo.py
24+
.. literalinclude:: ../tests/test_foo.py
2525
:language: python
2626
:pyobject: test_foo
2727

@@ -30,33 +30,34 @@ If you run :command:`pytest` now, the test will be skipped because the expectati
3030
.. code-block:: console
3131
:emphasize-lines: 5,6
3232
33-
$ pytest --no-header --no-summary test/test_foo.py::test_foo
33+
$ pytest --no-header --no-summary tests/test_foo.py::test_foo
3434
============================= test session starts ==============================
3535
collected 1 item
3636
37-
test/test_foo.py::test_foo SKIPPED (Base directory for pattern-matcher
38-
does not exist: `…/pytest-matcher/master/test/data/expected`) [100%]
37+
tests/test_foo.py::test_foo SKIPPED (Base directory for pattern-matcher
38+
does not exist: `…/pytest-matcher/master/tests/data/expected`) [100%]
3939
4040
============================== 1 skipped in 0.01s ==============================
4141
4242
4343
Add the :option:`pm-patterns-base-dir` option to the `Pytest configuration file`_
44-
pointing, for example, to :file:`test/data/expected`. Run :command:`pytest` with
44+
pointing, for example, to :file:`tests/data/expected`. Run :command:`pytest` with
4545
the :option:`--pm-save-patterns` option to write the initial expectation file:
4646

4747
.. code-block:: console
4848
:emphasize-lines: 5,6
4949
50-
$ pytest --pm-save-patterns --no-header --no-summary test/test_foo.py::test_foo
50+
$ pytest --pm-save-patterns --no-header --no-summary tests/test_foo.py::test_foo
5151
============================= test session starts ==============================
5252
collecting ... collected 1 item
5353
54-
test/test_foo.py::test_foo SKIPPED (Pattern file saved to
55-
`…/pytest-matcher/master/test/data/expected/test_foo/test_foo.out`) [100%]
54+
tests/test_foo.py::test_foo SKIPPED (Pattern file saved to
55+
`…/pytest-matcher/master/tests/data/expected/test_foo/test_foo.out`) [100%]
5656
5757
============================== 1 skipped in 0.02s ==============================
5858
59-
Review the stored pattern file :file:`test/data/expected/test_foo/test_foo.out` and add it to your VCS.
59+
Review the stored pattern file :file:`tests/data/expected/test_foo/test_foo.out` and add it to
60+
your VCS.
6061

6162
.. note::
6263

@@ -71,11 +72,11 @@ output matches expectations:
7172
.. code-block:: console
7273
:emphasize-lines: 5
7374
74-
$ pytest --no-header --no-summary test/test_foo.py::test_foo
75+
$ pytest --no-header --no-summary tests/test_foo.py::test_foo
7576
============================= test session starts ==============================
7677
collected 1 item
7778
78-
test/test_foo.py::test_foo PASSED [100%]
79+
tests/test_foo.py::test_foo PASSED [100%]
7980
8081
============================== 1 passed in 0.01s ===============================
8182
@@ -85,20 +86,20 @@ output matches expectations:
8586
If the captured output contains values that change from run to run, for example timestamps
8687
or filesystem paths, you can match the output using regular expressions:
8788

88-
.. literalinclude:: ../test/test_foo.py
89+
.. literalinclude:: ../tests/test_foo.py
8990
:language: python
9091
:pyobject: test_regex
9192

9293
Store the pattern file for this test and rerun :command:`pytest` with the ``-vv`` option:
9394

9495
.. code-block:: console
95-
:emphasize-lines: 24,28
96+
:emphasize-lines: 24,25,28,29
9697
97-
$ pytest -vv --no-header test/test_foo.py::test_regex
98+
$ pytest -vv --no-header tests/test_foo.py::test_regex
9899
============================= test session starts ==============================
99100
collecting ... collected 1 item
100101
101-
test/test_foo.py::test_regex FAILED [100%]
102+
tests/test_foo.py::test_regex FAILED [100%]
102103
103104
=================================== FAILURES ===================================
104105
__________________________________ test_regex __________________________________
@@ -112,43 +113,43 @@ Store the pattern file for this test and rerun :command:`pytest` with the ``-vv`
112113
113114
stdout, _ = capfd.readouterr()
114115
115-
> assert expected_out.match(stdout) ==True
116+
> assert expected_out.match(stdout) == True
116117
E AssertionError: assert
117118
E The test output doesn't match the expected regex.
118-
E (from `…/pytest-matcher/master/test/data/expected/test_foo/test_regex.out`):
119+
E (from `…/pytest-matcher/master/tests/data/expected/test_foo/test_regex.out`):
119120
E ---[BEGIN actual output]---
120121
E Current date: 2024-03-02 21:59:03.792447
121-
E Current module: …/pytest-matcher/master/test/test_foo.py
122+
E Current module: …/pytest-matcher/master/tests/test_foo.py
122123
E ---[END actual output]---
123124
E ---[BEGIN expected regex]---
124125
E Current date: 2024-03-02 21:58:32.289679
125-
E Current module: …/pytest-matcher/master/test/test_foo.py
126+
E Current module: …/pytest-matcher/master/tests/test_foo.py
126127
E ---[END expected regex]---
127128
128-
test/test_foo.py:26: AssertionError
129+
tests/test_foo.py:26: AssertionError
129130
=========================== short test summary info ============================
130-
FAILED test/test_foo.py::test_regex - AssertionError: assert
131+
FAILED tests/test_foo.py::test_regex - AssertionError: assert
131132
============================== 1 failed in 0.03s ===============================
132133
133134
To make it match, edit the expectation file and replace the changing parts with regular
134135
expressions:
135136

136137
.. code-block::
137-
:caption: ``test/data/expect/test_foo/test_regex.out``
138+
:caption: ``tests/data/expect/test_foo/test_regex.out``
138139
139140
Current date: [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?
140-
Current module: .*/test/test_foo.py
141+
Current module: .*/tests/test_foo.py
141142
142143
The test will now pass:
143144

144145
.. code-block:: console
145146
:emphasize-lines: 5
146147
147-
$ pytest --no-header --no-summary test/test_foo.py::test_regex
148+
$ pytest --no-header --no-summary tests/test_foo.py::test_regex
148149
============================= test session starts ==============================
149150
collected 1 item
150151
151-
test/test_foo.py::test_regex PASSED [100%]
152+
tests/test_foo.py::test_regex PASSED [100%]
152153
153154
============================== 1 passed in 0.01s ===============================
154155

pyproject.toml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ all = [
108108
dead-check = "vulture src/pytest_matcher"
109109
dist-check = "twine check dist/*"
110110
license-check = "reuse lint"
111-
lint-check = "ruff check doc src/pytest_matcher test"
111+
lint-check = "ruff check doc src/pytest_matcher tests"
112112
pyproject-check = "validate-pyproject {root:real}/pyproject.toml"
113113
spell-check = "codespell"
114-
type-check = "mypy src/pytest_matcher test"
115-
# Testing commands
116-
cov = "hatch env run -e test cov"
117-
test = "hatch env run -e test test"
114+
type-check = "mypy src/pytest_matcher"
115+
# Testing shortcut commands
116+
cov = "hatch test --cov"
117+
test = "hatch test -i pytest=pytest.7x,pytest.8x"
118118
# Misc commands
119119
annotate-code = "reuse annotate -c \"$(git config --get user.name) <$(git config --get user.email)>\" -t code -l GPL-3.0-or-later {args}"
120120
annotate-misc = "reuse annotate -c \"$(git config --get user.name) <$(git config --get user.email)>\" -t misc -l CC0-1.0 {args}"
@@ -124,8 +124,8 @@ fix-spelling = "codespell -i 3 -w"
124124
[tool.hatch.envs.doc]
125125
description = "Environment to build documentation"
126126
dependencies = [
127-
"sphinx < 8.0.0"
128-
, "sphinx_rtd_theme >= 2.0.0"
127+
"sphinx"
128+
, "sphinx_rtd_theme"
129129
]
130130
installer = "uv"
131131
template = "doc"
@@ -135,26 +135,31 @@ build = "sphinx-build --color -j auto doc/ build/sphinx/html/"
135135
link-check = "sphinx-build --color -b linkcheck doc/ build/sphinx/link-check/"
136136
show = "xdg-open build/sphinx/html/index.html"
137137

138-
[tool.hatch.envs.test]
138+
[tool.hatch.envs.hatch-test]
139139
description = "Unit tests environment"
140-
template = "test"
140+
dependencies = [
141+
"coverage-enable-subprocess == 1.0",
142+
"coverage[toml] ~= 7.4",
143+
"pytest-randomly ~= 3.15",
144+
"pytest-rerunfailures ~= 14.0",
145+
"pytest-xdist[psutil] ~= 3.5",
146+
]
141147
installer = "uv"
142148

143-
[[tool.hatch.envs.test.matrix]]
149+
[[tool.hatch.envs.hatch-test.matrix]]
144150
pytest = ["pytest.8x", "pytest.7x"]
145151

146-
[tool.hatch.envs.test.overrides]
152+
[tool.hatch.envs.hatch-test.overrides]
147153
matrix.pytest.dependencies = [
148154
"coverage[toml]"
149155
, "pytest-cov"
150156
, { value = "pytest ~= 8.0", if = ["pytest.8x"] }
151157
, { value = "pytest ~= 7.0", if = ["pytest.7x"] }
152158
]
153159

154-
[tool.hatch.envs.test.scripts]
160+
[tool.hatch.envs.hatch-test.scripts]
161+
run = "pytest{env:HATCH_TEST_ARGS:} -o cache_dir=build/.{matrix:pytest}_cache {verbosity:flag} {args}"
155162
cov = "pytest --cov-report term --cov-report html:build/coverage-{matrix:pytest} --cov=pytest_matcher"
156-
show-cov = "xdg-open build/coverage-{matrix:pytest}/index.html"
157-
test = "pytest -o cache_dir=build/.{matrix:pytest}_cache {verbosity:flag} {args}"
158163

159164
[tool.hatch.version]
160165
source = "vcs"
@@ -229,7 +234,7 @@ minversion = "7.0"
229234
python_files = ["test_*.py"]
230235
python_classes = ["*Tester"]
231236
python_functions = ["*_test", "*_test_?", "*_test_??", "test_*"]
232-
testpaths = ["test"]
237+
testpaths = ["tests"]
233238
xfail_strict = true
234239

235240
# https://docs.astral.sh/ruff/configuration/
@@ -256,12 +261,12 @@ select = ["ALL"]
256261
, "D100" # Missing docstring in public module
257262
, "A001" # Variable `copyright` is shadowing a Python builtin
258263
]
259-
"test/test_*.py" = [
264+
"tests/test_*.py" = [
260265
"ANN001" # Missing type annotation for function argument
261266
, "D" # Documentation issues
262267
, "PLR2004" # Magic value used in comparison
263268
]
264-
"test/test_foo.py" = [
269+
"tests/test_foo.py" = [
265270
"T201" # `print` found
266271
, "E712" # Comparison to `True`
267272
, "DTZ005" # The use of `datetime.datetime.now()` without `tz` argument is not allowed

src/pytest_matcher/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def pytest_addoption(parser: pytest.Parser) -> None:
469469
parser.addini(
470470
'pm-patterns-base-dir'
471471
, help='Base directory used for storing pattern files.'
472-
, default=pathlib.Path('test/data/expected')
472+
, default=pathlib.Path('tests/data/expected')
473473
)
474474
parser.addini(
475475
'pm-pattern-file-fmt'
File renamed without changes.

0 commit comments

Comments
 (0)