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

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

[nongnu] elpa/bash-completion 0a757bd5f4 238/313: Test calling completio


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion 0a757bd5f4 238/313: Test calling completion from non-comint and non-bash shell buffers.
Date: Sat, 3 Dec 2022 10:59:34 -0500 (EST)

branch: elpa/bash-completion
commit 0a757bd5f4bbcf9bfd31660e66fb19dcff164bba
Author: Stephane Zermatten <szermatt@gmx.net>
Commit: Stephane Zermatten <szermatt@gmx.net>

    Test calling completion from non-comint and non-bash shell buffers.
    
    Also avoid using the single-process solution for non-bash shell
    buffers.
---
 README.md                                |  4 +-
 bash-completion.el                       | 26 ++++++++----
 test/bash-completion-integration-test.el | 69 ++++++++++++++++++++++++--------
 3 files changed, 71 insertions(+), 28 deletions(-)

diff --git a/README.md b/README.md
index ed89d2eae5..bef0c428b4 100644
--- a/README.md
+++ b/README.md
@@ -24,9 +24,7 @@ running in term mode.  Also, term mode is not available in
 shell-command prompts.
 
 Bash completion can also be run programatically, outside of a
-shell-mode command, by setting
-`bash-completion-use-separate-processes` to a non-nil value (which is
-the default) and by calling
+shell-mode command by calling
 `bash-completion-dynamic-complete-nocomint`.
 
 ## INSTALLATION
diff --git a/bash-completion.el b/bash-completion.el
index cf12f08552..4915379a5a 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -181,7 +181,8 @@ perform completion. If nil, the process associated with the
 current buffer is used to perform completion. Even when this
 variable is set to nil, a separate process can be used to perform
 completion when:
-- no process is associated with the current buffer
+- the current buffer is not a comint buffer
+- no bash process is associated with the current buffer
 - an error occurred while trying to get completions"
   :type 'boolean
   :group 'bash-completion)
