-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_this_test.py
69 lines (56 loc) · 2.04 KB
/
run_this_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
import os, subprocess
import sublime, sublime_plugin, io
class RunThisTestCommand(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()
if self.filetype == 'unknown':
self.exit_with_alert()
else:
line_number = self.window.active_view().rowcol(self.window.active_view().sel()[0].begin())[0]+1
self.run_test(line_number)
def run_test(self, line):
formatted_line = self.format_line(line)
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):
return "%s %s:%s" % (self.filetype, self.filepath, line)
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)