class Codecov::SimpleCov::Formatter

Constants

RESULT_FILE_NAME

Public Instance Methods

format(report) click to toggle source
# File lib/codecov/formatter.rb, line 12
def format(report)
  result = {
    'meta' => {
      'version' => "codecov-ruby/v#{::Codecov::VERSION}"
    }
  }
  result.update(result_to_codecov(report))

  begin
    result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME)
    File.write(result_path, result['codecov'])
    overflow = result['coverage'].to_s.length > 256 ? '...' : ''
    puts "Coverage report generated to #{result_path}.\n#{result['coverage'].to_s.[](0, 255)}#{overflow}"
  rescue Errno::ENOENT => e
    puts e
    puts "Could not write coverage report to file.\n#{result}"
  end

  result
end

Private Instance Methods

file_network() click to toggle source
# File lib/codecov/formatter.rb, line 52
def file_network
  invalid_file_types = [
    'woff', 'eot', 'otf', # fonts
    'gif', 'png', 'jpg', 'jpeg', 'psd', # images
    'ptt', 'pptx', 'numbers', 'pages', 'md', 'txt', 'xlsx', 'docx', 'doc', 'pdf', 'csv', # docs
    'yml', 'yaml', '.gitignore'
  ].freeze

  invalid_directories = [
    'node_modules/',
    'storage/',
    'tmp/',
    'vendor/'
  ]

  network = []
  Dir['**/*'].keep_if do |file|
    if File.file?(file) && !file.end_with?(*invalid_file_types) && invalid_directories.none? { |dir| file.include?(dir) }
      network.push(file)
    end
  end

  network.push('<<<<<< network')
  network
end
file_to_codecov(file) click to toggle source

Format coverage data for a single file for the Codecov.io API.

@param file [SimpleCov::SourceFile] The file to process. @return [Array<nil, Integer>]

# File lib/codecov/formatter.rb, line 104
def file_to_codecov(file)
  # Initial nil is required to offset line numbers.
  [nil] + file.lines.map do |line|
    if line.skipped?
      nil
    else
      line.coverage
    end
  end
end
result_to_codecov(result) click to toggle source

Format SimpleCov coverage data for the Codecov.io API.

@param result [SimpleCov::Result] The coverage data to process. @return [Hash]

# File lib/codecov/formatter.rb, line 39
def result_to_codecov(result)
  {
    'codecov' => result_to_codecov_report(result),
    'coverage' => result_to_codecov_coverage(result),
    'messages' => result_to_codecov_messages(result)
  }
end
result_to_codecov_coverage(result) click to toggle source

Format SimpleCov coverage data for the Codecov.io coverage API.

@param result [SimpleCov::Result] The coverage data to process. @return [Hash<String, Array>]

# File lib/codecov/formatter.rb, line 82
def result_to_codecov_coverage(result)
  result.files.each_with_object({}) do |file, memo|
    memo[shortened_filename(file)] = file_to_codecov(file)
  end
end
result_to_codecov_messages(result) click to toggle source

Format SimpleCov coverage data for the Codecov.io messages API.

@param result [SimpleCov::Result] The coverage data to process. @return [Hash<String, Hash>]

# File lib/codecov/formatter.rb, line 92
def result_to_codecov_messages(result)
  result.files.each_with_object({}) do |file, memo|
    memo[shortened_filename(file)] = file.lines.each_with_object({}) do |line, lines_memo|
      lines_memo[line.line_number.to_s] = 'skipped' if line.skipped?
    end
  end
end
result_to_codecov_report(result) click to toggle source
# File lib/codecov/formatter.rb, line 47
def result_to_codecov_report(result)
  report = file_network.join("\n").concat("\n")
  report.concat({ 'coverage' => result_to_codecov_coverage(result) }.to_json)
end
shortened_filename(file) click to toggle source

Get a filename relative to the project root. Based on github.com/colszowka/simplecov-html, copyright Christoph Olszowka.

@param file [SimpleCov::SourceFile] The file to use. @return [String]

# File lib/codecov/formatter.rb, line 120
def shortened_filename(file)
  file.filename.gsub(/^#{::SimpleCov.root}/, '.').gsub(%r{^\./}, '')
end