test-org-attach.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. (should (equal "data/ab/cd12345"
  74. (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. (file-relative-name (org-attach-dir))))))
  79. (should (equal '("fileA" "fileB")
  80. (org-test-in-example-file org-test-attachments-file
  81. (goto-char 401) ;; H3.1
  82. (let ((org-attach-use-inheritance t))
  83. ;; This is where it get's a bit sketchy...! DIR always has
  84. ;; priority over ID, even if ID is declared "higher up" in the
  85. ;; tree. This can potentially be revised. But it is also
  86. ;; pretty clean. DIR is always higher in priority than ID right
  87. ;; now, no matter the depth in the tree.
  88. (org-attach-file-list (org-attach-dir)))))))
  89. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/1 ()
  90. "Attach file at point in dired to subtree."
  91. (should
  92. (let ((a-filename (make-temp-file "a"))) ; file is an attach candidate.
  93. (unwind-protect
  94. (org-test-with-temp-text-in-file
  95. "* foo :foo:"
  96. (split-window)
  97. (dired temporary-file-directory)
  98. (cl-assert (eq 'dired-mode major-mode))
  99. (revert-buffer)
  100. (dired-goto-file a-filename)
  101. ; action
  102. (call-interactively #'org-attach-dired-to-subtree)
  103. ; check
  104. (delete-window)
  105. (cl-assert (eq 'org-mode major-mode))
  106. (beginning-of-buffer)
  107. (search-forward "* foo")
  108. ; expectation. tag ATTACH has been appended.
  109. (cl-reduce (lambda (x y) (or x y))
  110. (mapcar (lambda (x) (string-equal "ATTACH" x))
  111. (plist-get
  112. (plist-get
  113. (org-element-at-point) 'headline) :tags))))
  114. (delete-file a-filename)))))
  115. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/2 ()
  116. "Attach 2 marked files."
  117. (should
  118. (let ((a-filename (make-temp-file "a"))
  119. (b-filename (make-temp-file "b"))) ; attach candidates.
  120. (unwind-protect
  121. (org-test-with-temp-text-in-file
  122. "* foo"
  123. (split-window)
  124. (dired temporary-file-directory)
  125. (cl-assert (eq 'dired-mode major-mode))
  126. (revert-buffer)
  127. (dired-goto-file a-filename)
  128. (dired-mark 1)
  129. (dired-goto-file b-filename)
  130. (dired-mark 1)
  131. ; action
  132. (call-interactively #'org-attach-dired-to-subtree)
  133. ; check
  134. (delete-window)
  135. (cl-assert (eq 'org-mode major-mode))
  136. (beginning-of-buffer)
  137. (search-forward "* foo")
  138. (and (file-exists-p (concat (org-attach-dir) "/"
  139. (file-name-nondirectory a-filename)))
  140. (file-exists-p (concat (org-attach-dir) "/"
  141. (file-name-nondirectory b-filename)))))
  142. (delete-file a-filename)
  143. (delete-file b-filename)))))
  144. (provide 'test-org-attach)
  145. ;;; test-org-attach.el ends here