Class: Bolt::Runners::LegacyTestUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/runners/legacy_test_unit.rb

Constant Summary collapse

MAPPINGS =
/(\.\/app\/|\.\/lib\/|\.\/test\/functional|\.\/test\/unit)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLegacyTestUnit

Returns a new instance of LegacyTestUnit.



16
17
18
# File 'lib/bolt/runners/legacy_test_unit.rb', line 16

def initialize
  fix_test_unit_io
end

Instance Attribute Details

#notifierObject

Returns the value of attribute notifier.



14
15
16
# File 'lib/bolt/runners/legacy_test_unit.rb', line 14

def notifier
  @notifier
end

#test_ioObject

Returns the value of attribute test_io.



14
15
16
# File 'lib/bolt/runners/legacy_test_unit.rb', line 14

def test_io
  @test_io
end

Instance Method Details

#file_verified?(filename) ⇒ Boolean

check whether file exists

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/bolt/runners/legacy_test_unit.rb', line 86

def file_verified?(filename)
  if !File.exists?(filename)
    notifier.test_file_missing(filename)
    puts "=> ERROR: could not find test file: #{filename}"
    return false
  end
  return true
end

#fix_test_unit_ioObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bolt/runners/legacy_test_unit.rb', line 20

def fix_test_unit_io
  # Test::Unit stdio capture workaround, taken from Roman2K-rails-test-serving
  self.test_io = StringIO.new
  io = test_io
  Test::Unit::UI::Console::TestRunner.class_eval do
    alias_method :old_initialize, :initialize
    def initialize(suite, output_level, io=Thread.current["test_runner_io"])
      old_initialize(suite, output_level, io)
    end
  end
  Thread.current["test_runner_io"] = io
end

#handle(file) ⇒ Object

handle specified file



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bolt/runners/legacy_test_unit.rb', line 34

def handle(file)
  
  # force reload of file
  $".delete(file)
  $".delete(File.join(Dir.pwd, file))
  
  # reload tests and classes
  if file.match(/(test\/functional|test\/unit|app\/controllers|app\/models|lib\/)/)
    puts '=================='
    klassname = file.sub('test/unit/', '').sub('test/functional/', '').sub('app/controllers/', '').sub('app/models/', '').sub('lib/', '')
    klass_to_be_tested = klassname.sub('.rb', '').gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    

    # remove all methods - don't worry, the reload will bring them back refreshed
    begin
      klass = eval(klass_to_be_tested)
      klass.instance_methods.each { |m| 
        begin
          klass.send(:remove_method, m)
        rescue
        end
      }
    rescue NameError
    end
  end

  if file.include?('app/controllers') or file.include?('app/models') or file.include?('lib/')
    begin
      filename = File.join(Dir.pwd, file)
      filename << '.rb' if !filename.match('.rb')
      load filename
    rescue LoadError
      notifier.error("Error in #{file}", $!)
      return []
    rescue ArgumentError
      notifier.error("Error in #{file}", $!)
      return []
    end
  end

  puts '=> Test::Unit running test for ' + file
  test_files = translate(file)
  
  puts '==== Test::Unit running: ' + test_files.join(', ') + ' ===='
  
  run(test_files) if test_files != []
  
  puts '==== Test::Unit completed run ===='
  
end

#run(files) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/bolt/runners/legacy_test_unit.rb', line 133

def run(files)
  file = files.first
  puts "** Running #{file}"
  # make sure that you reload the test file
  #load file
  #contents = File.open(file).read
  # puts contents
  #eval contents
  
  # This is Rails' String#camelcase
  klassname = file.sub('test/functional/', '').sub('test/unit/', '')
  test_class = klassname.sub('.rb', '').gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  
  # create dummy wrapper modules if test is in subfolder
  test_class.split('::').each do |part|
    eval "module ::#{part}; end" if !part.match('Test')
  end
  
  $".delete(file)
  
  #(defined?(ActiveRecord::Base) ? ActiveRecord::Base.instance_eval { subclasses }.each { |c| c.reset_column_information } : nil)
  #(defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies.clear : nil)
  
  begin
    require file
  rescue LoadError
    notifier.error("Error in #{file}", $!)
    puts $!
    return
  rescue ArgumentError
    notifier.error("Error in #{file}", $!)
    puts $!
    return
  rescue SyntaxError
    notifier.error("Error in #{file}", $!)
    puts $!
    return
  end
  
  # TODO: change that to run multiple suites
  #klass = Kernel.const_get(test_class) - this threw errors
  klass = eval(test_class)
  
  Test::Unit::UI::Console::TestRunner.run(klass)

  # Invoke method to test that writes to stdout.
  result = test_io.string.to_s.dup

  # clear the buffer 
  test_io.truncate(0)
  
  # sent result to notifier
  notifier.result(file, result.split("\n").compact.last)

  # sent result to stdio
  puts result
  
end

#translate(file) ⇒ Object

mapping is a copied and modified version of mislav/rspactor Inspector#translate



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bolt/runners/legacy_test_unit.rb', line 96

def translate(file)
  
  basename = File.basename(file)
  candidates = []
  test_filename = nil
  case file
    when %r:^app/controllers/:
      test_filename = file.sub('.rb', '_test.rb').sub('app/controllers', 'test/functional')
    when %r:^app/models/:
      test_filename = "test/unit/#{basename.sub('.rb', '_test.rb')}"
    when %r:^app/views/:
      file = file.sub('app/views/', '')
      directory = file.split('/')[0..-2].compact.join('/')
      test_filename = "test/functional/#{directory}_controller_test.rb"
    when %r:^test/:
      test_filename = file
    when %r:^lib/:
      # map libs to units
      test_filename = "test/unit/#{file.sub('lib/', '').sub('.rb', '_test.rb')}"
    when 'config/routes.rb'
      test_filename = "test/functional/#{basename.sub('.rb', '_test.rb')}"
      #candidates << 'controllers' << 'helpers' << 'views'
    when 'config/database.yml', 'db/schema.rb'
      #candidates << 'models'
    else
      #
  end
  if test_filename and file_verified?(test_filename)
    candidates << test_filename
  end
  if candidates == []
    puts "=> NOTICE: could not find test file for: #{file}"
  end
  # puts candidates.inspect
  candidates
end