Skip to content

Commit 43fba3a

Browse files
committed
Improve testing of missing toml parser
Add a new constant (in a dummy module) which can be used to explicitly disable TOML parsing, solely for the purpose of testing. Comments on the variable explain why it is necessary. Also cleanup tox config a little. Remove the depends listing in favor of ordering environments in the env_list in a sensible way. This allows for parallel invocation only when the coverage environments are absent, but this is still possible. For example, `tox r -e cov_clean; tox p -m ci; tox r -e cov` would imitate CI usage.
1 parent e45abd3 commit 43fba3a

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ jobs:
6060
key: tox-os-${{ runner.os }}-py${{ matrix.py }}-weeknum-${{ steps.get-week-number.outputs.cachedate }}-job-${{ github.job }}-hash-${{ hashFiles('tox.ini', 'setup.cfg') }}
6161

6262
- name: test
63-
run: python -m tox run -m ci
63+
run: |
64+
python -m tox run-parallel -m ci
65+
python -m tox run -e cov
6466
- name: twine-check
6567
run: python -m tox run -e twine-check
6668

src/check_jsonschema/_testing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import sys
3+
4+
PYTEST_LOADED = "pytest" in sys.modules
5+
6+
# this constant exists in service of the testsuite being able to forcibly disable the TOML
7+
# loader
8+
# the reason for this is that
9+
# - toml loading is enabled on py3.11+ with 'tomllib'
10+
# - on python < 3.11, toml loading is enabled with 'tomli'
11+
# - on python < 3.11, *coverage requires tomli*
12+
#
13+
# the end result is that it is not possible without special trickery to measure coverage
14+
# in the disabled TOML state
15+
# in order to measure coverage, we need 'tomli', so we will include this "magical" test
16+
# constant in order to "forcibly" disable the 'tomli' usage
17+
FORCE_TOML_DISABLED = False
18+
19+
if PYTEST_LOADED and os.getenv("FORCE_TOML_DISABLED") == "1":
20+
FORCE_TOML_DISABLED = True

src/check_jsonschema/parsers/toml.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import sys
55
import typing as t
66

7+
from check_jsonschema._testing import FORCE_TOML_DISABLED
8+
79
if sys.version_info < (3, 11):
810
try:
911
import tomli as toml_implementation
1012

1113
has_toml = True
12-
except ImportError:
14+
except ImportError: # pragma: no cover
1315
has_toml = False
1416
else:
1517
has_toml = True
@@ -54,10 +56,10 @@ def _normalize(data: t.Any) -> t.Any:
5456

5557

5658
# present a bool for detecting that it's enabled
57-
ENABLED = has_toml
59+
ENABLED = has_toml and not FORCE_TOML_DISABLED
5860

5961

60-
if has_toml:
62+
if ENABLED:
6163
ParseError: type[Exception] = toml_implementation.TOMLDecodeError
6264

6365
def load(stream: t.BinaryIO) -> t.Any:

tox.ini

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
[tox]
2-
# desired label:
3-
# ci = py{,-json5,-pyjson5,-tomli}{,-format}"
4-
#
5-
# a tox issue will be filed to ask how this could be better supported
6-
# for now, 'ci' is an expanded matrix:
7-
labels =
8-
ci = py, py-format, py-json5, py-json5-format, py-pyjson5, py-pyjson5-format, py-tomli, py-tomli-format, coverage
9-
102
envlist =
3+
mypy
114
cov_clean
125
py{311,310,39,38,37}
13-
py{37,310}-tomli{,-format}
6+
py310-{notoml,tomli-format}
147
py{37,311}-{json5,pyjson5}
158
cov
169
skip_missing_interpreters = true
1710
minversion = 4.0.0
1811

12+
labels =
13+
ci = py-notoml, py-tomli-format, py-json5, py-pyjson5
14+
1915
[testenv]
2016
description = "run tests with pytest"
2117
usedevelop = true
@@ -25,10 +21,9 @@ deps =
2521
pyjson5: pyjson5
2622
tomli: tomli
2723
format: jsonschema[format]
24+
set_env =
25+
notoml: FORCE_TOML_DISABLED=1
2826
commands = coverage run -m pytest {posargs}
29-
depends =
30-
py{,37,38,39,310,311}{,-json5,-pyjson5,-tomli}: cov_clean
31-
cov: py{,37,38,39,310,311}{,-json5,-pyjson5,-tomli}
3227

3328
[testenv:cov_clean]
3429
description = "erase coverage data to prepare for a new run"
@@ -40,7 +35,7 @@ commands = coverage erase
4035
description = "combine and report coverage data"
4136
deps = coverage
4237
skip_install = true
43-
commands_pre = coverage combine
38+
commands_pre = - coverage combine
4439
commands = coverage report --skip-covered
4540

4641
[testenv:mypy]

0 commit comments

Comments
 (0)