Class: Bolt::Runners::TestUnit

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

Constant Summary collapse

MAPPINGS =

mappings define which folders hold the files that the listener should listen to

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

class map specifies the folders holding classes that can be reloaded

/(test\/functional\/|test\/unit\/|app\/controllers\/|app\/models\/|lib\/)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#clear_class, #file_verified?, #handle, #initialized, #load_file, #reload, #resolve_class, #resolve_classname

Constructor Details

#initializeTestUnit

Returns a new instance of TestUnit.



21
22
23
# File 'lib/bolt/runners/test_unit.rb', line 21

def initialize
  fix_test_unit_io
end

Instance Attribute Details

#notifierObject

Returns the value of attribute notifier.



19
20
21
# File 'lib/bolt/runners/test_unit.rb', line 19

def notifier
  @notifier
end

#test_ioObject

Returns the value of attribute test_io.



19
20
21
# File 'lib/bolt/runners/test_unit.rb', line 19

def test_io
  @test_io
end

Instance Method Details

#fix_test_unit_ioObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bolt/runners/test_unit.rb', line 25

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

#run(files) ⇒ Object

Run the supplied files (tests)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
132
# File 'lib/bolt/runners/test_unit.rb', line 80

def run(files)
  file = files.first
  puts "** Running #{file}"
  
  class_filename = file.sub(self.class::CLASS_MAP, '')
  
  # get the class
  test_class = resolve_classname(class_filename)
  
  # create dummy wrapper modules if test is in subfolder
  test_class.split('::').each do |part|
    eval "module ::#{part}; end" if !part.match('Test')
  end
  
  # TODO: make this reload use load_file
  $".delete(file)
  
  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)
  Test::Unit.run = false
  
  # 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

Translate a filename into a test filename

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



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
# File 'lib/bolt/runners/test_unit.rb', line 42

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}" if Bolt.verbose?
  end
  # puts candidates.inspect
  candidates
end