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



158
159
160
161
# File 'lib/gorp/test.rb', line 158

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

Instance Method Details

#attach_to_mediatorObject



163
164
165
166
167
168
169
170
# File 'lib/gorp/test.rb', line 163

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
# File 'lib/gorp/test.rb', line 172

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

  tickets = {
    'rails' => 'https://rails.lighthouseapp.com/projects/8994/tickets/',
    'ruby'  => 'http://redmine.ruby-lang.org/issues/show/',
    'bundler'  => 'http://github.com/carlhuda/bundler/issues/issue/',
    'will_paginate' => 'http://github.com/mislav/will_paginate/issues#issue/'
  }

  if 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
    if fault.message =~ /RuntimeError: Ticket (\w+):(\d+): (.*)/ 
      x.p :class => 'traceback' do
        x.a "Ticket #{$2}", :href => tickets[$1]+$2
        x.text! ': ' + $3
      end
    else
      x.pre fault.message, :class=>'traceback'
    end
  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



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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/gorp/test.rb', line 238

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>\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, 'status'), 'w') do |status|
    status.puts @result.to_s
  end

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