forked from CraigWilliams/TestChooser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_chooser.py
93 lines (76 loc) · 2.78 KB
/
test_chooser.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
import os, subprocess
import sublime, sublime_plugin, io
class TestChooserCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], name = ""):
self.settings = sublime.load_settings('TestChooser.sublime-settings')
search_terms = self.settings.get('search_terms')
self.terminal = self.settings.get('terminal')
self.filepath = self.window.active_view().file_name()
self.filetype = self.set_file_type()
self.testable_lines = ["Run'em All"]
if self.filetype == 'unknown':
self.exit_with_alert()
else:
line_number = 0
for line in io.open(self.filepath, 'r'):
line_number += 1
for word in search_terms:
if line.strip().startswith(word):
self.testable_lines.append("%03d:%s" % (line_number, line))
break
chosen_test = self.choose_test()
def choose_test(self):
self.window.show_quick_panel(self.testable_lines, self.chose_item)
def chose_item(self, chosen_test):
if chosen_test != -1:
formatted_line = self.format_line(self.testable_lines[chosen_test])
if self.terminal == "iTerm":
applescript = self.iterm_script()
self.execute_cmd(applescript, formatted_line)
else:
applescript = self.terminal_script()
self.execute_cmd(applescript, formatted_line)
self.activate_sublime()
def format_line(self, line):
if line == "Run'em All":
if 'rake' in self.filetype:
return "%s SPEC='%s'" % (self.filetype, self.filepath)
else:
return "%s '%s'" % (self.filetype, self.filepath)
else:
items = line.split(":")
if 'rake' in self.filetype:
return "%s SPEC='%s:%s'" % (self.filetype, self.filepath, items[0])
else:
return "%s '%s:%s'" % (self.filetype, self.filepath, items[0])
def execute_cmd(self, applescript, formatted_line):
cmd = applescript.replace("$cmd", formatted_line)
cmd = "osascript -e '%s'" % cmd
os.system(cmd)
def set_file_type(self):
if 'feature' in self.filepath:
return self.settings.get('cucumber_command')
elif 'spec' in self.filepath:
return self.settings.get('rspec_command')
else:
return 'unknown'
def exit_with_alert(self):
sublime.error_message('This is not a Cucumber or RSpec file.')
def iterm_script(self):
return """
tell application "iTerm"
tell current session of terminal 1
write text "$cmd"
end tell
end tell
"""
def terminal_script(self):
return """
tell application "Terminal"
tell window 1
do script "$cmd" in selected tab
end tell
end tell
"""
def activate_sublime(self):
subprocess.Popen("""osascript -e 'tell app "Sublime Text 2" to activate'""", shell=True)