forked from guillermooo/Vintageous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runner.py
242 lines (189 loc) · 8.38 KB
/
test_runner.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import sublime
import sublime_plugin
from itertools import chain
import io
import os
import unittest
import tempfile
# A tuple: (low level file_descriptor, path) as returned by `tempfile.mkstemp()`.
TEST_DATA_PATH = None
def make_temp_file():
"""Creates an new temporary file.
"""
global TEST_DATA_PATH
TEST_DATA_PATH = tempfile.mkstemp()
class TestsState(object):
running = False
view = None
suite = None
@staticmethod
def reset():
TestsState.view = None
TestsState.suite = None
@staticmethod
def reset_view_state():
TestsState.view.settings().set('vintage', {})
TestsState.view.sel().clear()
TestsState.view.sel().add(sublime.Region(0, 0))
TESTS_SETTINGS = 'Vintageous.tests.vi.test_settings'
TESTS_REGISTERS = 'Vintageous.tests.vi.test_registers'
TESTS_MARKS = 'Vintageous.tests.vi.test_marks'
TESTS_STATE = 'Vintageous.tests.test_state'
TESTS_KEYS = 'Vintageous.tests.vi.test_keys'
TESTS_MAPPINGS = 'Vintageous.tests.vi.test_mappings'
TESTS_TEXT_OBJECTS = 'Vintageous.tests.vi.test_text_objects'
TESTS_TEXT_OBJECTS_A_WORD = 'Vintageous.tests.vi.test_a_word'
TESTS_TEXT_OBJECTS_PARAGRAPH = 'Vintageous.tests.vi.test_find_paragraph_text_object'
TESTS_CMDS_SET_MOTION = 'Vintageous.tests.commands.test_set_motion'
TESTS_CMDS_MOTION_VI_L = 'Vintageous.tests.commands.test__vi_l'
TESTS_CMDS_MOTION_VI_H = 'Vintageous.tests.commands.test__vi_h'
TESTS_CMDS_MOTION_VI_BIG_G = 'Vintageous.tests.commands.test__vi_big_g'
TESTS_CMDS_MOTION_VI_G_G = 'Vintageous.tests.commands.test__vi_g_g'
TESTS_CMDS_MOTION_VI_DOLLAR = 'Vintageous.tests.commands.test__vi_dollar'
TESTS_CMDS_MOTION_VI_J = 'Vintageous.tests.commands.test__vi_j'
TESTS_CMDS_MOTION_VI_K = 'Vintageous.tests.commands.test__vi_k'
TESTS_CMDS_MOTION_VI_E = 'Vintageous.tests.commands.test__vi_e'
TESTS_CMDS_MOTION_VI_BIG_F = 'Vintageous.tests.commands.test__vi_big_f'
TESTS_CMDS_MOTION_VI_PERCENT = 'Vintageous.tests.commands.test__vi_percent'
TESTS_CMDS_MOTION_VI_ANTILAMBDA = 'Vintageous.tests.commands.test__vi_antilambda'
TESTS_CMDS_MOTION_VI_ZERO = 'Vintageous.tests.commands.test__vi_zero'
TESTS_CMDS_ACTION_CTRL_X = 'Vintageous.tests.commands.test__ctrl_x_and__ctrl_a'
TESTS_CMDS_ACTION_VI_S = 'Vintageous.tests.commands.test__vi_s'
TESTS_CMDS_ACTION_VI_BIG_I = 'Vintageous.tests.commands.test__vi_big_i'
TESTS_CMDS_ACTION_VI_BIG_A = 'Vintageous.tests.commands.test__vi_big_a'
TESTS_CMDS_ACTION_VI_CC = 'Vintageous.tests.commands.test__vi_cc'
TESTS_CMDS_ACTION_VI_BIG_S = 'Vintageous.tests.commands.test__vi_big_s'
TESTS_CMDS_MOTION_VI_VISUAL_O = 'Vintageous.tests.commands.test__vi_visual_o'
TESTS_CMDS_ACTION_VI_DD = 'Vintageous.tests.commands.test__vi_dd'
TESTS_CMDS_ACTION_VI_BIG_J = 'Vintageous.tests.commands.test__vi_big_j'
TESTS_EX_CMDS_COPY = 'Vintageous.tests.ex.test_copy'
TESTS_EX_CMDS_MOVE = 'Vintageous.tests.ex.test_move'
TESTS_EX_CMDS_DELETE = 'Vintageous.tests.ex.test_delete'
TESTS_EX_CMDS_SHELL_OUT = 'Vintageous.tests.ex.test_shell_out'
TESTS_UNITS_WORD = 'Vintageous.tests.vi.test_word'
TESTS_UNITS_BIG_WORD = 'Vintageous.tests.vi.test_big_word'
TESTS_UNITS_WORD_END = 'Vintageous.tests.vi.test_word_end'
TESTS_CMDS_ALL_SUPPORT = [TESTS_CMDS_SET_MOTION]
TESTS_CMDS_ALL_ACTIONS = [
TESTS_CMDS_ACTION_CTRL_X,
TESTS_CMDS_ACTION_VI_CC,
TESTS_CMDS_ACTION_VI_S,
TESTS_CMDS_ACTION_VI_BIG_I,
TESTS_CMDS_ACTION_VI_BIG_A,
TESTS_CMDS_ACTION_VI_DD,
TESTS_CMDS_ACTION_VI_BIG_J,
]
TESTS_CMDS_ALL_MOTIONS = [
TESTS_CMDS_MOTION_VI_L,
TESTS_CMDS_MOTION_VI_H,
TESTS_CMDS_MOTION_VI_BIG_G,
TESTS_CMDS_MOTION_VI_G_G,
TESTS_CMDS_MOTION_VI_DOLLAR,
TESTS_CMDS_MOTION_VI_J,
TESTS_CMDS_MOTION_VI_K,
TESTS_CMDS_MOTION_VI_E,
TESTS_CMDS_MOTION_VI_BIG_F,
TESTS_CMDS_ACTION_VI_BIG_S,
TESTS_CMDS_MOTION_VI_VISUAL_O,
TESTS_CMDS_MOTION_VI_PERCENT,
TESTS_CMDS_MOTION_VI_ANTILAMBDA,
TESTS_CMDS_MOTION_VI_ZERO,
]
TESTS_ALL_TEXT_OBJECTS = [
TESTS_TEXT_OBJECTS,
TESTS_TEXT_OBJECTS_A_WORD,
TESTS_TEXT_OBJECTS_PARAGRAPH,
]
TESTS_EX_CMDS = [
TESTS_EX_CMDS_COPY,
TESTS_EX_CMDS_MOVE,
TESTS_EX_CMDS_DELETE,
TESTS_EX_CMDS_SHELL_OUT,
]
TESTS_UNITS_ALL = [TESTS_UNITS_WORD,
TESTS_UNITS_BIG_WORD,
TESTS_UNITS_WORD_END,]
TESTS_CMDS_ALL = TESTS_CMDS_ALL_MOTIONS + TESTS_CMDS_ALL_ACTIONS + TESTS_CMDS_ALL_SUPPORT
test_suites = {
'_storage_': ['_pt_run_tests', [TESTS_MARKS,
TESTS_SETTINGS,
TESTS_REGISTERS]],
'settings': ['_pt_run_tests', [TESTS_SETTINGS]],
'registers': ['_pt_run_tests', [TESTS_REGISTERS]],
'marks': ['_pt_run_tests', [TESTS_MARKS]],
'state': ['_pt_run_tests', [TESTS_STATE]],
'keys': ['_pt_run_tests', [TESTS_KEYS, TESTS_MAPPINGS]],
'commands': ['_pt_run_tests', TESTS_CMDS_ALL],
'units': ['_pt_run_tests', TESTS_UNITS_ALL],
'ex_cmds': ['_pt_run_tests', TESTS_EX_CMDS],
# '_sel_': ['_pt_run_tests', TESTS_SEL_RELATED],
'objects': ['_pt_run_tests', TESTS_ALL_TEXT_OBJECTS],
}
# Combine all tests under one key for convenience. Ignore keys starting with an underscore. Use
# these for subsets of all the remaining tests that you don't want repeated under '_all_'.
# Convert to list so the 'chain' doesn't get exhausted after the first use.
all_tests = list(chain(*[data[1] for (key, data) in test_suites.items() if not key.startswith('_')]))
test_suites['_all_'] = ['_pt_run_tests', all_tests]
class _ptPrintResults(sublime_plugin.TextCommand):
def run(self, edit, content):
view = sublime.active_window().new_file()
view.insert(edit, 0, content)
view.set_scratch(True)
class ShowVintageousTestSuites(sublime_plugin.WindowCommand):
"""Displays a quick panel listing all available test stuites.
"""
def run(self):
TestsState.running = True
self.window.show_quick_panel(sorted(test_suites.keys()), self.run_suite)
def run_suite(self, idx):
if idx == -1:
return
suite_name = sorted(test_suites.keys())[idx]
TestsState.suite = suite_name
command_to_run, _ = test_suites[suite_name]
self.window.run_command(command_to_run, dict(suite_name=suite_name))
class _ptRunTests(sublime_plugin.WindowCommand):
def run(self, suite_name):
make_temp_file()
# We open the file here, but Sublime Text loads it asynchronously, so we continue in an
# event handler, once it's been fully loaded.
self.window.open_file(TEST_DATA_PATH[1])
class _ptTestDataDispatcher(sublime_plugin.EventListener):
def on_load(self, view):
try:
if (view.file_name() and view.file_name() == TEST_DATA_PATH[1] and
TestsState.running):
TestsState.running = False
TestsState.view = view
_, suite_names = test_suites[TestsState.suite]
suite = unittest.TestLoader().loadTestsFromNames(suite_names)
bucket = io.StringIO()
unittest.TextTestRunner(stream=bucket, verbosity=1).run(suite)
view.run_command('_pt_print_results', {'content': bucket.getvalue()})
w = sublime.active_window()
# Close data view.
w.run_command('prev_view')
TestsState.view.set_scratch(True)
w.run_command('close')
w.run_command('next_view')
# Ugly hack to return focus to the results view.
w.run_command('show_panel', {'panel': 'console', 'toggle': True})
w.run_command('show_panel', {'panel': 'console', 'toggle': True})
except Exception as e:
print(e)
finally:
try:
os.close(TEST_DATA_PATH[0])
except Exception as e:
print("Could not close temp file...")
print(e)
class WriteToBuffer(sublime_plugin.TextCommand):
"""Replaces the buffer's content with the specified `text`.
`text`: Text to be written to the buffer.
`file_name`: If this file name does not match the receiving view's, abort.
"""
def run(self, edit, file_name='', text=''):
if not file_name:
return
if self.view.file_name().lower() == file_name.lower():
self.view.replace(edit, sublime.Region(0, self.view.size()), text)