test-org-attach.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; test-org-attach.el --- tests for org-attach.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017, 2019
  3. ;; Author: Marco Wahl
  4. ;; Keywords: internal
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'org-test)
  19. (require 'org-attach)
  20. (eval-and-compile (require 'cl-lib))
  21. (ert-deftest test-org-attach/dir ()
  22. "Test `org-attach-get' specifications."
  23. (should (equal "Text in fileA\n"
  24. (org-test-in-example-file org-test-attachments-file
  25. (goto-char 157) ;; First attachment link
  26. (org-open-at-point)
  27. (buffer-string))))
  28. (should-not (equal "Text in fileB\n"
  29. (org-test-in-example-file org-test-attachments-file
  30. (goto-char 219) ;; Second attachment link
  31. (let ((org-attach-use-inheritance nil))
  32. (org-open-at-point)
  33. (buffer-string)))))
  34. (should (equal "Text in fileB\n"
  35. (org-test-in-example-file org-test-attachments-file
  36. (goto-char 219) ;; Second attachment link
  37. (let ((org-attach-use-inheritance t))
  38. (org-open-at-point)
  39. (buffer-string)))))
  40. (should-not (equal "att1"
  41. (org-test-in-example-file org-test-attachments-file
  42. (goto-char 179) ;; H1.1
  43. (let ((org-attach-use-inheritance nil))
  44. (org-attach-dir)))))
  45. (should (equal "att1"
  46. (org-test-in-example-file org-test-attachments-file
  47. (goto-char 179) ;; H1.1
  48. (let ((org-attach-use-inheritance t))
  49. (org-attach-dir)))))
  50. (should (equal '("fileC" "fileD")
  51. (org-test-in-example-file org-test-attachments-file
  52. (goto-char 239) ;; H1.2
  53. (org-attach-file-list (org-attach-dir)))))
  54. (should (equal '("fileC" "fileD")
  55. (org-test-in-example-file org-test-attachments-file
  56. (goto-char 239) ;; H1.2
  57. (org-attach-file-list (org-attach-dir)))))
  58. (should (equal '("fileE")
  59. (org-test-in-example-file org-test-attachments-file
  60. (goto-char 289) ;; H2
  61. (let ((org-attach-id-dir "data/"))
  62. (org-attach-file-list (org-attach-dir))))))
  63. (should (equal "peek-a-boo\n"
  64. (org-test-in-example-file org-test-attachments-file
  65. (goto-char 289) ;; H2
  66. (let ((org-attach-id-dir "data/"))
  67. (org-attach-open-in-emacs)
  68. (buffer-string)))))
  69. (should (equal '("fileA" "fileB")
  70. (org-test-in-example-file org-test-attachments-file
  71. (goto-char 336) ;; H3
  72. (org-attach-file-list (org-attach-dir)))))
  73. ;; Test for folder not initialized in the filesystem
  74. (should-not (org-test-in-example-file org-test-attachments-file
  75. (goto-char 401) ;; H3.1
  76. (let ((org-attach-use-inheritance nil)
  77. (org-attach-id-dir "data/"))
  78. (org-attach-dir))))
  79. ;; Not yet initialized folder should be found if no-fs-check is
  80. ;; non-nil
  81. (should (equal "data/ab/cd12345"
  82. (org-test-in-example-file org-test-attachments-file
  83. (goto-char 401) ;; H3.1
  84. (let ((org-attach-use-inheritance nil)
  85. (org-attach-id-dir "data/"))
  86. (file-relative-name (org-attach-dir nil t))))))
  87. (should (equal '("fileA" "fileB")
  88. (org-test-in-example-file org-test-attachments-file
  89. (goto-char 401) ;; H3.1
  90. (let ((org-attach-use-inheritance t))
  91. ;; This is where it get's a bit sketchy...! DIR always has
  92. ;; priority over ID, even if ID is declared "higher up" in the
  93. ;; tree. This can potentially be revised. But it is also
  94. ;; pretty clean. DIR is always higher in priority than ID right
  95. ;; now, no matter the depth in the tree.
  96. (org-attach-file-list (org-attach-dir)))))))
  97. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/1 ()
  98. "Attach file at point in dired to subtree."
  99. (should
  100. (let ((a-filename (make-temp-file "a")) ; file is an attach candidate.
  101. (org-attach-id-dir "data/"))
  102. (unwind-protect
  103. (org-test-with-temp-text-in-file
  104. "* foo :foo:"
  105. (split-window)
  106. (let ((org-buffer (current-buffer))
  107. (dired-buffer (dired temporary-file-directory)))
  108. (cl-assert (eq 'dired-mode major-mode))
  109. (revert-buffer)
  110. (dired-goto-file a-filename)
  111. ; action
  112. (call-interactively #'org-attach-dired-to-subtree)
  113. ; check
  114. (delete-window)
  115. (switch-to-buffer org-buffer)
  116. (cl-assert (eq 'org-mode major-mode)))
  117. (beginning-of-buffer)
  118. (search-forward "* foo")
  119. ; expectation. tag ATTACH has been appended.
  120. (cl-reduce (lambda (x y) (or x y))
  121. (mapcar (lambda (x) (string-equal "ATTACH" x))
  122. (plist-get
  123. (plist-get
  124. (org-element-at-point) 'headline) :tags))))
  125. (delete-file a-filename)))))
  126. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/2 ()
  127. "Attach 2 marked files."
  128. (should
  129. (let ((a-filename (make-temp-file "a"))
  130. (b-filename (make-temp-file "b")) ; attach candidates.
  131. (org-attach-id-dir "data/"))
  132. (unwind-protect
  133. (org-test-with-temp-text-in-file
  134. "* foo"
  135. (split-window)
  136. (let ((org-buffer (current-buffer))
  137. (dired-buffer (dired temporary-file-directory)))
  138. (cl-assert (eq 'dired-mode major-mode))
  139. (revert-buffer)
  140. (dired-goto-file a-filename)
  141. (dired-mark 1)
  142. (dired-goto-file b-filename)
  143. (dired-mark 1)
  144. ; action
  145. (call-interactively #'org-attach-dired-to-subtree)
  146. ; check
  147. (delete-window)
  148. (switch-to-buffer org-buffer))
  149. (cl-assert (eq 'org-mode major-mode))
  150. (beginning-of-buffer)
  151. (search-forward "* foo")
  152. (and (file-exists-p (concat (org-attach-dir) "/"
  153. (file-name-nondirectory a-filename)))
  154. (file-exists-p (concat (org-attach-dir) "/"
  155. (file-name-nondirectory b-filename)))))
  156. (delete-file a-filename)
  157. (delete-file b-filename)))))
  158. (provide 'test-org-attach)
  159. ;;; test-org-attach.el ends here