test_api.py 2.9 KB

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