test_api.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. @unittest.mock.patch("os.environ", {"SOME": "VAR", "OTHER": "VALUE"})
  21. def test_start_engine_mac():
  22. app_support_path = "/Users/me/Library/Application Support"
  23. with unittest.mock.patch("subprocess.Popen") as popen_mock:
  24. with unittest.mock.patch("sys.platform", "darwin"):
  25. tooncher.start_engine(
  26. engine_path=pathlib.PosixPath(
  27. app_support_path + "/Toontown Rewritten/Toontown Rewritten"
  28. ),
  29. gameserver="gameserver",
  30. playcookie="cookie",
  31. check=True,
  32. )
  33. popen_mock.assert_called_once_with(
  34. args=[
  35. "/Users/me/Library/Application Support/Toontown Rewritten/Toontown Rewritten"
  36. ],
  37. check=True,
  38. cwd=pathlib.PosixPath(
  39. "/Users/me/Library/Application Support/Toontown Rewritten"
  40. ),
  41. env={
  42. "SOME": "VAR",
  43. "OTHER": "VALUE",
  44. "TTR_GAMESERVER": "gameserver",
  45. "TTR_PLAYCOOKIE": "cookie",
  46. "DYLD_LIBRARY_PATH": app_support_path
  47. + "/Toontown Rewritten/Libraries.bundle",
  48. "DYLD_FRAMEWORK_PATH": app_support_path + "/Toontown Rewritten/Frameworks",
  49. },
  50. )
  51. def test_start_engine_xorg():
  52. with unittest.mock.patch("subprocess.Popen") as popen_mock:
  53. with unittest.mock.patch("os.environ", {"XAUTHORITY": "/home/me/.Xauthority"}):
  54. with unittest.mock.patch("sys.platform", "linux"):
  55. tooncher.start_engine(
  56. engine_path=pathlib.PosixPath("/opt/toontown-rewritter/TTREngine"),
  57. gameserver="gameserver.tld",
  58. playcookie="cookie123",
  59. check=False,
  60. )
  61. popen_mock.assert_called_once_with(
  62. args=["/opt/toontown-rewritter/TTREngine"],
  63. check=False,
  64. cwd=pathlib.PosixPath("/opt/toontown-rewritter"),
  65. env={
  66. "TTR_GAMESERVER": "gameserver.tld",
  67. "TTR_PLAYCOOKIE": "cookie123",
  68. "XAUTHORITY": "/home/me/.Xauthority",
  69. },
  70. )