Skip to content

Commit

Permalink
Add a bit of infrastructure for testing transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Dec 25, 2022
1 parent a145912 commit 0e4a00d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ Icon
*.vmx
*.log
*.prt
*.bsp
*.cache

# Ignore BSPs, but only in some folders.
instances/**/*.bsp
examples/**/*.bsp

# Output of "compiled" FGDs
build/
build_tmp/
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
trio_mode = true
1 change: 1 addition & 0 deletions src/hammeraddons/bsp_transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
__all__ = [
'check_control_enabled',
'Context', 'trans', 'run_transformations',
'TransFunc', 'TRANSFORMS',
]


Expand Down
4 changes: 4 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest-trio>=0.8.0
pytest>>7.0.0
pytest-datadir>=1.3.1
pytest-regressions>=2.2.0
Empty file added tests/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions tests/test_transforms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import importlib
import sys
from pathlib import Path
from typing import Awaitable, Callable

import pytest

from hammeraddons.bsp_transform import Context, TransFunc, TRANSFORMS
from srctools.bsp import BSP
from srctools.filesys import VirtualFileSystem
from srctools.game import Game
from srctools.packlist import PackList


@pytest.fixture
def blank_ctx(shared_datadir: Path) -> Context:
"""Build a blank context."""
bsp = BSP(shared_datadir / 'blank.bsp')
game = Game(shared_datadir)
fsys = VirtualFileSystem({})
return Context(
fsys,
bsp.ents,
PackList(fsys),
bsp,
game,
)


def get_transform_func(module_name: str, transform: str) -> Callable[[Context], Awaitable[None]]:
"""Import the builtin transforms, then fetch this context."""
folder = str(Path(__file__, '..', '..', '..', 'transforms').resolve())
print(f'Adding "{folder}" to path.')

sys.path.append(folder)
try:
importlib.import_module(module_name)
return TRANSFORMS[transform]
finally:
sys.path.remove(folder)
Binary file added tests/test_transforms/data/blank.bsp
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/test_transforms/data/gameinfo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"GameInfo"
{
game "HALF-LIFE 2"
title "HALF-LIFE'"
type singleplayer_only

FileSystem
{
SteamAppId 220
SearchPaths
{
}
}
}

0 comments on commit 0e4a00d

Please sign in to comment.