branch: externals/async commit 3f91ce8b963cd6909c6578f87b5545fd761f1547 Merge: aee2947bd5 7df20ed3b8 Author: Thierry Volpiatto <thie...@posteo.net> Commit: GitHub <nore...@github.com>
Merge pull request #174 from Fuco1/bugfix/autoclose-err-buffer feat: automatically close error buffer when process finishes --- async.el | 9 ++++++--- tests/test-async.el | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/async.el b/async.el index ffbd99ef6b..aa0ccaba0c 100644 --- a/async.el +++ b/async.el @@ -401,17 +401,20 @@ object will return the process object when the program is finished. Set DEFAULT-DIRECTORY to change PROGRAM's current working directory." (let* ((buf (generate-new-buffer (concat "*" name "*"))) - (buf-err (and async-debug - (generate-new-buffer (concat "*" name "*:err")))) + (buf-err (generate-new-buffer (concat "*" name ":err*"))) (proc (let ((process-connection-type nil)) (make-process :name name :buffer buf :stderr buf-err :command (cons program program-args))))) + (set-process-sentinel + (get-buffer-process buf-err) + (lambda (proc _change) + (unless (or async-debug (buffer-live-p proc)) + (kill-buffer (process-buffer proc))))) (with-current-buffer buf (set (make-local-variable 'async-callback) finish-func) - (set (make-local-variable 'async-read-marker) (set-marker (make-marker) (point-min) buf)) (set-marker-insertion-type async-read-marker nil) diff --git a/tests/test-async.el b/tests/test-async.el index 7bc9ef0e4c..eda5c616c0 100644 --- a/tests/test-async.el +++ b/tests/test-async.el @@ -1,4 +1,5 @@ ;; -*- lexical-binding: t -*- +(require 'subr-x) (require 'buttercup) (require 'async) @@ -206,6 +207,55 @@ (expect (car messages) :to-equal t) (expect (cadr messages) :to-equal (make-string 10485760 ?x)))))) + (describe "Handling process buffers" + + (it "should automatically close stdout and stderr buffer when process exits" + + (let ((messages nil)) + (async-start + (lambda () + (message "This is a test") + (sleep-for 0.5) + 222) + + (lambda (result) + (push (format "Async process done, result should be 222: %s" result) messages))) + + (sleep-for 1) + + (expect (string-join (nreverse messages) "\n") + :to-equal "Async process done, result should be 222: 222") + (expect (cl-find-if (lambda (x) (string-match-p "emacs" x)) (mapcar #'buffer-name (buffer-list))) + :to-be nil) + (expect (cl-find-if (lambda (x) (string-match-p "emacs:err" x)) (mapcar #'buffer-name (buffer-list))) + :to-be nil))) + + (it "should keep stdout and stderr buffer when process exits if debug is active" + + (unwind-protect + (let ((messages nil) + (async-debug t)) + (async-start + (lambda () + (message "This is a test") + (sleep-for 0.5) + 222) + + (lambda (result) + (push (format "Async process done, result should be 222: %s" result) messages))) + + (sleep-for 1) + + (expect (string-join (nreverse messages) "\n") + :to-equal "Async process done, result should be 222: 222") + (expect (cl-find-if (lambda (x) (string-match-p "emacs" x)) (mapcar #'buffer-name (buffer-list))) + :to-be-truthy) + (expect (cl-find-if (lambda (x) (string-match-p "emacs:err" x)) (mapcar #'buffer-name (buffer-list))) + :to-be-truthy)) + (let ((kill-buffer-query-functions nil)) + (kill-buffer "*emacs*") + (kill-buffer "*emacs:err*"))))) + (describe "Injecting environment" (it "should construct a form for injecting the current environment"