From: Debian Python Team <team+python@tracker.debian.org>
Date: Mon, 11 Nov 2024 17:14:38 +1100
Subject: Allow tests to run with non-default Python

---
 unittests/CustomRenderer.py  | 18 ++++++++++++++----
 unittests/FunctionalTests.py | 11 +++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/unittests/CustomRenderer.py b/unittests/CustomRenderer.py
index c5db320..5df9650 100644
--- a/unittests/CustomRenderer.py
+++ b/unittests/CustomRenderer.py
@@ -3,7 +3,6 @@ import subprocess
 from pathlib import Path
 
 plastex_dir = str(Path(__file__).absolute().parent.parent)
-sys.path.append(plastex_dir)
 
 def test_custom_renderer(tmpdir):
     try:
@@ -31,7 +30,14 @@ class Renderer(BaseRenderer):
 """)
 
 
-    os.environ["PYTHONPATH"] = plastex_dir
+    ppath = ':'.join([
+        os.environ.get('PYTHONPATH', ''),
+        str(plastex_dir),
+        str(tmpdir)
+    ])
+    env = os.environ.copy()
+    env['PYTHONPATH'] = ppath
+
     # We check the return code manually instead of setting check=True for more
     # readable error
     ret = subprocess.run(["plastex", "--renderer=CustomRenderer", "test.tex"], cwd=str(tmpdir), check=False)
@@ -61,8 +67,12 @@ class Renderer(BaseRenderer):
 \end{document}
 """)
 
-    ppath = ':'.join(sys.path + [str(tmpdir)])
-    env = os.environ
+    ppath = ':'.join([
+        os.environ.get('PYTHONPATH', ''),
+        str(plastex_dir),
+        str(tmpdir)
+    ])
+    env = os.environ.copy()
     env['PYTHONPATH'] = ppath
     # We check the return code manually instead of setting check=True for more
     # readable error
diff --git a/unittests/FunctionalTests.py b/unittests/FunctionalTests.py
index 7230085..1e2fa80 100755
--- a/unittests/FunctionalTests.py
+++ b/unittests/FunctionalTests.py
@@ -22,6 +22,9 @@ from pathlib import Path
 import pytest
 
 
+plastex_dir = str(Path(__file__).absolute().parent.parent)
+
+
 def which(name, path=None, exts=('',)):
     """
     Search PATH for a binary.
@@ -51,6 +54,14 @@ class Process(object):
             kwargs['stdout'] = subprocess.PIPE
         if 'stderr' not in kwargs:
             kwargs['stderr'] = subprocess.STDOUT
+        if 'env' not in kwargs:
+            ppath = ':'.join([
+                os.environ.get('PYTHONPATH', ''),
+                str(plastex_dir),
+            ])
+            env = os.environ.copy()
+            env['PYTHONPATH'] = ppath
+            kwargs['env'] = env
         self.process = subprocess.Popen(args, **kwargs)
         self.log = self.process.stdout.read().decode('utf8')
         self.process.wait()
