Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make backtrace cleaning the responsibility of a class

Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  • Loading branch information...
commit eeafa75ca9b7f542b740eb2dd43470265f860382 1 parent 973a03b
@samphippen samphippen authored
View
22 lib/rspec/core/backtrace_cleaner.rb
@@ -0,0 +1,22 @@
+module RSpec
+ module Core
+ class BacktraceCleaner
+
+ attr_accessor :include_patterns
+ attr_accessor :discard_patterns
+
+ def initialize(include_patterns, discard_patterns)
+ @include_patterns = include_patterns
+ @discard_patterns = discard_patterns
+ end
+
+ def include?(line)
+ if @include_patterns.any? {|p| line =~ p}
+ return true
+ else
+ return not(@discard_patterns.any? {|p| line =~ p})
+ end
+ end
+ end
+ end
+end
View
5 lib/rspec/core/configuration.rb
@@ -1,4 +1,5 @@
require 'fileutils'
+require 'rspec/core/backtrace_cleaner'
require 'rspec/core/ruby_project'
module RSpec
@@ -90,6 +91,7 @@ def self.add_setting(name, opts={})
# `--backtrace` on the command line, in a `.rspec` file, or in the
# `rspec_options` attribute of RSpec's rake task.
add_setting :backtrace_clean_patterns
+ add_setting :backtrace_include_patterns
# Path to use if no path is provided to the `rspec` command (default:
# `"spec"`). Allows you to just type `rspec` instead of `rspec spec` to
@@ -202,6 +204,7 @@ def initialize
@pattern = '**/*_spec.rb'
@failure_exit_code = 1
@backtrace_clean_patterns = DEFAULT_BACKTRACE_PATTERNS.dup
+ @backtrace_include_patterns = [Regexp.new(Dir.getwd)]
@default_path = 'spec'
@filter_manager = FilterManager.new
@preferred_options = {}
@@ -282,7 +285,7 @@ def cleaned_from_backtrace?(line)
# TODO (David 2011-12-25) why are we asking the configuration to do
# stuff? Either use the patterns directly or enapsulate the filtering
# in a BacktraceCleaner object.
- backtrace_clean_patterns.any? { |regex| line =~ regex }
+ not RSpec::Core::BacktraceCleaner.new(backtrace_include_patterns, backtrace_clean_patterns).include?(line)
end
# Returns the configured mock framework adapter module
View
42 spec/rspec/core/backtrace_cleaner_spec.rb
@@ -0,0 +1,42 @@
+require "spec_helper"
+
+module RSpec::Core
+ describe BacktraceCleaner do
+ context "with no patterns" do
+ it "keeps all lines" do
+ lines = ["/tmp/a_file", "some_random_text", "hello\330\271!"]
+ cleaner = BacktraceCleaner.new([], [])
+ expect(lines.all? {|line| cleaner.include? line}).to be_true
+ end
+ end
+
+ context "with a discard pattern but no keep patterns" do
+ it "discards lines that match the discard pattern" do
+ cleaner = BacktraceCleaner.new([], [/remove/])
+ expect(cleaner.include? "remove me").to be_false
+ end
+
+ it "keeps lines that do not match the discard pattern" do
+ cleaner = BacktraceCleaner.new([], [/remove/])
+ expect(cleaner.include? "apple").to be_true
+ end
+ end
+
+ context "with a discard pattern and a keep pattern" do
+ it "discards lines that match the discard pattern but not the keep pattern" do
+ cleaner = BacktraceCleaner.new([/keep/], [/discard/])
+ expect(cleaner.include? "discard").to be_false
+ end
+
+ it "keeps lines that match the keep pattern and the discard pattern" do
+ cleaner = BacktraceCleaner.new([/hi/], [/.*/])
+ expect(cleaner.include? "hi").to be_true
+ end
+
+ it "keeps lines that do not match the keep pattern, but do not match a discard pattern" do
+ cleaner = BacktraceCleaner.new([/hi/], [/delete/])
+ expect(cleaner.include? "fish").to be_true
+ end
+ end
+ end
+end
View
4 spec/rspec/core/configuration_spec.rb
@@ -982,6 +982,10 @@ def metadata_hash(*args)
it "returns false for files in projects containing 'gems' in the name" do
expect(config.cleaned_from_backtrace?('code/my-gems-plugin/lib/plugin.rb')).to be_false
end
+
+ it "returns false for something in the current working directory" do
+ expect(config.cleaned_from_backtrace?("#{Dir.getwd}/arbitrary")).to be_false
+ end
end
describe "#debug=true" do
Please sign in to comment.
Something went wrong with that request. Please try again.