|
| 1 | +import shutil |
| 2 | +import subprocess |
| 3 | +import time |
| 4 | +from collections.abc import Generator |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +def run_build(config_path: Path) -> subprocess.CompletedProcess[str]: |
| 11 | + return subprocess.run( |
| 12 | + ["mkcd2app", "build", str(config_path)], |
| 13 | + capture_output=True, |
| 14 | + text=True, |
| 15 | + timeout=300, |
| 16 | + check=True, |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def run_empty_game_build() -> subprocess.CompletedProcess[str]: |
| 21 | + yaml_path = Path.cwd() / "tests" / "fixtures" / "empty_game_build_to_statics.yaml" |
| 22 | + return run_build(yaml_path) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="module") |
| 26 | +def empty_game_build_to_statics() -> Generator[None]: |
| 27 | + run_empty_game_build() |
| 28 | + |
| 29 | + yield |
| 30 | + |
| 31 | + dir_path = Path.cwd() / "examples" / "testing" / "empty_game_build" |
| 32 | + shutil.rmtree(dir_path) |
| 33 | + |
| 34 | + |
| 35 | +def test_empty_game_build_to_statics_static(empty_game_build_to_statics: None) -> None: |
| 36 | + build_dir = Path("examples/testing/empty_game_build") |
| 37 | + static_dist = build_dir / "empty-game-website-filled-built" / "dist" |
| 38 | + |
| 39 | + # check for index.html and assets dir |
| 40 | + assert (static_dist / "index.html").is_file(), "expected index.html file present" |
| 41 | + assert (static_dist / "assets").is_dir(), "expected assets directory present" |
| 42 | + # ensure nonzero amount of css and js files |
| 43 | + css_files = list((static_dist / "assets").glob("*.css")) |
| 44 | + js_files = list((static_dist / "assets").glob("*.js")) |
| 45 | + assert css_files, "expected css files present" |
| 46 | + assert js_files, "expected js files present" |
| 47 | + |
| 48 | + |
| 49 | +def test_empty_game_build_to_statics_static_singlefile( |
| 50 | + empty_game_build_to_statics: None, |
| 51 | +) -> None: |
| 52 | + build_dir = Path("examples/testing/empty_game_build") |
| 53 | + static_singlefile_dist = ( |
| 54 | + build_dir / "empty-game-website-filled-singlefile-built" / "dist" |
| 55 | + ) |
| 56 | + |
| 57 | + # ensure no other files or directories are present in the dist |
| 58 | + items = list(static_singlefile_dist.iterdir()) |
| 59 | + assert len(items) == 1, ( |
| 60 | + f"expected only index.html in {static_singlefile_dist}, found: {[p.name for p in items]}" |
| 61 | + ) |
| 62 | + only = items[0] |
| 63 | + assert only.is_file(), "expected only index.html present" |
| 64 | + assert only.name == "index.html", "expected only index.html present" |
| 65 | + |
| 66 | + |
| 67 | +def test_empty_game_build_to_statics_second_build_caches( |
| 68 | + empty_game_build_to_statics: None, |
| 69 | +) -> None: |
| 70 | + start_time = time.monotonic() |
| 71 | + run_empty_game_build() # this should do nothing file wise, all hit caches |
| 72 | + end_time = time.monotonic() |
| 73 | + elapsed_time = end_time - start_time |
| 74 | + |
| 75 | + assert elapsed_time < 5, ( |
| 76 | + f"expected second build to all hit cache and take less than 5s, took {elapsed_time}s" |
| 77 | + ) |
0 commit comments