From 417fe98aea57fc1870427b2a4d331f072d604646 Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Fri, 11 Aug 2023 18:40:26 +0300 Subject: [PATCH] Do the test export via pytest --- .github/workflows/patch.yml | 30 --------------------- .github/workflows/python.yml | 41 +++++++++++++++++++++++++---- tests/conftest.py | 51 +++++++++++++++++++++++++++++++++--- tests/test_full_patch.py | 14 ++++++++++ tests/test_lua_util.py | 10 +++---- tests/test_schema.py | 10 ++----- 6 files changed, 103 insertions(+), 53 deletions(-) delete mode 100644 .github/workflows/patch.yml create mode 100644 tests/test_full_patch.py diff --git a/.github/workflows/patch.yml b/.github/workflows/patch.yml deleted file mode 100644 index 4cc0d45f8..000000000 --- a/.github/workflows/patch.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Patcher - -on: - push: - branches: - - 'main' - -jobs: - starter_preset_patcher: - runs-on: self-hosted - - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - submodules: 'recursive' - - - name: Create venv - run: python -m venv venv - - - name: Install Python packages - run: venv/bin/python -m pip install --upgrade pip setuptools - - - name: Install - run: venv/bin/python -m pip install -e . - - - name: Export - run: - venv/bin/python -m open_dread_rando --input-path "$DREAD_PATH" --output-path export/ --input-json tests/test_files/starter_preset_patcher.json \ No newline at end of file diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 505bbc21f..5e61cf23c 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -2,6 +2,7 @@ name: Python Package on: pull_request: + merge_group: push: branches: - '*' @@ -65,16 +66,46 @@ jobs: with: name: python-package-distributions path: dist/ - - - name: Install Python packages - run: python -m pip install --upgrade pip pytest pytest-cov lupa - name: install built wheel - run: python -m pip install dist/*.whl + run: python -m pip install "$(ls dist/*.whl)[test]" shell: bash - name: test - run: python -m pytest --cov open_dread_rando --cov-report=xml tests + run: python -m pytest --cov src --cov-report=xml --skip-if-missing + + - name: codecov + uses: codecov/codecov-action@v3 + with: + fail_ci_if_error: false + files: ./coverage.xml + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + + full_test: + runs-on: self-hosted + needs: + - build + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + submodules: 'recursive' + + - name: Create venv + run: python -m venv venv + + - name: Install Python packages + run: venv/bin/python -m pip install --upgrade pip + + - name: install built wheel + run: venv/bin/python -m pip install -e .[test] + + - name: Run Tests + run: + venv/bin/python -m pytest --cov src --cov-report=xml --durations=100 - name: codecov uses: codecov/codecov-action@v3 diff --git a/tests/conftest.py b/tests/conftest.py index b95973d42..5bd322f72 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,12 +1,47 @@ +import json +import os +import typing from pathlib import Path import lupa import pytest +_FAIL_INSTEAD_OF_SKIP = True + + +def get_env_or_skip(env_name, override_fail: typing.Optional[bool] = None): + if override_fail is None: + fail_or_skip = _FAIL_INSTEAD_OF_SKIP + else: + fail_or_skip = override_fail + if env_name not in os.environ: + if fail_or_skip: + pytest.fail(f"Missing environment variable {env_name}") + else: + pytest.skip(f"Skipped due to missing environment variable {env_name}") + return os.environ[env_name] + + +class TestFilesDir: + def __init__(self, root: Path): + self.root = root + + def joinpath(self, *paths) -> Path: + return self.root.joinpath(*paths) + + def read_json(self, *paths) -> typing.Union[dict, list]: + with self.joinpath(*paths).open() as f: + return json.load(f) -@pytest.fixture() -def test_files(): - return Path(__file__).parent.joinpath("test_files") + +@pytest.fixture(scope="session") +def dread_path(): + return Path(get_env_or_skip("DREAD_PATH")) + + +@pytest.fixture(scope="session") +def test_files_dir() -> TestFilesDir: + return TestFilesDir(Path(__file__).parent.joinpath("test_files")) @pytest.fixture() @@ -19,3 +54,13 @@ def lua_runtime(): function Game.SetForceSkipCutscenes() end """) return runtime + + +def pytest_addoption(parser): + parser.addoption('--skip-if-missing', action='store_false', dest="fail_if_missing", + default=True, help="Skip tests instead of missing, in case any asset is missing") + + +def pytest_configure(config: pytest.Config): + global _FAIL_INSTEAD_OF_SKIP + _FAIL_INSTEAD_OF_SKIP = config.option.fail_if_missing diff --git a/tests/test_full_patch.py b/tests/test_full_patch.py new file mode 100644 index 000000000..1d1975cdb --- /dev/null +++ b/tests/test_full_patch.py @@ -0,0 +1,14 @@ +from open_dread_rando import dread_patcher + + +def test_export(dread_path, tmp_path, test_files_dir): + output_path = tmp_path.joinpath("out") + configuration = test_files_dir.read_json("starter_preset_patcher.json") + + dread_patcher.patch_extracted( + input_path=dread_path, + output_path=output_path, + configuration=configuration, + ) + assert len(list(output_path.rglob("*.lc"))) == 32 + diff --git a/tests/test_lua_util.py b/tests/test_lua_util.py index 93b478541..346155400 100644 --- a/tests/test_lua_util.py +++ b/tests/test_lua_util.py @@ -1,4 +1,3 @@ -from pathlib import Path from unittest.mock import MagicMock from open_dread_rando.misc_patches import lua_util @@ -16,7 +15,7 @@ def test_create_script_copy_exists(): editor.add_new_asset.assert_not_called() -def test_template_replacement(): +def test_template_replacement(test_files_dir): generated_code = lua_util.replace_lua_template("randomizer_progressive_template.lua", { "name": "RandomizerTestPowerup", "parent": "RandomizerPowerup", @@ -25,9 +24,6 @@ def test_template_replacement(): ], }) - test_files: Path = Path(__file__).parent.joinpath("test_files") + expected_code = test_files_dir.joinpath("randomizer_progressive_expected.lua").read_text() - with test_files.joinpath("randomizer_progressive_expected.lua").open() as f: - expected_code = f.read() - - assert generated_code == expected_code + assert generated_code == expected_code diff --git a/tests/test_schema.py b/tests/test_schema.py index a5300312c..d17b8827d 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1,13 +1,7 @@ -import json -from pathlib import Path - from open_dread_rando import dread_patcher -def test_starter_preset(): - test_files: Path = Path(__file__).parent.joinpath("test_files") - - with test_files.joinpath("starter_preset_patcher.json").open() as f: - configuration = json.load(f) +def test_starter_preset(test_files_dir): + configuration = test_files_dir.read_json("starter_preset_patcher.json") dread_patcher.validate(configuration)