Before I submit the updated patch, does this look like a better approach?
In the test for `org-base-buffer-file-name' I changed it to explicitly find the current buffer file name and use that:
(ert-deftest test-org-base-buffer-file-name ()
"Test `org-base-buffer-file-name'."
(should
(org-test-with-temp-text-in-file
"File"
(let ((base-filename (buffer-file-name)))
(and
;; Confirm that we get the same answer whether we provide the buffer or use the default
(equal base-filename (org-base-buffer-file-name))
(equal base-filename (org-base-buffer-file-name (current-buffer)))))))
(should
(org-test-with-temp-text-in-file
"File with capture buffer"
(let ((org-capture-templates '(("t" "Test" entry (here) "* Test Header\n\n")))
(base-filename (buffer-file-name)))
(org-capture nil "t")
(and
;; Confirm that we get the same answer whether we provide the buffer or use the default
(equal base-filename (org-base-buffer-file-name))
(equal base-filename (org-base-buffer-file-name (current-buffer)))
(equal base-filename (org-base-buffer-file-name (buffer-base-buffer (current-buffer))))
))))
(should-not
(org-test-with-temp-text
"Buffer without file"
(org-base-buffer-file-name))))
For the tangle test, I also capture the base buffer filename, use an explicit tangle filename, and ensure that the call to `org-babel-tangle' returns the expected list of tangled files:
;; See
https://list.orgmode.org/87msfxd81c.fsf@localhost/T/#t (ert-deftest ob-tangle/tangle-from-capture-buffer ()
"Test tangling of source blocks from within a capture buffer.
This is to ensure that we properly resolve the buffer name."
(org-test-with-temp-text-in-file
"* Header\n\nCapture after this point:\n<point>"
(let ((tangle-filename (format "%s.el" (buffer-file-name))))
(should
(unwind-protect
(progn
(let ((org-capture-templates '(("t" "Test" entry (here) "* Test Header\n\n"))))
(org-capture nil "t")
(goto-char (point-max))
(insert
(format "
#+begin_src elisp :tangle \"%s\" :comments org
(message \"FOO\")
#+end_src" tangle-filename))
(search-backward "message")
;; Confirm that we tangled to the right file
(equal (org-babel-tangle 4) (list tangle-filename))))
;; Clean up the tangled file with the filename from org-test-with-temp-text-in-file
(delete-file tangle-filename))))))