From df2ec700637f4da7c10a69030fe8f072b58564c7 Mon Sep 17 00:00:00 2001 From: uk-ar Date: Tue, 16 Dec 2014 01:32:17 +0900 Subject: [PATCH] Enable to open rspec result from emacs --- README.md | 12 ++++++++++++ lib/guard/rspec/options.rb | 1 + lib/guard/rspec/runner.rb | 20 ++++++++++++++++++++ spec/lib/guard/rspec/runner_spec.rb | 14 ++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/README.md b/README.md index a108cb64..1cd586a9 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ failed_mode: :focus # What to do with failed specs all_after_pass: true # Run all specs after changed specs pass, default: false all_on_start: true # Run all the specs at startup, default: false launchy: nil # Pass a path to an rspec results file, e.g. ./tmp/spec_results.html +emacs: nil # Pass a path to an rspec results file, e.g. ./tmp/spec_results.txt notification: false # Display notification after the specs are done running, default: true run_all: { cmd: 'custom rspec command', message: 'custom message' } # Custom options to use when running all specs title: 'My project' # Display a custom title for the notification, default: 'RSpec results' @@ -108,6 +109,17 @@ guard :rspec, cmd: 'rspec -f html -o ./tmp/spec_results.html', launchy: './tmp/s end ``` +### Using Emacs to view rspec results + +guard-rspec can be configured to launch a results file in lieu of outputing rspec results to the terminal. +Configure your Guardfile with the emacs option: + +``` ruby +guard :rspec, cmd: 'rspec -o ./spec_results.txt', emacs: './spec_results.txt' do + # ... +end +``` + ## Development * Documentation hosted at [RubyDoc](http://rubydoc.info/github/guard/guard-rspec/master/frames). diff --git a/lib/guard/rspec/options.rb b/lib/guard/rspec/options.rb index a1da6cee..a8cffcde 100644 --- a/lib/guard/rspec/options.rb +++ b/lib/guard/rspec/options.rb @@ -10,6 +10,7 @@ module Options cmd: nil, cmd_additional_args: nil, launchy: nil, + emacs: nil, notification: true, title: "RSpec results" } diff --git a/lib/guard/rspec/runner.rb b/lib/guard/rspec/runner.rb index f481a1d3..fc2df448 100644 --- a/lib/guard/rspec/runner.rb +++ b/lib/guard/rspec/runner.rb @@ -86,6 +86,25 @@ def _open_launchy ::Launchy.open(options[:launchy]) if pn.exist? end + def _open_emacs(failed_paths) + return unless options[:emacs] + pn = Pathname.new(options[:emacs]) + elisp = <<-"EOS".gsub(/\s+/, " ").strip + (display-buffer + (save-excursion + (with-current-buffer (find-file-noselect \"#{pn}\" t) + (revert-buffer t t) + (ansi-color-apply-on-region (point-min)(point-max)) + (set-buffer-modified-p nil) + (compilation-mode t) + (current-buffer)))) + EOS + IO.popen(["emacsclient", "--eval", elisp]) do |p| + p.readlines + p.close + end if pn.exist? && !failed_paths.empty? + end + def _run_all_after_pass return unless options[:all_after_pass] run_all @@ -105,6 +124,7 @@ def _process_run_result(result, all) inspector.failed(failed_paths) notifier.notify(summary) _open_launchy + _open_emacs(failed_paths) _run_all_after_pass if !all && result end diff --git a/spec/lib/guard/rspec/runner_spec.rb b/spec/lib/guard/rspec/runner_spec.rb index 1f729907..811e95b8 100644 --- a/spec/lib/guard/rspec/runner_spec.rb +++ b/spec/lib/guard/rspec/runner_spec.rb @@ -210,6 +210,20 @@ runner.run(paths) end + context "with emacs option" do + let(:options) { { cmd: "rspec", emacs: "emacs_path" } } + + before do + allow(Pathname).to receive(:new). + with("emacs_path") { double(exist?: true) } + end + + it "opens emacs" do + expect(IO).to receive(:popen). + with(array_including("emacsclient", "--eval")) + runner.run(paths) + end + end end it "notifies success" do