Class: Foodcritic::Junit::Outputter
- Inherits:
-
Object
- Object
- Foodcritic::Junit::Outputter
- Defined in:
- lib/foodcritic/junit/outputter.rb
Instance Attribute Summary collapse
-
#cookbook_name ⇒ Object
readonly
Returns the value of attribute cookbook_name.
-
#current_file_name ⇒ Object
readonly
Returns the value of attribute current_file_name.
-
#current_violation ⇒ Object
readonly
Returns the value of attribute current_violation.
-
#current_violation_lines ⇒ Object
readonly
Returns the value of attribute current_violation_lines.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#violations ⇒ Object
readonly
Returns the value of attribute violations.
Instance Method Summary collapse
-
#initialize(input) ⇒ Outputter
constructor
A new instance of Outputter.
- #output_file_path ⇒ Object
- #parse_violations ⇒ Object
- #run ⇒ Object
- #violations_as_xml ⇒ Object
- #write_output ⇒ Object
- #xml ⇒ Object
- #xml_for_violation(violation) ⇒ Object
Constructor Details
#initialize(input) ⇒ Outputter
Returns a new instance of Outputter.
13 14 15 16 17 18 19 |
# File 'lib/foodcritic/junit/outputter.rb', line 13 def initialize(input) @input = input @violations = [] @cookbook_name = ENV['COOKBOOK_NAME'] || File.basename(Dir.pwd) @output_dir = Pathname.new(ENV['FOODCRITIC_JUNIT_OUTPUT_DIR'] || 'test/reports') @output_file = ENV['FOODCRITIC_JUNIT_OUTPUT_FILE'] || 'foodcritic-report.xml' end |
Instance Attribute Details
#cookbook_name ⇒ Object (readonly)
Returns the value of attribute cookbook_name.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def cookbook_name @cookbook_name end |
#current_file_name ⇒ Object (readonly)
Returns the value of attribute current_file_name.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def current_file_name @current_file_name end |
#current_violation ⇒ Object (readonly)
Returns the value of attribute current_violation.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def current_violation @current_violation end |
#current_violation_lines ⇒ Object (readonly)
Returns the value of attribute current_violation_lines.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def current_violation_lines @current_violation_lines end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def input @input end |
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def output_dir @output_dir end |
#output_file ⇒ Object (readonly)
Returns the value of attribute output_file.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def output_file @output_file end |
#violations ⇒ Object (readonly)
Returns the value of attribute violations.
10 11 12 |
# File 'lib/foodcritic/junit/outputter.rb', line 10 def violations @violations end |
Instance Method Details
#output_file_path ⇒ Object
53 54 55 |
# File 'lib/foodcritic/junit/outputter.rb', line 53 def output_file_path output_dir.join(output_file) end |
#parse_violations ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/foodcritic/junit/outputter.rb', line 26 def parse_violations input.each_line do |line| line.chomp! @current_file_name = line if File.exist?(line) violation = line.match(/(?<rule>[A-Z]+\d+?):\s(?<message>.+?)$/) if violation # We got a new violation, store the current one + it's lines if current_violation violations.push({ rule: current_violation[:rule], message: current_violation[:message], file_name: current_file_name, lines: current_violation_lines.join("\n") }) end @current_violation = violation @current_violation_lines = [] else current_violation_lines << line.lstrip if current_violation_lines end end end |
#run ⇒ Object
21 22 23 24 |
# File 'lib/foodcritic/junit/outputter.rb', line 21 def run parse_violations write_output end |
#violations_as_xml ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/foodcritic/junit/outputter.rb', line 74 def violations_as_xml if violations.any? violations.map { |v| xml_for_violation(v) }.join("\n") else '<testcase classname="foodcritic-junit" name="No Errors"/>' end end |
#write_output ⇒ Object
57 58 59 60 61 |
# File 'lib/foodcritic/junit/outputter.rb', line 57 def write_output output_dir.mkpath File.open(output_file_path, 'w') { |f| f.puts(xml) } STDERR.puts("Wrote #{output_file_path}") end |
#xml ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/foodcritic/junit/outputter.rb', line 63 def xml <<-EOS <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name=#{cookbook_name.encode(xml: :attr)} timestamp=#{Time.now.utc.iso8601.to_s.encode(xml: :attr)}> #{violations_as_xml} </testsuite> </testsuites> EOS end |
#xml_for_violation(violation) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/foodcritic/junit/outputter.rb', line 82 def xml_for_violation(violation) name = "#{violation[:rule]}: #{violation[:message]}".encode(xml: :attr) file_name = violation[:file_name].encode(xml: :attr) location = "Located in #{violation[:file_name]}".encode(xml: :attr) <<-EOS <testcase name=#{name} classname=#{file_name} assertions="0" time="0"> <error type=#{name} message=#{location}> #{violation[:lines].encode(xml: :text)} </error> </testcase> EOS end |