From 41f9a10be587b47454ca8ef26f6a8247605cbe34 Mon Sep 17 00:00:00 2001 From: Dustin Farris Date: Sat, 27 Sep 2025 15:43:53 -0700 Subject: [PATCH] fix(capture): self-remove org-roam-capture-run-new-node-hook-a advice This was incorrectly removing advice that doesn't exist, leading to the actual advice being perpetually added without removal. Amend: ed94524964a0b03538a84ae03c89ec61527ffe7d --- org-roam-capture.el | 2 +- tests/test-org-roam-capture.el | 48 +++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/org-roam-capture.el b/org-roam-capture.el index 4ab306f..00e96b7 100644 --- a/org-roam-capture.el +++ b/org-roam-capture.el @@ -607,7 +607,7 @@ capture target." "Advice to run after the Org-capture template is placed." (when (org-roam-capture--get :new-node-p) (run-hooks 'org-roam-capture-new-node-hook)) - (advice-remove #'org-capture-place-template #'org-roam-capture--place-template-a)) + (advice-remove #'org-capture-place-template #'org-roam-capture-run-new-node-hook-a)) (defun org-roam-capture--create-id-for-entry () "Create the ID for the new entry." diff --git a/tests/test-org-roam-capture.el b/tests/test-org-roam-capture.el index 2aa3793..6c16b31 100644 --- a/tests/test-org-roam-capture.el +++ b/tests/test-org-roam-capture.el @@ -177,30 +177,54 @@ #'org-capture-place-template) :to-be nil))) - (it "org-roam-capture-run-new-node-hook-a runs hook when new node" - (let ((hook-ran nil)) + (describe "org-roam-capture-run-new-node-hook-a" + (it "runs hook when new node" + (let ((hook-ran nil)) + (cl-letf* (((symbol-function 'org-roam-capture--get) + (lambda (prop) + (if (eq prop :new-node-p) + t + nil)))) + + ;; Add test hook + (add-hook 'org-roam-capture-new-node-hook + (lambda () (setq hook-ran t))) + + ;; Add the advice + (advice-add #'org-capture-place-template :after #'org-roam-capture-run-new-node-hook-a) + + ;; Call the function + (org-roam-capture-run-new-node-hook-a nil) + + ;; Verify hook ran + (expect hook-ran :to-be t) + + ;; Clean up + (remove-hook 'org-roam-capture-new-node-hook + (lambda () (setq hook-ran t)))))) + + (it "removes itself as advice after running" (cl-letf* (((symbol-function 'org-roam-capture--get) (lambda (prop) (if (eq prop :new-node-p) t nil)))) - ;; Add test hook - (add-hook 'org-roam-capture-new-node-hook - (lambda () (setq hook-ran t))) - ;; Add the advice (advice-add #'org-capture-place-template :after #'org-roam-capture-run-new-node-hook-a) + ;; Verify advice is added + (expect (advice-member-p #'org-roam-capture-run-new-node-hook-a + #'org-capture-place-template) + :to-be-truthy) + ;; Call the function (org-roam-capture-run-new-node-hook-a nil) - ;; Verify hook ran - (expect hook-ran :to-be t) - - ;; Clean up - (remove-hook 'org-roam-capture-new-node-hook - (lambda () (setq hook-ran t))))))) + ;; Verify advice was removed after running + (expect (advice-member-p #'org-roam-capture-run-new-node-hook-a + #'org-capture-place-template) + :to-be nil))))) (describe "org-roam-capture target-entry-p detection" (it "detects entry target for file+olp"