Class: DeepCover::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/deep_cover/cli.rb,
lib/deep_cover/cli/tools.rb,
lib/deep_cover/cli/commands/exec.rb,
lib/deep_cover/cli/commands/help.rb,
lib/deep_cover/cli/commands/clear.rb,
lib/deep_cover/cli/commands/clone.rb,
lib/deep_cover/cli/commands/merge.rb,
lib/deep_cover/cli/commands/gather.rb,
lib/deep_cover/cli/commands/report.rb,
lib/deep_cover/cli/commands/version.rb,
lib/deep_cover/cli/commands/short_help.rb,
lib/deep_cover/cli/commands/run_expression.rb

Defined Under Namespace

Modules: Tools

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_option(arg, options, scope) ⇒ Object

Consider defaults only for display



15
16
17
18
19
# File 'lib/deep_cover/cli.rb', line 15

def self.build_option(arg, options, scope)
  default = options.delete(:default)
  options[:desc] = "#{options[:desc]} (default: #{default})" if default
  super(arg, options, scope)
end

.exit_on_failure?Boolean

exit_code should be non-zero when the parsing fails

Returns:

  • (Boolean)


44
45
46
# File 'lib/deep_cover/cli.rb', line 44

def self.exit_on_failure?
  true
end

.help(shell, subcommand = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deep_cover/cli/commands/help.rb', line 10

def self.help(shell, subcommand = false)
  list = printable_commands(true, subcommand)
  Thor::Util.thor_classes_in(self).each do |klass|
    list += klass.printable_commands(false)
  end
  list.sort! { |a, b| a[0] <=> b[0] }

  main_commands, list = Tools.extract_commands_for_help(list, :exec, :clone)

  shell.say 'Main commands:'
  shell.print_table(main_commands, indent: 2, truncate: true)

  lower_level_commands, list = Tools.extract_commands_for_help(list, :gather, :report, :clear, :merge)
  shell.say
  shell.say 'Lower-level commands:'
  shell.print_table(lower_level_commands, indent: 2, truncate: true)

  shell.say
  shell.say 'Misc commands:'
  shell.print_table(list, indent: 2, truncate: true)

  shell.say
  class_options_help(shell)
end

.short_help(shell) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/deep_cover/cli/commands/short_help.rb', line 10

def self.short_help(shell)
  list = printable_commands(true, false)
  Thor::Util.thor_classes_in(self).each do |klass|
    list += klass.printable_commands(false)
  end

  main_commands, _ignored = Tools.extract_commands_for_help(list, :exec, :clone)

  shell.say 'Main commands:'
  shell.print_table(main_commands, indent: 2, truncate: true)

  shell.say
  shell.say 'More commands:'
  shell.say '  deep-cover help [COMMAND]  # Full help with a list of lower-level commands and all options'
end

Instance Method Details

#clearObject



6
7
8
# File 'lib/deep_cover/cli/commands/clear.rb', line 6

def clear
  DeepCover.persistence.clear_directory
end

#clone(*command_parts) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/deep_cover/cli/commands/clone.rb', line 13

def clone(*command_parts)
  if command_parts.empty?
    command_parts = CLI_DEFAULTS[:command]
    puts "No command specified, using default of: #{command_parts.join(' ')}"
  end

  require_relative '../../instrumented_clone_reporter'
  InstrumentedCloneReporter.new(**processed_options.merge(command: command_parts)).run
end

#exec(*command_parts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/deep_cover/cli/commands/exec.rb', line 17

def exec(*command_parts)
  if command_parts.empty?
    command_parts = CLI_DEFAULTS[:command]
    puts "No command specified, using default of: #{command_parts.join(' ')}"
  end

  DeepCover.config.set(**processed_options.slice(*DEFAULTS.keys))

  require 'yaml'
  env_var = {'DEEP_COVER' => 'gather',
             'DEEP_COVER_OPTIONS' => YAML.dump(DeepCover.config.to_hash_for_serialize),
  }

  DeepCover.delete_trackers
  exit_code = Tools.run_command_or_exit(shell, env_var, *command_parts)
  coverage = Coverage.load
  puts coverage.report(**DeepCover.config)
  exit(exit_code)
end

#gather(*command_parts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/deep_cover/cli/commands/gather.rb', line 12

def gather(*command_parts)
  if command_parts.empty?
    warn set_color('`gather` needs a command to run', :red)
    exit(1)
  end

  require 'yaml'
  env_var = {'DEEP_COVER' => 'gather',
             'DEEP_COVER_OPTIONS' => YAML.dump(processed_options.slice(*DEFAULTS.keys)),
  }

  exit_code = Tools.run_command_or_exit(shell, env_var, *command_parts)
  exit(exit_code)
end

#mergeObject



6
7
8
# File 'lib/deep_cover/cli/commands/merge.rb', line 6

def merge
  DeepCover.persistence.merge_persisted_trackers
end

#reportObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/deep_cover/cli/commands/report.rb', line 10

def report
  coverage = Coverage.load
  puts coverage.report(**processed_options)

  overall_coverage = coverage.analysis.overall
  minimum_coverage = processed_options[:'minimum-coverage'].to_f
  if overall_coverage < minimum_coverage
    puts "Overall coverage #{format('%.2f', overall_coverage)} is less than minimum #{format('%.2f', minimum_coverage)}"
    exit 1
  end
end

#run_expression(expression = nil) ⇒ Object



8
9
10
11
12
# File 'lib/deep_cover/cli/commands/run_expression.rb', line 8

def run_expression(expression = nil)
  require_relative '../../expression_debugger'
  expression ||= STDIN.read.tap { STDIN.reopen('/dev/tty') }
  ExpressionDebugger.new(expression, **options.transform_keys(&:to_sym)).show
end

#short_helpObject



6
7
8
# File 'lib/deep_cover/cli/commands/short_help.rb', line 6

def short_help
  self.class.short_help(shell)
end

#versionObject



10
11
12
13
# File 'lib/deep_cover/cli/commands/version.rb', line 10

def version
  require 'deep_cover/version'
  puts "deep-cover version #{DeepCover::VERSION}"
end