Class: CheckstyleReport::CheckstyleReport

Inherits:
Object
  • Object
show all
Defined in:
lib/checkstyle_report/checkstyle_report.rb

Overview

Class for create report in Checkstyle format

Instance Method Summary collapse

Constructor Details

#initialize(path, report_filename, checks) ⇒ CheckstyleReport

Initializes a new instance of CheckstyleReport

Parameters:

  • path (String)

    The path where the report file will be saved

  • report_filename (String)

    The name of the report file

  • checks (Array<CheckstyleError>)

    An array of CheckstyleError objects



13
14
15
16
17
# File 'lib/checkstyle_report/checkstyle_report.rb', line 13

def initialize(path, report_filename, checks)
  @path = path
  @report_filename = report_filename
  @checks = checks
end

Instance Method Details

#create_reportObject

Creates a Checkstyle report XML file

This method groups the errors by file, creates a new CheckstyleFile object for each file, and writes the report to an XML file.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/checkstyle_report/checkstyle_report.rb', line 23

def create_report
  checkstyle_files = sort_checks(@checks)
  report = Nokogiri::XML::Builder.new do |xml|
    xml.checkstyle(version: '8.38') do
      checkstyle_files
        .map { |file, errors| CheckstyleFile.new(file, errors) }
        .each { |file| write_file(xml, file) }
    end
  end

  File.open("#{@path}/#{@report_filename}.xml", 'w') do |file|
    file.write(report.to_xml)
  end
end

#sort_checks(checks) ⇒ Hash<String, Array<CheckstyleError>>

Sorts the checks by file

and the values are arrays of CheckstyleError objects

Parameters:

Returns:

  • (Hash<String, Array<CheckstyleError>>)

    A hash where the keys are file names



43
44
45
46
47
48
49
50
51
52
# File 'lib/checkstyle_report/checkstyle_report.rb', line 43

def sort_checks(checks)
  checkstyle_files = {}
  checks.each do |check|
    checkstyle_file = checkstyle_files[check.source]
    checkstyle_files[check.source] = [] if checkstyle_file.nil?
    checkstyle_files[check.source] += [check] if check.severity != CheckstyleError::SEVERITY_NORMAL
  end

  checkstyle_files
end

#write_error(xml, error) ⇒ Object

Writes an error element to the XML report

Parameters:

  • xml (Nokogiri::XML::Builder)

    The XML builder object

  • error (CheckstyleError)

    The CheckstyleError object to be written



70
71
72
73
74
75
76
77
78
# File 'lib/checkstyle_report/checkstyle_report.rb', line 70

def write_error(xml, error)
  xml.error(
    line: error.line,
    column: error.column,
    severity: error.severity,
    message: error.message,
    source: error.source
  )
end

#write_file(xml, checkstyle_file) ⇒ Object

Writes a file element to the XML report

Parameters:

  • xml (Nokogiri::XML::Builder)

    The XML builder object

  • checkstyle_file (CheckstyleFile)

    The CheckstyleFile object to be written



58
59
60
61
62
63
64
# File 'lib/checkstyle_report/checkstyle_report.rb', line 58

def write_file(xml, checkstyle_file)
  xml.file(name: checkstyle_file.file) do
    checkstyle_file.errors.each do |error|
      write_error(xml, error)
    end
  end
end