Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do the test export via pytest #249

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions .github/workflows/patch.yml

This file was deleted.

41 changes: 36 additions & 5 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Python Package

on:
pull_request:
merge_group:
push:
branches:
- '*'
Expand Down Expand Up @@ -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
Expand Down
51 changes: 48 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
14 changes: 14 additions & 0 deletions tests/test_full_patch.py
Original file line number Diff line number Diff line change
@@ -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

10 changes: 3 additions & 7 deletions tests/test_lua_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pathlib import Path
from unittest.mock import MagicMock

from open_dread_rando.misc_patches import lua_util
Expand All @@ -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",
Expand All @@ -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
10 changes: 2 additions & 8 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -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)