Skip to content

Commit 3b377b7

Browse files
Merge pull request #28 from jmgarnier/master
Make gem work for projects with Ruby >= 2.7 and source code with UTF-8
2 parents df4ca35 + 360357f commit 3b377b7

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/reverse_coverage/formatters/html/formatter.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,42 @@ def formatted_source_file(source_file)
5858
end
5959

6060
def readfile(source_file)
61-
File.open(filename(source_file), "rb", &:readlines)
61+
load_source(filename(source_file))
62+
end
63+
64+
def load_source(file_name)
65+
lines = []
66+
# The default encoding is UTF-8
67+
File.open(file_name, "rb:UTF-8") do |file|
68+
line = file.gets
69+
70+
# Check for shbang
71+
if /\A#!/.match?(line)
72+
lines << line
73+
line = file.gets
74+
end
75+
return lines unless line
76+
77+
check_magic_comment(file, line)
78+
lines.concat([line], file.readlines)
79+
end
80+
81+
lines
82+
end
83+
84+
def check_magic_comment(file, line)
85+
# Check for encoding magic comment
86+
# Encoding magic comment must be placed at first line except for shbang
87+
if (match = /\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/.match(line))
88+
file.set_encoding(match[1], "UTF-8")
89+
end
6290
end
6391

6492
def grouped(files)
6593
grouped = {}
6694
grouped_files = []
6795

96+
# TODO Someday / Maybe. Make this list configurable
6897
groups = {
6998
'Controllers' => %r{/app/controllers},
7099
'Channels' => %r{/app/channels},

lib/reverse_coverage/main.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ def add(example)
2929
lines.each_with_index do |changed, line_index|
3030
next if changed.nil? || changed.zero?
3131

32-
file_info = { file_path: file_path, line_index: line_index }
33-
34-
save_changes(changes, example_data, file_info)
35-
save_changes(coverage_matrix, example_data, file_info)
32+
save_changes(changes, example_data, file_path: file_path, line_index: line_index)
33+
save_changes(coverage_matrix, example_data, file_path: file_path, line_index: line_index)
3634
end
3735
end
3836

0 commit comments

Comments
 (0)