Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #843 from samphippen/backtrace-cleaner
Backtrace cleaner
- Loading branch information
Showing
7 changed files
with
202 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module RSpec | ||
module Core | ||
class BacktraceCleaner | ||
|
||
DEFAULT_EXCLUSION_PATTERNS = [ | ||
/\/lib\d*\/ruby\//, | ||
/org\/jruby\//, | ||
/bin\//, | ||
%r|/gems/|, | ||
/spec\/spec_helper\.rb/, | ||
/lib\/rspec\/(core|expectations|matchers|mocks)/ | ||
] | ||
|
||
attr_accessor :inclusion_patterns | ||
attr_accessor :exclusion_patterns | ||
|
||
def initialize(inclusion_patterns=nil, exclusion_patterns=DEFAULT_EXCLUSION_PATTERNS.dup) | ||
@exclusion_patterns = exclusion_patterns | ||
|
||
if inclusion_patterns.nil? | ||
@inclusion_patterns = (matches_an_exclusion_pattern? Dir.getwd) ? [Regexp.new(Dir.getwd)] : [] | ||
else | ||
@inclusion_patterns = inclusion_patterns | ||
end | ||
end | ||
|
||
def exclude?(line) | ||
@inclusion_patterns.none? {|p| line =~ p} and matches_an_exclusion_pattern?(line) | ||
end | ||
|
||
def full_backtrace=(true_or_false) | ||
@exclusion_patterns = true_or_false ? [] : DEFAULT_EXCLUSION_PATTERNS.dup | ||
end | ||
|
||
private | ||
|
||
def matches_an_exclusion_pattern?(line) | ||
@exclusion_patterns.any? {|p| line =~ p} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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.exclude? line}).to be_false | ||
end | ||
end | ||
|
||
context "with an exclusion pattern but no inclusion patterns" do | ||
it "excludes lines that match the exclusion pattern" do | ||
cleaner = BacktraceCleaner.new([], [/remove/]) | ||
expect(cleaner.exclude? "remove me").to be_true | ||
end | ||
|
||
it "keeps lines that do not match the exclusion pattern" do | ||
cleaner = BacktraceCleaner.new([], [/remove/]) | ||
expect(cleaner.exclude? "apple").to be_false | ||
end | ||
end | ||
|
||
context "with an exclusion pattern and an inclusion pattern" do | ||
it "excludes lines that match the exclusion pattern but not the inclusion pattern" do | ||
cleaner = BacktraceCleaner.new([/keep/], [/discard/]) | ||
expect(cleaner.exclude? "discard").to be_true | ||
end | ||
|
||
it "keeps lines that match the inclusion pattern and the exclusion pattern" do | ||
cleaner = BacktraceCleaner.new([/hi/], [/.*/]) | ||
expect(cleaner.exclude? "hi").to be_false | ||
end | ||
|
||
it "keeps lines that match neither pattern" do | ||
cleaner = BacktraceCleaner.new([/hi/], [/delete/]) | ||
expect(cleaner.exclude? "fish").to be_false | ||
end | ||
end | ||
|
||
context "with an exclusion pattern that matches the current working directory" do | ||
it "defaults to having one inclusion pattern, the current working directory" do | ||
cleaner = BacktraceCleaner.new(nil, [/.*/]) | ||
expect(Dir.getwd =~ cleaner.inclusion_patterns.first).to be_true | ||
end | ||
end | ||
|
||
context "with an exclusion pattern that does not match the current working directory" do | ||
it "defaults to having no exclusion patterns" do | ||
cleaner = BacktraceCleaner.new(nil, [/i_wont_match_a_directory/]) | ||
expect(cleaner.inclusion_patterns.length).to be_zero | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters