class Coverband::Utils::SourceFile

Constants

NOT_AVAILABLE

Attributes

coverage[R]

The array of coverage data received from the Coverage.result

filename[R]

The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)

first_updated_at[R]

the date this version of the file first started to record coverage

last_updated_at[R]

the date this version of the file last saw any coverage activity

never_loaded[R]

meta data that the file was never loaded during boot or runtime

Public Class Methods

new(filename, file_data) click to toggle source
# File lib/coverband/utils/source_file.rb, line 94
def initialize(filename, file_data)
  @filename = filename
  @runtime_relavant_lines = nil
  if file_data.is_a?(Hash)
    @coverage = file_data["data"]
    @first_updated_at = @last_updated_at = NOT_AVAILABLE
    @first_updated_at = Time.at(file_data["first_updated_at"]) if file_data["first_updated_at"]
    @last_updated_at = Time.at(file_data["last_updated_at"]) if file_data["last_updated_at"]
    @never_loaded = file_data["never_loaded"] || false
  else
    # TODO: Deprecate this code path this was backwards compatability from 3-4
    @coverage = file_data
    @first_updated_at = NOT_AVAILABLE
    @last_updated_at = NOT_AVAILABLE
  end
end

Public Instance Methods

build_lines() click to toggle source
# File lib/coverband/utils/source_file.rb, line 138
def build_lines
  coverage_exceeding_source_warn if coverage.size > src.size

  lines = src.map.with_index(1) { |src, i|
    Coverband::Utils::SourceFile::Line.new(src, i, coverage[i - 1])
  }

  process_skipped_lines(lines)
end
coverage_exceeding_source_warn() click to toggle source

Warning to identify condition from Issue #56

# File lib/coverband/utils/source_file.rb, line 149
def coverage_exceeding_source_warn
  warn "Warning: coverage data from Coverage [#{coverage.size}] exceeds line count in #{filename} [#{src.size}]"
end
covered_lines() click to toggle source

Returns all covered lines as SimpleCov::SourceFile::Line

# File lib/coverband/utils/source_file.rb, line 191
def covered_lines
  @covered_lines ||= lines.select(&:covered?)
end
covered_lines_count() click to toggle source
# File lib/coverband/utils/source_file.rb, line 195
def covered_lines_count
  covered_lines&.count
end
covered_percent() click to toggle source

The coverage for this file in percent. 0 if the file has no relevant lines

# File lib/coverband/utils/source_file.rb, line 159
def covered_percent
  return 100.0 if no_lines?

  return 0.0 if relevant_lines.zero?

  # handle edge case where runtime in dev can go over 100%
  [Float(covered_lines.size * 100.0 / relevant_lines.to_f), 100.0].min
end
covered_strength() click to toggle source
# File lib/coverband/utils/source_file.rb, line 172
def covered_strength
  return 0.0 if relevant_lines.zero?

  round_float(lines_strength / relevant_lines.to_f, 1)
end
formatted_covered_percent() click to toggle source
# File lib/coverband/utils/source_file.rb, line 168
def formatted_covered_percent
  covered_percent&.round(2)
end
line(number) click to toggle source

Access SimpleCov::SourceFile::Line source lines by line number

# File lib/coverband/utils/source_file.rb, line 154
def line(number)
  lines[number - 1]
end
line_coverage(index) click to toggle source
# File lib/coverband/utils/source_file.rb, line 199
def line_coverage(index)
  lines[index]&.coverage
end
lines() click to toggle source

Returns all source lines for this file as instances of SimpleCov::SourceFile::Line, and thus including coverage data. Aliased as :source_lines

# File lib/coverband/utils/source_file.rb, line 133
def lines
  @lines ||= build_lines
end
Also aliased as: source_lines
lines_of_code() click to toggle source

Returns the number of relevant lines (covered + missed)

# File lib/coverband/utils/source_file.rb, line 221
def lines_of_code
  covered_lines.size + missed_lines.size
end
lines_strength() click to toggle source
# File lib/coverband/utils/source_file.rb, line 182
def lines_strength
  lines.map(&:coverage).compact.reduce(:+)
end
missed_lines() click to toggle source

Returns all lines that should have been, but were not covered as instances of SimpleCov::SourceFile::Line

# File lib/coverband/utils/source_file.rb, line 205
def missed_lines
  @missed_lines ||= lines.select(&:missed?)
end
never_lines() click to toggle source

Returns all lines that are not relevant for coverage as SimpleCov::SourceFile::Line instances

# File lib/coverband/utils/source_file.rb, line 211
def never_lines
  @never_lines ||= lines.select(&:never?)
end
no_lines?() click to toggle source
# File lib/coverband/utils/source_file.rb, line 178
def no_lines?
  lines.length.zero? || (lines.length == never_lines.size)
end
process_skipped_lines(lines) click to toggle source

Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks as skipped.

# File lib/coverband/utils/source_file.rb, line 227
def process_skipped_lines(lines)
  skipping = false

  lines.each do |line|
    if Coverband::Utils::LinesClassifier.no_cov_line?(line.src)
      skipping = !skipping
      line.skipped!
    elsif skipping
      line.skipped!
    end
  end
end
project_filename() click to toggle source

The path to this source file relative to the projects directory

# File lib/coverband/utils/source_file.rb, line 119
def project_filename
  @filename.sub(/^#{Coverband.configuration.root}/, "")
end
relative_path() click to toggle source
# File lib/coverband/utils/source_file.rb, line 248
def relative_path
  RelativeFileConverter.convert(filename)
end
relevant_lines() click to toggle source
# File lib/coverband/utils/source_file.rb, line 186
def relevant_lines
  @runtime_relavant_lines || (lines.size - never_lines.size - skipped_lines.size)
end
runtime_relavant_calculations(runtime_relavant_lines) { |self| ... } click to toggle source
# File lib/coverband/utils/source_file.rb, line 111
def runtime_relavant_calculations(runtime_relavant_lines)
  @runtime_relavant_lines = runtime_relavant_lines
  yield self
ensure
  @runtime_relavant_lines = nil
end
short_name() click to toggle source

a bug that existed in simplecov was not checking that root was at the start of the file name I had previously patched this in my local Rails app

# File lib/coverband/utils/source_file.rb, line 243
def short_name
  filename.sub(/^#{Coverband.configuration.root}/, ".")
    .gsub(%r{^\.\/}, "")
end
skipped_lines() click to toggle source

Returns all lines that were skipped as SimpleCov::SourceFile::Line instances

# File lib/coverband/utils/source_file.rb, line 216
def skipped_lines
  @skipped_lines ||= lines.select(&:skipped?)
end
source()
Alias for: src
source_lines()
Alias for: lines
src() click to toggle source

The source code for this file. Aliased as :source

# File lib/coverband/utils/source_file.rb, line 124
def src
  # We intentionally read source code lazily to
  # suppress reading unused source code.
  @src ||= File.open(filename, "rb", &:readlines)
end
Also aliased as: source

Private Instance Methods

round_float(float, places) click to toggle source

ruby 1.9 could use Float#round(places) instead @return [Float]

# File lib/coverband/utils/source_file.rb, line 256
def round_float(float, places)
  factor = Float(10 * places)
  Float((float * factor).round / factor)
end