Skip to content

Commit

Permalink
Fix encoding problem
Browse files Browse the repository at this point in the history
incompatible character encodings: ASCII-8BIT and UTF-8

Taken from simplecov-ruby/simplecov#866
  • Loading branch information
DanielePalombo committed Jul 7, 2023
1 parent 315c718 commit b12230d
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/deprecations_collector/formatters/html/formatter.rb
Expand Up @@ -54,7 +54,35 @@ 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 check_magic_comment(file, line)
# Check for encoding magic comment
# Encoding magic comment must be placed at first line except for shbang
if (match = /\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/.match(line))
file.set_encoding(match[1], "UTF-8")
end
end

def grouped(files)
Expand Down

0 comments on commit b12230d

Please sign in to comment.