Module: SimpleCov::CLI::CommandHelpers

Included in:
Affected, Badge, Clean, Completions, Coverage, DeadCode, DeadCode::Output, Diff, History, Merge, Open, Patch, Ratchet, Ratchet::Output, Report, Serve, Show, Status, Tests, Uncovered, Watch
Defined in:
lib/simplecov/cli/command_helpers.rb

Defined Under Namespace

Classes: HelpRequested

Constant Summary collapse

STATS_ROW_FORMAT =
"  %<label>-7s %<pct>s (%<covered>d / %<total>d)"

Instance Method Summary collapse

Instance Method Details

#build_parserObject



53
54
55
56
57
58
# File 'lib/simplecov/cli/command_helpers.rb', line 53

def build_parser
  OptionParser.new do |parser|
    yield parser if block_given?
    on_help(parser)
  end
end

#command_nameObject



16
17
18
# File 'lib/simplecov/cli/command_helpers.rb', line 16

def command_name
  name.split("::").last.downcase
end

#common_options(parser, opts) ⇒ Object



47
48
49
50
51
# File 'lib/simplecov/cli/command_helpers.rb', line 47

def common_options(parser, opts)
  parser.on("--input PATH") { |v| opts[:input] = v }
  parser.on("--json") { opts[:json] = true }
  parser.on("--no-color") { opts[:no_color] = true }
end

#error(stderr, message) ⇒ Object



24
25
26
27
# File 'lib/simplecov/cli/command_helpers.rb', line 24

def error(stderr, message)
  stderr.puts("simplecov #{command_name}: #{message}")
  1
end

#error_nil(stderr, message) ⇒ Object

error returns 1 for an exit status; query helpers need nil so their callers can tell "reported" from a real answer.



31
32
33
34
# File 'lib/simplecov/cli/command_helpers.rb', line 31

def error_nil(stderr, message)
  error(stderr, message)
  nil
end

#on_help(parser) ⇒ Object

mutant:disable — optparse matches an abbreviated -h against --help whether or not the short form is declared, so dropping it changes nothing an example could see. Declared anyway, so the flag appears in the usage text.



71
72
73
# File 'lib/simplecov/cli/command_helpers.rb', line 71

def on_help(parser)
  parser.on("--help", "-h") { raise HelpRequested }
end

#one?(count) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/simplecov/cli/command_helpers.rb', line 20

def one?(count)
  (count - 1).zero?
end

#parse_common(args, defaults = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/simplecov/cli/command_helpers.rb', line 75

def parse_common(args, defaults = {})
  opts = {input: CLI.default_input, json: false, no_color: false} #: Hash[Symbol, untyped]
  opts.merge!(defaults)
  rest =
    build_parser do |parser|
      common_options(parser, opts)
      yield parser, opts if block_given?
    end.parse(args)
  [opts, rest]
end

#quiet_option(parser, opts) ⇒ Object

mutant:disable OptionParser answers -q as an unambiguous abbreviation of --quiet whether or not the short form is declared.



63
64
65
# File 'lib/simplecov/cli/command_helpers.rb', line 63

def quiet_option(parser, opts)
  parser.on("-q", "--quiet") { opts[:quiet] = true }
end

#recorded_contexts(document, opts, stderr) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/simplecov/cli/command_helpers.rb', line 36

def recorded_contexts(document, opts, stderr)
  contexts = document["contexts"]
  if contexts.nil?
    return error_nil(stderr, "no test contexts recorded in #{opts.fetch(:input)}. Enable `track_tests` in " \
                             "your `SimpleCov.start` block and rerun the suite to record them")
  end
  return contexts if contexts.instance_of?(Array) && contexts.all?(String)

  CoverageFile.report_invalid(stderr, command_name, opts.fetch(:input), '"contexts" must be an array of strings')
end

#stats_row(label, percent_text, covered, total) ⇒ Object



86
87
88
# File 'lib/simplecov/cli/command_helpers.rb', line 86

def stats_row(label, percent_text, covered, total)
  format(STATS_ROW_FORMAT, label: "#{label}:", pct: percent_text, covered: covered.to_i, total: total.to_i)
end