test_api.py 2.6 KB

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