-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotn.el
98 lines (80 loc) · 2.88 KB
/
gotn.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;;; gotn.el --- Utility tool for Go to run test at point
;; Author: Yauhen Lazurkin
;; URL: https://github.com/yauhenl/gotn
;; Keywords: tools
;; Version: 1.0
;;; Commentary:
;; Quick start:
;; Install `gotn` tool with `go get github.com/yauhenl/gotn`
;; and make sure it is in the PATH
;;
;; Define a go-mode hook and bindings:
;; (require 'gotn)
;; (add-hook 'go-mode-hook
;; (lambda ()
;; (local-set-key (kbd "C-c t") 'gotn-run-test))
;; (local-set-key (kbd "C-c C-t") 'gotn-run-test-package)))
;;
;; For more details visit: https://github.com/yauhen-l/gotn
;;; Code:
(require 'compile)
(add-to-list 'compilation-error-regexp-alist '("\\([a-zA-Z0-9_]+\\.go\\):\\([0-9]+\\)" 1 2))
(defcustom gotn-go-test-case-command "go test -v -run"
"The command to run test case."
:type 'string
:group 'gotn)
(defcustom gotn-go-test-package-command "go test -v ."
"The command to run tests of current pacakge."
:type 'string
:group 'gotn)
(defcustom gotn-go-test-package-test-fallback t
"Whether no test case under position run all package tests."
:type 'boolean
:group 'gotn)
(defun gotn--compilation-name (_mode-name)
"Name of the go test. _MODE-NAME is unused."
"*Go test*")
(defun gotn--run-test-as-compilation (cmd)
"Run CMD in ‘gotn-mode’."
(compilation-start cmd
'gotn-mode
'gotn--compilation-name))
;;;###autoload
(defun gotn-run-test-package ()
"Run go test of current package."
(interactive)
(gotn--run-test-as-compilation gotn-go-test-package-command))
;;;###autoload
(defun gotn-run-test (point)
"Run go test at POINT."
(interactive "d")
(condition-case nil
(let ((gotn-out (gotn--call point)))
(if (= (car gotn-out) 0)
(gotn--run-test-as-compilation (concat gotn-go-test-case-command " ^" (car (cdr gotn-out)) "$"))
(if gotn-go-test-package-test-fallback
(gotn--run-test-as-compilation gotn-go-test-package-command)
(message (format "Could not run gotn binary: %s" (cdr gotn-out))))))))
(defun gotn--call (point)
"Call `gotn' to get test name at POINT."
(if (not (buffer-file-name (current-buffer)))
(error "Cannot use gotn on a buffer without a file name")
(let ((out
(shell-command-to-string
(concat "gotn -f "
(file-truename (buffer-file-name (current-buffer)))
" -p "
(number-to-string (position-bytes point))))))
(if (string= (substring out -1 nil) "\n")
(list 1 (substring out 0 -1))
(list 0 out)))))
(defvar gotn-mode-map
(nconc (make-sparse-keymap) compilation-mode-map)
"Keymap for gotn major mode.")
(define-derived-mode gotn-mode compilation-mode "gotn"
"Major mode for the gotn compilation buffer."
(use-local-map gotn-mode-map)
(setq truncate-lines t)
(font-lock-add-keywords nil nil))
(provide 'gotn)
;;; gotn.el ends here