From fca62384537e1a32e867f4d3181e0b2b79396383 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Simen=20Heggest=C3=B8yl?= Date: Wed, 9 Aug 2017 15:34:34 +0200 Subject: [PATCH] Handle non-zero exit status from psql more gracefully * lisp/progmodes/sql.el (sql-postgres-list-databases): Handle non-zero exit statuses from `psql -ltX' more gracefully by returning nil. * test/lisp/progmodes/sql-tests.el (sql-tests-postgres-list-databases-error): New test. --- lisp/progmodes/sql.el | 7 ++++--- test/lisp/progmodes/sql-tests.el | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el index b176e52950c..48e21605a33 100644 --- a/lisp/progmodes/sql.el +++ b/lisp/progmodes/sql.el @@ -1095,9 +1095,10 @@ add your name with a \"-U\" prefix (such as \"-Umark\") to the list." "Return a list of available PostgreSQL databases." (when (executable-find sql-postgres-program) (let ((res '())) - (dolist (row (process-lines sql-postgres-program "-ltX")) - (when (string-match "^ \\([[:alnum:]-_]+\\) +|.*" row) - (push (match-string 1 row) res))) + (ignore-errors + (dolist (row (process-lines sql-postgres-program "-ltX")) + (when (string-match "^ \\([[:alnum:]-_]+\\) +|.*" row) + (push (match-string 1 row) res)))) (nreverse res)))) ;; Customization for Interbase diff --git a/test/lisp/progmodes/sql-tests.el b/test/lisp/progmodes/sql-tests.el index 27a72aa2c23..f75005f737f 100644 --- a/test/lisp/progmodes/sql-tests.el +++ b/test/lisp/progmodes/sql-tests.el @@ -43,5 +43,15 @@ (should (equal (sql-postgres-list-databases) '("db-name-1" "db_name_2"))))) +(ert-deftest sql-tests-postgres-list-databases-error () + "Test that nil is returned when `psql -ltX' fails." + (cl-letf + (((symbol-function 'executable-find) + (lambda (_command) t)) + ((symbol-function 'process-lines) + (lambda (_program &rest _args) + (error)))) + (should-not (sql-postgres-list-databases)))) + (provide 'sql-tests) ;;; sql-tests.el ends here -- 2.39.2