Skip to content

Commit

Permalink
Refactor code to ignore invalid byte sequences.
Browse files Browse the repository at this point in the history
The previous implementation made the `classify` method too long for
Rubocop. It also was ignoring invalid byte sequences in only one of the
two places where we loop through the lines to look for :nocov:.
  • Loading branch information
BMorearty committed Mar 10, 2018
1 parent 67bd66a commit e26ae2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions lib/simplecov/lines_classifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@ def self.no_cov_line
@no_cov_line ||= /^(\s*)#(\s*)(\:#{SimpleCov.nocov_token}\:)/
end

def self.no_cov_line?(line)
line =~ no_cov_line
rescue ArgumentError
# E.g., line contains an invalid byte sequence in UTF-8
false
end

def self.whitespace_line?(line)
line =~ WHITESPACE_OR_COMMENT_LINE
rescue ArgumentError
# E.g., line contains an invalid byte sequence in UTF-8
false
end

def classify(lines)
skipping = false

lines.map do |line|
begin
if line =~ self.class.no_cov_line
skipping = !skipping
NOT_RELEVANT
elsif skipping || line =~ WHITESPACE_OR_COMMENT_LINE
NOT_RELEVANT
else
RELEVANT
end
rescue ArgumentError
# E.g., line contains an invalid byte sequence in UTF-8
if self.class.no_cov_line?(line)
skipping = !skipping
NOT_RELEVANT
elsif skipping || self.class.whitespace_line?(line)
NOT_RELEVANT
else
RELEVANT
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def process_skipped_lines(lines)
skipping = false

lines.each do |line|
if line.src =~ SimpleCov::LinesClassifier.no_cov_line
if SimpleCov::LinesClassifier.no_cov_line?(line.src)
skipping = !skipping
line.skipped!
elsif skipping
Expand Down

0 comments on commit e26ae2f

Please sign in to comment.