forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstest.py
93 lines (75 loc) · 2.2 KB
/
gstest.py
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
from gosubl import gs
from gosubl import mg9
import os
import re
import sublime
import sublime_plugin
DOMAIN = 'GsTest'
TEST_PAT = re.compile(r'^((Test|Example|Benchmark)\w*)')
class GsTestCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return gs.is_go_source_view(self.window.active_view())
def run(self):
def f(res, err):
if err:
gs.notify(DOMAIN, err)
return
mats = {}
args = {}
decls = res.get('file_decls', [])
decls.extend(res.get('pkg_decls', []))
for d in decls:
name = d['name']
prefix, _ = match_prefix_name(name)
kind = d['kind'].lstrip('+- ')
if prefix and kind == 'func' and d['repr'] == '':
mats[prefix] = True
args[name] = name
names = sorted(args.keys())
ents = ['Run all tests and examples']
for k in ['Test', 'Benchmark', 'Example']:
if mats.get(k):
s = 'Run %ss Only' % k
ents.append(s)
if k == 'Benchmark':
args[s] = ['-test.run=none', '-test.bench="%s.*"' % k]
else:
args[s] = ['-test.run="%s.*"' % k]
for k in names:
ents.append(k)
if k.startswith('Benchmark'):
args[k] = ['-test.run=none', '-test.bench="^%s$"' % k]
else:
args[k] = ['-test.run="^%s$"' % k]
def cb(i, win):
if i >= 0:
a = args.get(ents[i], [])
win.active_view().run_command('gs9o_open', {'run': gs.lst('go', 'test', a)})
gs.show_quick_panel(ents, cb)
win, view = gs.win_view(None, self.window)
if view is None:
return
vfn = gs.view_fn(view)
src = gs.view_src(view)
pkg_dir = ''
if view.file_name():
pkg_dir = os.path.dirname(view.file_name())
mg9.declarations(vfn, src, pkg_dir, f)
def match_prefix_name(s):
m = TEST_PAT.match(s)
return (m.group(2), m.group(1)) if m else ('', '')
def handle_action(view, action):
fn = view.file_name()
prefix, name = match_prefix_name(view.substr(view.word(gs.sel(view))))
ok = prefix and fn and fn.endswith('_test.go')
if ok:
if action == 'right-click':
pat = '^%s.*' % prefix
else:
pat = '^%s$' % name
if prefix == 'Benchmark':
cmd = ['go', 'test', '-test.run=none', '-test.bench="%s"' % pat]
else:
cmd = ['go', 'test', '-test.run="%s"' % pat]
view.run_command('gs9o_open', {'run': cmd})
return ok