diff --git a/Gemfile.lock b/Gemfile.lock index eba4c11d..e016d71d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -152,4 +152,4 @@ DEPENDENCIES test-unit BUNDLED WITH - 2.1.2 + 2.1.4 diff --git a/lib/simplecov/source_file.rb b/lib/simplecov/source_file.rb index 4815c494..4cb9f632 100644 --- a/lib/simplecov/source_file.rb +++ b/lib/simplecov/source_file.rb @@ -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 @@ -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|