Skip to content

Instantly share code, notes, and snippets.

@dinshaw
Last active September 20, 2020 23:11
Show Gist options
  • Save dinshaw/1209f97cbb6a59449fb3b3560725f12a to your computer and use it in GitHub Desktop.
Save dinshaw/1209f97cbb6a59449fb3b3560725f12a to your computer and use it in GitHub Desktop.
SublimText 3 Plugin to run specs in an external terminal window.
on execInNewTab(_title, _command)
tell application "Terminal"
activate
set currentTab to do script _command
set custom title of tab 1 of window 1 to _title
end
end execInTerminalTab
on execInTerminalTab(_command, _window, _tab)
tell application "Terminal"
activate
set frontmost of _window to true
set selected of _tab to true
do script "clear" in tab 1 of window 1
do script _command in tab 1 of window 1
end
end execInTerminalTab
on run argv
set _command to item 1 of argv
set _foundTab to false
set _expected_title to (item 2 of argv)
if length of argv is 2
tell application "Terminal"
repeat with w in windows
tell w
repeat with t in tabs
set _title to (custom title of t)
if _title = _expected_title then
set _foundTab to true
set _window to w
set _tab to t
exit repeat
end if
end repeat
end tell
if _foundTab then
exit repeat
end if
end repeat
end tell
if _foundTab then
execInTerminalTab(_command, _window, _tab)
else
execInNewTab(_expected_title, _command)
end if
end if
end run
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):
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 \"rails_helper\"")
twin_file.write("\n\nRSpec.describe " + constant_name + " do")
twin_file.write("\n\tdescribe \"\" do")
twin_file.write("\n\tend")
twin_file.write("\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:
command = "be rspec "
elif path.find("/test/") > 0:
command = "be rake test TEST="
else:
return sublime.error_message("This file does not appear to be a spec or test.")
# 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\/.*|\/test\/.*", "", path)
if single:
line_number, column = self.view.rowcol(self.view.sel()[0].begin())
line_number += 1
if path.find("/spec/") > 0:
path += ":" + str(line_number)
# if path.find("/test/") > 0:
# path += " -l " + str(line_number)
cmd = 'osascript '
cmd += '"' + sublime.packages_path() + '/User/run_command.applescript"'
cmd += ' "cd ' + root_path + ' && ' + command + path + '"'
cmd += ' "Ruby Tests"'
os.system(cmd)
@dinshaw
Copy link
Author

dinshaw commented Sep 20, 2020

The big win here is running the spec file in a external window, but there is also a 'switch to spec file' command that will create a spec for you if none exists.

To install, put both these files in:
~/Library/Application Support/Sublime Text 3/Local/Packages/User
Add the keybindings (of your choice)

    { "keys": ["super+."], "command": "open_rspec_file" },
    { "keys": ["super+shift+r"], "command": "run_tests", "args": {"single": [true|false]}  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment