emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/bash-completion b4ae893243 268/313: Test prompt and histor


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion b4ae893243 268/313: Test prompt and history manipulation, fix $? in prompt.
Date: Sat, 3 Dec 2022 10:59:36 -0500 (EST)

branch: elpa/bash-completion
commit b4ae893243a907849e926b7d9f5555fe635b9644
Author: Stephane Zermatten <stephane@boomer.lan>
Commit: Stephane Zermatten <stephane@boomer.lan>

    Test prompt and history manipulation, fix $? in prompt.
    
    This change adds tests to cover:
     - PROMPT_COMMAND
     - PS1
     - history content
    in single-process mode.
    
    It also fixes a bug that these tests uncovered, where the value of $?
    valid at the time PROMPT_COMMAND was evaluated was incorrect.
---
 bash-completion.el                       |  3 +-
 test/bash-completion-integration-test.el | 90 +++++++++++++++++++++++++++++---
 2 files changed, 84 insertions(+), 9 deletions(-)

diff --git a/bash-completion.el b/bash-completion.el
index 6f558ed14b..f6aee9e22c 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -1263,11 +1263,12 @@ function __emacs_complete_prompt {
   PROMPT_COMMAND=__emacs_complete_recover_prompt
 }; \
 function __emacs_complete_recover_prompt {
+  local r=$?
   PS1=\"${__emacs_complete_ps1}\"
   PROMPT_COMMAND=\"${__emacs_complete_pc}\"
   unset __emacs_complete_ps1 __emacs_complete_pc
   if [[ -n \"$PROMPT_COMMAND\" ]]; then
-    eval \"$PROMPT_COMMAND\"
+    (exit $r); eval \"$PROMPT_COMMAND\"
   fi
 }" process)
           (bash-completion--setup-bash-common process))
diff --git a/test/bash-completion-integration-test.el 
b/test/bash-completion-integration-test.el
index 6ed2dc1c6f..1056bb73bf 100644
--- a/test/bash-completion-integration-test.el
+++ b/test/bash-completion-integration-test.el
@@ -92,8 +92,7 @@
           (setq shell-buffer (shell (generate-new-buffer-name
                                      "*bash-completion_test-with-shell*")))
           (with-current-buffer shell-buffer
-             (bash-completion--wait-for-regexp
-              (get-buffer-process shell-buffer) comint-prompt-regexp 3.0)
+             (bash-completion_test-wait-for-prompt)
              (let ((comint-dynamic-complete-functions 
'(bash-completion-dynamic-complete))
                    (completion-at-point-functions '(comint-completion-at-point 
t)))
                (progn ,@body))))
@@ -116,14 +115,24 @@
   (buffer-substring-no-properties
    (line-beginning-position) (point)))
 
-(defun bash-completion_test-send (command)
+(defun bash-completion_test-send (command &optional complete)
   "Execute COMMAND in a shell buffer."
   (goto-char (point-max))
-  (delete-region (line-beginning-position) (line-end-position))
-  (insert command)
-  (comint-send-input)
+  (let ((command-start (point)))
+    (delete-region (line-beginning-position) (line-end-position))
+    (insert command)
+    (when complete (completion-at-point))
+    (comint-send-input)
+    (bash-completion_test-wait-for-prompt command-start)))
+
+(defun bash-completion_test-wait-for-prompt (&optional limit)
   (bash-completion--wait-for-regexp
-   (get-buffer-process (current-buffer)) comint-prompt-regexp 3.0))
+   (get-buffer-process shell-buffer) comint-prompt-regexp 3.0 limit))
+
+(defun bash-completion_test-buffer-string (&optional start end)
+  (delete-trailing-whitespace (point-min) (point-max))
+  (untabify (point-min) (point-max))
+  (buffer-substring-no-properties (or start (point-min)) (or end (point-max))))
 
 (defun bash-completion_test-candidates (complete-me)
   "Complete COMPLETE-ME and returns the candidates."
@@ -143,8 +152,12 @@ for testing completion."
     (prog1
         test-env-dir
       (with-temp-file (expand-file-name "bashrc" test-env-dir)
+        (insert "export PATH=/bin\n")
         (insert (format "cd '%s'\n" test-env-dir))
-        (insert bashrc))
+        (insert bashrc)
+        (insert "\n")
+        (insert "HISTFILE=/dev/null\n")
+        (insert "history -c\n"))
       (let ((default-directory test-env-dir))
         (make-directory "some/directory" 'parents)
         (make-directory "some/other/directory" 'parents)))))
@@ -454,4 +467,65 @@ for testing completion."
    (should (equal "ls \"~/some/" (bash-completion_test-complete "ls \"~/so")))
    (should (equal "ls '~/some/" (bash-completion_test-complete "ls '~/so")))))
 
+(ert-deftest bash-completion-integration-prompt-command ()
+  "Tests PROMPT_COMMAND storage and recovery in single-process mode."
+  (bash-completion_test-with-shell-harness
+   "prompt_count=0
+function _prompt {
+  PS1=\"[$prompt_count]:$? $ \"
+  prompt_count=$(( $prompt_count + 1 ))
+}
+PROMPT_COMMAND=_prompt
+"
+   nil ; use-separate-process
+   (bash-completion_test-send "ls -1 so" 'complete)
+   (bash-completion_test-send "tru" 'complete)
+   (bash-completion_test-send "fals" 'complete)
+   (should (equal
+            (bash-completion_test-buffer-string)
+            "[0]:0 $ ls -1 some/
+directory
+other
+[1]:0 $ true
+[2]:0 $ false
+[3]:1 $ "))))
+
+(ert-deftest bash-completion-integration-ps1 ()
+  "Tests PS1 storage and recovery in single-process mode."
+  (bash-completion_test-with-shell-harness
+   "PS1='$? $ '"
+   nil ; use-separate-process
+   (bash-completion_test-send "ls -1 so" 'complete)
+   (bash-completion_test-send "tru" 'complete)
+   (bash-completion_test-send "fals" 'complete)
+   (should (equal
+            (bash-completion_test-buffer-string)
+            "0 $ ls -1 some/
+directory
+other
+0 $ true
+0 $ false
+1 $ "))))
+
+(ert-deftest bash-completion-integration-prompt-history ()
+  "Tests that history is not polluted by completion."
+  (bash-completion_test-with-shell-harness
+   "PS1='$ '"
+   nil ; use-separate-process
+   (bash-completion_test-send "ls -1 so" 'complete)
+   (bash-completion_test-send "tru" 'complete)
+   (bash-completion_test-send "fals" 'complete)
+   (let ((history-start (line-beginning-position)))
+     (bash-completion_test-send "history")
+     (untabify (point-min) (point-max))
+     (delete-trailing-whitespace (point-min) (point-max))
+     (should (equal
+              (bash-completion_test-buffer-string history-start)
+              "history
+    1  ls -1 some/
+    2  true
+    3  false
+    4  history
+$ ")))))
+
 ;;; bash-completion-integration-test.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]