Fix problems with org-attach-git, failed tests

Ref. mail "[O] git-annex-related org-attach tests failing on
master"
https://lists.gnu.org/archive/html/emacs-orgmode/2019-09/msg00030.html
* lisp/org-attach-git.el (org-attach-git-commit): Add optional
argument to function contract to make it work with
`org-attach-after-change-hook'.  Even though that argument is
not used in the actual code (due to legacy functionality).

* testing/lisp/test-org-attach.el
(test-org-attach/dired-attach-to-next-best-subtree/1)
(test-org-attach/dired-attach-to-next-best-subtree/2):
Modify tests to also work if user has git-annex installed

* testing/lisp/test-org-attach-git.el
(test-org-attach-git/use-annex): Correct errors from previous
commit, where git-annex was refactored out from org-attach
into it's separate module.
This commit is contained in:
Gustav Wikström 2019-09-08 14:12:46 +02:00
parent f2f646b6c9
commit 930a1bcfc5
3 changed files with 52 additions and 44 deletions

View File

@ -23,10 +23,9 @@
;;; Commentary: ;;; Commentary:
;; An extention to org-attach. If the attachment-directory to an ;; An extention to org-attach. If `org-attach-id-dir' is initialized
;; outline node (using either DIR or ID) is initialized as a Git ;; as a Git repository, then org-attach-git will automatically commit
;; repository, then org-attach-git will automatically commit changes ;; changes when it sees them. Requires git-annex.
;; when it sees them.
;;; Code: ;;; Code:
@ -81,9 +80,12 @@ Signals an error if the file content is not available and it was not retrieved."
(message "Running git annex get \"%s\"." path-relative) (message "Running git annex get \"%s\"." path-relative)
(call-process "git" nil nil nil "annex" "get" path-relative))))) (call-process "git" nil nil nil "annex" "get" path-relative)))))
(defun org-attach-git-commit () (defun org-attach-git-commit (&optional attach-dir)
"Commit changes to git if `org-attach-id-dir' is properly initialized. "Commit changes to git if `org-attach-id-dir' is properly initialized.
This checks for the existence of a \".git\" directory in that directory." This checks for the existence of a \".git\" directory in that directory.
Takes one optional argument ATTACH-DIR for the sake of being
compatible with hook `org-attach-after-change-hook'."
(let* ((dir (expand-file-name org-attach-id-dir)) (let* ((dir (expand-file-name org-attach-id-dir))
(git-dir (vc-git-root dir)) (git-dir (vc-git-root dir))
(use-annex (org-attach-git-use-annex)) (use-annex (org-attach-git-use-annex))

View File

@ -33,12 +33,12 @@
,@body)))) ,@body))))
(ert-deftest test-org-attach-git/use-annex () (ert-deftest test-org-attach-git/use-annex ()
(test-org-attach-annex/with-annex (test-org-attach-git/with-annex
(let ((org-attach-git-annex-cutoff 1)) (let ((org-attach-git-annex-cutoff 1))
(should (org-attach-use-annex))) (should (org-attach-git-use-annex)))
(let ((org-attach-git-annex-cutoff nil)) (let ((org-attach-git-annex-cutoff nil))
(should-not (org-attach-use-annex)))) (should-not (org-attach-git-use-annex))))
;; test with non annex directory ;; test with non annex directory
(let ((tmpdir (make-temp-file "org-annex-test" t "/"))) (let ((tmpdir (make-temp-file "org-annex-test" t "/")))
@ -46,11 +46,11 @@
(let ((default-directory tmpdir) (let ((default-directory tmpdir)
(org-attach-id-dir tmpdir)) (org-attach-id-dir tmpdir))
(shell-command "git init") (shell-command "git init")
(should-not (org-attach-use-annex))) (should-not (org-attach-git-use-annex)))
(delete-directory tmpdir 'recursive)))) (delete-directory tmpdir 'recursive))))
(ert-deftest test-org-attach-git/get-maybe () (ert-deftest test-org-attach-git/get-maybe ()
(test-org-attach-annex/with-annex (test-org-attach-git/with-annex
(let ((path (expand-file-name "test-file")) (let ((path (expand-file-name "test-file"))
(annex-dup (make-temp-file "org-annex-test" t "/"))) (annex-dup (make-temp-file "org-annex-test" t "/")))
(with-temp-buffer (with-temp-buffer
@ -71,21 +71,21 @@
(shell-command "git annex drop --force test-file") (shell-command "git annex drop --force test-file")
;; test getting the file from the dup when we should ALWAYS get ;; test getting the file from the dup when we should ALWAYS get
(should (not (file-exists-p (file-symlink-p (expand-file-name "test-file"))))) (should (not (file-exists-p (file-symlink-p (expand-file-name "test-file")))))
(let ((org-attach-annex-auto-get t)) (let ((org-attach-git-annex-auto-get t))
(org-attach-annex-get-maybe (expand-file-name "test-file")) (org-attach-git-annex-get-maybe (expand-file-name "test-file"))
;; check that the file has the right contents ;; check that the file has the right contents
(with-temp-buffer (with-temp-buffer
(insert-file-contents path) (insert-file-contents path)
(should (string-equal "hello world\n" (buffer-string))))) (should (string-equal "hello world\n" (buffer-string)))))
;; test getting the file from the dup when we should NEVER get ;; test getting the file from the dup when we should NEVER get
(shell-command "git annex drop --force test-file") (shell-command "git annex drop --force test-file")
(let ((org-attach-annex-auto-get nil)) (let ((org-attach-git-annex-auto-get nil))
(should-error (org-attach-annex-get-maybe (expand-file-name "test-file")))) (should-error (org-attach-git-annex-get-maybe (expand-file-name "test-file"))))
(let ((org-attach-annex-auto-get 'ask) (let ((org-attach-git-annex-auto-get 'ask)
(called nil)) (called nil))
(cl-letf (((symbol-function 'y-or-n-p) (cl-letf (((symbol-function 'y-or-n-p)
(lambda (_) (setq called 'was-called) t))) (lambda (_) (setq called 'was-called) t)))
(org-attach-annex-get-maybe (expand-file-name "test-file")) (org-attach-git-annex-get-maybe (expand-file-name "test-file"))
;; check that the file has the right contents ;; check that the file has the right contents
(with-temp-buffer (with-temp-buffer
(insert-file-contents path) (insert-file-contents path)

View File

@ -108,12 +108,14 @@
(ert-deftest test-org-attach/dired-attach-to-next-best-subtree/1 () (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/1 ()
"Attach file at point in dired to subtree." "Attach file at point in dired to subtree."
(should (should
(let ((a-filename (make-temp-file "a"))) ; file is an attach candidate. (let ((a-filename (make-temp-file "a")) ; file is an attach candidate.
(org-attach-id-dir "data/"))
(unwind-protect (unwind-protect
(org-test-with-temp-text-in-file (org-test-with-temp-text-in-file
"* foo :foo:" "* foo :foo:"
(split-window) (split-window)
(dired temporary-file-directory) (let ((org-buffer (current-buffer))
(dired-buffer (dired temporary-file-directory)))
(cl-assert (eq 'dired-mode major-mode)) (cl-assert (eq 'dired-mode major-mode))
(revert-buffer) (revert-buffer)
(dired-goto-file a-filename) (dired-goto-file a-filename)
@ -121,7 +123,8 @@
(call-interactively #'org-attach-dired-to-subtree) (call-interactively #'org-attach-dired-to-subtree)
; check ; check
(delete-window) (delete-window)
(cl-assert (eq 'org-mode major-mode)) (switch-to-buffer org-buffer)
(cl-assert (eq 'org-mode major-mode)))
(beginning-of-buffer) (beginning-of-buffer)
(search-forward "* foo") (search-forward "* foo")
; expectation. tag ATTACH has been appended. ; expectation. tag ATTACH has been appended.
@ -136,12 +139,14 @@
"Attach 2 marked files." "Attach 2 marked files."
(should (should
(let ((a-filename (make-temp-file "a")) (let ((a-filename (make-temp-file "a"))
(b-filename (make-temp-file "b"))) ; attach candidates. (b-filename (make-temp-file "b")) ; attach candidates.
(org-attach-id-dir "data/"))
(unwind-protect (unwind-protect
(org-test-with-temp-text-in-file (org-test-with-temp-text-in-file
"* foo" "* foo"
(split-window) (split-window)
(dired temporary-file-directory) (let ((org-buffer (current-buffer))
(dired-buffer (dired temporary-file-directory)))
(cl-assert (eq 'dired-mode major-mode)) (cl-assert (eq 'dired-mode major-mode))
(revert-buffer) (revert-buffer)
(dired-goto-file a-filename) (dired-goto-file a-filename)
@ -152,6 +157,7 @@
(call-interactively #'org-attach-dired-to-subtree) (call-interactively #'org-attach-dired-to-subtree)
; check ; check
(delete-window) (delete-window)
(switch-to-buffer org-buffer))
(cl-assert (eq 'org-mode major-mode)) (cl-assert (eq 'org-mode major-mode))
(beginning-of-buffer) (beginning-of-buffer)
(search-forward "* foo") (search-forward "* foo")