gotest.el icon indicating copy to clipboard operation
gotest.el copied to clipboard

subtest support

Open chmouel opened this issue 4 years ago • 2 comments

It would be nice if there was a way to suport testsuite subtest, I currently have this hack where if I detect the current line as "name": "name of test",

it will run the testname with "/name_of_test" appended to gotest, this hack look like this :

(defun my-gotest-maybe-ts-run()
    (interactive)
    (let ((testrunname)
          (gotest (cadr (go-test--get-current-test-info))))
      (save-excursion
        (goto-char (line-beginning-position))
        (re-search-forward "name:[[:blank:]]*\"\\([^\"]*\\)\"" (line-end-position) t))
      (setq testrunname (match-string-no-properties 1))
      (if testrunname
          (setq gotest (format "%s/%s" gotest (shell-quote-argument
                                               (replace-regexp-in-string " " "_" testrunname)))))
      (go-test--go-test (concat "-run " gotest "\\$ ."))))

this works relatively well but that's really depend if the user is on the "name" line and I wonder if this could make it smarter before submitting it.

perhaps the only way to know that properly is to do this with tree-sitter or see if lsp can help.

chmouel avatar Nov 04 '21 09:11 chmouel

This is what I'm using and it seems to work ok so far:

(defun my-go-test--get-current-subtest-info ()
  "Find subtest."
  (save-excursion
    (end-of-line)
    (let ((gotest-before-search (cadr (go-test--get-current-test-info))))
      (if (search-backward-regexp "suite.Run(\"\\([^,]*\\)\"" nil t)
          (let ((subtest-match (match-string-no-properties 1))
                (gotest (cadr (go-test--get-current-test-info))))
            (if (string= gotest gotest-before-search)
                (shell-quote-argument
                 (format ".*/%s/%s$" gotest
                         (replace-regexp-in-string "[[:space:]]" "_" subtest-match)))))))))

(defun my-gotest-current-test ()
  "Launch subtest if found, otherwise run current test."
    (interactive)
    (let ((subtest (my-go-test--get-current-subtest-info)))
      (if subtest
          (go-test--go-test (concat "-run " subtest " ."))
        (go-test-current-test))))

ahobson avatar Jun 30 '23 18:06 ahobson