conftest.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Integration tests that run the engine inside real desktop sessions, each one headless and private to the test.
  2. Needs the engine binary (LWE_BINARY, default build/output/linux-wallpaperengine), Wallpaper Engine's assets
  3. (LWE_ASSETS_DIR) and a scene wallpaper (LWE_TEST_WALLPAPER, a workshop item folder). A session whose programs are not
  4. installed here is skipped. Tests only ever talk to the sessions they start: never to the desktop of the machine they
  5. run on.
  6. """
  7. import os
  8. import sys
  9. from pathlib import Path
  10. import pytest
  11. REPO = Path(__file__).resolve().parents[2]
  12. sys.path.insert(0, str(REPO / "tools"))
  13. from desktops.session import KINDS, Session, Unsupported # noqa: E402
  14. STEAM_ROOTS = (
  15. Path("/workspace/SteamLibrary"),
  16. Path.home() / ".local/share/Steam",
  17. Path.home() / ".steam/steam",
  18. )
  19. def _first_existing(paths):
  20. return next((path for path in paths if path.exists()), None)
  21. @pytest.fixture(scope="session")
  22. def engine():
  23. binary = Path(os.environ.get("LWE_BINARY", REPO / "build/output/linux-wallpaperengine"))
  24. assets = os.environ.get("LWE_ASSETS_DIR") or _first_existing(
  25. [root / "steamapps/common/wallpaper_engine/assets" for root in STEAM_ROOTS]
  26. )
  27. wallpaper = os.environ.get("LWE_TEST_WALLPAPER") or _first_existing(
  28. [root / "steamapps/workshop/content/431960/1846362380" for root in STEAM_ROOTS]
  29. )
  30. missing = [what for what, value in (("the engine binary", binary if binary.exists() else None),
  31. ("the assets", assets), ("a test wallpaper", wallpaper)) if not value]
  32. if missing:
  33. pytest.skip(f"missing {', '.join(missing)}, see tests/desktops/conftest.py")
  34. return {"binary": str(binary), "assets": str(assets), "wallpaper": str(wallpaper)}
  35. @pytest.fixture(scope="session")
  36. def desktop_factory(tmp_path_factory):
  37. """one session at a time, shared by the tests of that session: a full desktop is several hundred MB"""
  38. started = {}
  39. available = Session.available()
  40. def get(name):
  41. if available.get(name) is None:
  42. pytest.skip(f"{name} is not installed here and has no rootfs, run: python3 tools/desktop_session.py setup")
  43. for other in [key for key in started if key != name]:
  44. started.pop(other).stop()
  45. if name not in started:
  46. session = Session.of(name)
  47. try:
  48. session.start()
  49. except Exception as error:
  50. pytest.fail(f"{name} did not start: {error}")
  51. started[name] = session
  52. return started[name]
  53. yield get
  54. for session in started.values():
  55. session.stop()
  56. @pytest.fixture
  57. def desktop(request, desktop_factory):
  58. return desktop_factory(request.param)
  59. def changed_fraction(before, after, threshold=24):
  60. """share of the pixels that differ by more than the threshold in any channel"""
  61. import numpy
  62. from PIL import Image
  63. a = numpy.asarray(Image.open(before).convert("RGB"), dtype=numpy.int16)
  64. b = numpy.asarray(Image.open(after).convert("RGB"), dtype=numpy.int16)
  65. if a.shape != b.shape:
  66. return 1.0
  67. return float((numpy.abs(a - b).max(axis=2) > threshold).mean())