Skip to content

Instantly share code, notes, and snippets.

@luislavena
Created July 24, 2010 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luislavena/488393 to your computer and use it in GitHub Desktop.
Save luislavena/488393 to your computer and use it in GitHub Desktop.
diff --git a/lib/rspec/core/formatters/base_formatter.rb b/lib/rspec/core/formatters/base_formatter.rb
index 7f2a4f8..6f8c0f6 100644
--- a/lib/rspec/core/formatters/base_formatter.rb
+++ b/lib/rspec/core/formatters/base_formatter.rb
@@ -138,11 +138,11 @@ module RSpec
def read_failed_line(exception, example)
original_file = example.file_path.to_s.downcase
- matching_line = exception.backtrace.detect { |line| line.split(':').first.downcase == original_file.downcase }
+ matching_line = exception.backtrace.detect { |line| line.match(/(.+?):(\d+)(|:\d+)/)[1].downcase == original_file.downcase }
return "Unable to find matching line from backtrace" if matching_line.nil?
- file_path, line_number = matching_line.split(':')
+ file_path, line_number = matching_line.match(/(.+?):(\d+)(|:\d+)/)[1..2]
if File.exist?(file_path)
open(file_path, 'r') { |f| f.readlines[line_number.to_i - 1] }
else
diff --git a/lib/rspec/core/metadata.rb b/lib/rspec/core/metadata.rb
index ba940e9..4f6f4f1 100644
--- a/lib/rspec/core/metadata.rb
+++ b/lib/rspec/core/metadata.rb
@@ -164,7 +164,7 @@ EOM
def file_and_line_number(metadata)
entry = candidate_entries_from_caller(metadata).first
- entry && entry.split(":")
+ entry && entry.match(/(.+?):(\d+)(|:\d+)/)[1..2]
end
def candidate_entries_from_caller(metadata)
diff --git a/spec/rspec/core/metadata_spec.rb b/spec/rspec/core/metadata_spec.rb
index 8cd3d44..fdbf9d3 100644
--- a/spec/rspec/core/metadata_spec.rb
+++ b/spec/rspec/core/metadata_spec.rb
@@ -131,6 +131,16 @@ module RSpec
])
m[:example_group][:file_path].should == __FILE__
end
+ it "finds the first spec file in the caller array with drive letter" do
+ m = Metadata.new
+ m.process(:caller => [
+ "foo",
+ "C:/path/to/file_spec.rb:#{__LINE__}",
+ "bar_spec.rb:23",
+ "baz"
+ ])
+ m[:example_group][:file_path].should == "C:/path/to/file_spec.rb"
+ end
it "is nil if there are no spec files found", :full_backtrace => true do
m = Metadata.new
m.process(:caller => [
@@ -153,6 +163,16 @@ module RSpec
])
m[:example_group][:line_number].should == __LINE__ - 4
end
+ it "finds the line number with the first spec file with drive letter" do
+ m = Metadata.new
+ m.process(:caller => [
+ "foo",
+ "C:/path/to/file_spec.rb:#{__LINE__}",
+ "bar_spec.rb:23",
+ "baz"
+ ])
+ m[:example_group][:line_number].should == __LINE__ - 4
+ end
it "uses the number after the first : for ruby 1.9" do
m = Metadata.new
m.process(:caller => [
325 examples, 0 failures, 3 pending
ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
323 examples, 9 failures, 3 pending
1) RSpec::Core::ExampleGroup #metadata adds the the file_path to metadata
Failure/Error: Unable to find C to read failed line
expected: "C:/Users/Luis/Projects/oss/rspec-dev/rspec-core/spec/rspec/core/example_group_spec.rb",
got: "C" (using ==)
# ./spec/rspec/core/example_group_spec.rb:190
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
2) RSpec::Core::ExampleGroup #metadata has a reader for file_path
Failure/Error: Unable to find C to read failed line
expected: "C:/Users/Luis/Projects/oss/rspec-dev/rspec-core/spec/rspec/core/example_group_spec.rb",
got: "C" (using ==)
# ./spec/rspec/core/example_group_spec.rb:194
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
3) RSpec::Core::ExampleGroup #metadata adds the line_number to metadata
Failure/Error: Unable to find C to read failed line
expected: 198,
got: 0 (using ==)
Diff:
@@ -1,2 +1,2 @@
-198
+0
# ./spec/rspec/core/example_group_spec.rb:198
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
4) RSpec::Core::Metadata file path finds the first spec file in the caller array
Failure/Error: Unable to find C to read failed line
expected: "C:/Users/Luis/Projects/oss/rspec-dev/rspec-core/spec/rspec/core/metadata_spec.rb",
got: "C" (using ==)
# ./spec/rspec/core/metadata_spec.rb:132
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
5) RSpec::Core::Metadata line number finds the line number with the first spec file in the backtrace
Failure/Error: Unable to find C to read failed line
expected: 150,
got: 0 (using ==)
Diff:
@@ -1,2 +1,2 @@
-150
+0
# ./spec/rspec/core/metadata_spec.rb:154
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
6) RSpec::Core::Metadata line number uses the number after the first : for ruby 1.9
Failure/Error: Unable to find C to read failed line
expected: 160,
got: 0 (using ==)
Diff:
@@ -1,2 +1,2 @@
-160
+0
# ./spec/rspec/core/metadata_spec.rb:164
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
7) RSpec::Core::Metadata metadata for example extracts file path from caller
Failure/Error: Unable to find C to read failed line
expected: "C:/Users/Luis/Projects/oss/rspec-dev/rspec-core/spec/rspec/core/metadata_spec.rb",
got: "C" (using ==)
# ./spec/rspec/core/metadata_spec.rb:191
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
8) RSpec::Core::Metadata metadata for example extracts line number from caller
Failure/Error: Unable to find C to read failed line
expected: 169,
got: 0 (using ==)
Diff:
@@ -1,2 +1,2 @@
-169
+0
# ./spec/rspec/core/metadata_spec.rb:195
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
9) RSpec::Core::Metadata metadata for example extracts location from caller
Failure/Error: Unable to find C to read failed line
expected: "C:/Users/Luis/Projects/oss/rspec-dev/rspec-core/spec/rspec/core/metadata_spec.rb:169",
got: "C:0" (using ==)
# ./spec/rspec/core/metadata_spec.rb:199
# ./spec/spec_helper.rb:70
# ./spec/spec_helper.rb:46:in `instance_eval'
# ./spec/spec_helper.rb:46:in `sandboxed'
# ./spec/spec_helper.rb:70
# ./spec/autotest/failed_results_re_spec.rb:3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment