From 7f57120fdabd29763bd15a706d305bf4df47335c Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Fri, 27 Dec 2024 05:00:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20draft=20version=20fallback?= =?UTF-8?q?=20helper=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/ext_test.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/ext_test.py diff --git a/tests/ext_test.py b/tests/ext_test.py new file mode 100644 index 0000000..2ffc3f1 --- /dev/null +++ b/tests/ext_test.py @@ -0,0 +1,58 @@ +"""The Sphinx extension interface module tests.""" + +import pytest + +from sphinx.config import Config as SphinxConfig + +from sphinxcontrib.towncrier.ext import _get_draft_version_fallback + + +release_sentinel = object() +version_sentinel = object() + + +@pytest.fixture +def sphinx_config() -> SphinxConfig: + """Initialize a Sphinx config stub for testing.""" + return SphinxConfig( + overrides={'release': release_sentinel, 'version': version_sentinel}, + ) + + +@pytest.mark.parametrize( + ('autoversion_mode', 'expected_version'), + ( + ('sphinx-release', release_sentinel), + ('sphinx-version', version_sentinel), + ('draft', '[UNRELEASED DRAFT]'), + ), +) +def test__get_draft_version_fallback_known_strategy( # noqa: WPS116, WPS118 + autoversion_mode: str, + expected_version: object, + sphinx_config: SphinxConfig, +) -> None: + """Check that valid strategies source correct values.""" + computed_version = _get_draft_version_fallback.__wrapped__( + autoversion_mode, + sphinx_config, + ) + assert computed_version == expected_version + + +@pytest.mark.parametrize('autoversion_mode', ('blah', '', 'v1.0')) +def test__get_draft_version_fallback_invalid_strategy( # noqa: WPS116, WPS118 + autoversion_mode: str, + sphinx_config: SphinxConfig, +) -> None: + """Ensure invalid strategy yields an exception.""" + expected_error_msg = ( + '^Expected "strategy" to be one of ' + r"{'[\w,\s'-]+'} " + fr'but got {autoversion_mode !r}$' + ) + with pytest.raises(ValueError, match=expected_error_msg): + _get_draft_version_fallback.__wrapped__( + autoversion_mode, + sphinx_config, + )