Class: Relevance::Tarantula::HtmlReporter

Inherits:
Object
  • Object
show all
Includes:
Relevance::Tarantula
Defined in:
lib/relevance/tarantula/html_reporter.rb

Defined Under Namespace

Classes: HtmlResultOverview

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Relevance::Tarantula

#log, #rails_root, #tarantula_home, #verbose

Constructor Details

#initialize(basedir) ⇒ HtmlReporter

Returns a new instance of HtmlReporter.



9
10
11
12
13
# File 'lib/relevance/tarantula/html_reporter.rb', line 9

def initialize(basedir)
  @basedir = basedir    
  @results = Struct.new(:successes, :failures).new([], [])
  FileUtils.mkdir_p(@basedir)
end

Instance Attribute Details

#basedirObject

Returns the value of attribute basedir.



4
5
6
# File 'lib/relevance/tarantula/html_reporter.rb', line 4

def basedir
  @basedir
end

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/relevance/tarantula/html_reporter.rb', line 4

def results
  @results
end

Instance Method Details

#class_for_code(code) ⇒ Object

CSS class for HTML status codes



100
101
102
# File 'lib/relevance/tarantula/html_reporter.rb', line 100

def class_for_code(code)
  "r#{Integer(code)/100}" 
end

#copy_stylesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/relevance/tarantula/html_reporter.rb', line 38

def copy_styles
  # not using cp_r because it picks up .svn crap
  FileUtils.mkdir_p(File.join(basedir, "stylesheets"))
  Dir.glob("#{tarantula_home}/laf/stylesheets/*.css").each do |file|
    FileUtils.cp(file, File.join(basedir, "stylesheets")) 
  end
  FileUtils.mkdir_p(File.join(basedir, "images"))
  Dir.glob("#{tarantula_home}/laf/images/*.{jpg,gif,png}").each do |file|
    FileUtils.cp(file, File.join(basedir, "images")) 
  end
  FileUtils.mkdir_p(File.join(basedir, "javascripts"))
  Dir.glob("#{tarantula_home}/laf/javascripts/*.js").each do |file|
    FileUtils.cp(file, File.join(basedir, "javascripts")) 
  end
end

#create_detail_report(result) ⇒ Object



33
34
35
36
# File 'lib/relevance/tarantula/html_reporter.rb', line 33

def create_detail_report(result)
  template = ERB.new(template("detail.html.erb"))
  output(result.file_name, template.result(result.send(:binding)), result.test_name)
end

#create_indexObject



54
55
56
57
# File 'lib/relevance/tarantula/html_reporter.rb', line 54

def create_index
  template = ERB.new(template("index.html.erb"))
  output("index.html", template.result(binding))
end

#finish_report(test_name) ⇒ Object



26
27
28
29
30
31
# File 'lib/relevance/tarantula/html_reporter.rb', line 26

def finish_report(test_name)
  puts "Writing results to #{basedir}"
  copy_styles  unless styles_exist?
  create_index unless index_exists?
  update_index(test_name)
end

#index_exists?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/relevance/tarantula/html_reporter.rb', line 71

def index_exists?
  File.exists?(File.join(basedir, "index.html"))
end

#output(name, body, subdir = '') ⇒ Object



92
93
94
95
96
97
# File 'lib/relevance/tarantula/html_reporter.rb', line 92

def output(name, body, subdir = '')
  FileUtils.mkdir_p(File.join(basedir, subdir)) unless subdir.empty?
  File.open(File.join(basedir, subdir, name), "w") do |file|
    file.write body
  end
end

#report(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/relevance/tarantula/html_reporter.rb', line 15

def report(result)
  return if result.nil?
  
  create_detail_report(result)
  
  collection = result.success ? results.successes : results.failures
  collection << HtmlResultOverview.new(
    result.code, result.url, result.description, result.method, result.referrer, result.file_name
  )
end

#results_html(test_name) ⇒ Object



83
84
85
86
# File 'lib/relevance/tarantula/html_reporter.rb', line 83

def results_html(test_name)
  template = ERB.new(template("test_report.html.erb"))
  template.result(binding)
end

#styles_exist?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/relevance/tarantula/html_reporter.rb', line 75

def styles_exist?
  File.exists?(File.join(basedir, "stylesheets", "tarantula.css"))
end

#tab_html(test_name) ⇒ Object



79
80
81
# File 'lib/relevance/tarantula/html_reporter.rb', line 79

def tab_html(test_name)
  "<li><a href='##{test_name}'><span>#{test_name}</span></a></li>"
end

#template(name) ⇒ Object



88
89
90
# File 'lib/relevance/tarantula/html_reporter.rb', line 88

def template(name)
  File.read(File.join(File.dirname(__FILE__), name))
end

#update_index(test_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/relevance/tarantula/html_reporter.rb', line 59

def update_index(test_name)    
  File.open(File.join(basedir, "index.html"), "r+") do |file|
    doc = Hpricot file.read
    tabs_container = doc.search "#tabs-container ul"
    results_container = doc.search "#results-container"
    tabs_container.append tab_html(test_name)
    results_container.append results_html(test_name)
    file.rewind
    file.write doc.to_s
  end
end