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