Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang/go: Added more features for testing in golang layer #3250

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions layers/+lang/go/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,29 @@ 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 x x~ | run "go run" for the current 'main' package |
| ~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 x x~ | run "go run" for the current 'main' 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 |
| ~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
Expand Down
3 changes: 3 additions & 0 deletions layers/+lang/go/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
53 changes: 43 additions & 10 deletions layers/+lang/go/packages.el
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -21,9 +21,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")))

(defun spacemacs/go-run-main ()
(interactive)
Expand All @@ -42,10 +72,13 @@
"mxx" 'spacemacs/go-run-main
"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 ()
Expand Down