From 63fc8e41e9ead787b2d754941dbde8743b16e2e4 Mon Sep 17 00:00:00 2001 From: Matthew Seiler Date: Sun, 1 Dec 2024 18:17:53 -0500 Subject: [PATCH] Reset config singleton for all tests in test_main.py Fixes order-dependent test failures in test_main.py caused by tests that tried to initialize the singleton BandersnatchConfig instance when it had already been created. This left the config out of sync with what the tests expected. Some tests in this module were already doing this with a call to a 'setup' function that erased the Singleton metaclass's stored instances. This moves that into a pytest fixture and applies it to the whole module. --- src/bandersnatch/tests/test_main.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/bandersnatch/tests/test_main.py b/src/bandersnatch/tests/test_main.py index 47fcdd14d..07093ea0c 100644 --- a/src/bandersnatch/tests/test_main.py +++ b/src/bandersnatch/tests/test_main.py @@ -24,11 +24,16 @@ async def empty_dict(*args: Any, **kwargs: Any) -> dict: return {} -def setup() -> None: - """simple setup function to clear Singleton._instances before each test""" +@pytest.fixture +def reset_config_singleton() -> None: + """Reset the singleton BandersnatchConfig instance""" Singleton._instances = {} +# Use the above fixture for every test function in the current module +pytestmark = pytest.mark.usefixtures("reset_config_singleton") + + def test_main_help(capfd: CaptureFixture) -> None: sys.argv = ["bandersnatch", "--help"] with pytest.raises(SystemExit): @@ -103,7 +108,6 @@ def test_main_reads_config_values(mirror_mock: mock.MagicMock, tmpdir: Path) -> def test_main_reads_custom_config_values( mirror_mock: "BandersnatchMirror", logging_mock: mock.MagicMock, customconfig: Path ) -> None: - setup() conffile = str(customconfig / "bandersnatch.conf") sys.argv = ["bandersnatch", "-c", conffile, "mirror"] main(asyncio.new_event_loop()) @@ -114,7 +118,6 @@ def test_main_reads_custom_config_values( def test_main_throws_exception_on_unsupported_digest_name( customconfig: Path, ) -> None: - setup() conffile = str(customconfig / "bandersnatch.conf") parser = configparser.ConfigParser() parser.read(conffile)