all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ankit Gadiya via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Randy Taylor <dev@rjt.dev>
Cc: Eli Zaretskii <eliz@gnu.org>, 70939@debbugs.gnu.org
Subject: bug#70939: [PATCH] Add commands to run unit tests in go-ts-mode
Date: Sun, 7 Jul 2024 01:14:01 +0530	[thread overview]
Message-ID: <CAN7zea0LVarjkL8NNVeTXAfjvmj=VvFV+ZmxSMTfdzfBaJo2XA@mail.gmail.com> (raw)
In-Reply-To: <3YG1jNZQXdsT7DtRW9TTuDWpXHZ6qNpBW-7sfn36Mo7fzUp9dEKcHxCBEjpFVeJUbqP8YB9WXd8KyR9LuUERxjVUlLfw8MiE6DGcngF9lG4=@rjt.dev>

[-- Attachment #1: Type: text/plain, Size: 1732 bytes --]

Apologies for the delay again.

> > > For the commit message, I'm not sure we need that paragraph especially when it's already described in the news. Eli what do you think?

Thanks Randy and Eli, I've updated the commit message to be shorter now.

> > > +*** New unit test commands.
> > > +Two new commands are now available to run unit tests.
> > > Three?
>
> This still needs to be updated.

Thanks, I fixed it.

>
> A few more comments:
> +(defun go-ts-mode--get-test-regexp-at-point ()
> +  "Return a regular expression for tests at point.
>                                    ^ the
>
> Could go-ts-mode--get-test-regexp-at-point and go-ts-mode-test-file use if-let?

I wasn't familiar with if-let, thanks for this. Incorporated it in
both the functions.

>
> Also, the indentation looks off in go-ts-mode-test-function-at-point (2 extra spaces methinks).

Fixed this, thanks.

>
> > >
> > > I'm also wondering if we should include "current" in the go-ts-mode-test-file and go-ts-mode-test-package function names. Maybe someone would expect that they would get prompted to select a file or package to test? Maybe I'm overthinking that :). Eli what do you think?
> >
> >
> > I'll wait for Eli to reply before incorporating the changes :).
>
> And he chimed in - let's go with his suggestions.

Updated both the function names.
go-ts-mode-test-this-file
go-ts-mode-test-this-package


> > Additionally, I also noticed that the emacs-30 branch is cut. I wanted
> > to check if I
> > need to rebase my patch onto master or emacs-30 branch?
>
> I would guess master, but let's see what Eli says.

I rebased my patch on the master now. Requesting a re-review.

Thanks

--
Ankit

[-- Attachment #2: 0001-Add-commands-to-run-unit-tests-in-go-ts-mode.patch --]
[-- Type: application/octet-stream, Size: 6319 bytes --]

From 4be577defc99c06091fc0e2954707cc51373bfec Mon Sep 17 00:00:00 2001
From: Ankit R Gadiya <git@argp.in>
Date: Tue, 14 May 2024 00:14:03 +0530
Subject: [PATCH] Add commands to run unit tests in go-ts-mode

Three new commands are added in the go-ts-mode to run unit tests.

* lisp/progmodes/go-ts-mode.el (go-ts-mode-build-tags): New variable.
(go-ts-mode-map): New map variable.
(go-ts-mode--get-build-tags-flag): New function.
(go-ts-mode--compile-test): New function.
(go-ts-mode--find-defun-at): New function.
(go-ts-mode--get-function-regexp): New function.
(go-ts-mode--get-functions-in-range): New function.
(go-ts-mode--get-test-regexp-at-point): New function.
(go-ts-mode-test-function-at-point): New function.
(go-ts-mode-test-file): New function.
(go-ts-mode-test-package): New function.
* etc/NEWS: Mention the change.

(Bug#70939)
---
 etc/NEWS                     | 19 ++++++++
 lisp/progmodes/go-ts-mode.el | 88 +++++++++++++++++++++++++++++++++++-
 2 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index ba58fa7b319..35a761fbeeb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -70,6 +70,25 @@ Advanced" node in the EWW manual.
 By customizing 'shr-image-zoom-levels', you can change the list of zoom
 levels that SHR cycles through when calling 'shr-zoom-image'.
 
+** Go-ts mode
+
++++
+*** New unit test commands.
+Three new commands are now available to run unit tests.
+
+The 'go-ts-mode-test-function-at-point' command runs the 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 t' in 'go-ts-mode'.
+
+The 'go-ts-mode-test-this-file' command runs all unit tests in the current
+file. It is bound to 'C-c C-t f' in 'go-ts-mode'.
+
+The 'go-ts-mode-test-this-package' command runs all unit tests under the
+package of the current buffer.  It is bound to 'C-c C-t p' in 'go-ts-mode'.
+
+The 'go-ts-mode-build-tags' variable is available to set a list of build
+tags for the test commands.
+
 ** Emacs Lisp mode
 
 ---
diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index 2d3e6aac090..3fe427fa911 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -46,6 +46,12 @@
   :safe 'integerp
   :group 'go)
 
+(defcustom go-ts-mode-build-tags nil
+  "List of Go build tags for the test commands."
+  :version "30.1"
+  :type '(repeat string)
+  :group 'go)
+
 (defvar go-ts-mode--syntax-table
   (let ((table (make-syntax-table)))
     (modify-syntax-entry ?+   "."      table)
@@ -242,7 +248,10 @@
 (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 t" #'go-ts-mode-test-function-at-point
+  "C-c C-t f" #'go-ts-mode-test-this-file
+  "C-c C-t p" #'go-ts-mode-test-this-package)
 
 ;;;###autoload
 (define-derived-mode go-ts-mode prog-mode "Go"
@@ -375,6 +384,83 @@ comment already exists, jump to it."
      (<= (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 the compile flag for build tags.
+This function respects the `go-ts-mode-build-tags' variable for
+specifying build tags."
+  (if go-ts-mode-build-tags
+      (format "-tags %s" (string-join go-ts-mode-build-tags ","))
+    ""))
+
+(defun go-ts-mode--compile-test (regexp)
+  "Compile the tests matching REGEXP.
+This function respects the `go-ts-mode-build-tags' 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-function-regexp (name)
+  (if name
+      (format "^%s$" name)
+    (error "No test function found")))
+
+(defun go-ts-mode--get-functions-in-range (start end)
+  "Return a list with the names of all defuns in the range START to END."
+  (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)))
+    (cond ((or (not node)
+               (> start node-end)
+               (< end node-start))
+           nil)
+          ((or (not (equal (treesit-node-type node) "function_declaration"))
+               (not (string-prefix-p "Test" name)))
+           (go-ts-mode--get-functions-in-range (treesit-node-end node) end))
+          (t
+           (cons (go-ts-mode--get-function-regexp 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 the tests at point.
+If region is active, the regexp will include all the functions under the
+region."
+  (if-let ((range (if (region-active-p)
+                      (list (region-beginning) (region-end))
+                    (list (point) (point))))
+           (funcs (apply #'go-ts-mode--get-functions-in-range range)))
+      (string-join funcs "|")
+    (error "No test function found")))
+
+(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
+run.  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-this-file ()
+  "Run all the unit tests in the current file."
+  (interactive)
+  (if-let ((defuns (go-ts-mode--get-functions-in-range (point-min) (point-max))))
+      (go-ts-mode--compile-test (string-join defuns "|"))
+    (error "No test functions found in the current file")))
+
+(defun go-ts-mode-test-this-package ()
+  "Run all the unit tests under the 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.45.2


  parent reply	other threads:[~2024-07-06 19:44 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-14 14:04 bug#70939: [PATCH] Add commands to run unit tests in go-ts-mode Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-14 16:52 ` Eli Zaretskii
2024-05-14 17:24   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-14 17:59     ` Eli Zaretskii
2024-05-15  2:36 ` Randy Taylor
2024-05-15  4:55   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16  2:32     ` Randy Taylor
2024-05-16  8:27       ` Eli Zaretskii
2024-05-16 15:03         ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16 16:01           ` Eli Zaretskii
2024-05-18  9:54             ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-25  2:35               ` Randy Taylor
2024-05-28  2:30                 ` Randy Taylor
2024-05-28 19:58                   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-19 18:17                     ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-21  2:40                       ` Randy Taylor
2024-06-23 14:46                         ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-26  2:26                           ` Randy Taylor
2024-06-26 11:27                             ` Eli Zaretskii
2024-06-26 12:31                               ` Randy Taylor
2024-07-06 19:44                             ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-07-06 22:08                               ` Stefan Kangas
2024-07-06 22:30                                 ` Dmitry Gutov
2024-07-07  7:26                                   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-10 23:43                               ` Randy Taylor
2024-07-11  7:33                                 ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-11 14:21                                   ` Randy Taylor
2024-07-12  6:23                                     ` Eli Zaretskii
2024-07-12 11:10                                       ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-12 11:21                                         ` Eli Zaretskii
2024-07-21  6:05                                   ` Eli Zaretskii
2024-06-23 14:56                         ` Eli Zaretskii
2024-05-17  2:27       ` Randy Taylor
2024-05-18  8:55         ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-15 11:21   ` Eli Zaretskii
2024-05-16  1:24     ` Randy Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN7zea0LVarjkL8NNVeTXAfjvmj=VvFV+ZmxSMTfdzfBaJo2XA@mail.gmail.com' \
    --to=bug-gnu-emacs@gnu.org \
    --cc=70939@debbugs.gnu.org \
    --cc=ankit@argp.in \
    --cc=dev@rjt.dev \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.