From f385bbb52ee43ac7a750e43d2eabaa53165b1ad0 Mon Sep 17 00:00:00 2001 From: nichmor Date: Fri, 26 Jul 2024 18:34:02 +0300 Subject: [PATCH 1/2] feat: add load_all_tests --- src/rattler_build_conda_compat/loader.py | 13 ++++++++ tests/__snapshots__/test_rattler_loader.ambr | 33 ++++++++++++++++++++ tests/data/recipe_tests.yaml | 30 ++++++++++++++++++ tests/test_rattler_loader.py | 21 +++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 tests/data/recipe_tests.yaml diff --git a/src/rattler_build_conda_compat/loader.py b/src/rattler_build_conda_compat/loader.py index 35ce741..33a7fb7 100644 --- a/src/rattler_build_conda_compat/loader.py +++ b/src/rattler_build_conda_compat/loader.py @@ -151,3 +151,16 @@ def load_all_requirements(content: dict[str, Any]) -> dict[str, Any]: requirements_section[section] = list(visit_conditional_list(section_reqs)) return requirements_section + + +def load_all_tests(content: dict[str, Any]) -> list[dict]: + tests_section = content.get("tests", []) + if not tests_section: + return [] + + evaluated_tests = [] + + for section in tests_section: + evaluated_tests.extend(list(visit_conditional_list(section))) + + return evaluated_tests diff --git a/tests/__snapshots__/test_rattler_loader.ambr b/tests/__snapshots__/test_rattler_loader.ambr index ae33579..59acca8 100644 --- a/tests/__snapshots__/test_rattler_loader.ambr +++ b/tests/__snapshots__/test_rattler_loader.ambr @@ -1,4 +1,37 @@ # serializer version: 1 +# name: test_load_all_tests + list([ + dict({ + 'files': dict({ + 'recipe': list([ + 'more_tests/*.py', + ]), + 'source': list([ + 'tests/', + 'test.py', + '*.sh', + ]), + }), + 'requirements': dict({ + 'run': list([ + 'pytest', + ]), + }), + 'script': list([ + 'echo "Hello world"', + 'pytest ./tests', + ]), + }), + dict({ + 'python': dict({ + 'imports': list([ + 'mypkg', + 'mypkg.subpkg', + ]), + }), + }), + ]) +# --- # name: test_load_recipe_with_missing_selectors dict({ 'package': dict({ diff --git a/tests/data/recipe_tests.yaml b/tests/data/recipe_tests.yaml new file mode 100644 index 0000000..c8c2c71 --- /dev/null +++ b/tests/data/recipe_tests.yaml @@ -0,0 +1,30 @@ +package: + name: for-test + version: 1.0.0 + +tests: + - script: + - echo "Hello world" + - pytest ./tests + + requirements: + run: + - pytest + + files: + source: + - tests/ + - test.py + - "*.sh" + + recipe: + - more_tests/*.py + + + # This test section tries to import the Python modules and errors if it can't + - if: unix + then: + - python: + imports: + - mypkg + - mypkg.subpkg diff --git a/tests/test_rattler_loader.py b/tests/test_rattler_loader.py index 60bd1af..775cddd 100644 --- a/tests/test_rattler_loader.py +++ b/tests/test_rattler_loader.py @@ -5,6 +5,7 @@ from rattler_build_conda_compat.loader import ( load_all_requirements, + load_all_tests, load_yaml, parse_recipe_config_file, ) @@ -37,3 +38,23 @@ def test_load_recipe_with_missing_selectors(snapshot) -> None: ) assert loaded_variants == snapshot + + +def test_load_all_tests(snapshot) -> None: + recipe_content = Path("tests/data/recipe_tests.yaml").read_text() + + recipe_content = load_yaml(recipe_content) + + loaded_tests = load_all_tests(recipe_content) + assert loaded_tests == snapshot + + # validate that tests section is a list of dictionaries + # and also verify that dictionary preserver the right inner dict + # let's find script test + script_test = next(test for test in loaded_tests if "script" in test) + # validate that it has run requirements as dictionary + assert script_test["requirements"]["run"][0] == "pytest" + + # let's find python test + python_test = next(test for test in loaded_tests if "python" in test) + assert "mypkg" in python_test["python"]["imports"] From b8d5283639710a927bb1c31c85be0b04bd7b839b Mon Sep 17 00:00:00 2001 From: nichmor Date: Fri, 26 Jul 2024 18:35:17 +0300 Subject: [PATCH 2/2] misc: remove some recipe comments --- tests/data/recipe_tests.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/data/recipe_tests.yaml b/tests/data/recipe_tests.yaml index c8c2c71..ab27969 100644 --- a/tests/data/recipe_tests.yaml +++ b/tests/data/recipe_tests.yaml @@ -20,8 +20,6 @@ tests: recipe: - more_tests/*.py - - # This test section tries to import the Python modules and errors if it can't - if: unix then: - python: