conftest.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.runner import BASE # noqa: E402
  14. from desktops.session import KINDS, Session, Unsupported # noqa: E402
  15. STEAM_ROOTS = (
  16. Path("/workspace/SteamLibrary"),
  17. Path.home() / ".local/share/Steam",
  18. Path.home() / ".steam/steam",
  19. )
  20. def _first_existing(paths):
  21. return next((path for path in paths if path.exists()), None)
  22. @pytest.fixture(scope="session")
  23. def engine():
  24. binary = Path(os.environ.get("LWE_BINARY", REPO / "build/output/linux-wallpaperengine"))
  25. assets = os.environ.get("LWE_ASSETS_DIR") or _first_existing(
  26. [root / "steamapps/common/wallpaper_engine/assets" for root in STEAM_ROOTS]
  27. )
  28. wallpaper = os.environ.get("LWE_TEST_WALLPAPER") or _first_existing(
  29. [root / "steamapps/workshop/content/431960/1846362380" for root in STEAM_ROOTS]
  30. )
  31. missing = [what for what, value in (("the engine binary", binary if binary.exists() else None),
  32. ("the assets", assets), ("a test wallpaper", wallpaper)) if not value]
  33. if missing:
  34. pytest.skip(f"missing {', '.join(missing)}, see tests/desktops/conftest.py")
  35. # renamed so we_manager's pkill -f linux-wallpaperengine misses it
  36. link = BASE / "lwe-test-engine"
  37. link.parent.mkdir(parents=True, exist_ok=True)
  38. if not link.is_symlink() or os.readlink(link) != str(binary.resolve()):
  39. link.unlink(missing_ok=True)
  40. link.symlink_to(binary.resolve())
  41. return {"binary": str(link), "assets": str(assets), "wallpaper": str(wallpaper)}
  42. @pytest.fixture(scope="session")
  43. def desktop_factory(tmp_path_factory):
  44. """one session at a time, shared by the tests of that session: a full desktop is several hundred MB"""
  45. started = {}
  46. available = Session.available()
  47. def get(name):
  48. if available.get(name) is None:
  49. pytest.skip(f"{name} is not installed here and has no rootfs, run: python3 tools/desktop_session.py setup")
  50. for other in [key for key in started if key != name]:
  51. started.pop(other).stop()
  52. if name not in started:
  53. session = Session.of(name)
  54. try:
  55. session.start()
  56. except Exception as error:
  57. pytest.fail(f"{name} did not start: {error}")
  58. started[name] = session
  59. return started[name]
  60. yield get
  61. for session in started.values():
  62. session.stop()
  63. @pytest.fixture
  64. def desktop(request, desktop_factory):
  65. return desktop_factory(request.param)
  66. def changed_fraction(before, after, threshold=24):
  67. """share of the pixels that differ by more than the threshold in any channel"""
  68. import numpy
  69. from PIL import Image
  70. a = numpy.asarray(Image.open(before).convert("RGB"), dtype=numpy.int16)
  71. b = numpy.asarray(Image.open(after).convert("RGB"), dtype=numpy.int16)
  72. if a.shape != b.shape:
  73. return 1.0
  74. return float((numpy.abs(a - b).max(axis=2) > threshold).mean())