module SolargraphTestCoverage::ReporterHelpers

Some helper functions for the diagnostics

Public Class Methods

test_path() click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 79
def self.test_path
  File.join(Dir.pwd, Config.test_dir)
end

Public Instance Methods

branch_warnings(source, results) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 48
def branch_warnings(source, results)
  uncovered_branches(results).map { |branch| branch_coverage_warning(source, branch.report) }
end
exclude_file?(source_filename) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 6
def exclude_file?(source_filename)
  return true if source_filename.start_with? test_path

  Config.exclude_paths.any? { |path| source_filename.sub(Dir.pwd, '').include? path }
end
line_warnings(source, results) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 44
def line_warnings(source, results)
  uncovered_lines(results).map { |line| line_coverage_warning(source, line) }
end
messages(source, results) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 34
def messages(source, results)
  messages = [
    line_warnings(source, results),
    branch_warnings(source, results),
    test_passing_error(source, results)
  ]

  messages.flatten.compact
end
range(start_line, start_column, end_line, end_column) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 75
def range(start_line, start_column, end_line, end_column)
  Solargraph::Range.from_to(start_line, start_column, end_line, end_column).to_hash
end
run_test(source) click to toggle source

@return [Hash]

# File lib/solargraph_test_coverage/reporter_helpers.rb, line 26
def run_test(source)
  ForkProcess.run do
    Coverage.start(lines: true, branches: true)
    runner = TestRunner.with(test_file(source)).run!
    Coverage.result.fetch(source.location.filename, {}).merge({ test_status: runner.passed? })
  end
end
test_file(source) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 18
def test_file(source)
  relative_filepath = source.location.filename.sub(Dir.pwd, '').split('/').reject(&:empty?)
  relative_filepath[0] = Config.test_dir

  File.join(Dir.pwd, relative_filepath.join('/')).sub('.rb', Config.test_file_suffix)
end
test_passing_error(source, results) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 52
def test_passing_error(source, results)
  results[:test_status] ? [] : [test_failing_error(source)]
end
test_path() click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 83
def test_path
  ReporterHelpers.test_path
end
uncovered_branches(results) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 71
def uncovered_branches(results)
  Branch.build_from(results).reject(&:covered?)
end
uncovered_lines(results) click to toggle source

Adapted from SingleCov Coverage returns nil for untestable lines (like 'do', 'end', 'if' keywords) otherwise returns int showing how many times a line was called

[nil, 1, 0, 1, 0] -> [3, 5]
Returns array of line numbers with 0 coverage
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 62
def uncovered_lines(results)
  return [] unless results[:lines]

  results[:lines].each_with_index
                 .select { |c, _| c&.zero? }
                 .map { |_, i| i }
                 .compact
end
using_debugger?(source) click to toggle source
# File lib/solargraph_test_coverage/reporter_helpers.rb, line 12
def using_debugger?(source)
  source.code.include?('binding.pry') ||
    source.code.include?('byebug') ||
    source.code.include?('debugger')
end