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

Refactor stormlib unit tests #96

Merged
merged 12 commits into from
Oct 21, 2024
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

[flake8]
max-line-length = 110
max-doc-length = 200
max-doc-length = 200
per-file-ignores = conftest.py:F401
Binary file removed src/richchk/mpq/stormlib/dlls/windows/Storm.dll
Binary file not shown.
Empty file.
2 changes: 1 addition & 1 deletion src/richchk/mpq/stormlib/stormlib_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StormLibFinder:
_SCRIPT_PATH = os.path.dirname(__file__)
_MAC_STORM_INTEL = os.path.join(_SCRIPT_PATH, "dlls/macos/libStorm.dylib")
_MAC_STORM_M1 = os.path.join(_SCRIPT_PATH, "dlls/macos/libstorm.9.22.0.dylib")
_WINDOWS_STORM = os.path.join(_SCRIPT_PATH, "dlls/windows/Storm.dll")
_WINDOWS_STORM = os.path.join(_SCRIPT_PATH, "dlls\\windows\\StormLib.dll")
_LINUX_STORM_X86_64 = os.path.join(_SCRIPT_PATH, "dlls/linux/libstorm.so.9.22.0")

@classmethod
Expand Down
1 change: 0 additions & 1 deletion test/chk_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def _extract_chk_section_name_from_file_path(file_path: str) -> str:
MACOS_STORMLIB_M1 = Path(
Path.joinpath(_RESOURCES_DIR_PATH, "stormlib/macos/libstorm.9.22.0.dylib")
).absolute()

LINUX_STORMLIB_X86_64 = Path(
Path.joinpath(_RESOURCES_DIR_PATH, "stormlib/linux/libstorm.so.9.22.0")
).absolute()
Expand Down
7 changes: 7 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Makes all pytest fixtures available to use across any unit test.

See: https://gist.github.com/peterhurford/09f7dcda0ab04b95c026c60fa49c2a68
"""
import pytest

from .helpers.stormlib_test_helper import embedded_stormlib, embedded_stormlib_path
13 changes: 0 additions & 13 deletions test/helpers/stormlib_helper.py

This file was deleted.

60 changes: 60 additions & 0 deletions test/helpers/stormlib_test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import platform
from test.chk_resources import LINUX_STORMLIB_X86_64, MACOS_STORMLIB_M1
from typing import Optional

import pytest

from richchk.model.mpq.stormlib.stormlib_file_path import StormLibFilePath
from richchk.mpq.stormlib.stormlib_loader import StormLibLoader
from richchk.mpq.stormlib.stormlib_wrapper import StormLibWrapper


def run_test_if_mac_m1() -> bool:
return (
platform.system().lower() == "darwin" and platform.machine().lower() == "arm64"
)


def run_test_if_linux_x86_64() -> bool:
return (
platform.system().lower() == "linux" and platform.machine().lower() == "x86_64"
)


def run_test_if_supported_os() -> bool:
return any([run_test_if_mac_m1(), run_test_if_linux_x86_64()])


def _first_true(iterable, default=False, predicate=None):
"""Returns the first true value or the *default* if there is no true value."""
return next(filter(predicate, iterable), default)


def _get_embedded_stormlib_path() -> Optional[str]:
possible_stormlibs: tuple[tuple[bool, str], ...] = (
(run_test_if_mac_m1(), str(MACOS_STORMLIB_M1)),
(run_test_if_linux_x86_64(), str(LINUX_STORMLIB_X86_64)),
)
maybe_embedded_stormlib = _first_true(
possible_stormlibs, default=None, predicate=lambda x: x[0] is True
)
if maybe_embedded_stormlib:
return maybe_embedded_stormlib[1]


@pytest.fixture(scope="function")
def embedded_stormlib_path() -> Optional[str]:
return _get_embedded_stormlib_path()


@pytest.fixture(scope="function")
def embedded_stormlib() -> Optional[StormLibWrapper]:
embedded_stormlib_path = _get_embedded_stormlib_path()
if embedded_stormlib_path:
return StormLibWrapper(
StormLibLoader.load_stormlib(
path_to_stormlib=StormLibFilePath(
_path_to_stormlib_dll=embedded_stormlib_path
)
)
)
2 changes: 1 addition & 1 deletion test/io/mpq/stacraft_wav_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
EXAMPLE_WAV_FILE,
MACOS_STORMLIB_M1,
)
from ...helpers.stormlib_helper import run_test_if_mac_m1
from ...helpers.stormlib_test_helper import run_test_if_mac_m1

# the canonical place the CHK is stored in a SCX/SCM map file
_CHK_MPQ_PATH = "staredit\\scenario.chk"
Expand Down
2 changes: 1 addition & 1 deletion test/io/mpq/starcraft_mpq_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
EXAMPLE_STARCRAFT_SCX_MAP,
MACOS_STORMLIB_M1,
)
from ...helpers.stormlib_helper import run_test_if_mac_m1
from ...helpers.stormlib_test_helper import run_test_if_mac_m1

# the canonical place the CHK is stored in a SCX/SCM map file
_CHK_MPQ_PATH = "staredit\\scenario.chk"
Expand Down
2 changes: 1 addition & 1 deletion test/io/mpq/starcraft_wav_metadata_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
EXAMPLE_STARCRAFT_SCM_MAP,
MACOS_STORMLIB_M1,
)
from ...helpers.stormlib_helper import run_test_if_mac_m1
from ...helpers.stormlib_test_helper import run_test_if_mac_m1

# these are the 3 WAV files in COMPLEX_STARCRAFT_SCX_MAP
# these are the paths each WAV file is stored inside the MPQ archive
Expand Down
5 changes: 4 additions & 1 deletion test/mpq/stormlib/search/stormlib_file_searcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
LINUX_STORMLIB_X86_64,
MACOS_STORMLIB_M1,
)
from ....helpers.stormlib_helper import run_test_if_linux_x86_64, run_test_if_mac_m1
from ....helpers.stormlib_test_helper import (
run_test_if_linux_x86_64,
run_test_if_mac_m1,
)

# the canonical place the CHK is stored in a SCX/SCM map file
_CHK_MPQ_PATH = "staredit\\scenario.chk"
Expand Down
17 changes: 12 additions & 5 deletions test/mpq/stormlib/stormlib_helper_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import platform

import pytest

from richchk.mpq.stormlib.stormlib_helper import StormLibHelper

from ...chk_resources import MACOS_STORMLIB_M1
from ...helpers.stormlib_test_helper import run_test_if_supported_os


def _run_test_if_mac_m1() -> bool:
Expand All @@ -13,12 +15,17 @@ def _run_test_if_mac_m1() -> bool:


def test_it_create_stormlib_wrapper_from_embedded_dll():
if _run_test_if_mac_m1():
if run_test_if_supported_os():
stormlib = StormLibHelper.load_stormlib(path_to_stormlib_dll=None)
assert os.path.exists(stormlib.stormlib.path_to_stormlib.path_to_stormlib_dll)


def test_it_create_stormlib_wrapper_from_provided_dll():
if _run_test_if_mac_m1():
stormlib = StormLibHelper.load_stormlib(path_to_stormlib_dll=MACOS_STORMLIB_M1)
@pytest.mark.usefixtures("embedded_stormlib_path")
def test_it_creates_stormlib_wrapper_from_provided_dll(
embedded_stormlib_path,
):
if embedded_stormlib_path:
stormlib = StormLibHelper.load_stormlib(
path_to_stormlib_dll=embedded_stormlib_path
)
assert os.path.exists(stormlib.stormlib.path_to_stormlib.path_to_stormlib_dll)
Loading
Loading