Class: HTMLRunner

Inherits:
Test::Unit::UI::Console::TestRunner
  • Object
show all
Defined in:
lib/gorp/test.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(suite) ⇒ Object



182
183
184
185
# File 'lib/gorp/test.rb', line 182

def self.run suite
  @@sections = suite.sections
  super
end

Instance Method Details

#attach_to_mediatorObject



187
188
189
190
191
192
193
194
# File 'lib/gorp/test.rb', line 187

def attach_to_mediator
  super
  @html_tests = []
  @mediator.add_listener(Test::Unit::TestResult::FAULT,
    &method(:html_fault))
  @mediator.add_listener(Test::Unit::UI::TestRunnerMediator::FINISHED,
    &method(:html_summary))
end

#html_fault(fault) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/gorp/test.rb', line 196

def html_fault fault
  if $standalone
    puts fault
    x = $x
  else
    x = Builder::XmlMarkup.new(:indent => 2)
  end

  tickets = {
    'rails' => 'https://github.com/rails/rails/issues/',
    'sprockets-rails' => 'https://github.com/rails/sprockets-rails/issues/',
    'turbolinks' => 'https://github.com/rails/turbolinks/issues/',
    'i18n' => 'https://github.com/svenfuchs/i18n/issues/',
    'activemerchant' => 'https://github.com/Shopify/active_merchant/issues/',
    'ruby'  => 'http://redmine.ruby-lang.org/issues/show/',
    'bundler'  => 'http://github.com/carlhuda/bundler/issues/issue/',
    'jquery-rails' => 'https://github.com/rails/jquery-rails/issues/',
    'will_paginate' => 'http://github.com/mislav/will_paginate/issues#issue/'
  }

  if fault.message =~ /RuntimeError: Ticket ([-\w]+):(\d+): (.*)/ 
    x.p :class => 'traceback' do
      x.a "Ticket #{$2}", :href => tickets[$1]+$2
      x.text! ': ' + $3
    end
  elsif fault.respond_to? :location
    location = fault.location
    location.shift while location.first.to_s. =~ /testing.assertions.selector/
    location.pop   while location.last.to_s.  =~ /gorp.lib.gorp.test/
    x.pre fault.message.sub(".\n<false> is not true",'') +
      "\n\nTraceback:\n  " + location.join("\n  "),
      :class=>'traceback'
  else
    x.pre fault.message, :class=>'traceback'
  end

  if fault.test_name =~ /^test_([\d.]+)_.*\(\w+\)$/
    name = $1
    sections = @@sections
    return unless sections.has_key? name

    # indicate failure in the toc
    sections[:contents][/<a href="#section-#{name}"()>/,1] = 
      ' style="color:red; font-weight:bold"'

    # provide details in the section itself
    sections[name][/<\/a>()/,1] = x.target!

    # add to the todos
    x = Builder::XmlMarkup.new(:indent => 2)
    x.li do
      x.a "Section #{name}", :href => "#section-#{name}"
      if fault.message =~ /RuntimeError: Ticket (\w+):(\d+): (.*)/ 
        x.text! '['
        x.a "Ticket #{$2}", :href => tickets[$1]+$2
        x.text! ']: ' + $3
      else
        x.text! ': '
        x.tt fault.message.sub(".\n<false> is not true",'').
          sub(/ but was\n.*/, '.').
          sub(/"((?:\\"|[^"])+)"/) {
            '"' + ($1.length>80 ? $1[0..72]+'...' : $1) + '"'
          }
      end
    end
    sections[:todos][/() *<\/ul>/,1] = x.target!.gsub(/^/,'      ')
  end
end

#html_summary(elapsed) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/gorp/test.rb', line 265

def html_summary elapsed
  # terminate server
  Gorp::Commands.stop_server

  open(File.join($WORK, "#{$output}.html"),'w') do |output|
    sections = @@sections
    output.write(sections.delete(:head))
    output.write("<body class='awdwr'>\n    ")
    output.write(sections.delete(:contents))
    env = sections.delete(:env)
    todos = sections.delete(:todos)
    tail = sections.delete(:tail)
    sections.keys.sort_by {|key| key.split('.').map {|n| n.to_i}}.each do |n|
      output.write(sections[n])
    end

    if sections.empty?
      output.write($x.target!)
    end

    if env
      output.write('<a class="toc" id="env">')
      output.write(env)
    else
      $x = Builder::XmlMarkup.new(:indent => 2)
      $x.a(:class => 'toc', :id => 'env') {$x.h2 'Environment'}
      $stdout = StringIO.open('','w')
      Gorp.dump_env
      $stdout = STDOUT
      output.write($x.target!)
    end

    if todos
      output.write('<a class="toc" id="todos">')
      todos.sub! /<ul.*\/ul>/m, '<h2>None!</h2>' unless todos.include? '<li>'
      output.write(todos)
    end
    output.write("\n  </body>")
    output.write(tail)
  end

  open(File.join($WORK, "#{$output}.status"), 'w') do |status|
    status.puts @result.to_s
  end

  at_exit { raise SystemExit.new(1) } unless @result.passed?
end