1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-05 19:31:02 -08:00
emacs/test/lisp/eshell/esh-opt-tests.el
Jim Porter 90de226e21 Raise an error from 'eval-eval-using-options' for unknown options
* lisp/eshell/em-basic.el (eshell/echo): Add -E option.

* lisp/eshell/esh-opt.el (eshell--process-option): Raise an error if
an unknown option is encountered, even when :external is nil.

* test/lisp/eshell/esh-opt-tests.el (esh-opt-process-args-test)
(test-eshell-eval-using-options): Add test cases for this.
2022-01-21 13:06:11 +01:00

239 lines
8.2 KiB
EmacsLisp

;;; esh-opt-tests.el --- esh-opt test suite -*- lexical-binding:t -*-
;; Copyright (C) 2018-2022 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(require 'ert)
(require 'esh-opt)
(ert-deftest esh-opt-process-args-test ()
"Unit tests which verify correct behavior of `eshell--process-args'."
(should
(equal '(t)
(eshell--process-args
"sudo" '("-a")
'((?a "all" nil show-all
"do not ignore entries starting with .")))))
(should
(equal '("root" "world")
(eshell--process-args
"sudo" '("-u" "root" "world")
'((?u "user" t user
"execute a command as another USER")))))
(should
(equal '(nil "emerge" "-uDN" "world")
(eshell--process-args
"sudo" '("emerge" "-uDN" "world")
'((?u "user" t user
"execute a command as another USER")
:parse-leading-options-only))))
(should
(equal '("root" "emerge" "-uDN" "world")
(eshell--process-args
"sudo" '("-u" "root" "emerge" "-uDN" "world")
'((?u "user" t user
"execute a command as another USER")
:parse-leading-options-only))))
(should
(equal '("DN" "emerge" "world")
(eshell--process-args
"sudo" '("-u" "root" "emerge" "-uDN" "world")
'((?u "user" t user
"execute a command as another USER")))))
;; Test :external.
(cl-letf (((symbol-function 'eshell-search-path) #'ignore))
(should
(equal '(nil "/some/path")
(eshell--process-args
"ls" '("/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with .")
:external "ls")))))
(cl-letf (((symbol-function 'eshell-search-path) #'identity))
(should
(equal '(no-catch eshell-ext-command "ls")
(should-error
(eshell--process-args
"ls" '("-u" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with .")
:external "ls"))
:type 'no-catch))))
(cl-letf (((symbol-function 'eshell-search-path) #'ignore))
(should-error
(eshell--process-args
"ls" '("-u" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with .")
:external "ls"))
:type 'error)))
(ert-deftest test-eshell-eval-using-options ()
"Tests for `eshell-eval-using-options'."
;; Test short options.
(eshell-eval-using-options
"ls" '("-a" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(should (eq show-all t))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(should (eq show-all nil))
(should (equal args '("/some/path"))))
;; Test long options.
(eshell-eval-using-options
"ls" '("--all" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(should (eq show-all t))
(should (equal args '("/some/path"))))
;; Test options with constant values.
(eshell-eval-using-options
"ls" '("/some/path" "-h")
'((?h "human-readable" 1024 human-readable
"print sizes in human readable format"))
(should (eql human-readable 1024))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("/some/path" "--human-readable")
'((?h "human-readable" 1024 human-readable
"print sizes in human readable format"))
(should (eql human-readable 1024))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("/some/path")
'((?h "human-readable" 1024 human-readable
"print sizes in human readable format"))
(should (eq human-readable nil))
(should (equal args '("/some/path"))))
;; Test options with user-specified values.
(eshell-eval-using-options
"ls" '("-I" "*.txt" "/some/path")
'((?I "ignore" t ignore-pattern
"do not list implied entries matching pattern"))
(should (equal ignore-pattern "*.txt"))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("-I*.txt" "/some/path")
'((?I "ignore" t ignore-pattern
"do not list implied entries matching pattern"))
(should (equal ignore-pattern "*.txt"))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("--ignore" "*.txt" "/some/path")
'((?I "ignore" t ignore-pattern
"do not list implied entries matching pattern"))
(should (equal ignore-pattern "*.txt"))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("--ignore=*.txt" "/some/path")
'((?I "ignore" t ignore-pattern
"do not list implied entries matching pattern"))
(should (equal ignore-pattern "*.txt"))
(should (equal args '("/some/path"))))
;; Test multiple short options in a single token.
(eshell-eval-using-options
"ls" '("-al" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with .")
(?l nil long-listing listing-style
"use a long listing format"))
(should (eq t show-all))
(should (eql listing-style 'long-listing))
(should (equal args '("/some/path"))))
(eshell-eval-using-options
"ls" '("-aI*.txt" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with .")
(?I "ignore" t ignore-pattern
"do not list implied entries matching pattern"))
(should (eq t show-all))
(should (equal ignore-pattern "*.txt"))
(should (equal args '("/some/path"))))
;; Test that "--" terminates options.
(eshell-eval-using-options
"ls" '("--" "-a")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(should (eq show-all nil))
(should (equal args '("-a"))))
(eshell-eval-using-options
"ls" '("--" "--all")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(should (eq show-all nil))
(should (equal args '("--all"))))
;; Test :parse-leading-options-only.
(eshell-eval-using-options
"sudo" '("-u" "root" "whoami")
'((?u "user" t user "execute a command as another USER")
:parse-leading-options-only)
(should (equal user "root"))
(should (equal args '("whoami"))))
(eshell-eval-using-options
"sudo" '("--user" "root" "whoami")
'((?u "user" t user "execute a command as another USER")
:parse-leading-options-only)
(should (equal user "root"))
(should (equal args '("whoami"))))
(eshell-eval-using-options
"sudo" '("emerge" "-uDN" "world")
'((?u "user" t user "execute a command as another USER"))
(should (equal user "DN"))
(should (equal args '("emerge" "world"))))
(eshell-eval-using-options
"sudo" '("emerge" "-uDN" "world")
'((?u "user" t user "execute a command as another USER")
:parse-leading-options-only)
(should (eq user nil))
(should (equal args '("emerge" "-uDN" "world"))))
;; Test unrecognized options.
(should-error
(eshell-eval-using-options
"ls" '("-u" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(ignore show-all)))
(should-error
(eshell-eval-using-options
"ls" '("-au" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(ignore show-all)))
(should-error
(eshell-eval-using-options
"ls" '("--unrecognized" "/some/path")
'((?a "all" nil show-all
"do not ignore entries starting with ."))
(ignore show-all))))
(provide 'esh-opt-tests)
;;; esh-opt-tests.el ends here