Skip to content

Commit

Permalink
Add custom test environments to tox
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat committed Feb 5, 2025
1 parent abcc356 commit 7aad16c
Show file tree
Hide file tree
Showing 13 changed files with 1,574 additions and 173 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
- cron: "26 1 * * 4"
workflow_dispatch:

concurrency:
group: codeql-${{ github.ref }}
cancel-in-progress: true

jobs:
analyze:
name: Analyze
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
pull_request:
branches: [main]

concurrency:
group: codespell-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
# performance analysis in order to generate initial data.
workflow_dispatch:

concurrency:
group: codespeed-${{ github.ref }}
cancel-in-progress: true

jobs:
benchmarks:
runs-on: ubuntu-24.04
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
- main
workflow_dispatch:

concurrency:
group: docs-${{ github.ref }}
cancel-in-progress: true

jobs:
build-deploy:
runs-on: ubuntu-24.04
Expand Down
69 changes: 29 additions & 40 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,28 @@ on:
branches: [main]
workflow_dispatch:

concurrency:
group: test-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: >-
Test ${{ matrix.py }}
Test ${{ matrix.py || matrix.tox_env }} on
${{ matrix.os == 'windows-latest' && 'Windows' || (matrix.os == 'macos-latest' && 'macOS' || 'Ubuntu') }}
${{ matrix.isort != '' && format('isort={0} ', matrix.isort) || ''}}
${{ matrix.black != '' && format('black={0} ', matrix.black) || ''}}
${{ matrix.pydantic != '' && format('pydantic={0} ', matrix.pydantic) || ''}}
strategy:
fail-fast: false
matrix:
py: ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8"]
os: [ubuntu-24.04, windows-latest, macos-latest]
isort: [''] # '' mean not set, aka use the default
black: ['']
pydantic: ['']
tox_env: ['']
include:
- py: 3.12
black: 24.1.0
- py: 3.12
black: 23.12.1
- py: 3.12
black: 22.1.0
- py: 3.9
black: 19.10b0
pydantic: 1.8.2
- py: 3.8
pydantic: 1.8.2
- py: 3.8
isort: 4.3.21
pydantic: 1.5.1
- tox_env: py3.12-black24
- tox_env: py3.12-black23
- tox_env: py3.12-black22
- tox_env: py3.9-black19
- tox_env: py3.8-pydantic18
- tox_env: py3.8-isort4
runs-on: ${{ matrix.os == '' && 'ubuntu-24.04' || matrix.os }}
env:
OS: ${{ matrix.os == '' && 'ubuntu-24.04' || matrix.os}}
Expand All @@ -47,31 +38,26 @@ jobs:
fetch-depth: 0
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5
with:
python-version: ${{ matrix.py }}
- name: Install tox
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
- name: Setup Python test environment
run: tox run -vv --notest --skip-missing-interpreters false -e ${{ matrix.py }}
- name: Install custom package versions
if: ${{ matrix.isort != '' || matrix.black != '' || matrix.pydantic != ''}}
run: >-
tox exec --no-list-dependencies --skip-uv-sync --skip-pkg-install -e ${{ matrix.py }} -- uv pip install
${{ matrix.isort != '' && format('isort[pyproject]=={0} ', matrix.isort) || ''}}
${{ matrix.black != '' && format('black=={0} ', matrix.black) || ''}}
${{ matrix.pydantic != '' && format('pydantic=={0} ', matrix.pydantic) || ''}}
run: tox run -vv --notest --skip-missing-interpreters false -e ${{ matrix.py || matrix.tox_env }}
env:
UV_PYTHON_PREFERENCE: "only-managed"
- name: Run test suite
run: tox run --skip-uv-sync --skip-pkg-install -e ${{ matrix.py }}
run: tox run --skip-uv-sync --skip-pkg-install -e ${{ matrix.py || matrix.tox_env }}
env:
UV_PYTHON_PREFERENCE: "only-managed"
- name: Rename coverage report file
run: |
import os; import sys
os.rename(f".tox/.coverage.${{ matrix.py }}", f".tox/.coverage.${{ matrix.py }}-${{ matrix.os }}-${{ matrix.isort }}-${{ matrix.black }}-${{ matrix.pydantic }}")
os.rename(f".tox/.coverage.${{ matrix.py || matrix.tox_env }}", f".tox/.coverage.${{ matrix.py || matrix.tox_env}}-${{ matrix.os }}")
shell: python
- name: Upload coverage data
uses: actions/upload-artifact@v4
with:
include-hidden-files: true
name: .coverage.${{ matrix.py }}.${{ matrix.os }}.${{ matrix.isort }}.${{ matrix.black }}.${{ matrix.pydantic }}
name: .coverage.${{ matrix.py || matrix.tox_env }}-${{ matrix.os == '' && 'ubuntu-24.04' || matrix.os }}
path: ".tox/.coverage.*"
retention-days: 3

