emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
42ad5e1a3ed639993eb3e7b22845b14381a1e9b9 blob 10441 bytes (raw)

  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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
 
;;; test-org-protocol.el --- tests for org-protocol.el                  -*- lexical-binding: t; -*-

;; Copyright (c)  Sacha Chua
;; Authors: Sacha Chua

;; This file is not part of GNU Emacs.

;; 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 <http://www.gnu.org/licenses/>.

;;; Code:

(require 'cl-lib)

(unless (featurep 'org-protocol)
  (signal 'missing-test-dependency "Support for org-protocol"))

(ert-deftest test-org-protocol/org-protocol-parse-parameters ()
  "Test `org-protocol-parse-parameters' specifications."
  ;; Ignore lists
  (let ((data (org-protocol-parse-parameters '(:url "abc" :title "def") nil)))
    (should (string= (plist-get data :url) "abc"))
    (should (string= (plist-get data :title) "def")))
  ;; Parse new-style links
  (let ((data (org-protocol-parse-parameters "url=abc&title=def" t)))
    (should (string= (plist-get data :url) "abc"))
    (should (string= (plist-get data :title) "def")))
  ;; Parse new-style complex links
  (let* ((url (concat "template=p&"
		      "url=https%3A%2F%2Forgmode.org%2Forg.html%23capture-protocol&"
		      "title=The%20Org%20Manual&"
		      "body=9.4.2%20capture%20protocol"))
	 (data (org-protocol-parse-parameters url t)))
    (should (string= (plist-get data :template) "p"))
    (should (string= (plist-get data :url) "https://orgmode.org/org.html#capture-protocol"))
    (should (string= (plist-get data :title) "The Org Manual"))
    (should (string= (plist-get data :body) "9.4.2 capture protocol")))
  ;; Parse old-style links
  (let ((data (org-protocol-parse-parameters "abc/def" nil '(:url :title))))
    (should (string= (plist-get data :url) "abc"))
    (should (string= (plist-get data :title) "def")))
  ;; Parse old-style links even without keys
  (let ((data (org-protocol-parse-parameters "b/abc/def" nil)))
    (should (equal data '("b" "abc" "def"))))
  ;; Parse old-style links with key/val pairs
  (let ((data (org-protocol-parse-parameters "b/abc/extrakey/extraval" nil '(:param1 :param2))))
    (should (string= (plist-get data :param1) "b"))
    (should (string= (plist-get data :param2) "abc"))
    (should (string= (plist-get data :extrakey) "extraval"))))

(ert-deftest test-org-protocol/org-protocol-store-link ()
  "Test `org-protocol-store-link' specifications."
  ;; Old link style
  (let ((uri "/some/directory/org-protocol:/store-link:/URL/TITLE"))
    (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
    (should (equal (car org-stored-links) '("URL" "TITLE"))))
  ;; URL encoded
  (let ((uri (format "/some/directory/org-protocol:/store-link:/%s/TITLE"
		     (url-hexify-string "http://example.com"))))
    (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
    (should (equal (car org-stored-links) '("http://example.com" "TITLE"))))
  ;; Handle multiple slashes, old link style
  (let ((uri "/some/directory/org-protocol://store-link://URL2//TITLE2"))
    (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
    (should (equal (car org-stored-links) '("URL2" "TITLE2"))))
  ;; New link style
  (let ((uri "/some/directory/org-protocol://store-link?url=URL3&title=TITLE3"))
    (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
    (should (equal (car org-stored-links) '("URL3" "TITLE3")))))

(ert-deftest test-org-protocol/org-protocol-capture ()
  "Test `org-protocol-capture' specifications."
  (let* ((org-protocol-default-template-key "t")
	 (temp-file-name (make-temp-file "org-protocol-test"))
	 (org-capture-templates
	  `(("t" "Test" entry (file ,temp-file-name) "** TODO\n\n%i\n\n%a\n" :kill-buffer t)
	    ("x" "With params" entry (file ,temp-file-name) "** SOMEDAY\n\n%i\n\n%a\n" :kill-buffer t)
	    ("X" "Just the template" entry (file ,temp-file-name) "** Hello World\n\n%i\n\nGoodbye World\n" :kill-buffer t)))
	 (test-urls
	  '(
	    ;; Old style:
	    ;; - multiple slashes
	    ("/some/directory/org-protocol:/capture:/URL/TITLE"
	     . "** TODO\n\n\n\n[[URL][TITLE]]\n")
	    ;; - body specification
	    ("/some/directory/org-protocol:/capture:/URL/TITLE/BODY"
	     . "** TODO\n\nBODY\n\n[[URL][TITLE]]\n")
	    ;; - template
	    ("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY"
	     . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
	    ;; - query parameters, not sure how to include them in template
	    ("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY/from/example"
	     . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
	    ;; New style:
	    ;; - multiple slashes
	    ("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE"
	     . "** TODO\n\n\n\n[[NEWURL][TITLE]]\n")
	    ;; - body specification
	    ("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE&body=BODY"
	     . "** TODO\n\nBODY\n\n[[NEWURL][TITLE]]\n")
	    ;; - template
	    ("/some/directory/org-protocol:/capture?template=x&url=NEWURL&title=TITLE&body=BODY"
	     . "** SOMEDAY\n\nBODY\n\n[[NEWURL][TITLE]]\n")
	    ;; - no url specified
	    ("/some/directory/org-protocol:/capture?template=x&title=TITLE&body=BODY"
	    . "** SOMEDAY\n\nBODY\n\nTITLE\n")
	    ;; - no title specified
	    ("/some/directory/org-protocol:/capture?template=x&url=NEWURL&body=BODY"
	     . "** SOMEDAY\n\nBODY\n\n[[NEWURL][NEWURL]]\n")
	    ;; - just the template
	    ("/some/directory/org-protocol:/capture?template=X"
	     . "** Hello World\n\n\n\nGoodbye World\n")
	    ;; - query parameters, not sure how to include them in template
	    ("/some/directory/org-protocol:/capture?template=x&url=URL&title=TITLE&body=BODY&from=example"
	     . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
	    )))
    ;; Old link style
    (mapc
     (lambda (test-case)
       (let ((uri (car test-case)))
	 (org-protocol-check-filename-for-protocol uri (list uri) nil)
	 (should (string= (buffer-string) (cdr test-case)))
	 (org-capture-kill)))
     test-urls)
    (delete-file temp-file-name)))

(ert-deftest test-org-protocol/org-protocol-open-source ()
  "Test org-protocol://open-source links."
  (let* ((temp-file-name1 (make-temp-file "org-protocol-test1"))
	 (temp-file-name2 (make-temp-file "org-protocol-test2"))
	 (temp-file-name3 (make-temp-file "org-protocol-test3" nil ".md"))
	 (temp-file-name4 (make-temp-file "org-protocol-test4" nil ".c"))
	 (org-protocol-project-alist
	  `((test1
	     :base-url "http://example.com/"
	     :online-suffix ".html"
	     :working-directory ,(file-name-directory temp-file-name1))
	    (test2
	     :base-url "http://another.example.com/"
	     :online-suffix ".js"
	     :working-directory ,(file-name-directory temp-file-name2))
	    (test3
	     :base-url "https://blog-example.com/"
	     :working-directory ,(file-name-directory temp-file-name3)
	     :online-suffix ".html"
	     :working-suffix ".md"
	     :rewrites (("\\(https://blog-example.com/[0-9]+/[0-9]+/[0-9]+/\\)" . ".md")))
	    (test4
	     :base-url "http://as-is.example.com/"
	     :working-directory ,(file-name-directory temp-file-name4))))
	 (test-cases
	  (list
	   ;; Old-style URLs
	   (cons
	    (concat "/some/directory/org-protocol:/open-source:/"
		    (url-hexify-string
		     (concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
	    temp-file-name1)
	   (cons
	    (concat "/some/directory/org-protocol:/open-source:/"
		    (url-hexify-string
		     (concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
	    temp-file-name2)
	   (cons
	    (concat "/some/directory/org-protocol:/open-source:/"
	   	    (url-hexify-string
	   	     (concat "https://blog-example.com/" (file-name-nondirectory (file-name-sans-extension temp-file-name3)) ".html")))
	    temp-file-name3)
	   (cons
	    (concat "/some/directory/org-protocol:/open-source:/"
	   	    (url-hexify-string
	   	     (concat "http://as-is.example.com/" (file-name-nondirectory temp-file-name4))))
	    temp-file-name4)
	   ;; New-style URLs
	   (cons
	    (concat "/some/directory/org-protocol:/open-source?url="
		    (url-hexify-string
		     (concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
	    temp-file-name1)
	   (cons
	    (concat "/some/directory/org-protocol:/open-source?url="
		    (url-hexify-string
		     (concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
	    temp-file-name2)
	   (cons
	    (concat "/some/directory/org-protocol:/open-source?url="
	   	    (url-hexify-string
	   	     (concat "https://blog-example.com/" (file-name-nondirectory (file-name-sans-extension temp-file-name3)) ".html")))
	    temp-file-name3)
	   (cons
	    (concat "/some/directory/org-protocol:/open-source?url="
		    (url-hexify-string
		     (concat "http://as-is.example.com/" (file-name-nondirectory temp-file-name4))))
	    temp-file-name4))))
    (mapc (lambda (test-case)
	    (should (string=
		     (org-protocol-check-filename-for-protocol
		      (car test-case)
		      (list (car test-case)) nil)
		     (cdr test-case))))
	  test-cases)
    (delete-file temp-file-name1)
    (delete-file temp-file-name2)
    (delete-file temp-file-name3)
    (delete-file temp-file-name4)))

(defun test-org-protocol/org-protocol-greedy-handler (fname)
  ;; fname should be a list of parsed items
  (should (listp fname))
  nil)

(ert-deftest test-org-protocol/org-protocol-with-greedy-handler ()
  "Check that greedy handlers are called with all the filenames."
  (let ((org-protocol-protocol-alist
	 '(("protocol-a" :protocol "greedy" :function test-org-protocol/org-protocol-greedy-handler :kill-client t :greedy t))))
    ;; Neither of these should signal errors
    (let ((uri "/some/dir/org-protocol://greedy?a=b&c=d")
	  (uri2 "/some/dir/org-protocol://greedy?e=f&g=h"))
      (org-protocol-check-filename-for-protocol uri (list uri uri2) nil))))


;; TODO: Verify greedy protocol handling
;;; test-org-protocol.el ends here
debug log:

solving 42ad5e1a3 ...
found 42ad5e1a3 in https://list.orgmode.org/orgmode/8882FF49-05B4-4ADD-8180-F14E9DB70138@gmail.com/ ||
	https://list.orgmode.org/orgmode/8882FF49-05B4-4ADD-8180-F14E9DB70138@gmail.com/
found 6ee368dcf in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 6ee368dcfabae9d06b3e3b11940ef4421c6a0d4f	testing/lisp/test-org-protocol.el

applying [1/2] https://list.orgmode.org/orgmode/8882FF49-05B4-4ADD-8180-F14E9DB70138@gmail.com/
index 6ee368dcf..42ad5e1a3 100644

error: corrupt patch at line 12

git apply error: exit status=128
trying https://list.orgmode.org/orgmode/8882FF49-05B4-4ADD-8180-F14E9DB70138@gmail.com/

applying [2/2] https://list.orgmode.org/orgmode/8882FF49-05B4-4ADD-8180-F14E9DB70138@gmail.com/
diff --git a/testing/lisp/test-org-protocol.el b/testing/lisp/test-org-protocol.el
index 6ee368dcf..42ad5e1a3 100644

2:45: space before tab in indent.
	   	    (url-hexify-string
2:46: space before tab in indent.
	   	     (concat "https://blog-example.com/" (file-name-nondirectory (file-name-sans-extension temp-file-name3)) ".html")))
2:50: space before tab in indent.
	   	    (url-hexify-string
2:51: space before tab in indent.
	   	     (concat "http://as-is.example.com/" (file-name-nondirectory temp-file-name4))))
2:64: space before tab in indent.
	   	    (url-hexify-string
Checking patch testing/lisp/test-org-protocol.el...
Applied patch testing/lisp/test-org-protocol.el cleanly.
warning: squelched 1 whitespace error
warning: 6 lines add whitespace errors.

index at:
100644 42ad5e1a3ed639993eb3e7b22845b14381a1e9b9	testing/lisp/test-org-protocol.el

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).