Project

General

Profile

Download (4.67 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / test_runner.rb @ f92e695a

1 b50ceaa2 Tom McKay
if RUBY_VERSION > "2.2"
2
  # Coverage - Keep these two lines at the top of this file
3
  require 'simplecov'
4
  require 'coveralls'
5
6
  SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter,
7
                          Coveralls::SimpleCov::Formatter]
8
  SimpleCov.start do
9 c06f1783 Tom McKay
    minimum_coverage 36
10 b50ceaa2 Tom McKay
    maximum_coverage_drop 0.1
11
    refuse_coverage_drop
12
    track_files "lib/**/*.rb"
13
    add_filter '/test/'
14
  end
15
end
16
17
require 'rubygems'
18
require 'logger'
19
require 'minitest/unit'
20
require 'minitest/autorun'
21
require 'mocha/setup'
22
23
require './test/vcr_setup'
24
25
begin
26
  require 'debugger'
27
rescue LoadError
28
  puts 'Debugging not enabled.'
29
end
30
31
module MiniTest
32
  class Unit
33
    class TestCase
34
      def cassette_name
35
        test_name = self.__name__.gsub('test_', '')
36
        parent = (self.class.name.split('::')[-2] || '').underscore
37
        self_class = self.class.name.split('::')[-1].underscore.gsub('test_', '')
38
        "#{parent}/#{self_class}/#{test_name}"
39
      end
40
41 a2eda8bf Tom McKay
      def start_vcr
42 b50ceaa2 Tom McKay
        options = self.class.respond_to?(:cassette_options) ? self.class.cassette_options : {}
43
        VCR.insert_cassette(cassette_name, options)
44 a2eda8bf Tom McKay
      end
45
46
      def stop_vcr
47 b50ceaa2 Tom McKay
        VCR.eject_cassette
48
      end
49
50
      class << self
51
        attr_accessor :support
52
53
        def suite_cassette_name
54
          parent = (self.name.split('::')[-2] || '').underscore
55
          self_class = self.name.split('::')[-1].underscore.gsub('test_', '')
56
          "#{parent}/#{self_class}/suite"
57
        end
58
      end
59
    end
60
  end
61
end
62
63
class CustomMiniTestRunner
64
  class Unit < MiniTest::Unit
65
    def before_suites
66
      # code to run before the first test
67
    end
68
69
    def after_suites
70
      # code to run after the last test
71
    end
72
73
    def _run_suites(suites, type)
74
      if ENV['suite']
75
        suites = suites.select do |suite|
76
          suite.name == ENV['suite']
77
        end
78
      end
79
      before_suites
80
      super(suites, type)
81
    ensure
82
      after_suites
83
    end
84
85
    def _run_suite(suite, type)
86
      options = suite.respond_to?(:cassette_options) ? suite.cassette_options : {}
87
      if logging?
88
        puts "Running Suite #{suite.inspect} - #{type.inspect} "
89
      end
90
      if suite.respond_to?(:before_suite)
91
        VCR.use_cassette(suite.suite_cassette_name, options) do
92
          suite.before_suite
93
        end
94
      end
95
      super(suite, type)
96
    ensure
97
      if suite.respond_to?(:after_suite)
98
        VCR.use_cassette(suite.suite_cassette_name, options) do
99
          suite.after_suite
100
        end
101
      end
102
      if logging?
103
        puts "Completed Running Suite #{suite.inspect} - #{type.inspect} "
104
      end
105
    end
106
107
    def logging?
108
      ENV['logging']
109
    end
110
  end
111
end
112
113
class CsvMiniTestRunner
114
  def run_tests(suite, options = {})
115
    mode      = options[:mode] || 'none'
116
    test_name = options[:test_name] || nil
117
    logging   = options[:logging] || false
118
119
    MiniTest::Unit.runner = CustomMiniTestRunner::Unit.new
120
121
    vcr_config(mode)
122
123
    if test_name && File.exist?(test_name)
124
      require test_name
125
    elsif test_name
126
      require "./test/#{test_name}_test.rb"
127
    else
128
      Dir["./test/#{suite}/*_test.rb"].each { |file| require file }
129
    end
130
  end
131
132
  def vcr_config(mode)
133 a2eda8bf Tom McKay
    load_config_settings
134 b50ceaa2 Tom McKay
    if mode == 'all'
135
      configure_vcr(:all)
136
    elsif mode == 'new_episodes'
137
      configure_vcr(:new_episodes)
138
    elsif mode == 'once'
139
      configure_vcr(:once)
140
    else
141
      configure_vcr(:none)
142
    end
143
  end
144 a2eda8bf Tom McKay
145
  def load_config_settings
146
    if File.exists? 'test/config.yml'
147
      HammerCLI::Settings.load_from_file 'test/config.yml'
148
    else
149
      HammerCLI::Settings.load({
150
                                 :ui => {
151
                                   :interactive => true,
152
                                   :per_page => 20,
153
                                   :history_file => './log/history'
154
                                 },
155
                                 :watch_plain => true,
156
                                 :log_dir => './log',
157
                                 :log_level => 'error',
158
                                 :log_api_calls => false,
159
                                 :log_size => 5,
160
                                 :csv => {
161
                                   :enable_module => true
162
                                 },
163
                                 :foreman => {
164
                                   :enable_module => true,
165
                                   :host => 'https://localhost',
166
                                   :username => 'admin',
167
                                   :password => 'changeme'
168
                                 },
169
                                 :katello => {
170
                                   :enable_module => true
171
                                 }
172
                               })
173
    end
174
  end
175 b50ceaa2 Tom McKay
end