-
Notifications
You must be signed in to change notification settings - Fork 119
/
run_ruby_test.py
319 lines (270 loc) · 12.7 KB
/
run_ruby_test.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import re
import functools
import sublime
import string
import sublime_plugin
class ShowInPanel:
def __init__(self, window):
self.window = window
def display_results(self):
self.panel = self.window.get_output_panel("exec")
self.window.run_command("show_panel", {"panel": "output.exec"})
self.panel.settings().set("color_scheme", "Packages/RubyTest/TestConsole.tmTheme")
class ShowInScratch:
def __init__(self, window):
self.window = window
self.active_for = 0
self.copied_until = 0
def display_results(self):
self.panel = self.window.get_output_panel("exec")
self.window.run_command("hide_panel")
self.view = self.window.new_file()
self.view.set_scratch(True)
self.view.set_name("Test Results")
self.view.settings().set("color_scheme", "Packages/RubyTest/TestConsole.tmTheme")
self.view.set_read_only(True)
self.poll_copy()
def poll_copy(self):
# FIXME HACK: Stop polling after one minute
if self.active_for < 60000:
self.active_for += 50
sublime.set_timeout(self.copy_stuff, 50)
def copy_stuff(self):
size = self.panel.size()
content = self.panel.substr(sublime.Region(self.copied_until, size))
if content:
self.copied_until = size
self.view.set_read_only(False)
edit = self.view.begin_edit()
self.view.insert(edit, self.view.size(), content)
self.view.end_edit(edit)
self.view.set_read_only(True)
self.poll_copy()
class TestMethodMatcher(object):
def __init__(self):
self.matchers = [TestMethodMatcher.UnitTest, TestMethodMatcher.ShouldaTest]
def find_first_match_in(self, test_file_content):
for matcher in self.matchers:
test_name = matcher.find_first_match(test_file_content)
if test_name:
return test_name
class UnitTest(object):
@staticmethod
def find_first_match(test_file_content):
match_obj = re.search('\s?([a-zA-Z_\d]+tset)\s+fed', test_file_content) # 1st search for 'def test_name'
if match_obj:
return match_obj.group(1)[::-1]
match_obj = re.search('\s?[\"\']([a-zA-Z_\"\'\s\d]+)[\"\']\s+tset', test_file_content) # 2nd search for 'test "name"'
if match_obj:
test_name = match_obj.group(1)[::-1]
return "test_%s" % test_name.replace("\"", "\\\"").replace(" ", "_").replace("'", "\\'")
return None
class ShouldaTest(object):
@staticmethod
def find_first_match(test_file_content):
match_obj = re.search('\s?(([\"][^\"]*[\"]|[\'][^\']*[\'])\s+dluohs)', test_file_content) # search for 'should "name"'
if not match_obj:
return None
test_name = match_obj.group(1)[::-1]
return "%s%s%s" % ("/", test_name.replace("should", "").strip(), "/")
class BaseRubyTask(sublime_plugin.TextCommand):
def load_config(self):
s = sublime.load_settings("RubyTest.sublime-settings")
global RUBY_UNIT; RUBY_UNIT = s.get("ruby_unit_exec").split()
global ERB_EXEC; ERB_EXEC = s.get("erb_exec").split()
global CUCUMBER_UNIT; CUCUMBER_UNIT = s.get("ruby_cucumber_exec").split()
global RSPEC_UNIT; RSPEC_UNIT = s.get("ruby_rspec_exec").split()
global RUBY_UNIT_FOLDER; RUBY_UNIT_FOLDER = s.get("ruby_unit_folder")
global CUCUMBER_UNIT_FOLDER; CUCUMBER_UNIT_FOLDER = s.get("ruby_cucumber_folder")
global RSPEC_UNIT_FOLDER; RSPEC_UNIT_FOLDER = s.get("ruby_rspec_folder")
global USE_SCRATCH; USE_SCRATCH = s.get("ruby_use_scratch")
if s.get("save_on_run"):
self.window().run_command("save_all")
def save_test_run(self, command, working_dir):
s = sublime.load_settings("RubyTest.last-run")
s.set("last_test_run", string.join(command))
s.set("last_test_working_dir", working_dir)
sublime.save_settings("RubyTest.last-run")
def run_shell_command(self, command, working_dir):
if not command:
return False
self.save_test_run(command, working_dir)
self.view.window().run_command("exec", {
"cmd": command,
"working_dir": working_dir,
"file_regex": r"([^ ]*\.rb):?(\d*)"
})
self.display_results()
return True
def display_results(self):
display = ShowInScratch(self.window()) if USE_SCRATCH else ShowInPanel(self.window())
display.display_results()
def window(self):
return self.view.window()
class BaseFile(object):
def __init__(self, file_name):
self.folder_name, self.file_name = os.path.split(file_name)
self.absolute_path = file_name
def verify_syntax_command(self): return None
def possible_alternate_files(self): return []
def run_all_tests_command(self): return None
def get_project_root(self): return None
def find_project_root(self, partition_folder):
to_find = os.sep + partition_folder + os.sep
project_root, _, _ = self.absolute_path.partition(to_find)
return project_root
def relative_file_path(self, partition_folder):
to_find = os.sep + partition_folder + os.sep
_, _, relative_path = self.absolute_path.partition(to_find)
return partition_folder + os.sep + relative_path
def get_current_line_number(self, view):
char_under_cursor = view.sel()[0].a
return view.rowcol(char_under_cursor)[0] + 1
def features(self): return []
class AnonymousFile(BaseFile):
def __init__(self):
True
class RubyFile(BaseFile):
def verify_syntax_command(self): return RUBY_UNIT + ["-c", self.absolute_path]
def possible_alternate_files(self): return [self.file_name.replace(".rb", "_spec.rb"), self.file_name.replace(".rb", "_test.rb"), self.file_name.replace(".rb", ".feature")]
def features(self): return ["verify_syntax", "switch_to_test", "rails_generate", "extract_variable"]
class UnitFile(RubyFile):
def possible_alternate_files(self): return [self.file_name.replace("_test.rb", ".rb")]
def run_all_tests_command(self): return RUBY_UNIT + ["-Itest", self.relative_file_path(RUBY_UNIT_FOLDER)]
def run_single_test_command(self, view):
region = view.sel()[0]
line_region = view.line(region)
text_string = view.substr(sublime.Region(region.begin() - 2000, line_region.end()))
text_string = text_string.replace("\n", "\\N")
text_string = text_string[::-1]
test_name = TestMethodMatcher().find_first_match_in(text_string)
if test_name is None:
sublime.error_message("No test name!")
return
return RUBY_UNIT + ["-Itest", self.relative_file_path(RUBY_UNIT_FOLDER), "-n", test_name]
def features(self): return super(BaseRubyTask.UnitFile, self).features() + ["run_test"]
def get_project_root(self): return self.find_project_root(RUBY_UNIT_FOLDER)
class CucumberFile(BaseFile):
def possible_alternate_files(self): return [self.file_name.replace(".feature", ".rb")]
def run_all_tests_command(self): return CUCUMBER_UNIT + [self.relative_file_path(CUCUMBER_UNIT_FOLDER)]
def run_single_test_command(self, view): return CUCUMBER_UNIT + [self.relative_file_path(CUCUMBER_UNIT_FOLDER), "-l" + str(self.get_current_line_number(view))]
def features(self): return ["run_test"]
def get_project_root(self): return self.find_project_root(CUCUMBER_UNIT_FOLDER)
class RSpecFile(RubyFile):
def possible_alternate_files(self): return [self.file_name.replace("_spec.rb", ".rb")]
def run_all_tests_command(self): return RSPEC_UNIT + [self.relative_file_path(RSPEC_UNIT_FOLDER)]
def run_single_test_command(self, view): return RSPEC_UNIT + [self.relative_file_path(RSPEC_UNIT_FOLDER), "-l" + str(self.get_current_line_number(view))]
def features(self): return super(BaseRubyTask.RSpecFile, self).features() + ["run_test"]
def get_project_root(self): return self.find_project_root(RSPEC_UNIT_FOLDER)
class ErbFile(BaseFile):
def verify_syntax_command(self): return ["sh", "-c", ERB_EXEC.join(" ") + " -xT - " + self.file_name + " | " + RUBY_UNIT + " -c"]
def can_verify_syntax(self): return True
def features(self): return ["verify_syntax"]
def file_type(self, file_name = None):
file_name = file_name or self.view.file_name()
if not file_name: return BaseRubyTask.AnonymousFile()
if re.search('\w+\_test.rb', file_name):
return BaseRubyTask.UnitFile(file_name)
elif re.search('\w+\_spec.rb', file_name):
return BaseRubyTask.RSpecFile(file_name)
elif re.search('\w+\.feature', file_name):
return BaseRubyTask.CucumberFile(file_name)
elif re.search('\w+\.rb', file_name):
return BaseRubyTask.RubyFile(file_name)
elif re.search('\w+\.erb', file_name):
return BaseRubyTask.ErbFile(file_name)
else:
return BaseRubyTask.BaseFile(file_name)
class RunSingleRubyTest(BaseRubyTask):
def is_enabled(self): return 'run_test' in self.file_type().features()
def run(self, args):
self.load_config()
file = self.file_type()
command = file.run_single_test_command(self.view)
self.run_shell_command(command, file.get_project_root())
class RunAllRubyTest(BaseRubyTask):
def is_enabled(self): return 'run_test' in self.file_type().features()
def run(self, args):
self.load_config()
file = self.file_type(self.view.file_name())
command = file.run_all_tests_command()
if self.run_shell_command(command, file.get_project_root()):
pass
else:
sublime.error_message("Only *_test.rb, *_spec.rb, *.feature files supported!")
class RunLastRubyTest(BaseRubyTask):
def load_last_run(self):
self.load_config()
s = sublime.load_settings("RubyTest.last-run")
return (s.get("last_test_run").split(), s.get("last_test_working_dir"))
def run(self, args):
last_command, working_dir = self.load_last_run()
self.run_shell_command(last_command, working_dir)
class VerifyRubyFile(BaseRubyTask):
def is_enabled(self): return 'verify_syntax' in self.file_type().features()
def run(self, args):
self.load_config()
file = self.file_type()
command = file.verify_syntax_command()
if self.run_shell_command(command, file.get_project_root()):
pass
else:
sublime.error_message("Only .rb or .erb files supported!")
class SwitchBetweenCodeAndTest(BaseRubyTask):
def is_enabled(self): return 'switch_to_test' in self.file_type().features()
def run(self, args, split_view):
possible_alternates = self.file_type().possible_alternate_files()
alternates = self.project_files(lambda file: file in possible_alternates)
if alternates:
if split_view:
self.window().run_command('set_layout', {
"cols": [0.0, 0.5, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
})
self.window().focus_group(1)
if len(alternates) == 1:
self.window().open_file(alternates.pop())
else:
callback = functools.partial(self.on_selected, alternates)
self.window().show_quick_panel(alternates, callback)
else:
sublime.error_message("could not find " + str(possible_alternates))
def on_selected(self, alternates, index):
if index == -1:
return
self.window().open_file(alternates[index])
def walk(self, directory, ignored_directories = []):
ignored_directories = ['.git', 'vendor'] # Move this into config
for dir, dirnames, files in os.walk(directory):
dirnames[:] = [dirname for dirname in dirnames if dirname not in ignored_directories]
yield dir, dirnames, files
def project_files(self, file_matcher):
directories = self.window().folders()
return [os.path.join(dirname, file) for directory in directories for dirname, _, files in self.walk(directory) for file in filter(file_matcher, files)]
class RubyRailsGenerate(BaseRubyTask):
def is_enabled(self): return 'rails_generate' in self.file_type().features()
def run(self, args, type = "migration"):
self.window().show_input_panel("rails generate", type + " ", lambda s: self.generate(s), None, None)
def generate(self, argument):
command = ['rails', 'generate'] + argument.split()
self.run_shell_command(command, self.window().folders()[0])
class ShowTestPanel(BaseRubyTask):
def run(self, args):
self.window().run_command("show_panel", {"panel": "output.exec"})
class RubyExtractVariable(BaseRubyTask):
def is_enabled(self): return 'extract_variable' in self.file_type().features()
def run(self, args):
for selection in self.view.sel():
self.window().show_input_panel("Variable Name: ", '', lambda name: self.generate(selection, name), None, None)
def generate(self, selection, name):
extracted = self.view.substr(selection)
line = self.view.line(selection)
white_space = re.match("\s*", self.view.substr(line)).group()
edit = self.view.begin_edit()
try:
self.view.replace(edit, selection, name)
self.view.insert(edit, line.begin(), white_space + name + " = " + extracted + "\n")
finally:
self.view.end_edit(edit)