@@ -520,7 +521,7 @@ When doing completion outside of a comint buffer, call
 
 ;;;###autoload
 (defun bash-completion-dynamic-complete-nocomint
-    (comp-start comp-pos &optional dynamic-table)
+    (&optional comp-start comp-pos dynamic-table)
   "Return completion information for bash command at an arbitrary position.
 
 The bash command to be completed begins at COMP-START in the
@@ -537,7 +538,9 @@ Returns (list stub-start stub-end completions) with
    or a function, if DYNAMIC-TABLE is non-nil, a lambda such as the one
    returned by `completion-table-dynamic'"
   (when bash-completion-enabled
-    (let ((bash-completion-use-separate-processes
+    (let ((comp-start (or comp-start (line-beginning-position)))
+          (comp-pos (or comp-pos (point)))
+          (bash-completion-use-separate-processes
            bash-completion-use-separate-processes)
           (process (bash-completion-get-process)))
       (when (and (not process) (not bash-completion-use-separate-processes))
@@ -1170,12 +1173,19 @@ is set to t."
                 (error nil)))))))))
 
 (defun bash-completion--get-same-process ()
-  "Setup the process associated with the current buffer and return it."
+  "Return the BASH process associated with the current buffer.
+
+Return nil if the current buffer is not a comint buffer or is not
+associated with a command that looks like a bash shell.
+Completion will fallback to creating a separate process
+completion in these cases."
   (when (derived-mode-p 'comint-mode)
-    (let ((process (get-buffer-process (current-buffer))))
-      (unless (or (not process) (process-get process 'setup-done))
-        (bash-completion--setup-bash-common process))
-      process)))
+    (let* ((process (get-buffer-process (current-buffer)))
+           (command (when process (file-name-nondirectory (car 
(process-command process))))))
+      (when (and command (bash-completion-starts-with command "bash"))
+        (unless (process-get process 'setup-done)
+          (bash-completion--setup-bash-common process))
+        process))))
 
 (defun bash-completion-get-process ()
   "Setup and return a bash completion process.
diff --git a/test/bash-completion-integration-test.el 
b/test/bash-completion-integration-test.el
index d71ad721d3..9186082c92 100644
--- a/test/bash-completion-integration-test.el
+++ b/test/bash-completion-integration-test.el
@@ -73,22 +73,25 @@
   `(bash-completion_test-harness
     ,bashrc
     ,use-separate-process
-    (let ((shell-buffer))
-      (unwind-protect
-         (progn
-           (setq shell-buffer (shell (generate-new-buffer-name
-                                      "*bash-completion_test-with-shell*")))
-            (bash-completion--wait-for-prompt (get-buffer-process shell-buffer)
-                                              
(bash-completion--get-prompt-regexp)
-                                              3.0)
-           (with-current-buffer shell-buffer
-              (let ((comint-dynamic-complete-functions 
'(bash-completion-dynamic-complete)))
-                (progn ,@body))))
-        (when shell-buffer
-          (when (and (buffer-live-p shell-buffer)
-                     (get-buffer-process shell-buffer))
-            (kill-process (get-buffer-process shell-buffer)))
-          (kill-buffer shell-buffer))))))
+    (bash-completion_test-with-shell ,@body)))
+
+(defmacro bash-completion_test-with-shell (&rest body)
+  `(let ((shell-buffer))
+     (unwind-protect
+        (progn
+          (setq shell-buffer (shell (generate-new-buffer-name
+                                     "*bash-completion_test-with-shell*")))
+          (with-current-buffer shell-buffer
+             (bash-completion--wait-for-prompt (get-buffer-process 
shell-buffer)
+                                               
(bash-completion--get-prompt-regexp)
+                                               3.0)
+             (let ((comint-dynamic-complete-functions 
'(bash-completion-dynamic-complete)))
+               (progn ,@body))))
+       (when shell-buffer
+         (when (and (buffer-live-p shell-buffer)
+                    (get-buffer-process shell-buffer))
+           (kill-process (get-buffer-process shell-buffer)))
+         (kill-buffer shell-buffer)))))
 
 (defun bash-completion_test-bash-major-version ()
   "Return the major version of the bash process."
@@ -96,7 +99,7 @@
 
 (defun bash-completion_test-complete (complete-me)
   (goto-char (point-max))
-  (comint-delete-input)
+  (delete-region (line-beginning-position) (line-end-position))
   (insert complete-me)
   (completion-at-point)
   (buffer-substring-no-properties
@@ -172,6 +175,38 @@ for testing completion."
                   (bash-completion_test-complete
                    "export SOMEPATH=some/directory:some/oth")))))
 
+(ert-deftest bash-completion-integration-nocomint-test ()
+  (bash-completion_test-harness
+   "function somefunction { echo ok; }\n"
+   nil ; use-separate-process=nil will be ignored
+   (with-temp-buffer
+     (let ((completion-at-point-functions 
'(bash-completion-dynamic-complete-nocomint)))
+       ;; complete bash builtin
+       (should (equal "readonly "
+                      (bash-completion_test-complete "reado")))
+       ;; complete command
+       (should (equal "somefunction "
+                      (bash-completion_test-complete "somef")))))))
+
+(ert-deftest bash-completion-integration-notbash-test ()
+  (bash-completion_test-harness
+   "function somefunction { echo ok; }\n"
+   ; use-separate-process=nil will be ignored because the shell is not
+   ; a bash shell.
+   nil 
+   (let ((explicit-shell-file-name "/bin/sh"))
+     (bash-completion_test-with-shell
+      ;; complete bash builtin
+      (should (equal "readonly "
+                     (bash-completion_test-complete "reado")))
+      ;; complete command
+      (should (equal "somefunction "
+                     (bash-completion_test-complete "somef")))
+
+      ;; make sure a separate process was used; in case /bin/sh is
+      ;; actually bash, the test could otherwise work just fine.
+      (should (not (null (cdr (assq nil bash-completion-processes)))))))))
+
 (ert-deftest bash-completion-integration-bash-4-default-completion ()
   (bash-completion_test-with-shell-harness
    (concat ; .bashrc



reply via email to

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