1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/spec/warnings_spy/reader.rb
Elliot Winkler caa87ae4f0 Fix WarningsSpy to capture more lines
If you had a bunch of lines that looked like this:

    /path/to/some/file:13:in `some_method': Some error (SomeException)
    Another line for the error
        /path/to/file1.rb:44:in `some_method'
        /path/to/file2.rb:29:in `some_other_method'

The first line would not be captured and re-send to standard out. This
commit fixes that.
2014-10-07 22:07:52 -06:00

83 lines
1.6 KiB
Ruby

class WarningsSpy
class Reader
attr_reader :warning_groups
def initialize(filesystem)
@warnings_file = filesystem.warnings_file
@recording = false
@current_group = []
@warning_groups = []
end
def read
warnings_file.rewind
warnings_file.each_line do |line|
process_line(line)
end
add_group(current_group)
end
protected
attr_reader :warnings_file, :current_group
private
def process_line(line)
if backtrace_line?(line)
unless recording?
start_of_error = find_start_of_error
if start_of_error
_, start_of_error_index = start_of_error
@current_group = current_group[start_of_error_index..-1]
end
@recording = true
end
else
if recording?
add_group(current_group)
current_group.clear
end
@recording = false
end
current_group << line
end
def find_start_of_error
current_group.each_with_index.to_a.reverse.detect do |line, _|
start_of_error?(line)
end
end
def start_of_error?(line)
line =~ /^.+?:\d+:in `[^']+':/
end
def add_group(group)
unless group.empty? || group_already_added?(group)
warning_groups << group
end
end
def group_already_added?(group_to_be_added)
warning_groups.any? do |group|
group == group_to_be_added
end
end
def backtrace_line?(line)
line =~ /^\s+/
end
def recording?
@recording
end
end
end