From b4426068cf863c7a390d5e24cdeb12c2424b6c76 Mon Sep 17 00:00:00 2001 From: Jean-Michel Garnier Date: Mon, 27 Sep 2021 13:50:59 +0200 Subject: [PATCH] Fix encoding problem incompatible character encodings: ASCII-8BIT and UTF-8 Taken from https://github.com/simplecov-ruby/simplecov/pull/866 --- .../formatters/html/formatter.rb | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/reverse_coverage/formatters/html/formatter.rb b/lib/reverse_coverage/formatters/html/formatter.rb index 7a4c339..e4f188f 100644 --- a/lib/reverse_coverage/formatters/html/formatter.rb +++ b/lib/reverse_coverage/formatters/html/formatter.rb @@ -58,7 +58,27 @@ def formatted_source_file(source_file) end def readfile(source_file) - File.open(filename(source_file), "rb", &:readlines) + load_source(filename(source_file)) + end + + def load_source(file_name) + lines = [] + # The default encoding is UTF-8 + File.open(file_name, "rb:UTF-8") do |file| + line = file.gets + + # Check for shbang + if /\A#!/.match?(line) + lines << line + line = file.gets + end + return lines unless line + + check_magic_comment(file, line) + lines.concat([line], file.readlines) + end + + lines end def grouped(files)