aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJim Porter2022-01-04 12:58:38 -0800
committerEli Zaretskii2022-01-12 16:58:37 +0200
commitdb745f37aec2adc44ec4b2eae0720e0365ed0ca9 (patch)
tree1c2b405ad2bc81d34fd2ae140c18855d78460665 /test
parent7ebcb4b6f2f4531ebc893bb3b2f74d6298bf9b41 (diff)
downloademacs-db745f37aec2adc44ec4b2eae0720e0365ed0ca9.tar.gz
emacs-db745f37aec2adc44ec4b2eae0720e0365ed0ca9.zip
Follow POSIX/GNU argument conventions for 'eshell-eval-using-options'
* lisp/eshell/esh-opt.el (eshell--split-switch): New function. (eshell-set-option): Allow setting a supplied value instead of always consuming from 'eshell--args'. (eshell--process-option): Support consuming option values specified as a single token. (eshell--process-args): For short options, pass full switch token to 'eshell--process-option'. * test/lisp/eshell/esh-opt-tests.el (esh-opt-process-args-test): Fix test. (test-eshell-eval-using-options): Add tests for various types of options. * doc/misc/eshell.texi (Defining new built-in commands): New subsection, describe how to use 'eshell-eval-using-options'. * etc/NEWS: Announce the change.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/eshell/esh-opt-tests.el151
1 files changed, 112 insertions, 39 deletions
diff --git a/test/lisp/eshell/esh-opt-tests.el b/test/lisp/eshell/esh-opt-tests.el
index 532adfb733a..255768635b1 100644
--- a/test/lisp/eshell/esh-opt-tests.el
+++ b/test/lisp/eshell/esh-opt-tests.el
@@ -57,7 +57,7 @@
57 '((?u "user" t user "execute a command as another USER") 57 '((?u "user" t user "execute a command as another USER")
58 :parse-leading-options-only)))) 58 :parse-leading-options-only))))
59 (should 59 (should
60 (equal '("world" "emerge") 60 (equal '("DN" "emerge" "world")
61 (eshell--process-args 61 (eshell--process-args
62 "sudo" 62 "sudo"
63 '("-u" "root" "emerge" "-uDN" "world") 63 '("-u" "root" "emerge" "-uDN" "world")
@@ -65,59 +65,132 @@
65 65
66(ert-deftest test-eshell-eval-using-options () 66(ert-deftest test-eshell-eval-using-options ()
67 "Tests for `eshell-eval-using-options'." 67 "Tests for `eshell-eval-using-options'."
68 ;; Test short options.
68 (eshell-eval-using-options 69 (eshell-eval-using-options
69 "sudo" '("-u" "root" "whoami") 70 "ls" '("-a" "/some/path")
70 '((?u "user" t user "execute a command as another USER") 71 '((?a "all" nil show-all
71 :parse-leading-options-only) 72 "do not ignore entries starting with ."))
72 (should (equal user "root"))) 73 (should (eq show-all t))
74 (should (equal args '("/some/path"))))
73 (eshell-eval-using-options 75 (eshell-eval-using-options
74 "sudo" '("--user" "root" "whoami") 76 "ls" '("/some/path")
75 '((?u "user" t user "execute a command as another USER") 77 '((?a "all" nil show-all
76 :parse-leading-options-only) 78 "do not ignore entries starting with ."))
77 (should (equal user "root"))) 79 (should (eq show-all nil))
80 (should (equal args '("/some/path"))))
78 81
82 ;; Test long options.
79 (eshell-eval-using-options 83 (eshell-eval-using-options
80 "sudo" '("emerge" "-uDN" "world") 84 "ls" '("--all" "/some/path")
81 '((?u "user" t user "execute a command as another USER")) 85 '((?a "all" nil show-all
82 (should (equal user "world"))) 86 "do not ignore entries starting with ."))
87 (should (eq show-all t))
88 (should (equal args '("/some/path"))))
89
90 ;; Test options with constant values.
83 (eshell-eval-using-options 91 (eshell-eval-using-options
84 "sudo" '("emerge" "-uDN" "world") 92 "ls" '("/some/path" "-h")
85 '((?u "user" t user "execute a command as another USER") 93 '((?h "human-readable" 1024 human-readable
86 :parse-leading-options-only) 94 "print sizes in human readable format"))
87 (should (eq user nil))) 95 (should (eql human-readable 1024))
96 (should (equal args '("/some/path"))))
97 (eshell-eval-using-options
98 "ls" '("/some/path" "--human-readable")
99 '((?h "human-readable" 1024 human-readable
100 "print sizes in human readable format"))
101 (should (eql human-readable 1024))
102 (should (equal args '("/some/path"))))
103 (eshell-eval-using-options
104 "ls" '("/some/path")
105 '((?h "human-readable" 1024 human-readable
106 "print sizes in human readable format"))
107 (should (eq human-readable nil))
108 (should (equal args '("/some/path"))))
88 109
110 ;; Test options with user-specified values.
111 (eshell-eval-using-options
112 "ls" '("-I" "*.txt" "/some/path")
113 '((?I "ignore" t ignore-pattern
114 "do not list implied entries matching pattern"))
115 (should (equal ignore-pattern "*.txt"))
116 (should (equal args '("/some/path"))))
117 (eshell-eval-using-options
118 "ls" '("-I*.txt" "/some/path")
119 '((?I "ignore" t ignore-pattern
120 "do not list implied entries matching pattern"))
121 (should (equal ignore-pattern "*.txt"))
122 (should (equal args '("/some/path"))))
89 (eshell-eval-using-options 123 (eshell-eval-using-options
90 "ls" '("-I" "*.txt" "/dev/null") 124 "ls" '("--ignore" "*.txt" "/some/path")
91 '((?I "ignore" t ignore-pattern 125 '((?I "ignore" t ignore-pattern
92 "do not list implied entries matching pattern")) 126 "do not list implied entries matching pattern"))
93 (should (equal ignore-pattern "*.txt"))) 127 (should (equal ignore-pattern "*.txt"))
128 (should (equal args '("/some/path"))))
129 (eshell-eval-using-options
130 "ls" '("--ignore=*.txt" "/some/path")
131 '((?I "ignore" t ignore-pattern
132 "do not list implied entries matching pattern"))
133 (should (equal ignore-pattern "*.txt"))
134 (should (equal args '("/some/path"))))
94 135
136 ;; Test multiple short options in a single token.
95 (eshell-eval-using-options 137 (eshell-eval-using-options
96 "ls" '("-l" "/dev/null") 138 "ls" '("-al" "/some/path")
97 '((?l nil long-listing listing-style 139 '((?a "all" nil show-all
98 "use a long listing format")) 140 "do not ignore entries starting with .")
99 (should (eql listing-style 'long-listing))) 141 (?l nil long-listing listing-style
142 "use a long listing format"))
143 (should (eq t show-all))
144 (should (eql listing-style 'long-listing))
145 (should (equal args '("/some/path"))))
100 (eshell-eval-using-options 146 (eshell-eval-using-options
101 "ls" '("/dev/null") 147 "ls" '("-aI*.txt" "/some/path")
102 '((?l nil long-listing listing-style 148 '((?a "all" nil show-all
103 "use a long listing format")) 149 "do not ignore entries starting with .")
104 (should (eq listing-style nil))) 150 (?I "ignore" t ignore-pattern
151 "do not list implied entries matching pattern"))
152 (should (eq t show-all))
153 (should (equal ignore-pattern "*.txt"))
154 (should (equal args '("/some/path"))))
105 155
156 ;; Test that "--" terminates options.
106 (eshell-eval-using-options 157 (eshell-eval-using-options
107 "ls" '("/dev/null" "-h") 158 "ls" '("--" "-a")
108 '((?h "human-readable" 1024 human-readable 159 '((?a "all" nil show-all
109 "print sizes in human readable format")) 160 "do not ignore entries starting with ."))
110 (should (eql human-readable 1024))) 161 (should (eq show-all nil))
162 (should (equal args '("-a"))))
111 (eshell-eval-using-options 163 (eshell-eval-using-options
112 "ls" '("/dev/null" "--human-readable") 164 "ls" '("--" "--all")
113 '((?h "human-readable" 1024 human-readable 165 '((?a "all" nil show-all
114 "print sizes in human readable format")) 166 "do not ignore entries starting with ."))
115 (should (eql human-readable 1024))) 167 (should (eq show-all nil))
168 (should (equal args '("--all"))))
169
170 ;; Test :parse-leading-options-only.
116 (eshell-eval-using-options 171 (eshell-eval-using-options
117 "ls" '("/dev/null") 172 "sudo" '("-u" "root" "whoami")
118 '((?h "human-readable" 1024 human-readable 173 '((?u "user" t user "execute a command as another USER")
119 "print sizes in human readable format")) 174 :parse-leading-options-only)
120 (should (eq human-readable nil)))) 175 (should (equal user "root"))
176 (should (equal args '("whoami"))))
177 (eshell-eval-using-options
178 "sudo" '("--user" "root" "whoami")
179 '((?u "user" t user "execute a command as another USER")
180 :parse-leading-options-only)
181 (should (equal user "root"))
182 (should (equal args '("whoami"))))
183 (eshell-eval-using-options
184 "sudo" '("emerge" "-uDN" "world")
185 '((?u "user" t user "execute a command as another USER"))
186 (should (equal user "DN"))
187 (should (equal args '("emerge" "world"))))
188 (eshell-eval-using-options
189 "sudo" '("emerge" "-uDN" "world")
190 '((?u "user" t user "execute a command as another USER")
191 :parse-leading-options-only)
192 (should (eq user nil))
193 (should (equal args '("emerge" "-uDN" "world")))))
121 194
122(provide 'esh-opt-tests) 195(provide 'esh-opt-tests)
123 196