From 29add7ad9a97cd276b1bd61959966707e4995452 Mon Sep 17 00:00:00 2001 From: Frank Hoffmann <15r10nk-git@polarbit.de> Date: Sun, 22 Dec 2024 10:30:32 +0100 Subject: [PATCH] fix: added `--use-uv` option to install dependencies during test-time --- changelog.d/20241222_102321_15r10nk-git.md | 43 ++++++++++++++++++++++ pyproject.toml | 8 ++-- tests/adapter/test_dataclass.py | 24 +++++++----- tests/conftest.py | 23 +++++++++++- tests/test_pydantic.py | 6 +-- 5 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 changelog.d/20241222_102321_15r10nk-git.md diff --git a/changelog.d/20241222_102321_15r10nk-git.md b/changelog.d/20241222_102321_15r10nk-git.md new file mode 100644 index 00000000..33c084e1 --- /dev/null +++ b/changelog.d/20241222_102321_15r10nk-git.md @@ -0,0 +1,43 @@ + + + + + + +### Fixed + +- uv is now only used during test time if you run the inline-snapshot tests with `pytest --use-uv` + This solves a problem if you want to package inline-snapshot in distributions (#165) + + + + diff --git a/pyproject.toml b/pyproject.toml index 641ba32d..2167f0c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,10 +110,12 @@ matrix.pytest.dependencies = [ [tool.hatch.envs.hatch-test] # Info for everyone who packages this library: -# The following dependencies are installed with uv during the pytest run +# The following dependencies are installed with uv if you run `pytest --use-uv` # and used for specific tests in specific versions: # - pydantic v1 & v2 # - attrs +# But you dont have to use uv to test this library. +# You can also just install the dependencies and use `pytest` normally extra-dependencies = [ "dirty-equals>=0.7.0", "hypothesis>=6.75.5", @@ -126,8 +128,8 @@ extra-dependencies = [ env-vars.TOP = "{root}" [tool.hatch.envs.hatch-test.scripts] -run = "pytest{env:HATCH_TEST_ARGS:} {args}" -run-cov = "coverage run -m pytest{env:HATCH_TEST_ARGS:} {args}" +run = "pytest{env:HATCH_TEST_ARGS:} --use-uv {args}" +run-cov = "coverage run -m pytest{env:HATCH_TEST_ARGS:} --use-uv {args}" cov-combine = "coverage combine" cov-report=["coverage report","coverage html"] diff --git a/tests/adapter/test_dataclass.py b/tests/adapter/test_dataclass.py index 0912645b..0059eadb 100644 --- a/tests/adapter/test_dataclass.py +++ b/tests/adapter/test_dataclass.py @@ -1,3 +1,4 @@ +import pytest from inline_snapshot import snapshot from inline_snapshot.extra import warns from inline_snapshot.testing._example import Example @@ -119,7 +120,12 @@ def test_something(): ) -def test_attrs_default_value(): +@pytest.fixture +def attrs_deps(use_uv): + yield (["attrs"] if use_uv else []) + + +def test_attrs_default_value(attrs_deps): Example( """\ from inline_snapshot import snapshot,Is @@ -138,7 +144,7 @@ def test_something(): """ ).run_pytest( ["--inline-snapshot=fix"], - extra_dependencies=["attrs"], + extra_dependencies=attrs_deps, changed_files=snapshot( { "test_something.py": """\ @@ -160,7 +166,7 @@ def test_something(): ), ).run_pytest( ["--inline-snapshot=update"], - extra_dependencies=["attrs"], + extra_dependencies=attrs_deps, changed_files=snapshot( { "test_something.py": """\ @@ -183,7 +189,7 @@ def test_something(): ) -def test_attrs_field_repr(): +def test_attrs_field_repr(attrs_deps): Example( """\ @@ -199,7 +205,7 @@ class container: """ ).run_pytest( ["--inline-snapshot=create"], - extra_dependencies=["attrs"], + extra_dependencies=attrs_deps, changed_files=snapshot( { "test_something.py": """\ @@ -216,11 +222,11 @@ class container: } ), ).run_pytest( - extra_dependencies=["attrs"], + extra_dependencies=attrs_deps, ) -def test_attrs_unmanaged(): +def test_attrs_unmanaged(attrs_deps): Example( """\ import datetime as dt @@ -246,10 +252,10 @@ def test(): """ ).run_pytest( ["--inline-snapshot=create,fix"], - extra_dependencies=["attrs"], + extra_dependencies=attrs_deps, changed_files=snapshot({}), ).run_pytest( - extra_dependencies=["attrs"], + extra_dependencies=attrs_deps, ) diff --git a/tests/conftest.py b/tests/conftest.py index 0d9aba3d..c05f77d3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,9 +43,30 @@ def check_pypy(request): yield +def pytest_addoption(parser): + parser.addoption( + "--use-uv", + action="store_true", + default=False, + help="install different package versions at test time", + ) + + +@pytest.fixture +def use_uv(request): + yield request.config.getoption("--use-uv") + + @pytest.fixture(params=["pydantic>=2.0.0", "pydantic<2.0.0"]) def pydantic_version(request): - yield request.param + use_uv = request.config.getoption("--use-uv") + + if use_uv: + yield [request.param] + else: # pragma: no cover + if request.param == "pydantic<2.0.0": + pytest.skip() + yield [] @pytest.fixture() diff --git a/tests/test_pydantic.py b/tests/test_pydantic.py index cc901b49..570af74f 100644 --- a/tests/test_pydantic.py +++ b/tests/test_pydantic.py @@ -22,7 +22,7 @@ def test_pydantic(): """ ).run_pytest( ["--inline-snapshot=create"], - extra_dependencies=[pydantic_version], + extra_dependencies=pydantic_version, changed_files=snapshot( { "test_something.py": """\ @@ -64,7 +64,7 @@ class container(BaseModel): """ ).run_pytest( ["--inline-snapshot=create"], - extra_dependencies=[pydantic_version], + extra_dependencies=pydantic_version, changed_files=snapshot( { "test_something.py": """\ @@ -101,7 +101,7 @@ def test_something(): """ ).run_pytest( ["--inline-snapshot=update"], - extra_dependencies=[pydantic_version], + extra_dependencies=pydantic_version, changed_files=snapshot( { "test_something.py": """\