* 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.
"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
(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