Class: MiniAutobot::Runner

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

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



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

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

.before_runObject

before hook where you have parsed @options when loading tests



41
42
43
44
45
46
47
48
49
# File 'lib/mini_autobot/runner.rb', line 41

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



77
78
79
80
81
# File 'lib/mini_autobot/runner.rb', line 77

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



83
84
85
86
87
88
89
90
91
# File 'lib/mini_autobot/runner.rb', line 83

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mini_autobot/runner.rb', line 52

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



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

def self.run args = []
  Minitest.load_plugins

  @options = Minitest.process_args args

  self.before_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.passed?
end

.run!(args) ⇒ Object



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

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