Module: SimpleCovMcp::CovUtil

Defined in:
lib/simplecov_mcp/util.rb

Class Method Summary collapse

Class Method Details

.detailed(arr) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/simplecov_mcp/util.rb', line 79

module_function def detailed(arr)
  rows = []
  arr.each_with_index do |hits, i|
    h = hits&.to_i
    rows << { 'line' => i + 1, 'hits' => h, 'covered' => h.positive? } if h
  end
  rows
end

.find_resultset(root, resultset: nil) ⇒ Object



49
50
51
# File 'lib/simplecov_mcp/util.rb', line 49

module_function def find_resultset(root, resultset: nil)
  Resolvers::ResolverFactory.find_resultset(root, resultset: resultset)
end

.log(msg) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simplecov_mcp/util.rb', line 15

module_function def log(msg)
  log_file = SimpleCovMcp.active_log_file

  case log_file
  when 'stdout'
    $stdout.puts "[#{Time.now.iso8601}] #{msg}"
  when 'stderr'
    $stderr.puts "[#{Time.now.iso8601}] #{msg}"
  else
    # Handles both nil (default) and custom file paths
    path_to_log = log_file || DEFAULT_LOG_FILESPEC
    File.open(File.expand_path(path_to_log), 'a') { |f| f.puts "[#{Time.now.iso8601}] #{msg}" }
  end
rescue => e
  # Fallback to stderr if file logging fails, but suppress in MCP mode
  # to avoid interfering with JSON-RPC protocol
  unless SimpleCovMcp.context.mcp_mode?
    begin
      $stderr.puts "[#{Time.now.iso8601}] LOGGING ERROR: #{e.message}"
      $stderr.puts "[#{Time.now.iso8601}] #{msg}"
    rescue
      # Silently ignore only stderr fallback failures
    end
  end
end

.lookup_lines(cov, file_abs) ⇒ Object



53
54
55
# File 'lib/simplecov_mcp/util.rb', line 53

module_function def lookup_lines(cov, file_abs)
  Resolvers::ResolverFactory.lookup_lines(cov, file_abs)
end

.safe_log(msg) ⇒ Object

Safe logging that never raises - use when logging should not interrupt execution. Unlike ‘log`, this method guarantees it will never propagate exceptions.



43
44
45
46
47
# File 'lib/simplecov_mcp/util.rb', line 43

module_function def safe_log(msg)
  log(msg)
rescue
  # Silently ignore all logging failures
end

.summary(arr) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/simplecov_mcp/util.rb', line 57

module_function def summary(arr)
  total = 0
  covered = 0
  arr.compact.each do |hits|
    total += 1
    covered += 1 if hits.to_i > 0
  end
  percentage = total.zero? ? 100.0 : ((covered.to_f * 100.0 / total) * 100).round / 100.0
  { 'covered' => covered, 'total' => total, 'percentage' => percentage }
end

.uncovered(arr) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/simplecov_mcp/util.rb', line 68

module_function def uncovered(arr)
  out = []

  arr.each_with_index do |hits, i|
    next if hits.nil?

    out << (i + 1) if hits.to_i.zero?
  end
  out
end