Browse Source

python3.5 / tests: fix FileNotFoundError due to strict pathlib.Path.resolve

Fabian Peter Hammerle 3 years ago
parent
commit
3660e11ec9
3 changed files with 29 additions and 19 deletions
  1. 1 0
      CHANGELOG.md
  2. 27 19
      tests/test_api.py
  3. 1 0
      tooncher/__init__.py

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
   - fixed `TypeError` due to `start_engine` passing `pathlib.Path`
     to `subprocess.Popen` for `cwd`
   - tests: fixed `AttributeError` due to unavailable `MagicMock.assert_called_once`
+  - tests: fixed `FileNotFoundError` due to strict `pathlib.Path.resolve`
 
 ## [1.0.2] - 2020-05-05
 ### Fixed

+ 27 - 19
tests/test_api.py

@@ -25,22 +25,25 @@ def test_start_engine():
 @unittest.mock.patch("os.environ", {"SOME": "VAR", "OTHER": "VALUE"})
 def test_start_engine_mac():
     app_support_path = "/Users/me/Library/Application Support"
+    engine_path = pathlib.Path(app_support_path).joinpath(
+        "Toontown Rewritten", "Toontown Rewritten"
+    )
     with unittest.mock.patch("subprocess.Popen") as popen_mock:
         with unittest.mock.patch("sys.platform", "darwin"):
-            tooncher.start_engine(
-                engine_path=pathlib.PosixPath(
-                    app_support_path + "/Toontown Rewritten/Toontown Rewritten"
-                ),
-                gameserver="gameserver",
-                playcookie="cookie",
-                check=True,
-            )
+            # python3.5's pathlib.Path.resolve raises FileNotFoundError
+            with unittest.mock.patch(
+                "pathlib.Path.resolve", new=lambda p: p.with_suffix(".resolved"),
+            ):
+                tooncher.start_engine(
+                    engine_path=engine_path,
+                    gameserver="gameserver",
+                    playcookie="cookie",
+                    check=True,
+                )
     popen_mock.assert_called_once_with(
-        args=[
-            "/Users/me/Library/Application Support/Toontown Rewritten/Toontown Rewritten"
-        ],
+        args=[str(engine_path) + ".resolved"],
         check=True,
-        cwd="/Users/me/Library/Application Support/Toontown Rewritten",
+        cwd=str(engine_path.parent),
         env={
             "SOME": "VAR",
             "OTHER": "VALUE",
@@ -57,14 +60,19 @@ def test_start_engine_xorg():
     with unittest.mock.patch("subprocess.Popen") as popen_mock:
         with unittest.mock.patch("os.environ", {"XAUTHORITY": "/home/me/.Xauthority"}):
             with unittest.mock.patch("sys.platform", "linux"):
-                tooncher.start_engine(
-                    engine_path=pathlib.PosixPath("/opt/toontown-rewritter/TTREngine"),
-                    gameserver="gameserver.tld",
-                    playcookie="cookie123",
-                    check=False,
-                )
+                with unittest.mock.patch(  # python3.5
+                    "pathlib.Path.resolve", new=lambda p: p.with_suffix(".resolved"),
+                ):
+                    tooncher.start_engine(
+                        engine_path=pathlib.PosixPath(
+                            "/opt/toontown-rewritter/TTREngine"
+                        ),
+                        gameserver="gameserver.tld",
+                        playcookie="cookie123",
+                        check=False,
+                    )
     popen_mock.assert_called_once_with(
-        args=["/opt/toontown-rewritter/TTREngine"],
+        args=["/opt/toontown-rewritter/TTREngine.resolved"],
         check=False,
         cwd="/opt/toontown-rewritter",
         env={

+ 1 - 0
tooncher/__init__.py

@@ -32,6 +32,7 @@ def start_engine(
     env = os.environ.copy()
     env["TTR_GAMESERVER"] = gameserver
     env["TTR_PLAYCOOKIE"] = playcookie
+    # .resolve(strict=True/False) is not available in python3.5
     engine_path = engine_path.resolve()
     if sys.platform == "darwin":
         env["DYLD_LIBRARY_PATH"] = str(engine_path.parent.joinpath("Libraries.bundle"))