Skip to content

Commit

Permalink
Implement “Run Again” command (⌃⌘R)
Browse files Browse the repository at this point in the history
  • Loading branch information
noniq committed Aug 12, 2014
1 parent 7911439 commit 59e4139
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Commands/Run Again.tmCommand
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>saveActiveFile</string>
<key>command</key>
<string>#!/usr/bin/env bash
. "$TM_BUNDLE_SUPPORT/lib/init-command"
cat &lt;&lt;'RUBYEOF' &gt; /tmp/textmate-command-$$.rb
require ENV['TM_BUNDLE_SUPPORT'] + "/lib/rspec/mate"
RSpec::Mate::Runner.new.run_again STDOUT
RUBYEOF
${TM_RUBY:-ruby} /tmp/textmate-command-$$.rb; exit_status=$?; rm /tmp/textmate-command-$$.rb; exit $exit_status
</string>
<key>input</key>
<string>none</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^@r</string>
<key>name</key>
<string>Run Again</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>uuid</key>
<string>CB648A00-9F6C-4D9B-BB43-DADB40DE4C8E</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
16 changes: 14 additions & 2 deletions Support/lib/rspec/mate/runner.rb
Expand Up @@ -25,6 +25,10 @@ def run_last_remembered_file(stdout, options={})
run(stdout, options)
end

def run_again(stdout)
run(stdout, :run_again => true)
end

def run_focussed(stdout, options={})
options.merge!(
{
Expand All @@ -41,8 +45,12 @@ def run(stdout, options)
old_stderr = $stderr
$stderr = stderr

argv = build_argv_from_options(options)
save_as_last_run(argv)
if options.delete(:run_again)
argv = load_argv_from_last_run
else
argv = build_argv_from_options(options)
save_as_last_run(argv)
end

Dir.chdir(project_directory) do
if use_binstub?
Expand Down Expand Up @@ -127,6 +135,10 @@ def save_as_last_run(args)
end
end

def load_argv_from_last_run
YAML.load_file(LAST_RUN_CACHE)
end

def project_directory
File.expand_path(ENV['TM_PROJECT_DIRECTORY']) rescue File.dirname(single_file)
end
Expand Down
38 changes: 38 additions & 0 deletions Support/spec/rspec/mate/runner_spec.rb
Expand Up @@ -15,6 +15,10 @@
"#{File.dirname(__FILE__)}/../../../lib/rspec/mate.rb"
)

# Make sure we don’t overwrite the “real” files when running the examples here
stub_const("RSpec::Mate::Runner::LAST_RUN_CACHE", "/tmp/textmate_rspec_last_run.test.yml")
stub_const("RSpec::Mate::Runner::LAST_REMEMBERED_FILE_CACHE", "/tmp/textmate_rspec_last_remembered_file_cache.test.txt")

@spec_mate = RSpec::Mate::Runner.new
@test_runner_io = StringIO.new
end
Expand Down Expand Up @@ -123,6 +127,40 @@
end
end

describe '#run_again' do
def self.it_works_for(method, &block)
it "works for #{method}" do
original_argv, rerun_argv = nil, nil
RSpec::Core::Runner.stub(:run) do |argv, stderr, stdout|
original_argv = argv.dup
end
instance_exec(&block)
original_argv.should_not be_nil
RSpec::Core::Runner.stub(:run) do |argv, stderr, stdout|
rerun_argv = argv.dup
end
@spec_mate.run_again(@test_runner_io)
rerun_argv.should eq original_argv
end
end

it_works_for '#run_file' do
ENV['TM_FILEPATH'] = fixtures_path('example_failing_spec.rb')
@spec_mate.run_file(@test_runner_io)
end

it_works_for '#run_files' do
ENV['TM_SELECTED_FILES'] = "foo/bar_spec.rb baz/baz/baz/baz_spec.rb"
@spec_mate.run_files(@test_runner_io)
end

it_works_for '#run_focused' do
ENV['TM_FILEPATH'] = fixtures_path('example_failing_spec.rb')
ENV['TM_LINE_NUMBER'] = '4'
@spec_mate.run_focussed(@test_runner_io)
end
end

describe "#run_focused" do
it "runs first spec when file and line 4 specified" do
ENV['TM_FILEPATH'] = fixtures_path('example_failing_spec.rb')
Expand Down
1 change: 1 addition & 0 deletions info.plist
Expand Up @@ -18,6 +18,7 @@
<string>240FDD9D-CD49-4917-9003-80A3E1DADFBB</string>
<string>B5906021-8E54-4863-A13B-EA46333DB5F2</string>
<string>67E726E0-63E2-4840-B8BB-37F665CD34B9</string>
<string>CB648A00-9F6C-4D9B-BB43-DADB40DE4C8E</string>
<string>------------------------------------</string>
<string>651EC6EE-6034-4090-88FD-BC06D36190A8</string>
<string>99ED184B-CA5F-41A8-AFA4-57640475185D</string>
Expand Down

0 comments on commit 59e4139

Please sign in to comment.