summaryrefslogtreecommitdiff
path: root/shell-command+-tests.el
blob: f84ce9ae965148e98fc500e06217187a99bd6015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;;; shell-command+-tests.el --- Tests for shell-command+  -*- lexical-binding: t; -*-

;; Copyright (C) 2021, 2022, 2023  Free Software Foundation, Inc.

;; Author: Philip Kaludercic <philipk@posteo.net>

;; This program 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.

;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Tests for the `shell-command+' parsers.
;;
;; FIXME: Due to the changes in 93233c9g a number of tests have been
;; disabled (see 834c026) that made implicit assumptions on the
;; internal structure of the code.  These tests should eventually be
;; added back to improve the automated testing.

;;; Code:

(require 'ert)
(require 'shell-command+)
(require 'pcase)

(ert-deftest sc+-basic ()
  "Make sure a simple command is recognized."
  (pcase-let* ((`(,path ,mode ,command ,rest)
                (shell-command+-parse "command")))
    (should (null path))
    (should (null mode))
    (should (string= command "command"))
    (should (string= rest "command"))))

(ert-deftest sc+-basic2 ()
  "Make sure a simple command with an argument is recognized."
  (pcase-let* ((`(,path ,mode ,command ,rest)
                (shell-command+-parse "command arg")))
    (should (null path))
    (should (null mode))
    (should (string= command "command"))
    (should (string= rest "command arg"))))

(ert-deftest sc+-basic3 ()
  "Make sure a simple command with an argument is recognized."
  (pcase-let* ((`(,path ,mode ,command ,rest)
                (shell-command+-parse "command arg arg2")))
    (should (null path))
    (should (null mode))
    (should (string= command "command"))
    (should (string= rest "command arg arg2"))))

;; (ert-deftest sc+-path ()
;;   "Make sure a simple command with an argument is recognized."
;;   (pcase-let* ((`(,path ,mode ,command ,rest)
;;                 (shell-command+-parse "... command arg arg2")))
;;     (should (string= path "..."))
;;     (should (null mode))
;;     (should (string= command "command"))
;;     (should (string= rest "command arg arg2"))))

;; (ert-deftest sc+-path2 ()
;;   "Make sure a simple command with an argument is recognized."
;;   (pcase-let* ((`(,path ,mode ,command ,rest)
;;                 (shell-command+-parse ".../dir command arg arg2")))
;;     (should (string= path ".../dir"))
;;     (should (null mode))
;;     (should (string= command "command"))
;;     (should (string= rest "command arg arg2"))))

