Module: Kettle::Jem::SelfTest::Reporter

Defined in:
lib/kettle/jem.rb

Class Method Summary collapse

Class Method Details

.append_self_test_table(lines, title, paths, status = nil) ⇒ Object



2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
# File 'lib/kettle/jem.rb', line 2431

def append_self_test_table(lines, title, paths, status = nil)
  return if paths.empty?

  lines << "## #{title} (#{paths.size})"
  lines << ""
  if status
    lines << "| File | Status |"
    lines << "|------|--------|"
    paths.each { |path| lines << "| #{path} | #{status} |" }
  else
    lines << "| File |"
    lines << "|------|"
    paths.each { |path| lines << "| #{path} |" }
  end
  lines << ""
end

.append_skipped_files(lines, skipped) ⇒ Object



2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
# File 'lib/kettle/jem.rb', line 2448

def append_skipped_files(lines, skipped)
  return if skipped.empty?

  lines << ""
  lines << '<details markdown="1">'
  lines << "<summary>Not Templated (#{skipped.size} files) - source-only files not produced by the template task</summary>"
  lines << ""
  lines << "These files are part of the gem source and are not expected to appear in the template output."
  lines << ""
  lines << "| File |"
  lines << "|------|"
  skipped.each { |path| lines << "| #{path} |" }
  lines << ""
  lines << "</details>"
end

.diff(file_a, file_b) ⇒ Object



2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
# File 'lib/kettle/jem.rb', line 2370

def diff(file_a, file_b)
  a = File.exist?(file_a.to_s) ? file_a.to_s : File::NULL
  b = File.exist?(file_b.to_s) ? file_b.to_s : File::NULL
  out, = Open3.capture2("diff", "-u", a, b)
  out
rescue Errno::ENOENT
  a_lines = File.exist?(file_a.to_s) ? File.readlines(file_a) : []
  b_lines = File.exist?(file_b.to_s) ? File.readlines(file_b) : []
  return "" if a_lines == b_lines

  ["--- #{file_a}", "+++ #{file_b}", *a_lines.map { |line| "-#{line.chomp}" }, *b_lines.map { |line| "+#{line.chomp}" }].join("\n") + "\n"
end

.summary(comparison, output_dir:, templating_environment: nil, diff_count: nil, now: Time.now) ⇒ Object



2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
# File 'lib/kettle/jem.rb', line 2383

def summary(comparison, output_dir:, templating_environment: nil, diff_count: nil, now: Time.now)
  matched = comparison.fetch(:matched, [])
  changed = comparison.fetch(:changed, [])
  added = comparison.fetch(:added, [])
  removed = comparison.fetch(:removed, [])
  skipped = comparison.fetch(:skipped, [])
  diff_count = changed.size if diff_count.nil?
  total = matched.size + changed.size + added.size + removed.size
  score = total.zero? ? 0.0 : (matched.size.to_f / total * 100).round(1)
  divergence = (100.0 - score).round(1)

  lines = ["# Template Self-Test Report", ""]
  lines << "**Date**: #{now.iso8601}"
  lines << "**Output**: `#{output_dir}`"
  lines << "**Score**: #{score}% (#{matched.size}/#{total} files unchanged)"
  lines << "**Divergence**: #{divergence}% (#{changed.size + added.size + removed.size}/#{total} files changed, added, or missing)"
  lines << ""
  environment_section = Kettle::Jem::TemplatingReport.markdown_section(snapshot: templating_environment) if templating_environment
  lines << environment_section if environment_section && !environment_section.empty?
  append_self_test_table(lines, "Changed Files", changed, "modified")
  append_self_test_table(lines, "New Files", added)
  if removed.any?
    lines << "## Not Templated - Unexpected (#{removed.size})"
    lines << ""
    lines << "These files exist in the source gem and appear to be within the template's"
    lines << "scope, but were not produced by the template task."
    lines << ""
    lines << "| File |"
    lines << "|------|"
    removed.each { |path| lines << "| #{path} |" }
    lines << ""
  end
  if changed.empty? && added.empty? && removed.empty?
    lines << "## All files match! :tada:"
  else
    lines << "## Detailed Diffs"
    lines << ""
    lines << if diff_count.to_i.positive?
      "See `report/diffs/` directory (#{diff_count} file#{"s" unless diff_count == 1})."
    else
      "No per-file diffs were generated for this run; `report/diffs/` is empty."
    end
  end
  append_skipped_files(lines, skipped)
  lines << ""
  lines.join("\n")
end