test_api.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import pathlib
  2. import shutil
  3. import subprocess
  4. import unittest.mock
  5. import tooncher
  6. def test_start_engine():
  7. process = tooncher.start_engine(
  8. engine_path=pathlib.Path(shutil.which("printenv")),
  9. gameserver="gameserver",
  10. playcookie="cookie",
  11. stdout=subprocess.PIPE,
  12. stderr=subprocess.PIPE,
  13. )
  14. assert isinstance(process, subprocess.Popen)
  15. stdout, stderr = process.communicate()
  16. assert not stderr
  17. env = stdout.strip().split(b"\n")
  18. assert b"TTR_GAMESERVER=gameserver" in env
  19. assert b"TTR_PLAYCOOKIE=cookie" in env
  20. def test_start_engine_mac():
  21. app_support_path = "/Users/me/Library/Application Support"
  22. with unittest.mock.patch("subprocess.Popen") as popen_mock:
  23. with unittest.mock.patch("sys.platform", "darwin"):
  24. tooncher.start_engine(
  25. engine_path=pathlib.PosixPath(
  26. app_support_path + "/Toontown Rewritten/Toontown Rewritten"
  27. ),
  28. gameserver="gameserver",
  29. playcookie="cookie",
  30. check=True,
  31. )
  32. popen_mock.assert_called_once_with(
  33. args=[
  34. "/Users/me/Library/Application Support/Toontown Rewritten/Toontown Rewritten"
  35. ],
  36. check=True,
  37. cwd=pathlib.PosixPath(
  38. "/Users/me/Library/Application Support/Toontown Rewritten"
  39. ),
  40. env={
  41. "TTR_GAMESERVER": "gameserver",
  42. "TTR_PLAYCOOKIE": "cookie",
  43. "DYLD_LIBRARY_PATH": app_support_path
  44. + "/Toontown Rewritten/Libraries.bundle",
  45. "DYLD_FRAMEWORK_PATH": app_support_path + "/Toontown Rewritten/Frameworks",
  46. },
  47. )
  48. def test_start_engine_xorg():
  49. with unittest.mock.patch("subprocess.Popen") as popen_mock:
  50. with unittest.mock.patch("os.environ", {"XAUTHORITY": "/home/me/.Xauthority"}):
  51. with unittest.mock.patch("sys.platform", "linux"):
  52. tooncher.start_engine(
  53. engine_path=pathlib.PosixPath("/opt/toontown-rewritter/TTREngine"),
  54. gameserver="gameserver.tld",
  55. playcookie="cookie123",
  56. check=False,
  57. )
  58. popen_mock.assert_called_once_with(
  59. args=["/opt/toontown-rewritter/TTREngine"],
  60. check=False,
  61. cwd=pathlib.PosixPath("/opt/toontown-rewritter"),
  62. env={
  63. "TTR_GAMESERVER": "gameserver.tld",
  64. "TTR_PLAYCOOKIE": "cookie123",
  65. "XAUTHORITY": "/home/me/.Xauthority",
  66. },
  67. )