Class: CukeSniffer::Formatter

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/cuke_sniffer/formatter.rb

Overview

Author

Robert Cochran ([email protected])

Copyright

Copyright © 2014 Robert Cochran

License

Distributes under the MIT License

Mixins: CukeSniffer::Constants Static class used to generate output for the CukeSniffer::CLI object.

Constant Summary

Constants included from Constants

Constants::COMMENT_REGEX, Constants::DATE_REGEX, Constants::DEFAULT_OUTPUT_FILE_NAME, Constants::FILE_IGNORE_LIST, Constants::HOOK_REGEX, Constants::HOOK_STYLES, Constants::MARKUP_SOURCE, Constants::SCENARIO_TITLE_STYLES, Constants::STEP_DEFINITION_REGEX, Constants::STEP_REGEX, Constants::STEP_STYLES, Constants::TAG_REGEX, Constants::THRESHOLDS

Class Method Summary collapse

Class Method Details

.build_page(cuke_sniffer, erb_file) ⇒ Object

Returns an ERB page built up for the passed file name



64
65
66
# File 'lib/cuke_sniffer/formatter.rb', line 64

def self.build_page(cuke_sniffer, erb_file)
  ERB.new(extract_markup(erb_file)).result(binding)
end

.console_improvement_list(improvement_list) ⇒ Object

Formats the improvement list data for summary



41
42
43
44
45
46
47
# File 'lib/cuke_sniffer/formatter.rb', line 41

def self.console_improvement_list(improvement_list)
  output = "  Improvements to make:\n"
  improvement_list.each do |improvement, count|
    output << "    (#{count}) #{improvement}\n"
  end
  output
end

.console_summary(name, summary) ⇒ Object

Formats the section data for a summary object



33
34
35
36
37
38
# File 'lib/cuke_sniffer/formatter.rb', line 33

def self.console_summary(name, summary)
  "  #{name}\n" +
      "    Min: #{summary[:min]}\n" +
      "    Max: #{summary[:max]}\n" +
      "    Average: #{summary[:average]}\n"
end

.extract_markup(template_name = "markup.html.erb", markup_source = MARKUP_SOURCE) ⇒ Object

Returns the markup for a desired erb file.



173
174
175
176
177
178
179
180
# File 'lib/cuke_sniffer/formatter.rb', line 173

def self.extract_markup(template_name = "markup.html.erb", markup_source = MARKUP_SOURCE)
  markup_location = "#{markup_source}/#{template_name}"
  markup = ""
  File.open(markup_location).each_line do |line|
    markup << line
  end
  markup
end

.format_html_file_name(file_name) ⇒ Object

Assigns an html extension if one is not provided for the passed file name



69
70
71
72
73
74
75
# File 'lib/cuke_sniffer/formatter.rb', line 69

def self.format_html_file_name(file_name)
  if file_name =~ /\.html$/
    file_name
  else
    file_name + ".html"
  end
end

.get_output_summary_nodes(cuke_sniffer) ⇒ Object

Returns a string of formatted output for all the object sections of summary



24
25
26
27
28
29
30
# File 'lib/cuke_sniffer/formatter.rb', line 24

def self.get_output_summary_nodes(cuke_sniffer)
  output = ""
  [:features, :scenarios, :step_definitions, :hooks].each do |summary_section|
    output += console_summary(summary_section.to_s.gsub("_", " ").capitalize, cuke_sniffer.summary[summary_section])
  end
  output
end

.output_console(cuke_sniffer) ⇒ Object

Prints out a summary of the results and the list of improvements to be made



13
14
15
16
17
18
19
20
21
# File 'lib/cuke_sniffer/formatter.rb', line 13

def self.output_console(cuke_sniffer)
  summary = cuke_sniffer.summary
  output = "Suite Summary" +
      "  Total Score: #{summary[:total_score]}\n" +
      get_output_summary_nodes(cuke_sniffer) +
      console_improvement_list(summary[:improvement_list])

  puts output
end

.output_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME, template_name = "standard_template") ⇒ Object

Creates a html file with the collected project details file_name defaults to “cuke_sniffer_results.html” unless specified Second parameter used for passing into the markup.

cuke_sniffer.output_html

Or

cuke_sniffer.output_html("results01-01-0001.html")


55
56
57
58
59
60
61
# File 'lib/cuke_sniffer/formatter.rb', line 55

def self.output_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME, template_name = "standard_template")
  cuke_sniffer = sort_cuke_sniffer_lists(cuke_sniffer)
  output = ERB.new(extract_markup("#{template_name}.html.erb")).result(binding)
  File.open(format_html_file_name(file_name), 'w') do |f|
    f.write(output)
  end
