From d84a0cc4d1adb8e1ecc2f0efaab56c2290880e35 Mon Sep 17 00:00:00 2001 From: slowjack2k Date: Wed, 8 Jan 2014 15:47:51 +0100 Subject: [PATCH 1/3] added pry debugger gems --- guard-rspec.gemspec | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/guard-rspec.gemspec b/guard-rspec.gemspec index 45f98448..13d5f431 100644 --- a/guard-rspec.gemspec +++ b/guard-rspec.gemspec @@ -23,4 +23,9 @@ Gem::Specification.new do |s| s.add_development_dependency 'bundler', '>= 1.3.5', '< 2.0' s.add_development_dependency 'rake', '~> 10.1' s.add_development_dependency 'launchy', '~> 2.4' + s.add_development_dependency 'pry' + s.add_development_dependency 'pry-remote' + s.add_development_dependency 'pry-stack_explorer' + s.add_development_dependency 'pry-debugger' + end From 250a568d4c6f487ecf934769b5947e544fa2a35f Mon Sep 17 00:00:00 2001 From: slowjack2k Date: Wed, 8 Jan 2014 15:49:28 +0100 Subject: [PATCH 2/3] fixed #243 make formatter rspec shared examples aware --- lib/guard/rspec/formatter.rb | 30 ++++++++++++++++++-- spec/lib/guard/rspec/formatter_spec.rb | 38 +++++++++++++++++++++----- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/lib/guard/rspec/formatter.rb b/lib/guard/rspec/formatter.rb index 331d0040..b9a4a90e 100644 --- a/lib/guard/rspec/formatter.rb +++ b/lib/guard/rspec/formatter.rb @@ -6,10 +6,29 @@ class RSpec class Formatter < ::RSpec::Core::Formatters::BaseFormatter TEMPORARY_FILE_PATH = './tmp/rspec_guard_result' + def self.extract_spec_location(metadata) + root_metadata = metadata + until spec_path?(metadata[:location]) + metadata = metadata[:example_group] + if !metadata + warn "no spec file found for #{root_metadata[:location]}" + return root_metadata[:location] + end + end + metadata[:location] + end + + def self.spec_path?(path) + flags = File::FNM_PATHNAME | File::FNM_DOTMATCH + if File.const_defined?(:FNM_EXTGLOB) # ruby >= 2 + flags |= File::FNM_EXTGLOB + end + File.fnmatch(::RSpec.configuration.pattern, path.sub(/:\d+\z/, ''), flags) + end + # Write summary to temporary file for runner def dump_summary(duration, total, failures, pending) - FileUtils.mkdir_p('tmp') - File.open(TEMPORARY_FILE_PATH, 'w') do |f| + write do |f| f.puts _message(total, failures, pending, duration) f.puts _failed_paths.join("\n") if failures > 0 end @@ -19,9 +38,14 @@ def dump_summary(duration, total, failures, pending) private + def write(&block) + FileUtils.mkdir_p('tmp') + File.open(TEMPORARY_FILE_PATH, 'w', &block) + end + def _failed_paths failed = examples.select { |e| e.execution_result[:status] == 'failed' } - failed.map { |e| e.metadata[:location] } + failed.map { |e| self.class.extract_spec_location(e.metadata) } end def _message(example_count, failure_count, pending_count, duration) diff --git a/spec/lib/guard/rspec/formatter_spec.rb b/spec/lib/guard/rspec/formatter_spec.rb index e694ca93..ad4553de 100644 --- a/spec/lib/guard/rspec/formatter_spec.rb +++ b/spec/lib/guard/rspec/formatter_spec.rb @@ -3,29 +3,54 @@ require 'guard/rspec/formatter' describe Guard::RSpec::Formatter do - let(:formatter) { Guard::RSpec::Formatter.new(StringIO.new) } + let(:writer){ + StringIO.new + } + let(:formatter) { + Guard::RSpec::Formatter.new(StringIO.new).tap{|formatter| + formatter.stub(:write) do |&block| + block.call writer + end + } + } describe '#dump_summary' do - after { File.delete('./tmp/rspec_guard_result') } + + let(:result){ + writer.rewind + writer.read + } + context 'with failures' do + let(:spec_filename){ + 'failed_location_spec.rb' + } + let(:failed_example) { double( execution_result: { status: 'failed' }, - metadata: { location: 'failed_location' } + metadata: { location: spec_filename } ) } it 'writes summary line and failed location in tmp dir' do allow(formatter).to receive(:examples) { [failed_example] } formatter.dump_summary(123, 3, 1, 0) - result = File.open('./tmp/rspec_guard_result').read - expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\nfailed_location\n$/ + expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/ end + + end + + it "should find the spec file for shared examples" do + metadata = {:location => './spec/support/breadcrumbs.rb:75', + :example_group => {:location => './spec/requests/breadcrumbs_spec.rb:218'} + } + + expect(described_class.extract_spec_location(metadata)).to eq './spec/requests/breadcrumbs_spec.rb:218' end context 'with only success' do it 'notifies success' do formatter.dump_summary(123, 3, 0, 0) - result = File.open('./tmp/rspec_guard_result').read expect(result).to match /^3 examples, 0 failures in 123\.0 seconds\n$/ end end @@ -33,7 +58,6 @@ context 'with pending' do it "notifies pending too" do formatter.dump_summary(123, 3, 0, 1) - result = File.open('./tmp/rspec_guard_result').read expect(result).to match /^3 examples, 0 failures \(1 pending\) in 123\.0 seconds\n$/ end end From cc175e174f8b622ae2f538083465c5c4d5d597f5 Mon Sep 17 00:00:00 2001 From: slowjack2k Date: Wed, 8 Jan 2014 16:32:57 +0100 Subject: [PATCH 3/3] fixed #243, filesnames and lines can be ambigous with shared example groups, so make them uniq --- lib/guard/rspec/formatter.rb | 2 +- spec/lib/guard/rspec/formatter_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/guard/rspec/formatter.rb b/lib/guard/rspec/formatter.rb index b9a4a90e..1612f4d4 100644 --- a/lib/guard/rspec/formatter.rb +++ b/lib/guard/rspec/formatter.rb @@ -45,7 +45,7 @@ def write(&block) def _failed_paths failed = examples.select { |e| e.execution_result[:status] == 'failed' } - failed.map { |e| self.class.extract_spec_location(e.metadata) } + failed.map { |e| self.class.extract_spec_location(e.metadata) }.sort.uniq end def _message(example_count, failure_count, pending_count, duration) diff --git a/spec/lib/guard/rspec/formatter_spec.rb b/spec/lib/guard/rspec/formatter_spec.rb index ad4553de..54581ee5 100644 --- a/spec/lib/guard/rspec/formatter_spec.rb +++ b/spec/lib/guard/rspec/formatter_spec.rb @@ -38,6 +38,12 @@ expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/ end + it 'writes only uniq filenames out' do + allow(formatter).to receive(:examples) { [failed_example, failed_example] } + formatter.dump_summary(123, 3, 1, 0) + expect(result).to match /^3 examples, 1 failures in 123\.0 seconds\n#{spec_filename}\n$/ + end + end it "should find the spec file for shared examples" do