Skip to content

Commit

Permalink
tests: handle contextmanager in the fixture run
Browse files Browse the repository at this point in the history
Instead of having fixture functions be decorated with @contextmanager,
handle it from get_result, which will use is as a context manager if it
is a generator function and get the result and add the tear down as
appropriate.
  • Loading branch information
enku committed Jul 26, 2024
1 parent 8ac4b99 commit 32fe878
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
29 changes: 12 additions & 17 deletions tests/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import datetime as dt
import importlib.metadata
import inspect
import itertools
import os
import tempfile
Expand Down Expand Up @@ -67,13 +68,11 @@ def dec(fn: FixtureFunction) -> FixtureFunction:
return dec


@contextmanager
def tmpdir(_options: FixtureOptions, _fixtures: Fixtures) -> FixtureContext[Path]:
with tempfile.TemporaryDirectory() as tempdir:
yield Path(tempdir)


@contextmanager
@depends("tmpdir")
def mock_environment(
options: FixtureOptions, fixtures: Fixtures
Expand All @@ -98,17 +97,12 @@ def settings(_options: FixtureOptions, _fixtures: Fixtures) -> Settings:
return Settings.from_environ()


@contextmanager
@depends("tmpdir")
def publisher(
options: FixtureOptions, fixtures: Fixtures
) -> FixtureContext[BuildPublisher]:
with mock_environment(options, fixtures):
mock_publisher: BuildPublisher = BuildPublisherFactory()
with _patch_publisher("jenkins", mock_publisher):
with _patch_publisher("repo", mock_publisher):
with _patch_publisher("storage", mock_publisher):
yield mock_publisher
@depends("mock_environment")
def publisher(_o: FixtureOptions, _f: Fixtures) -> FixtureContext[BuildPublisher]:
bp: BuildPublisher = BuildPublisherFactory()
pp = _patch_publisher
with pp("jenkins", bp), pp("repo", bp), pp("storage", bp):
yield bp


@depends("publisher")
Expand Down Expand Up @@ -276,8 +270,9 @@ def add_funcs(test: BaseTestCase, funcs: Iterable[FixtureFunction]) -> None:


def get_result(func: FixtureFunction, test: BaseTestCase) -> Any:
result = func(test._options, copy.copy(test.fixtures))
if hasattr(result, "__enter__") and hasattr(result, "__exit__"):
result = test.enterContext(result)
if inspect.isgeneratorfunction(func):
return test.enterContext(
contextmanager(func)(test._options, copy.copy(test.fixtures))
)

return result
return func(test._options, copy.copy(test.fixtures))
2 changes: 0 additions & 2 deletions tests/test_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io
import json
import os
from contextlib import contextmanager
from pathlib import Path
from typing import Any
from unittest import mock
Expand Down Expand Up @@ -564,7 +563,6 @@ def test_str(self) -> None:
self.assertEqual(str(project_path), "Gentoo/repos/marduk")


@contextmanager
def mock_jenkins(
_options: fixture.FixtureOptions, _fixtures: fixture.Fixtures
) -> fixture.FixtureContext[Jenkins]:
Expand Down
4 changes: 0 additions & 4 deletions tests/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# pylint: disable=missing-class-docstring,missing-function-docstring
import datetime as dt
from contextlib import contextmanager
from unittest import mock
from zoneinfo import ZoneInfo

Expand Down Expand Up @@ -307,7 +306,6 @@ def test_save(self) -> None:
self.assertEqual(r2, r3)


@contextmanager
def prepull_events(
_options: FixtureOptions, _fixtures: Fixtures
) -> FixtureContext[list[Build]]:
Expand All @@ -321,7 +319,6 @@ def prepull(build: Build) -> None:
yield events


@contextmanager
def postpull_events(
_options: FixtureOptions, _fixtures: Fixtures
) -> FixtureContext[list[tuple[Build, list[Package], GBPMetadata | None]]]:
Expand All @@ -337,7 +334,6 @@ def postpull(
yield events


@contextmanager
def publish_events(
_options: FixtureOptions, _fixtures: Fixtures
) -> FixtureContext[list[Build]]:
Expand Down

0 comments on commit 32fe878

Please sign in to comment.