Class: Gem::Guardian::ResultPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/gem/guardian/result_printer.rb

Overview

Formats verification results for human-readable CLI output.

Constant Summary collapse

USAGE =

CLI usage text.

<<~USAGE.freeze
  gem-guardian #{VERSION}

  Usage:
    gem-guardian verify [--lockfile Gemfile.lock]
    gem-guardian verify GEM:VERSION[:PLATFORM] [GEM:VERSION[:PLATFORM] ...]
    gem-guardian version

  Examples:
    gem-guardian verify
  gem-guardian verify sidekiq:8.0.8
  gem-guardian verify nokogiri:1.18.9:x86_64-linux
USAGE

Instance Method Summary collapse

Constructor Details

#initialize(stdout:) ⇒ ResultPrinter

Returns a new instance of ResultPrinter.

Parameters:

  • stdout (IO)

    output stream for formatted messages



8
9
10
# File 'lib/gem/guardian/result_printer.rb', line 8

def initialize(stdout:)
  @stdout = stdout
end

Instance Method Details

Prints an unexpected verifier error.



45
46
47
48
# File 'lib/gem/guardian/result_printer.rb', line 45

def print_error_result(result, label)
  @stdout.puts "ERROR #{label}"
  @stdout.puts "      #{result.error.class}: #{result.error.message}"
end

Prints lockfile checksum coverage.



51
52
53
54
55
56
57
58
59
# File 'lib/gem/guardian/result_printer.rb', line 51

def print_lockfile_coverage(lockfile_data)
  covered = lockfile_data.dependencies.size - lockfile_data.missing_checksum_dependencies.size
  total = lockfile_data.dependencies.size
  @stdout.puts "CHECKSUMS coverage: #{covered}/#{total}"

  lockfile_data.missing_checksum_dependencies.each do |dependency|
    @stdout.puts "MISSING #{dependency.name} #{dependency.version} #{dependency.platform}"
  end
end

Prints a checksum mismatch.



38
39
40
41
42
# File 'lib/gem/guardian/result_printer.rb', line 38

def print_mismatch_result(result, label)
  @stdout.puts "FAIL #{label}"
  @stdout.puts "     expected #{result.expected_sha256}"
  @stdout.puts "     actual   #{result.actual_sha256}"
end

Prints a successful verification result.



30
31
32
33
34
35
# File 'lib/gem/guardian/result_printer.rb', line 30

def print_ok_result(result, label, lockfile_mode)
  prefix = lockfile_mode && result.checksum_source == :rubygems ? "FALLBACK" : "PASS"
  @stdout.puts "#{prefix} #{label}"
  @stdout.puts "     sha256 #{result.actual_sha256}"
  @stdout.puts "     source #{result.checksum_source}" if lockfile_mode && result.checksum_source
end

Prints one verification result.



20
21
22
23
24
25
26
27
# File 'lib/gem/guardian/result_printer.rb', line 20

def print_result(result, lockfile_mode:)
  label = result_label(result)
  case result.status
  when :ok then print_ok_result(result, label, lockfile_mode)
  when :mismatch then print_mismatch_result(result, label)
  else print_error_result(result, label)
  end
end

Prints a collection of verification results.



13
14
15
16
17
# File 'lib/gem/guardian/result_printer.rb', line 13

def print_results(results, lockfile_mode:)
  results.each do |result|
    print_result(result, lockfile_mode:)
  end
end

#usageObject

Prints the CLI usage text.



62
63
64
# File 'lib/gem/guardian/result_printer.rb', line 62

def usage
  @stdout.puts(USAGE)
end