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.
This commit is contained in:
Simen Heggestøyl 2017-08-09 15:34:34 +02:00
parent 25a49f6496
commit fca6238453
2 changed files with 14 additions and 3 deletions

View 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

View file

@ -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