123456789_123456789_123456789_123456789_123456789_

Module: SimpleCov::SourceFile::SourceLoader

Relationships & Source Files
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

Class Method Summary

Class Method Details

.call(filename) (mod_func)

[ GitHub ]

  
# 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) (mod_func)

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.

[ GitHub ]

  
# 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) (mod_func)

[ GitHub ]

  
# 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) (mod_func)

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

[ GitHub ]

  
# 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 (mod_func)

[ GitHub ]

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

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