diff --git a/layers/+lang/go/README.org b/layers/+lang/go/README.org index 23b6275a5817..4a96ffe4b7fe 100644 --- a/layers/+lang/go/README.org +++ b/layers/+lang/go/README.org @@ -54,23 +54,28 @@ formatter, set the value of =gofmt-command=, e.g. #+begin_src emacs-lisp (setq gofmt-command "goimports") #+end_src +# +If you're using =gocheck= in your project you can use the =go-use-gocheck-for-testing= variable to enable suite testing and to get single function testing to work. * Working with Go ** Go commands (start with =m=): -| Key Binding | Description | -|-------------+-----------------------------------------------------------| -| ~SPC m h h~ | godoc at point | -| ~SPC m i g~ | goto imports | -| ~SPC m i a~ | add import | -| ~SPC m i r~ | remove unused import | -| ~SPC m e b~ | go-play buffer | -| ~SPC m e r~ | go-play region | -| ~SPC m e d~ | download go-play snippet | -| ~SPC m t p~ | run "go test" for the current package | -| ~SPC m g a~ | jump to matching test file or back from test to code file | -| ~SPC m g g~ | go jump to definition | -| ~SPC m r n~ | go rename | +| Key Binding | Description | +|-------------+---------------------------------------------------------------------------------------| +| ~SPC m h h~ | godoc at point | +| ~SPC m i g~ | goto imports | +| ~SPC m i a~ | add import | +| ~SPC m i r~ | remove unused import | +| ~SPC m e b~ | go-play buffer | +| ~SPC m e r~ | go-play region | +| ~SPC m e d~ | download go-play snippet | +| ~SPC m g a~ | jump to matching test file or back from test to code file | +| ~SPC m g g~ | go jump to definition | +| ~SPC m r n~ | go rename | +| ~SPC m t p~ | run "go test" for the current package | +| ~SPC m t P~ | run "go test" for the current package and all packages under it | +| ~SPC m t t~ | run "go test" for the function you're currently in (while you're in a _.test.go file) | +| ~SPC m t s~ | run "go test" for the suite you're currently in (requires gocheck) | ** Go Oracle diff --git a/layers/+lang/go/config.el b/layers/+lang/go/config.el index 0dcc9daec9f9..6682f7cd2157 100644 --- a/layers/+lang/go/config.el +++ b/layers/+lang/go/config.el @@ -13,3 +13,6 @@ ;; variables (spacemacs|defvar-company-backends go-mode) + +(defvar go-use-gocheck-for-testing nil + "If using gocheck for testing when running the tests -check.f will be used instead of -run to specify the test that will be ran. Gocheck is mandatory for testing suites.") diff --git a/layers/+lang/go/packages.el b/layers/+lang/go/packages.el index 5262aa3390d7..edd8b8773f0b 100644 --- a/layers/+lang/go/packages.el +++ b/layers/+lang/go/packages.el @@ -1,11 +1,11 @@ (setq go-packages - '( - company - company-go - flycheck - go-mode - go-eldoc - )) + '( + company + company-go + flycheck + go-mode + go-eldoc + )) (defun go/post-init-flycheck () (spacemacs/add-flycheck-hook 'go-mode)) @@ -20,9 +20,39 @@ (progn (add-hook 'before-save-hook 'gofmt-before-save) + (defun spacemacs/go-run-tests (args) + (interactive) + (save-selected-window + (async-shell-command (concat "go test " args)))) + (defun spacemacs/go-run-package-tests () (interactive) - (shell-command "go test")) + (spacemacs/go-run-tests "")) + + (defun spacemacs/go-run-package-tests-nested () + (interactive) + (spacemacs/go-run-tests "./...")) + + (defun spacemacs/go-run-test-current-function () + (interactive) + (if (string-match "_test\\.go" buffer-file-name) + (let ((test-method (if go-use-gocheck-for-testing + "-check.f" + "-run"))) + (save-excursion + (re-search-backward "^func[ ]+([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\(Test[[:alnum:]]+\\)(.*)") + (spacemacs/go-run-tests (concat test-method "='" (match-string-no-properties 2) "'")))) + (message "Must be in a _test.go file to run go-run-test-current-function"))) + + (defun spacemacs/go-run-test-current-suite () + (interactive) + (if (string-match "_test\.go" buffer-file-name) + (if go-use-gocheck-for-testing + (save-excursion + (re-search-backward "^func[ ]+([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\(Test[[:alnum:]]+\\)(.*)") + (spacemacs/go-run-tests (concat "-check.f='" (match-string-no-properties 1) "'"))) + (message "Gocheck is needed to test the current suite")) + (message "Must be in a _test.go file to run go-test-current-suite"))) (evil-leader/set-key-for-mode 'go-mode "mhh" 'godoc-at-point @@ -34,10 +64,13 @@ "med" 'go-download-play "mga" 'ff-find-other-file "mgg" 'godef-jump - "mtp" 'spacemacs/go-run-package-tests)))) + "mtt" 'spacemacs/go-run-test-current-function + "mts" 'spacemacs/go-run-test-current-suite + "mtp" 'spacemacs/go-run-package-tests + "mtP" 'spacemacs/go-run-package-tests-nested)))) (defun go/init-go-eldoc() - (add-hook 'go-mode-hook 'go-eldoc-setup)) + (add-hook 'go-mode-hook 'go-eldoc-setup)) (when (configuration-layer/layer-usedp 'auto-completion) (defun go/post-init-company ()