test-org-attach.el 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;;; test-org-attach.el --- Tests for Org Attach
  2. ;;
  3. ;; Copyright (c) 2016 Erik Hetzner
  4. ;; Authors: Erik Hetzner
  5. ;; This file is not part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (require 'org-attach)
  18. (require 'cl-lib)
  19. (defmacro test-org-attach-annex/with-annex (&rest body)
  20. `(let ((tmpdir (make-temp-file "org-annex-test" t)))
  21. (unwind-protect
  22. (let ((default-directory tmpdir)
  23. (org-attach-directory tmpdir))
  24. (shell-command "git init")
  25. (shell-command "git annex init")
  26. ,@body))))
  27. (ert-deftest test-org-attach/use-annex ()
  28. (org-test-for-executable "git-annex")
  29. (test-org-attach-annex/with-annex
  30. (let ((org-attach-git-annex-cutoff 1))
  31. (should (org-attach-use-annex)))
  32. (let ((org-attach-git-annex-cutoff nil))
  33. (should-not (org-attach-use-annex))))
  34. ;; test with non annex directory
  35. (let ((tmpdir (make-temp-file "org-annex-test" t)))
  36. (unwind-protect
  37. (let ((default-directory tmpdir)
  38. (org-attach-directory tmpdir))
  39. (shell-command "git init")
  40. (should-not (org-attach-use-annex)))
  41. (delete-directory tmpdir 'recursive))))
  42. (ert-deftest test-org-attach/get-maybe ()
  43. (org-test-for-executable "git-annex")
  44. (test-org-attach-annex/with-annex
  45. (let ((path (expand-file-name "test-file"))
  46. (annex-dup (make-temp-file "org-annex-test" t)))
  47. (with-temp-buffer
  48. (insert "hello world\n")
  49. (write-file path))
  50. (shell-command "git annex add test-file")
  51. (shell-command "git annex sync")
  52. ;; Set up remote & copy files there
  53. (let ((annex-original default-directory)
  54. (default-directory annex-dup))
  55. (shell-command (format "git clone %s ." (shell-quote-argument annex-original)))
  56. (shell-command "git annex init dup")
  57. (shell-command (format "git remote add original %s" (shell-quote-argument annex-original)))
  58. (shell-command "git annex get test-file")
  59. (shell-command "git annex sync"))
  60. (shell-command (format "git remote add dup %s" (shell-quote-argument annex-dup)))
  61. (shell-command "git annex sync")
  62. (shell-command "git annex drop --force test-file")
  63. ;; test getting the file from the dup when we should ALWAYS get
  64. (should (not (file-exists-p (file-symlink-p (expand-file-name "test-file")))))
  65. (let ((org-attach-annex-auto-get t))
  66. (org-attach-annex-get-maybe (expand-file-name "test-file"))
  67. ;; check that the file has the right contents
  68. (with-temp-buffer
  69. (insert-file-contents path)
  70. (should (string-equal "hello world\n" (buffer-string)))))
  71. ;; test getting the file from the dup when we should NEVER get
  72. (shell-command "git annex drop --force test-file")
  73. (let ((org-attach-annex-auto-get nil))
  74. (should-error (org-attach-annex-get-maybe (expand-file-name "test-file"))))
  75. (let ((org-attach-annex-auto-get 'ask)
  76. (called nil))
  77. (flet ((y-or-n-p (prompt)
  78. (setq called 'was-called)
  79. t))
  80. (org-attach-annex-get-maybe (expand-file-name "test-file"))
  81. ;; check that the file has the right contents
  82. (with-temp-buffer
  83. (insert-file-contents path)
  84. (should (string-equal "hello world\n" (buffer-string))))
  85. (should (eq called 'was-called)))))))
  86. ;;; test-org-attach.el ends here