From be8b9bf41c67ce0f4f18f540db026b97289b3786 Mon Sep 17 00:00:00 2001 From: Lin Sun Date: Fri, 3 May 2024 06:52:22 +0000 Subject: [PATCH] Enhance python-tests to adapt different Python interpreters (bug#70815) * test/lisp/progmodes/python-tests.el (python-tests-get-shell-interpreter): New function to get Python interpreter for testing; and also introduce new env variable EMACS_PYTHON_INTERPRETER to support customer Python interpreter. Co-authored-by: kobarity (cherry picked from commit a2ca5d01989cbe8d43f7763aa5ac374b49980c39) --- test/lisp/progmodes/python-tests.el | 102 ++++++++++++++++------------ 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 2960cca2c06..b19c5c31f16 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -21,6 +21,7 @@ ;;; Code: +(require 'cl-extra) (require 'ert) (require 'ert-x) (require 'python) @@ -58,7 +59,8 @@ turned off. Shell buffer will be killed on exit." (let ((dir (make-symbol "dir"))) `(with-temp-buffer (let ((python-indent-guess-indent-offset nil) - (python-shell-completion-native-enable nil)) + (python-shell-completion-native-enable nil) + (python-shell-interpreter (python-tests-get-shell-interpreter))) (python-mode) (unwind-protect ;; Prevent test failures when Jedi is used as a completion @@ -3738,7 +3740,19 @@ if x: ;;; Shell integration -(defvar python-tests-shell-interpreter "python") +(defvar python-tests-shell-interpreter nil) + +(defun python-tests-get-shell-interpreter () + "Get the shell interpreter. +If env string EMACS_PYTHON_INTERPRETER exists, use it as preferred one." + (if python-tests-shell-interpreter + python-tests-shell-interpreter + (setq python-tests-shell-interpreter + (or (when-let ((interpreter (getenv "EMACS_PYTHON_INTERPRETER"))) + (or (executable-find interpreter) + (error "Couldn't find EMACS_PYTHON_INTERPRETER(%s) in path" + interpreter))) + (cl-some #'executable-find '("python" "python3" "python2")))))) (ert-deftest python-shell-get-process-name-1 () "Check process name calculation sans `buffer-file-name'." @@ -4000,13 +4014,13 @@ if x: (ert-deftest python-shell-make-comint-1 () "Check comint creation for global shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) ;; The interpreter can get killed too quickly to allow it to clean ;; up the tempfiles that the default python-shell-setup-codes create, ;; so it leaves tempfiles behind, which is a minor irritation. (let* ((python-shell-setup-codes nil) (python-shell-interpreter - (executable-find python-tests-shell-interpreter)) + (python-tests-get-shell-interpreter)) (proc-name (python-shell-get-process-name nil)) (shell-buffer (python-tests-with-temp-buffer @@ -4024,10 +4038,10 @@ if x: (ert-deftest python-shell-make-comint-2 () "Check comint creation for internal shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((python-shell-setup-codes nil) (python-shell-interpreter - (executable-find python-tests-shell-interpreter)) + (python-tests-get-shell-interpreter)) (proc-name (python-shell-internal-get-process-name)) (shell-buffer (python-tests-with-temp-buffer @@ -4048,13 +4062,13 @@ if x: The command passed to `python-shell-make-comint' as argument must locally override global values set in `python-shell-interpreter' and `python-shell-interpreter-args' in the new shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((python-shell-setup-codes nil) (python-shell-interpreter "interpreter") (python-shell-interpreter-args "--some-args") (proc-name (python-shell-get-process-name nil)) (interpreter-override - (concat (executable-find python-tests-shell-interpreter) " " "-i")) + (concat (python-tests-get-shell-interpreter) " " "-i")) (shell-buffer (python-tests-with-temp-buffer "" (python-shell-make-comint interpreter-override proc-name nil))) @@ -4067,17 +4081,17 @@ and `python-shell-interpreter-args' in the new shell buffer." (should (eq major-mode 'inferior-python-mode)) (should (file-equal-p python-shell-interpreter - (executable-find python-tests-shell-interpreter))) + (python-tests-get-shell-interpreter))) (should (string= python-shell-interpreter-args "-i")))) (kill-buffer shell-buffer)))) (ert-deftest python-shell-make-comint-4 () "Check shell calculated prompts regexps are set." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((process-environment process-environment) (python-shell-setup-codes nil) (python-shell-interpreter - (executable-find python-tests-shell-interpreter)) + (python-tests-get-shell-interpreter)) (python-shell-interpreter-args "-i") (python-shell--prompt-calculated-input-regexp nil) (python-shell--prompt-calculated-output-regexp nil) @@ -4119,12 +4133,12 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-get-process-1 () "Check dedicated shell process preference over global." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-file "" (let* ((python-shell-setup-codes nil) (python-shell-interpreter - (executable-find python-tests-shell-interpreter)) + (python-tests-get-shell-interpreter)) (global-proc-name (python-shell-get-process-name nil)) (dedicated-proc-name (python-shell-get-process-name t)) (global-shell-buffer @@ -4152,12 +4166,12 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-internal-get-or-create-process-1 () "Check internal shell process creation fallback." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-file "" (should (not (process-live-p (python-shell-internal-get-process-name)))) (let* ((python-shell-interpreter - (executable-find python-tests-shell-interpreter)) + (python-tests-get-shell-interpreter)) (internal-process-name (python-shell-internal-get-process-name)) (internal-process (python-shell-internal-get-or-create-process)) (internal-shell-buffer (process-buffer internal-process))) @@ -4175,8 +4189,9 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-detect-1 () "Check prompt autodetection." - (skip-unless (executable-find python-tests-shell-interpreter)) - (let ((process-environment process-environment)) + (skip-unless (python-tests-get-shell-interpreter)) + (let ((process-environment process-environment) + (python-shell-interpreter (python-tests-get-shell-interpreter))) ;; Ensure no startup file is enabled (setenv "PYTHONSTARTUP" "") (should python-shell-prompt-detect-enabled) @@ -4184,8 +4199,9 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-detect-2 () "Check prompt autodetection with startup file. Bug#17370." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((process-environment process-environment) + (python-shell-interpreter (python-tests-get-shell-interpreter)) (startup-code (concat "import sys\n" "sys.ps1 = 'py> '\n" "sys.ps2 = '..> '\n" @@ -4201,7 +4217,7 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-detect-3 () "Check prompts are not autodetected when feature is disabled." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let ((process-environment process-environment) (python-shell-prompt-detect-enabled nil)) ;; Ensure no startup file is enabled @@ -4210,7 +4226,7 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-detect-4 () "Check warning is shown when detection fails." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((process-environment process-environment) ;; Trigger failure by removing prompts in the startup file (startup-code (concat "import sys\n" @@ -4231,7 +4247,7 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-detect-5 () "Check disabled warnings are not shown when detection fails." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((process-environment process-environment) (startup-code (concat "import sys\n" "sys.ps1 = ''\n" @@ -4252,7 +4268,7 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-detect-6 () "Warnings are not shown when detection is disabled." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((process-environment process-environment) (startup-code (concat "import sys\n" "sys.ps1 = ''\n" @@ -4416,7 +4432,7 @@ and `python-shell-interpreter-args' in the new shell buffer." (ert-deftest python-shell-prompt-set-calculated-regexps-6 () "Check detected prompts are included `regexp-quote'd." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (let* ((python-shell-prompt-input-regexps '("")) (python-shell-prompt-output-regexps '("")) (python-shell-prompt-regexp "") @@ -4426,6 +4442,7 @@ and `python-shell-interpreter-args' in the new shell buffer." (python-shell--prompt-calculated-input-regexp nil) (python-shell--prompt-calculated-output-regexp nil) (python-shell-prompt-detect-enabled t) + (python-shell-interpreter (python-tests-get-shell-interpreter)) (process-environment process-environment) (startup-code (concat "import sys\n" "sys.ps1 = 'p.> '\n" @@ -4799,7 +4816,7 @@ def foo(): (should (python-shell-completion-native-interpreter-disabled-p)))) (ert-deftest python-shell-completion-at-point-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell "" (python-shell-with-shell-buffer @@ -4813,7 +4830,7 @@ def foo(): (should-not (nth 2 (python-shell-completion-at-point)))))) (ert-deftest python-shell-completion-at-point-native-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell "" (python-shell-completion-native-turn-on) @@ -4892,14 +4909,14 @@ def foo(): "Return Jedi readline setup file if PYTHONSTARTUP is not set." (or (getenv "PYTHONSTARTUP") (with-temp-buffer - (if (eql 0 (call-process python-tests-shell-interpreter + (if (eql 0 (call-process (python-tests-get-shell-interpreter) nil t nil "-m" "jedi" "repl")) (string-trim (buffer-string)) "")))) (ert-deftest python-shell-completion-at-point-jedi-completer () "Check if Python shell completion works when Jedi completer is used." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (with-environment-variables (("PYTHONSTARTUP" (python-tests--pythonstartup-file))) (python-tests-with-temp-buffer-with-shell @@ -4944,7 +4961,7 @@ def foo(): ;;; Symbol completion (ert-deftest python-completion-at-point-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -4962,7 +4979,7 @@ import abc (ert-deftest python-completion-at-point-2 () "Should work regardless of the point in the Shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -4980,7 +4997,7 @@ import abc (ert-deftest python-completion-at-point-pdb-1 () "Should not complete PDB commands in Python buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import pdb @@ -4999,7 +5016,7 @@ print('Hello') (ert-deftest python-completion-at-point-while-running-1 () "Should not try to complete when a program is running in the Shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import time @@ -5015,7 +5032,7 @@ time.sleep(3) (should-not (with-timeout (1 t) (completion-at-point)))))) (ert-deftest python-completion-at-point-native-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -5034,7 +5051,7 @@ import abc (ert-deftest python-completion-at-point-native-2 () "Should work regardless of the point in the Shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -5052,7 +5069,7 @@ import abc (should (completion-at-point))))) (ert-deftest python-completion-at-point-native-with-ffap-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -5070,7 +5087,7 @@ import abc (should (completion-at-point))))) (ert-deftest python-completion-at-point-native-with-eldoc-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -5097,7 +5114,7 @@ import abc ;;; FFAP (ert-deftest python-ffap-module-path-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -5109,7 +5126,7 @@ import abc (ert-deftest python-ffap-module-path-while-running-1 () "Should not get module path when a program is running in the Shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import abc @@ -5185,7 +5202,7 @@ some_symbol some_other_symbol "some_symbol")))) (ert-deftest python-eldoc--get-doc-at-point-1 () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import time @@ -5198,7 +5215,7 @@ import time (ert-deftest python-eldoc--get-doc-at-point-while-running-1 () "Should not get documentation when a program is running in the Shell buffer." - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell " import time @@ -7416,8 +7433,9 @@ buffer with overlapping strings." ;; interpreter. (ert-deftest python-tests--run-python-selects-window () "Test for bug#31398. See also bug#44421 and bug#52380." - (skip-unless (executable-find python-tests-shell-interpreter)) - (let* ((buffer (process-buffer (run-python nil nil 'show))) + (skip-unless (python-tests-get-shell-interpreter)) + (let* ((python-shell-interpreter (python-tests-get-shell-interpreter)) + (buffer (process-buffer (run-python nil nil 'show))) (window (get-buffer-window buffer))) ;; We look at `selected-window' rather than `current-buffer' ;; because as `(elisp)Current buffer' says, the latter will only @@ -7487,7 +7505,7 @@ buffer with overlapping strings." "W0611: Unused import a.b.c (unused-import)")))))) (ert-deftest python-test--shell-send-block () - (skip-unless (executable-find python-tests-shell-interpreter)) + (skip-unless (python-tests-get-shell-interpreter)) (python-tests-with-temp-buffer-with-shell "print('current 0') for x in range(1,3): -- 2.39.5