Skip to content

Commit

Permalink
Merge pull request #669 from BMorearty/ignore-invalid-byte-sequences
Browse files Browse the repository at this point in the history
Ignore invalid byte sequences
  • Loading branch information
PragTob committed Mar 11, 2018
2 parents fbd3c3e + e26ae2f commit 449d293
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ unreleased

* (breaking) Stop handling string filters as regular expressions, use the dedicated regex filter if you need that behaviour. See [#616](https://github.com/colszowka/simplecov/pull/616) (thanks @yujinakayama)
* Avoid overwriting the last coverage results on unsuccessful test runs. See [#625](https://github.com/colszowka/simplecov/pull/625) (thanks @thomas07vt)
* Don't crash on invalid UTF-8 byte sequences.

0.15.1 (2017-09-11) ([changes](https://github.com/colszowka/simplecov/compare/v0.15.0...v0.15.1))
=======
Expand Down
18 changes: 16 additions & 2 deletions lib/simplecov/lines_classifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@ def self.no_cov_line
/^(\s*)#(\s*)(\:#{SimpleCov.nocov_token}\:)/o
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|
if line =~ self.class.no_cov_line
if self.class.no_cov_line?(line)
skipping = !skipping
NOT_RELEVANT
elsif skipping || line =~ WHITESPACE_OR_COMMENT_LINE
elsif skipping || self.class.whitespace_line?(line)
NOT_RELEVANT
else
RELEVANT
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
9 changes: 9 additions & 0 deletions spec/lines_classifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
expect(classified_lines.length).to eq 7
expect(classified_lines).to all be_relevant
end

it "determines invalid UTF-8 byte sequences as relevant" do
classified_lines = subject.classify [
"bytes = \"\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\"",
]

expect(classified_lines.length).to eq 1
expect(classified_lines).to all be_relevant
end
end

describe "not-relevant lines" do
Expand Down

0 comments on commit 449d293

Please sign in to comment.