test_api.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. engine_path = pathlib.Path(app_support_path).joinpath(
  24. "Toontown Rewritten", "Toontown Rewritten"
  25. )
  26. with unittest.mock.patch("subprocess.Popen") as popen_mock:
  27. with unittest.mock.patch("sys.platform", "darwin"):
  28. # python3.5's pathlib.Path.resolve raises FileNotFoundError
  29. with unittest.mock.patch(
  30. "pathlib.Path.resolve",
  31. new=lambda p: p.with_suffix(".resolved"),
  32. ):
  33. tooncher.start_engine(
  34. engine_path=engine_path,
  35. gameserver="gameserver",
  36. playcookie="cookie",
  37. check=True,
  38. )
  39. popen_mock.assert_called_once_with(
  40. args=[str(engine_path) + ".resolved"],
  41. check=True,
  42. cwd=str(engine_path.parent),
  43. env={
  44. "SOME": "VAR",
  45. "OTHER": "VALUE",
  46. "TTR_GAMESERVER": "gameserver",
  47. "TTR_PLAYCOOKIE": "cookie",
  48. "DYLD_LIBRARY_PATH": app_support_path
  49. + "/Toontown Rewritten/Libraries.bundle",
  50. "DYLD_FRAMEWORK_PATH": app_support_path + "/Toontown Rewritten/Frameworks",
  51. },
  52. )
  53. def test_start_engine_xorg():
  54. with unittest.mock.patch("subprocess.Popen") as popen_mock:
  55. with unittest.mock.patch("os.environ", {"XAUTHORITY": "/home/me/.Xauthority"}):
  56. with unittest.mock.patch("sys.platform", "linux"):
  57. with unittest.mock.patch( # python3.5
  58. "pathlib.Path.resolve",
  59. new=lambda p: p.with_suffix(".resolved"),
  60. ):
  61. tooncher.start_engine(
  62. engine_path=pathlib.PosixPath(
  63. "/opt/toontown-rewritter/TTREngine"
  64. ),
  65. gameserver="gameserver.tld",
  66. playcookie="cookie123",
  67. check=False,
  68. )
  69. popen_mock.assert_called_once_with(
  70. args=["/opt/toontown-rewritter/TTREngine.resolved"],
  71. check=False,
  72. cwd="/opt/toontown-rewritter",
  73. env={
  74. "TTR_GAMESERVER": "gameserver.tld",
  75. "TTR_PLAYCOOKIE": "cookie123",
  76. "XAUTHORITY": "/home/me/.Xauthority",
  77. },
  78. )