Skip to content

Commit

Permalink
refactor unit tests to also test Windows and be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmachine committed Oct 20, 2024
1 parent 716ccdd commit 42fc1b2
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 103 deletions.
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
4 changes: 2 additions & 2 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-14]
os: [ubuntu-latest, macos-14, windows-latest]
steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand All @@ -24,7 +24,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-14]
os: [ubuntu-latest, macos-14, windows-latest]
needs: pre-commit
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion test/chk_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ 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()
WINDOWS_STORMLIB = Path(
Path.joinpath(_RESOURCES_DIR_PATH, "stormlib/windows/Storm.dll")
).absolute()

EXAMPLE_STARCRAFT_SCX_MAP = Path(
Path.joinpath(_RESOURCES_DIR_PATH, "stormlib/example-stacraft-map.scx")
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: embedded_stormlib_path
"""
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.

74 changes: 74 additions & 0 deletions test/helpers/stormlib_test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import platform
from pathlib import PosixPath
from test.chk_resources import (
LINUX_STORMLIB_X86_64,
MACOS_STORMLIB_M1,
WINDOWS_STORMLIB,
)
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_windows() -> bool:
return platform.system().lower() == "windows"


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


def _first_true(iterable, default=False, predicate=None):
"Returns the first true value or the *default* if there is no true value."
# first_true([a,b,c], x) → a or b or c or x
# first_true([a,b], x, f) → a if f(a) else b if f(b) else x
return next(filter(predicate, iterable), default)


def _get_embedded_stormlib_path() -> Optional[str]:
possible_stormlibs: tuple[tuple[bool, PosixPath], ...] = (
(run_test_if_mac_m1(), MACOS_STORMLIB_M1),
(run_test_if_linux_x86_64(), LINUX_STORMLIB_X86_64),
(run_test_if_windows(), WINDOWS_STORMLIB),
)
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_create_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

0 comments on commit 42fc1b2

Please sign in to comment.