end

.output_junit_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object

Creates an xml file that can be read by Jenkins/Hudson in the junit format with issues organized and collated by file. Each file becomes a testsuite with corresponding failures associated to it. If no failures are found this will be marked as a pass by Jenkins/Hudson. file_name defaults to “cuke_sniffer_result.xml” unless specified

cuke_sniffer.output_xml

Or

cuke_sniffer.output_xml("cuke_sniffer01-01-0001.xml")


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
# File 'lib/cuke_sniffer/formatter.rb', line 114

def self.output_junit_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME)
  file_name = file_name + ".xml" unless file_name =~ /\.xml$/
  results = {}
  failures = 0
  suits={}
  current = cuke_sniffer.features
  current.concat cuke_sniffer.scenarios
  current.concat cuke_sniffer.step_definitions
  current.concat cuke_sniffer.hooks
  current.each do |test|
    location = test.location.gsub("#{Dir.pwd}/", '')
    location_no_line = location.gsub(/:[0-9]*/, '')
    line_num = location.include?(":") ? location.gsub(/.*:(.*)/, "\\1") : "full_file"
    errors = test.rules_hash.keys.map { |f| {:line => "line: #{line_num}",
                                             :error => f,
                                             :formatted => "Location: #{location}",
                                             :instances => "Instances: #{test.rules_hash[f]}"
    } }
    results[location_no_line] = results[location_no_line].nil? ? errors : results[location_no_line].concat(errors)
    failures += test.rules_hash.size
  end
  results.each do |location, failure|
    suits[location]=failure
  end
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.testsuites(:tests => results.size, :failures => failures) do
      suits.each do |location, failures|
        xml.testsuite(:name => location, :tests => 1, :failures => failures.length) do
          if failures.length == 0
            xml.testcase(:classname => location, :name => location, :time => 0, :status => 0)
          else
            failures.each do |failure|
              xml.testcase(:classname => location, :name => failure[:line], :time => 0, :status => failure[:instances]) do
                xml.failure(failure[:formatted], :type => 'failure', :message => "#{failure[:error]} #{failure[:instances]}")
              end
            end
          end
        end
      end
    end
  end
  output = builder.to_xml
  File.open(file_name, 'w') do |f|
    f.write(output)
  end
  # Return here to aid testing.
  output
end

.output_min_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object

Creates a html file with minimum information: Summary, Rules, Improvement List. file_name defaults to “cuke_sniffer_results.html” unless specified Second parameter used for passing into the markup.

cuke_sniffer.output_min_html

Or

cuke_sniffer.output_min_html("results01-01-0001.html")


83
84
85
# File 'lib/cuke_sniffer/formatter.rb', line 83

def self.output_min_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME)
  output_html(cuke_sniffer, file_name, "min_template")
end

.output_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object

Creates a xml file with the collected project details file_name defaults to “cuke_sniffer_result.xml” unless specified

cuke_sniffer.output_xml

Or

cuke_sniffer.output_xml("cuke_sniffer01-01-0001.xml")


97
98
99
100
101
102
103
104
105
# File 'lib/cuke_sniffer/formatter.rb', line 97

def self.output_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME)
  file_name = file_name + ".xml" unless file_name =~ /\.xml$/

  doc = Nokogiri::XML::Document.new
  doc.root = cuke_sniffer.to_xml
  open(file_name, "w") do |file|
    file << doc.serialize
  end
end

.rules_template(cuke_sniffer) ⇒ Object

Returns the Rules erb page that utilizes sub page sections of enabled and disabled rules



88
89
90
# File 'lib/cuke_sniffer/formatter.rb', line 88

def self.rules_template(cuke_sniffer)
  ERB.new(extract_markup("rules.html.erb")).result(binding)
end

.sort_cuke_sniffer_lists(cuke_sniffer) ⇒ Object

Sorts all of the lists on a cuke_sniffer object to be in descending order for each objects score.



164
165
166
167
168
169
170
# File 'lib/cuke_sniffer/formatter.rb', line 164

def self.sort_cuke_sniffer_lists(cuke_sniffer)
  cuke_sniffer.features = cuke_sniffer.features.sort_by { |feature| feature.total_score }.reverse
  cuke_sniffer.step_definitions = cuke_sniffer.step_definitions.sort_by { |step_definition| step_definition.score }.reverse
  cuke_sniffer.hooks = cuke_sniffer.hooks.sort_by { |hook| hook.score }.reverse
  cuke_sniffer.rules = cuke_sniffer.rules.sort_by { |rule| rule.score }.reverse
  cuke_sniffer
end