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



62
63
64
# File 'lib/cuke_sniffer/formatter.rb', line 62

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.



156
157
158
159
160
161
162
163
# File 'lib/cuke_sniffer/formatter.rb', line 156

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



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

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
# 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 in the junit with issues organized by file. file_name defaults to “cuke_sniffer_result.xml” unless specified

cuke_sniffer.output_xml

Or

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


110
111
112
113
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
# File 'lib/cuke_sniffer/formatter.rb', line 110

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
  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_num,
                                            :severity => test.rules_hash,
                                            :error => f,
                                            :formatted => "Severity: #{test.rules_hash[f]}\nLocation: #{location}\nError: #{f}"}}
    results[location_no_line] = results[location_no_line].nil? ? errors : results[location_no_line].concat(errors)
    failures += test.rules_hash.size
  end
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.testsuites(:tests => results.size, :failures => failures) do
      results.each do |location, failures|
        failures.each do |failure|
          xml.testcase(:classname => location, :name => failure[:line], :time => 0) do
            xml.failure(failure[:formatted], :type => 'failure', :message => failure[:error])
          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")


81
82
83
# File 'lib/cuke_sniffer/formatter.rb', line 81

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")


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

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



86
87
88
# File 'lib/cuke_sniffer/formatter.rb', line 86

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.



147
148
149
150
151
152
153
# File 'lib/cuke_sniffer/formatter.rb', line 147

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