diff --git a/tests/test_port_regen.py b/tests/test_port_regen.py index c52499417..5253b6221 100644 --- a/tests/test_port_regen.py +++ b/tests/test_port_regen.py @@ -1,3 +1,4 @@ +import subprocess import unittest from tools import port_regen as regen @@ -48,5 +49,13 @@ class CompileErrorTests(unittest.TestCase): self.assertEqual(regen.compile_errors("expected the root's order to differ"), []) +class InterpreterTests(unittest.TestCase): + def test_python_is_an_interpreter_that_runs(self): + found = regen.python() + done = subprocess.run([found, "-c", "import sys; sys.exit(sys.version_info < (3,))"], + capture_output=True) + self.assertEqual(done.returncode, 0, f"{found} is not a Python 3 that runs") + + if __name__ == "__main__": unittest.main() diff --git a/tools/port_regen.py b/tools/port_regen.py index 4d69b0986..58f33d52e 100644 --- a/tools/port_regen.py +++ b/tools/port_regen.py @@ -124,8 +124,23 @@ def prepare_local(root, version, found, store): run([python(), "tools/lab.py", "prepare", "--minecraft-dir", tree, "--server-jar", found["server"]], root) +_PYTHON = None + + def python(): - return shutil.which("python3") or "python3" + """The interpreter to run the other tools with. Git for Windows puts a + `python3` on PATH that is only the Store stub: it prints "Python" and exits + 49, so probe the names and keep the first one that runs Python 3.""" + global _PYTHON + if _PYTHON is None: + _PYTHON = "python3" + for name in ("python3", "python", "py"): + found = shutil.which(name) + probe = [found, "-c", "import sys; sys.exit(sys.version_info < (3,))"] if found else None + if probe and subprocess.run(probe, capture_output=True).returncode == 0: + _PYTHON = found + break + return _PYTHON class Failed(RuntimeError):