Skip to content

Commit

Permalink
Make sure encodings are in order in core
Browse files Browse the repository at this point in the history
Having it split up over to simplecov-html and part in "core"
feels weird.
  • Loading branch information
PragTob committed Feb 23, 2020
1 parent 8918161 commit 0082b3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ DEPENDENCIES
test-unit

BUNDLED WITH
2.1.2
2.1.4
11 changes: 10 additions & 1 deletion lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def read_lines(file, lines, current_line)
return lines unless current_line

set_encoding_based_on_magic_comment(file, current_line)
lines.concat([current_line], file.readlines)
lines.concat([current_line], ensure_remove_undefs(file.readlines))
end

RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX = /\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/.freeze
Expand All @@ -211,6 +211,15 @@ def set_encoding_based_on_magic_comment(file, line)
end
end

def ensure_remove_undefs(file_lines)
# invalid/undef replace are technically not really necessary but nice to
# have and work around a JRuby incompatibility. Also moved here from
# simplecov-html to have encoding shenaningans in one place. See #866
# also setting these option on `file.set_encoding` doesn't seem to work
# properly so it has to be done here.
file_lines.each { |line| line.encode!("UTF-8", invalid: :replace, undef: :replace) }
end

def build_lines
coverage_exceeding_source_warn if coverage_data["lines"].size > src.size
lines = src.map.with_index(1) do |src, i|
Expand Down

0 comments on commit 0082b3f

Please sign in to comment.