Class: Foodcritic::Junit::Outputter

Inherits:
Object
  • Object
show all
Defined in:
lib/foodcritic/junit/outputter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_nameObject (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_violationObject (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_linesObject (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

#inputObject (readonly)

Returns the value of attribute input.



10
11
12
# File 'lib/foodcritic/junit/outputter.rb', line 10

def input
  @input
end

#output_dirObject (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_fileObject (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

#violationsObject (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_pathObject



62
63
64
# File 'lib/foodcritic/junit/outputter.rb', line 62

def output_file_path
  output_dir.join(output_file)
end

#parse_violationsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/foodcritic/junit/outputter.rb', line 37

def parse_violations
  input.each_line do |line|
    line.chomp!

    if File.exist?(line)
      store_violation
      @current_violation = nil
      @current_violation_lines = []
      @current_file_name = line
    end

    violation = line.match(/(?<rule>[A-Z]+\d+?):\s(?<message>.+?)$/)

    if violation
      # We got a new violation, store the current one + it's lines
      store_violation
      @current_violation = violation
      @current_violation_lines = []
    else
      current_violation_lines << line.lstrip if current_violation_lines
    end
  end
  store_violation
end

#runObject



21
22
23
24
# File 'lib/foodcritic/junit/outputter.rb', line 21

def run
  parse_violations
  write_output
end

#store_violationObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/foodcritic/junit/outputter.rb', line 26

def store_violation
  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
end

#violations_as_xmlObject



83
84
85
86
87
88
89
# File 'lib/foodcritic/junit/outputter.rb', line 83

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_outputObject



66
67
68
69
70
# File 'lib/foodcritic/junit/outputter.rb', line 66

def write_output
  output_dir.mkpath
  File.open(output_file_path, 'w') { |f| f.puts(xml) }
  STDERR.puts("Wrote #{output_file_path}")
end

#xmlObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/foodcritic/junit/outputter.rb', line 72

def xml
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n<testsuite name=\#{cookbook_name.encode(xml: :attr)} timestamp=\#{Time.now.utc.iso8601.to_s.encode(xml: :attr)}>\n\#{violations_as_xml}\n</testsuite>\n</testsuites>\n  EOS\nend\n"

#xml_for_violation(violation) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/foodcritic/junit/outputter.rb', line 91

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)
  "<testcase name=\#{name} classname=\#{file_name} assertions=\"0\" time=\"0\">\n<error type=\#{name} message=\#{location}>\n\#{violation[:lines].encode(xml: :text)}\n</error>\n</testcase>\n  EOS\nend\n"