Subject: bug#70939: closed (Re: bug#70939: [PATCH] Add commands to run unit tests in go-ts-mode)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 21 Jul 2024 06:06:01 +0000

Your bug report

#70939: [PATCH] Add commands to run unit tests in go-ts-mode

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
70939: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=70939
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#70939: [PATCH] Add commands to run unit tests in go-ts-mode
From: Eli Zaretskii <[email-masked]>
To: Ankit Gadiya <[email-masked]>
Cc: [email-masked], [email-masked]
Date: Sun, 21 Jul 2024 09:05:24 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> From: Ankit Gadiya <[email-masked]>
> Date: Thu, 11 Jul 2024 13:03:32 +0530
> Cc: Eli Zaretskii <[email-masked]>, [email-masked]
> 
> > I only have a few comments about the commit message:
> >
> > Three new commands are added in the go-ts-mode to run unit tests.
> > I would just drop this line altogether, personally.
> >
> > (go-ts-mode-map): New map variable.
> > This should probably read something like Add new bindings.
> >
> > (go-ts-mode-test-file): New function.
> > (go-ts-mode-test-package): New function.
> > These two need to be updated (...-test-this-...).
> 
> Thanks, I'm sending the updated patch with these fixes.
> 
> > Everything else looks good to me. Thanks for working on this, Ankit.
> >
> > Eli, if you have no further comments please install when you get a chance. Thanks in advance.
> 
> Thank you for being patient through this review. After this interaction, I feel
> encouraged to send more patches in the future.

Now installed on the master branch, and closing the bug.

Thanks.

Subject: [PATCH] Add commands to run unit tests in go-ts-mode
From: Ankit Gadiya <[email-masked]>
To: [email-masked]
Date: Tue, 14 May 2024 19:34:48 +0530
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello folks,

This patch adds two new commands for go-ts-mode to run unit test cases.

The go-ts-mode-test-package command can run all the tests under the package
of
the current buffer. I've tested it to work with both Go module packages and
non-module packages. This command is bound to C-c C-p in the go-ts-mode-map.

The go-ts-mode-test-function-at-point command runs the current test
function. If
region is active then it runs all the test functions under the region. I'm
attaching a sample_test.go file as well for reviewers to test the patch.
This
command is bound to C-c C-t in the go-ts-mode-map.

-- 
Ankit
From 4988ef4962c8f3fde0ca0be7a1485488c7dca923 Mon Sep 17 00:00:00 2001
From: Ankit R Gadiya <[email-masked]>
Date: Tue, 14 May 2024 00:14:03 +0530
Subject: [PATCH] Add commands to run unit tests in go-ts-mode

---
 etc/NEWS                     | 10 ++++++
 lisp/progmodes/go-ts-mode.el | 63 +++++++++++++++++++++++++++++++++++-
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 34052764f5f..b9df7fdcaf3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1236,6 +1236,16 @@ This command adds a docstring comment to the current defun.  If a
 comment already exists, point is only moved to the comment.  It is
 bound to 'C-c C-d' in 'go-ts-mode'.
 
+*** New unit test commands.
+Two new commands are now available to run unit tests.
+
+The 'go-ts-mode-test-function-at-point' command runs unit test at
+point. If a region is active, it runs all the unit tests under the
+region. It is bound to 'C-c C-t' in 'go-ts-mode'.
+
+The 'go-ts-mode-test-package' command runs all unit tests under the
+package of the current buffer. It is bound to 'C-c C-p' in 'go-ts-mode'.
+
 ** Man mode
 
 +++
diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index aef224ab3fa..5540ddb61e6 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -237,7 +237,9 @@ go-ts-mode--font-lock-settings
 (defvar-keymap go-ts-mode-map
   :doc "Keymap used in Go mode, powered by tree-sitter"
   :parent prog-mode-map
-  "C-c C-d" #'go-ts-mode-docstring)
+  "C-c C-d" #'go-ts-mode-docstring
+  "C-c C-t" #'go-ts-mode-test-function-at-point
+  "C-c C-p" #'go-ts-mode-test-package)
 
 ;;;###autoload
 (define-derived-mode go-ts-mode prog-mode "Go"
@@ -370,6 +372,65 @@ go-ts-mode--comment-on-previous-line-p
      (<= (treesit-node-start node) point (treesit-node-end node))
      (string-equal "comment" (treesit-node-type node)))))
 
+(defun go-ts-mode--get-build-tags-flag ()
+  "Return compile flag for build tags.
+This function respects `go-build-tags' buffer-local variable for
+specifying build tags."
+  (if (local-variable-p 'go-build-tags)
+      (format "-tags %s" go-build-tags)
+    ""))
+
+(defun go-ts-mode--compile-test (regexp)
+  "Compiles the tests matching REGEXP.
+This function respects `go-build-tags' buffer-local variable for
+specifying build tags."
+  (compile (format "go test -v %s -run '%s'"
+                   (go-ts-mode--get-build-tags-flag)
+                   regexp)))
+
+(defun go-ts-mode--find-defun-at (start)
+  "Return the first defun node from START."
+  (let ((thing (or treesit-defun-type-regexp 'defun)))
+    (or (treesit-thing-at start thing)
+        (treesit-thing-next start thing))))
+
+(defun go-ts-mode--get-functions-in-range (start end)
+  "Return a list with names of all defuns in the range."
+  (let* ((node (go-ts-mode--find-defun-at start))
+	 (name (treesit-defun-name node))
+         (node-start (treesit-node-start node))
+         (node-end (treesit-node-end node)))
+    (if (or (not node)
+            (> start node-end)
+	    (< end node-start))
+	nil
+      (cons name (go-ts-mode--get-functions-in-range (treesit-node-end node) end)))))
+
+(defun go-ts-mode--get-test-regexp-at-point ()
+  "Return a regular expression for tests at point.
+If region is active, the regexp will include all the functions under the
+region."
+  (if (region-active-p)
+      (string-join (go-ts-mode--get-functions-in-range (region-beginning)
+                                                       (region-end))
+                   "|")
+    (treesit-defun-name (treesit-defun-at-point))))
+
+(defun go-ts-mode-test-function-at-point ()
+  "Run the unit test at point.
+If the point is anywhere in the test function, that function will be
+tested.  If the region is selected, all the functions under the region
+will be run."
+    (interactive)
+    (go-ts-mode--compile-test (go-ts-mode--get-test-regexp-at-point)))
+
+(defun go-ts-mode-test-package ()
+  "Run all the unit tests under current package."
+  (interactive)
+  (compile (format "go test -v %s -run %s"
+                   (go-ts-mode--get-build-tags-flag)
+                   default-directory)))
+
 ;; go.mod support.
 
 (defvar go-mod-ts-mode--syntax-table
-- 
2.39.2