diff --git a/ga_cli/cli.py b/ga_cli/cli.py index 9ecc4455c..e228cd5b6 100644 --- a/ga_cli/cli.py +++ b/ga_cli/cli.py @@ -32,6 +32,8 @@ def launch_frontend(cmd_parts, args=None): # 插入额外参数 if args: full_cmd.extend(args) + if full_cmd and full_cmd[0] == "python": + full_cmd[0] = sys.executable print(f"🚀 {' '.join(full_cmd)}") sys.stdout.flush() diff --git a/tests/test_ga_cli_interpreter.py b/tests/test_ga_cli_interpreter.py new file mode 100644 index 000000000..5284f2b0e --- /dev/null +++ b/tests/test_ga_cli_interpreter.py @@ -0,0 +1,32 @@ +import sys +import unittest +from unittest import mock + +from ga_cli import cli + + +class GaCliInterpreterTests(unittest.TestCase): + def test_python_launcher_uses_current_interpreter(self): + proc = mock.Mock() + with mock.patch.object(cli.subprocess, "Popen", return_value=proc) as popen, \ + mock.patch.object(cli.os, "chdir"): + cli.launch_frontend(["python", "{PROJECT_DIR}/agentmain.py"], ["--help"]) + + popen.assert_called_once() + command = popen.call_args.args[0] + self.assertEqual(command[0], sys.executable) + self.assertTrue(command[1].endswith("agentmain.py")) + self.assertEqual(command[2:], ["--help"]) + proc.wait.assert_called_once_with() + + def test_non_python_launcher_is_not_rewritten(self): + proc = mock.Mock() + with mock.patch.object(cli.subprocess, "Popen", return_value=proc) as popen, \ + mock.patch.object(cli.os, "chdir"): + cli.launch_frontend(["custom-runtime", "script"], None) + + self.assertEqual(popen.call_args.args[0][0], "custom-runtime") + + +if __name__ == "__main__": + unittest.main()