Expand All @@ -94,7 +80,7 @@ jobs:
- name: Build package to generate version
run: uv build --python 3.13 --python-preference only-managed --wheel . --out-dir dist
- name: Setup coverage tool
run: tox -e coverage --notest
run: tox run -e coverage --notest
env:
UV_PYTHON_PREFERENCE: only-managed
- name: Download coverage data
Expand All @@ -104,7 +90,7 @@ jobs:
pattern: .coverage.*
merge-multiple: true
- name: Combine and report coverage
run: tox -e coverage --skip-uv-sync --skip-pkg-install
run: tox run -e coverage --skip-uv-sync --skip-pkg-install
env:
UV_PYTHON_PREFERENCE: only-managed
- name: Upload HTML report
Expand All @@ -120,9 +106,10 @@ jobs:
fail_ci_if_error: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

check:
name: tox env ${{ matrix.tox_env }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
name: Run ${{ matrix.tox_env }} check
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Expand All @@ -132,8 +119,6 @@ jobs:
- docs
- pkg_meta
- readme
os:
- ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -144,5 +129,9 @@ jobs:
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
- name: Setup check suite
run: tox r -vv --notest --skip-missing-interpreters false -e ${{ matrix.tox_env }}
env:
UV_PYTHON_PREFERENCE: "only-managed"
- name: Run check for ${{ matrix.tox_env }}
run: tox run --skip-uv-sync --skip-pkg-install -e ${{ matrix.tox_env }}
env:
UV_PYTHON_PREFERENCE: "only-managed"
43 changes: 40 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies = [
"openapi-spec-validator>=0.2.8,<0.7",
"packaging",
"prance>=0.18.2",
"pydantic>=1.10",
"pydantic>=1.5",
"pysnooper>=0.4.1,<2",
"pyyaml>=6.0.1",
"tomli>=2.2.1,<3; python_version<='3.11'",
Expand Down Expand Up @@ -75,6 +75,7 @@ test = [
"pytest-cov>=5",
"pytest-mock>=3.14",
"pytest-xdist>=3.3.1",
"setuptools; python_version<'3.9'",
]
type = [
"pyright>=1.1.393",
Expand All @@ -88,6 +89,12 @@ docs = [
"mkdocs>=1.6",
"mkdocs-material>=9.5.31",
]
black19-pydantic18 = [ "black==19.10b0", "pydantic==1.8.2" ]
black22 = [ "black==22.1" ]
black23 = [ "black==23.12" ]
black24 = [ "black==24.1" ]
pydantic18 = [ "pydantic==1.8.2" ]
isort4-pydantic15 = [ "isort[pyproject]==4.3.21", "pydantic==1.5.1" ]
fix = [ "pre-commit>=3.5" ]
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.22" ]
coverage = [
Expand Down Expand Up @@ -129,13 +136,15 @@ filterwarnings = [
"ignore:^.*format of 'unknown-type' not understood for 'string' - using default.*",
"ignore:^.*unclosed file.*",
"ignore:^.*black doesn't support `experimental-string-processing` option for wrapping string literal in .*",
"ignore:^.*jsonschema.exceptions.RefResolutionError is deprecated as of version 4.18.0. If you wish to catch potential reference resolution errors, directly catch referencing.exceptions.Unresolvable..*",
"ignore:^.*`experimental string processing` has been included in `preview` and deprecated. Use `preview` instead..*",
]
norecursedirs = "tests/data/*"

[tool.coverage]
run.source = [ "datamodel_code_generator" ]
run.branch = true
run.omit = [ "scripts/*" ]
run.omit = [ "scripts/*", "tests/*" ]
report.ignore_errors = true
report.exclude_lines = [
"if self.debug:",
Expand All @@ -145,11 +154,39 @@ report.exclude_lines = [
"if TYPE_CHECKING:",
"if not TYPE_CHECKING:",
]
report.omit = [ "tests/*" ]
paths.source = [
"datamodel_code_generator",
".tox*/*/lib/python*/site-packages/datamodel_code_generator",
".tox*\\*\\Lib\\site-packages/datamodel_code_generator",
"*/datamodel_code_generator",
"*\\datamodel_code_generator",
]

[tool.pyright]
reportPrivateImportUsage = false

[tool.pydantic-pycharm-plugin]
ignore-init-method-arguments = true
parsable-types.str = [ "int", "float" ]

[tool.uv]
conflicts = [
[
{ group = "black24" },
{ group = "black22" },
{ group = "black23" },
{ group = "black19-pydantic18" },
{ group = "pydantic18" },
{ group = "isort4-pydantic15" },
{ group = "pkg-meta" },
],
[
{ group = "black24" },
{ group = "black22" },
{ group = "black23" },
{ group = "black19-pydantic18" },
{ group = "pydantic18" },
{ group = "isort4-pydantic15" },
{ group = "dev" },
],
]
23 changes: 18 additions & 5 deletions tests/main/openapi/test_main_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,11 @@ def test_main_modular_filename(tmpdir_factory: TempdirFactory) -> None:
)


def test_main_openapi_no_file(capsys: CaptureFixture) -> None:
def test_main_openapi_no_file(
capsys: CaptureFixture, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test main function on non-modular file with no output name."""

monkeypatch.chdir(tmp_path)
input_filename = OPEN_API_DATA_PATH / 'api.yaml'

with freeze_time(TIMESTAMP):
Expand Down Expand Up @@ -313,10 +315,15 @@ def test_main_openapi_no_file(capsys: CaptureFixture) -> None:
reason="Installed black doesn't support the old style",
)
def test_main_openapi_extra_template_data_config(
capsys: CaptureFixture, output_model, expected_output
capsys: CaptureFixture,
output_model,
expected_output,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test main function with custom config data in extra template."""

monkeypatch.chdir(tmp_path)
input_filename = OPEN_API_DATA_PATH / 'api.yaml'
extra_template_data = OPEN_API_DATA_PATH / 'extra_data.json'

Expand All @@ -337,9 +344,12 @@ def test_main_openapi_extra_template_data_config(
assert captured.err == inferred_message.format('openapi') + '\n'


def test_main_custom_template_dir_old_style(capsys: CaptureFixture) -> None:
def test_main_custom_template_dir_old_style(
capsys: CaptureFixture, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test main function with custom template directory."""

monkeypatch.chdir(tmp_path)
input_filename = OPEN_API_DATA_PATH / 'api.yaml'
custom_template_dir = DATA_PATH / 'templates_old_style'
extra_template_data = OPEN_API_DATA_PATH / 'extra_data.json'
Expand All @@ -363,7 +373,10 @@ def test_main_custom_template_dir_old_style(capsys: CaptureFixture) -> None:
assert captured.err == inferred_message.format('openapi') + '\n'


def test_main_openapi_custom_template_dir(capsys: CaptureFixture) -> None:
def test_main_openapi_custom_template_dir(
capsys: CaptureFixture, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
"""Test main function with custom template directory."""

input_filename = OPEN_API_DATA_PATH / 'api.yaml'
Expand Down
3 changes: 2 additions & 1 deletion tests/parser/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ def test_parse_array_schema():
)


def test_parse_nested_array():
def test_parse_nested_array(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
parser = JsonSchemaParser(
DATA_PATH / 'nested_array.json',
data_model_field_type=DataModelFieldBase,
Expand Down
Loading

0 comments on commit 7aad16c

Please sign in to comment.