Class: Megatest::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/megatest/cli.rb

Constant Summary collapse

InvalidArgument =
Class.new(ArgumentError)
RUNNERS =
{
  "report" => :report,
  "run" => :run,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program_name, out, err, argv, env) ⇒ CLI

Returns a new instance of CLI.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/megatest/cli.rb', line 26

def initialize(program_name, out, err, argv, env)
  @out = out
  @err = err
  @argv = argv.dup
  @processes = nil
  @config = Config.new(env)
  @program_name = @config.program_name = program_name
  @runner = nil
  @verbose = false
  @junit = false
end

Class Method Details

.run!Object



12
13
14
15
16
# File 'lib/megatest/cli.rb', line 12

def run!
  program_name = $PROGRAM_NAME
  program_name = "megatest" if program_name == `command -v megatest`.strip
  exit(new(program_name, $stdout, $stderr, ARGV, ENV).run)
end

Instance Method Details

#configureObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/megatest/cli.rb', line 59

def configure
  if @runner = RUNNERS[@argv.first]
    @argv.shift
  end

  Megatest.config = @config
  @parser = build_parser(@runner)
  @parser.parse!(@argv)
  @argv.shift if @argv.first == "--"
  @config
end

#reportObject

Raises:



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/megatest/cli.rb', line 98

def report
  queue = @config.build_queue

  raise InvalidArgument, "Only distributed queues can be summarized" unless queue.distributed?
  raise InvalidArgument, "Distributed queues require a build-id" unless @config.build_id
  raise InvalidArgument, @argv.join(" ") unless @argv.empty?

  Megatest.load_config(@argv)

  QueueReporter.new(@config, queue, @out).run(default_reporters) ? 0 : 1
end

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/megatest/cli.rb', line 38

def run
  configure
  case @runner
  when :report
    report
  when nil, :run
    run_tests
  else
    raise InvalidArgument, "Parsing failure"
  end
rescue InvalidArgument, OptionParser::ParseError => error
  if error.is_a?(InvalidArgument)
    @err.puts "invalid arguments: #{error.message}"
  else
    @err.puts error.message
  end
  @err.puts
  @err.puts @parser
  1
end

#run_testsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/megatest/cli.rb', line 71

def run_tests
  queue = @config.build_queue

  if queue.distributed?
    raise InvalidArgument, "Distributed queues require a build-id" unless @config.build_id
    raise InvalidArgument, "Distributed queues require a worker-id" unless @config.worker_id
  elsif queue.sharded?
    unless @config.valid_worker_index?
      raise InvalidArgument, "Splitting the queue requires a worker-id lower than workers-count, got: #{@config.worker_id.inspect}"
    end
  end

  @config.selectors = Selector.parse(@argv)
  Megatest.load_config(@config)
  Megatest.init(@config)
  test_cases = Megatest.load_tests(@config)

  if test_cases.empty?
    @err.puts "No tests to run"
    return 1
  end

  queue.populate(test_cases)
  executor.run(queue, default_reporters)
  queue.success? ? 0 : 1
end