Class: Megatest::CLI

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

Constant Summary collapse

InvalidArgument =
Class.new(ArgumentError)
COMMANDS =
{
  "bisect" => :bisect,
  "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.



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

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
  @command = nil
  @verbose = false
  @junit = false
end

Class Method Details

.run!Object



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

def run!
  program_name = ENV.fetch("MEGATEST_PROGRAM_NAME", $PROGRAM_NAME)
  if paths = ENV["PATH"]
    paths.split(":").each do |path|
      if program_name.start_with?(path)
        program_name = program_name.delete_prefix(path)
        program_name = program_name.delete_prefix("/")
        break
      end
    end
  end

  exit(new(program_name, $stdout, $stderr, ARGV, ENV).run)
end

Instance Method Details

#bisect_testsObject

Raises:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/megatest/cli.rb', line 125

def bisect_tests
  require "megatest/multi_process"
  raise InvalidArgument, "Distributed queues can't be bisected" if @queue.distributed?

  @config.selectors = Selector.new(@config).parse(@argv)
  Megatest.load_config(@config)
  Megatest.init(@config)
  test_cases = Megatest.load_tests(@config)
  @queue.populate(test_cases)
  candidates = @queue.dup

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

  unless failure = find_failing_test
    @err.puts "No failing test"
    return 1
  end

  bisect_queue(candidates, failure.test_id)
end

#configureObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/megatest/cli.rb', line 71

def configure
  Megatest.running = true

  if @command = COMMANDS[@argv.first]
    @argv.shift
  end

  Megatest.config = @config
  @parser = build_parser
  @parser.parse!(@argv)
  @argv.shift if @argv.first == "--"
  @queue = @config.build_queue
  @config.parallelize_maybe if @command == :run && !@queue.distributed? && !@queue.sharded?
  @config
end

#reportObject

Raises:



114
115
116
117
118
119
120
121
122
123
# File 'lib/megatest/cli.rb', line 114

def report
  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?

  @config.selectors = Selector.new(@config).parse(@argv)
  Megatest.load_config(@config)

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

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/megatest/cli.rb', line 48

def run
  configure
  case @command
  when :report
    report
  when :run
    run_tests
  when :bisect
    bisect_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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/megatest/cli.rb', line 87

def run_tests
  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.new(@config).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
  elsif test_cases.size == 1
    @config.jobs_count = 1
  end

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