diff --git a/tests-unit/compare_test/conftest_test.py b/tests-unit/compare_test/conftest_test.py new file mode 100644 index 000000000..01b19a942 --- /dev/null +++ b/tests-unit/compare_test/conftest_test.py @@ -0,0 +1,22 @@ +import os +import tempfile + +import pytest + +from tests.compare.conftest import gather_file_basenames + + +class TestGatherFileBasenames: + def test_returns_png_basenames(self): + with tempfile.TemporaryDirectory() as tmpdir: + open(os.path.join(tmpdir, "a.png"), "w").close() + open(os.path.join(tmpdir, "b.txt"), "w").close() + open(os.path.join(tmpdir, "c.png"), "w").close() + assert sorted(gather_file_basenames(tmpdir)) == ["a.png", "c.png"] + + def test_missing_directory_returns_empty_list(self): + assert gather_file_basenames("/nonexistent/path/12345") == [] + + def test_non_string_input_raises(self): + with pytest.raises(AssertionError): + gather_file_basenames(123) # type: ignore[arg-type] diff --git a/tests/compare/conftest.py b/tests/compare/conftest.py index dd5078c9e..e2368e4f5 100644 --- a/tests/compare/conftest.py +++ b/tests/compare/conftest.py @@ -27,12 +27,11 @@ def args_pytest(pytestconfig): return args -def gather_file_basenames(directory: str): - files = [] - for file in os.listdir(directory): - if file.endswith(".png"): - files.append(file) - return files +def gather_file_basenames(directory: str) -> list[str]: + assert isinstance(directory, str), f"directory must be str, got {type(directory).__name__}" + if not os.path.isdir(directory): + return [] + return [file for file in os.listdir(directory) if file.endswith(".png")] # Creates the list of baseline file names to use as a fixture def pytest_generate_tests(metafunc):