(ert-deftest sc+-input ()
  "Make sure input is recognized."
  (pcase-let* ((`(,path ,mode ,command ,rest)
                (shell-command+-parse "< command arg arg2")))
    (should (null path))
    (should (eq mode 'input))
    (should (string= command "command"))
    (should (string= rest "command arg arg2"))))

(ert-deftest sc+-output ()
  "Make sure output is recognized."
  (pcase-let* ((`(,path ,mode ,command ,rest)
                (shell-command+-parse "> command arg arg2")))
    (should (null path))
    (should (eq mode 'output))
    (should (string= command "command"))
    (should (string= rest "command arg arg2"))))

(ert-deftest sc+-pipe ()
  "Make sure pipe is recognized."
  (pcase-let* ((`(,path ,mode ,command ,rest)
                (shell-command+-parse "| command arg arg2")))
    (should (null path))
    (should (eq mode 'pipe))
    (should (string= command "command"))
    (should (string= rest "command arg arg2"))))

;; (ert-deftest sc+-input-and-path ()
;;   "Check if input and path conflict."
;;   (pcase-let* ((`(,path ,mode ,command ,rest)
;;                 (shell-command+-parse "... < command arg arg2")))
;;     (should (string= "..." path))
;;     (should (eq mode 'input))
;;     (should (string= command "command"))
;;     (should (string= rest "command arg arg2"))))

;; (ert-deftest sc+-output-and-path ()
;;   "Check if output and path conflict."
;;   (pcase-let* ((`(,path ,mode ,command ,rest)
;;                 (shell-command+-parse "... > command arg arg2")))
;;     (should (string= "..." path))
;;     (should (eq mode 'output))
;;     (should (string= command "command"))
;;     (should (string= rest "command arg arg2"))))

;; (ert-deftest sc+-pipe-and-path ()
;;   "Check if pipe and path conflict."
;;   (pcase-let* ((`(,path ,mode ,command ,rest)
;;                 (shell-command+-parse "... | command arg arg2")))
;;     (should (string= "..." path))
;;     (should (eq mode 'pipe))
;;     (should (string= command "command"))
;;     (should (string= rest "command arg arg2"))))

;; (ert-deftest sc+-file-replace ()
;;   "Check if %'s are replaced."
;;   (pcase-let* ((buffer-file-name "somefile")
;;                (`(,path ,mode ,command ,rest)
;;                    (shell-command+-parse "command %")))
;;     (should (null path))
;;     (should (null mode))
;;     (should (string= command "command"))
;;     (should (string= rest (concat "command " buffer-file-name)))))

;; (ert-deftest sc+-file-noreplace ()
;;   "Check if %'s are replaced."
;;   (pcase-let* ((`(,path ,mode ,command ,rest)
;;                 (shell-command+-parse "command \\%")))
;;     (should (null path))
;;     (should (null mode))
;;     (should (string= command "command"))
;;     (should (string= rest (concat "command \\%")))))

(ert-deftest sc+-expand ()
  "Test that `shell-command+-expand-path' works as expected."
  (let ((default-directory "/some/path/to/dir"))
    (pcase-dolist (`(,path . ,expand)
                   '(("."                       . "/some/path/to/dir")
                     (".."                      . "/some/path/to")
                     ("..."                     . "/some/path")
                     (".../dir"                 . "/some/path/dir")
                     (".../dir/..../else"        . "/else")
                     ("....."                   . "/")))
      (should (string= (shell-command+-expand-path path)
                       expand)))))

(ert-deftest sc+-tokenize ()
  "Test that `shell-command+-tokenize' works as expected."
  (pcase-dolist (`(,args ,expand ,list)
                 `(("a b c" nil ("a" "b" "c"))
                   ("a \"b c\" d" nil ("a" "b c" "d"))
                   ("a *.el d" nil ("a" "*.el" "d"))
                   ("a *.el d" t ("a" ,@(file-expand-wildcards "*.el") "d"))
                   ("a b *.el" nil ("a" "b" "*.el"))
                   ("a b *.el" t ("a" "b" ,@(file-expand-wildcards "*.el")))
                   ("a \"*.el\" d" nil ("a" "*.el" "d"))
                   ("a \"*.el\" d" t ("a" "*.el" "d"))
                   ("a \"b\\ c\" d" nil ("a" "b c" "d"))
                   ("a \"b\\\" c\" d" nil ("a" "b\" c" "d"))
                   ("a \"b\\\" \\\"c\" d" nil ("a" "b\" \"c" "d"))
                   ("a b \\c d" nil ("a" "b" "c" "d"))
                   ("a b \\cd" nil ("a" "b" "cd"))
                   ("a b\\ c d" nil ("a" "b c" "d"))
                   ("a b\\c d" nil ("a" "bc" "d"))
                   ("a b\\ c d" nil ("a" "b c" "d"))
                   ("a b\\\\c d" nil ("a" "b\\c" "d"))
                   ("a b\\\\ c d" nil ("a" "b\\" "c" "d"))
                   ("a b\\\\\\ c d" nil ("a" "b\\\\ c" "d"))
                   ("abcd\\ ef gh" nil ("abcd ef" "gh"))
                   ("abcd\\ ef  gh" nil ("abcd ef" "gh"))
                   ("abcd\\ ef  gh " nil ("abcd ef" "gh"))
                   (" abcd\\ ef  gh  " nil ("abcd ef" "gh"))))
    (should (equal (shell-command+-tokenize args expand)
                   list))))

(provide 'shell-command+-tests)
;;; shell-command+-tests.el ends here