Project

General

Profile

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

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

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
    minimum_coverage 26
10
    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
      def run_with_vcr
42
        options = self.class.respond_to?(:cassette_options) ? self.class.cassette_options : {}
43
        VCR.insert_cassette(cassette_name, options)
44
        stdout,stderr = capture {
45
          yield
46
        }
47
        VCR.eject_cassette
48
        return stdout,stderr
49
      end
50
51
      class << self
52
        attr_accessor :support
53
54
        def suite_cassette_name
55
          parent = (self.name.split('::')[-2] || '').underscore
56
          self_class = self.name.split('::')[-1].underscore.gsub('test_', '')
57
          "#{parent}/#{self_class}/suite"
58
        end
59
      end
60
61
62
    end
63
  end
64
end
65
66
class CustomMiniTestRunner
67
  class Unit < MiniTest::Unit
68
    def before_suites
69
      # code to run before the first test
70
    end
71
72
    def after_suites
73
      # code to run after the last test
74
    end
75
76
    def _run_suites(suites, type)
77
      if ENV['suite']
78
        suites = suites.select do |suite|
79
          suite.name == ENV['suite']
80
        end
81
      end
82
      before_suites
83
      super(suites, type)
84
    ensure
85
      after_suites
86
    end
87
88
    def _run_suite(suite, type)
89
      options = suite.respond_to?(:cassette_options) ? suite.cassette_options : {}
90
      if logging?
91
        puts "Running Suite #{suite.inspect} - #{type.inspect} "
92
      end
93
      if suite.respond_to?(:before_suite)
94
        VCR.use_cassette(suite.suite_cassette_name, options) do
95
          suite.before_suite
96
        end
97
      end
98
      super(suite, type)
99
    ensure
100
      if suite.respond_to?(:after_suite)
101
        VCR.use_cassette(suite.suite_cassette_name, options) do
102
          suite.after_suite
103
        end
104
      end
105
      if logging?
106
        puts "Completed Running Suite #{suite.inspect} - #{type.inspect} "
107
      end
108
    end
109
110
    def logging?
111
      ENV['logging']
112
    end
113
  end
114
end
115
116
class CsvMiniTestRunner
117
  def run_tests(suite, options = {})
118
    mode      = options[:mode] || 'none'
119
    test_name = options[:test_name] || nil
120
    logging   = options[:logging] || false
121
122
    MiniTest::Unit.runner = CustomMiniTestRunner::Unit.new
123
124
    vcr_config(mode)
125
126
    if test_name && File.exist?(test_name)
127
      require test_name
128
    elsif test_name
129
      require "./test/#{test_name}_test.rb"
130
    else
131
      Dir["./test/#{suite}/*_test.rb"].each { |file| require file }
132
    end
133
  end
134
135
  def vcr_config(mode)
136
    if mode == 'all'
137
      configure_vcr(:all)
138
    elsif mode == 'new_episodes'
139
      configure_vcr(:new_episodes)
140
    elsif mode == 'once'
141
      configure_vcr(:once)
142
    else
143
      configure_vcr(:none)
144
    end
145
  end
146
end