forked from brynary/sublime-text-2-user-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rspec.py
87 lines (71 loc) · 2.63 KB
/
rspec.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
import sublime
import sublime_plugin
import os, errno
import re
def get_twin_path(path):
spec_file = path.find("/spec/") >= 0
if spec_file:
if path.find("/lib/") > 0:
return path.replace("/spec/lib/","/lib/").replace("_spec.rb", ".rb")
else:
return path.replace("/spec/","/app/").replace("_spec.rb", ".rb")
else:
if path.find("/lib/") > 0:
return path.replace("/lib/", "/spec/lib/").replace(".rb", "_spec.rb")
else:
return path.replace("/app/", "/spec/").replace(".rb", "_spec.rb")
class OpenRspecFileCommand(sublime_plugin.WindowCommand):
def run(self, option):
self.views = []
window = self.window
current_file_path = self.window.active_view().file_name()
spec_file = current_file_path.find("/spec/") > 0
twin_path = get_twin_path(current_file_path)
path_parts = twin_path.split("/")
dirname = "/".join(path_parts[0:-1])
basename = path_parts[-1]
if not os.path.exists(twin_path) and sublime.ok_cancel_dialog(basename + " was not found. Create it?"):
self.mkdir_p(dirname)
twin_file = open(twin_path, "w")
constant_name = self.camelize(basename.replace(".rb", "").replace("_spec", ""))
if spec_file:
twin_file.write("class " + constant_name + "\nend")
else:
twin_file.write("require \"spec_helper\"\n\ndescribe " + constant_name + " do\nend")
twin_file.close()
if os.path.exists(twin_path):
view = window.open_file(twin_path)
view.run_command("revert")
else:
sublime.status_message("Not found: " + twin_path)
def mkdir_p(self, path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
pass
else: raise
def camelize(self, string):
return re.sub(r"(?:^|_)(.)", lambda x: x.group(0)[-1].upper(), string)
class RunTests(sublime_plugin.TextCommand):
def run(self, edit, single):
path = self.view.file_name()
if path.find("/spec/") < 0:
twin_path = get_twin_path(path)
if os.path.exists(twin_path):
path = twin_path
else:
return sublime.error_message("You're not in a spec, bro.")
root_path = re.sub("\/spec\/.*", "", path)
if single:
line_number, column = self.view.rowcol(self.view.sel()[0].begin())
line_number += 1
path += ":" + str(line_number)
command = "bundle exec rspec"
else:
command = "bundle exec rspec"
cmd = 'osascript '
cmd += '"' + sublime.packages_path() + '/User/run_command.applescript"'
cmd += ' "cd ' + root_path + ' && ' + command + ' ' + path + '"'
cmd += ' "Ruby Tests"'
os.system(cmd)