diff --git a/pyproject.toml b/pyproject.toml index 59199e9..eee6469 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,11 @@ include = [ "/tests", ] +[tool.hatch.envs.hatch-test] +extra-dependencies = [ + "TurboGears2 @ git+https://github.com/TurboGears/tg2.git@942335e4de87d36ec7dc480b1e0e41dc01e6f0ff", +] + [[tool.hatch.envs.hatch-test.matrix]] python = ["3.9", "3.10", "3.11", "3.12"] diff --git a/tests/integration/test_tg2.py b/tests/integration/test_tg2.py new file mode 100644 index 0000000..10d725e --- /dev/null +++ b/tests/integration/test_tg2.py @@ -0,0 +1,91 @@ +"""Test we don't break TurboGears' usage of our Python API.""" + +import dataclasses +import wsgiref.util +from pathlib import Path +from typing import Any + +import pytest + +from kajiki import i18n + +DATA = Path(__file__).resolve().parent.parent / "data" +GOLDEN = DATA / "golden" + + +@pytest.fixture +def tg2(): + """Import tg2 (if available), and cleanup its mess.""" + orig_gettext = i18n.gettext + try: + import tg + except ImportError: + pytest.skip("TurboGears not installed") + else: + yield tg + finally: + i18n.gettext = orig_gettext + + +@pytest.fixture +def tg2_app(tg2): + """Create a new tg2 app with defaults for testing. + + Returns: + A WSGI application. + """ + + class RootController(tg2.TGController): + @tg2.expose("kajiki_test_data.kitchensink") + def index(self): + return {} + + config = tg2.MinimalApplicationConfigurator() + config.update_blueprint( + { + "root_controller": RootController(), + "renderers": ["kajiki"], + "templating.kajiki.template_extension": ".html", + } + ) + return config.make_wsgi_app() + + +@dataclasses.dataclass +class Response: + """Encapsulates a response for testing.""" + + status: str + headers: list[tuple[str, str]] + body: str + + +def app_request(app: Any, path: str = "/", method: str = "GET") -> Response: + response_status = "" + response_headers = [] + + def start_response(status, headers): + nonlocal response_status + nonlocal response_headers + response_status = status + response_headers = headers + + environ = {} + wsgiref.util.setup_testing_defaults(environ) + environ["REQUEST_METHOD"] = method + environ["PATH_INFO"] = path + + response_body = b"".join(app(environ, start_response)) + + return Response( + status=response_status, + headers=response_headers, + body=response_body, + ) + + +def test_smoke_tg2(tg2_app): + """Do a basic smoke test of using kajiki renderer on tg2.""" + response = app_request(tg2_app, "/") + assert response.status == "200 OK" + assert response.body == (GOLDEN / "kitchensink1.html").read_bytes()