]> git.eshelyaron.com Git - emacs.git/commitdiff
Handle non-zero exit status from psql more gracefully
authorSimen Heggestøyl <simenheg@gmail.com>
Wed, 9 Aug 2017 13:34:34 +0000 (15:34 +0200)
committerSimen Heggestøyl <simenheg@gmail.com>
Tue, 5 Sep 2017 18:31:21 +0000 (20:31 +0200)
* 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
test/lisp/progmodes/sql-tests.el

index b176e52950c0c41d0f861c9fe159b534c086ac4c..48e21605a3307e700ef7b22b50206559c24a0ee1 100644 (file)
@@ -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
index 27a72aa2c239e1fbc2d270a37c9372bf876c9a17..f75005f737f3de87375ca25d7b62466a7109318f 100644 (file)
     (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