Class: MiniAutobot::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_autobot/runner.rb

Constant Summary collapse

@@rerun_count =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/mini_autobot/runner.rb', line 4

def options
  @options
end

Class Method Details

.after_run(&blk) ⇒ Object



8
9
10
# File 'lib/mini_autobot/runner.rb', line 8

def self.after_run(&blk)
  @after_hooks << blk
end

.before_runObject

before hook where you have parsed @options when loading tests



58
59
60
61
62
63
64
65
66
# File 'lib/mini_autobot/runner.rb', line 58

def self.before_run
  tests_yml_full_path = MiniAutobot.root.join('config/mini_autobot', 'tests.yml').to_s
  if File.exist? tests_yml_full_path
    self.load_tests(tests_yml_full_path)
  else
    puts "Config file #{tests_yml_full_path} doesn't exist"
    puts "mini_autobot doesn't know where your tests are located and how they are structured"
  end
end

.check_config(tests_yml) ⇒ Object



94
95
96
97
98
# File 'lib/mini_autobot/runner.rb', line 94

def self.check_config(tests_yml)
  raise "relative_path must be provided in #{tests_yml}" unless tests_yml['tests_dir']['relative_path'].is_a? String
  raise "multi-host must be provided in #{tests_yml}" unless [true, false].include?(tests_yml['tests_dir']['multi-host'])
  raise "default_host must be provided in #{tests_yml}" unless tests_yml['tests_dir']['default_host'].is_a? String
end

.configure_load_path(tests_dir_relative_path) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/mini_autobot/runner.rb', line 100

def self.configure_load_path(tests_dir_relative_path)
  tests_dir_full_path = MiniAutobot.root.join(tests_dir_relative_path).to_s
  if Dir.exist? tests_dir_full_path
    $LOAD_PATH << tests_dir_full_path
  else
    puts "Tests directory #{tests_dir_full_path} doesn't exist"
    puts "No test will run."
  end
end

.load_tests(tests_yml_full_path) ⇒ Object

only load tests you need by specifying env option in command line



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mini_autobot/runner.rb', line 69

def self.load_tests(tests_yml_full_path)
  tests_yml = YAML.load_file tests_yml_full_path

  self.check_config(tests_yml)

  tests_dir_relative_path = tests_yml['tests_dir']['relative_path']
  multi_host_flag = tests_yml['tests_dir']['multi-host']
  default_host = tests_yml['tests_dir']['default_host']
  host = @options[:env].split(/_/)[0] rescue default_host

  self.configure_load_path(tests_dir_relative_path)

  # load page_objects.rb first
  Dir.glob("#{tests_dir_relative_path}/#{multi_host_flag ? host+'/' : ''}*.rb") do |f|
    f.sub!(/^#{tests_dir_relative_path}\//, '')
    require f
  end

  # files under subdirectories shouldn't be loaded, eg. archive/
  Dir.glob("#{tests_dir_relative_path}/#{multi_host_flag ? host+'/' : ''}test_cases/*.rb") do |f|
    f.sub!(/^#{tests_dir_relative_path}\//, '')
    require f
  end
end

.run(args = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mini_autobot/runner.rb', line 18

def self.run args = []
  Minitest.load_plugins

  @options = Minitest.process_args args

  self.before_run

  reporter = self.single_run

  rerun_failure = @options[:rerun_failure]
  if rerun_failure && !reporter.passed?
    while @@rerun_count < rerun_failure && !reporter.passed?
      reporter = self.single_run
      @@rerun_count += 1
    end
  end

  reporter.passed?
end

.run!(args) ⇒ Object



12
13
14
15
16
# File 'lib/mini_autobot/runner.rb', line 12

def self.run!(args)
  exit_code = self.run(args)
  @after_hooks.reverse_each(&:call)
  Kernel.exit(exit_code || false)
end

.single_runObject

Inialize a new reporter, run test Return reporter, which carrys test result



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mini_autobot/runner.rb', line 40

def self.single_run
  reporter = Minitest::CompositeReporter.new
  reporter << Minitest::SummaryReporter.new(@options[:io], @options)
  reporter << Minitest::ProgressReporter.new(@options[:io], @options)

  Minitest.reporter = reporter # this makes it available to plugins
  Minitest.init_plugins @options
  Minitest.reporter = nil # runnables shouldn't depend on the reporter, ever

  reporter.start
  Minitest.__run reporter, @options
  Minitest.parallel_executor.shutdown
  reporter.report

  reporter
end