Module: SimpleCov::SourceFile::SourceLoader

Defined in:
lib/simplecov/source_file/source_loader.rb

Overview

Reads a source file into an array of lines, honoring the source’s shebang and ‘coding:` magic comment when present. Always transcodes to UTF-8 with invalid/undef bytes replaced — both for JRuby compatibility and to keep encoding shenanigans in one place (see #866).

Constant Summary collapse

SHEBANG_REGEX =
/\A#!/
RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX =
/\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/

Class Method Summary collapse

Class Method Details

.call(filename) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simplecov/source_file/source_loader.rb', line 16

def call(filename)
  lines = []
  # The default encoding is UTF-8
  File.open(filename, "rb:UTF-8") do |file|
    current_line = file.gets

    if shebang?(current_line)
      lines << current_line
      current_line = file.gets
    end

    read_lines(file, lines, current_line)
  end
end

.ensure_remove_undefs(file_lines) ⇒ Object

invalid/undef replace are technically not really necessary but nice to have and work around a JRuby incompatibility. Setting these options on ‘file.set_encoding` doesn’t seem to work properly, so it has to be done here.



54
55
56
57
58
59
60
# File 'lib/simplecov/source_file/source_loader.rb', line 54

def ensure_remove_undefs(file_lines)
  file_lines.each do |line|
    # simplecov:disable — defensive: only fires for non-UTF-8 source files
    line.encode!("UTF-8", invalid: :replace, undef: :replace) unless line.encoding == Encoding::UTF_8
    # simplecov:enable
  end
end

.read_lines(file, lines, current_line) ⇒ Object



35
36
37
38
39
40
# File 'lib/simplecov/source_file/source_loader.rb', line 35

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], ensure_remove_undefs(file.readlines))
end

.set_encoding_based_on_magic_comment(file, line) ⇒ Object

Encoding magic comment must be placed at first line except for shebang.



44
45
46
47
48
# File 'lib/simplecov/source_file/source_loader.rb', line 44

def set_encoding_based_on_magic_comment(file, line)
  if (match = RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX.match(line))
    file.set_encoding(match[1], "UTF-8")
  end
end

.shebang?(line) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/simplecov/source_file/source_loader.rb', line 31

def shebang?(line)
  SHEBANG_REGEX.match?(line)
end