Class: OpenStudioMeasureTester::RubocopResult

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_results) ⇒ RubocopResult

Returns a new instance of RubocopResult.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 12

def initialize(path_to_results)
  @path_to_results = path_to_results
  @error_status = false
  @total_files = 0
  @total_issues = 0
  @total_errors = 0
  @total_warnings = 0
  @total_info = 0
  @total_measures = 0

  @summary = {}

  @by_measure = {}

  parse_results
  to_file
end

Instance Attribute Details

#by_measureObject (readonly)

Returns the value of attribute by_measure.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def by_measure
  @by_measure
end

#error_statusObject (readonly)

Returns the value of attribute error_status.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def error_status
  @error_status
end

#file_errorsObject (readonly)

Returns the value of attribute file_errors.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def file_errors
  @file_errors
end

#file_infoObject (readonly)

Returns the value of attribute file_info.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def file_info
  @file_info
end

#file_issuesObject (readonly)

Returns the value of attribute file_issues.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def file_issues
  @file_issues
end

#file_warningsObject (readonly)

Returns the value of attribute file_warnings.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def file_warnings
  @file_warnings
end

#summaryObject (readonly)

Returns the value of attribute summary.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def summary
  @summary
end

#total_errorsObject (readonly)

Returns the value of attribute total_errors.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def total_errors
  @total_errors
end

#total_filesObject (readonly)

Returns the value of attribute total_files.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def total_files
  @total_files
end

#total_infoObject (readonly)

Returns the value of attribute total_info.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def total_info
  @total_info
end

#total_issuesObject (readonly)

Returns the value of attribute total_issues.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def total_issues
  @total_issues
end

#total_measuresObject (readonly)

Returns the value of attribute total_measures.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def total_measures
  @total_measures
end

#total_warningsObject (readonly)

Returns the value of attribute total_warnings.



10
11
12
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 10

def total_warnings
  @total_warnings
end

Instance Method Details

#parse_resultsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 30

def parse_results
  Dir[File.join(@path_to_results, 'rubocop-results.xml')].each do |file|
    puts "Parsing Rubocop report #{file}"

    measure_names = []

    @total_files = 0
    doc = REXML::Document.new(File.open(file)).root

    puts "Finished reading #{file}"

    # Go through the XML and find all the measure names first
    doc.elements.each('//checkstyle/file') do |rc_file|
      @total_files += 1
      measure_name = rc_file.attributes['name']
      if measure_name
        # If the user runs the tests from within the directory (where the measure.rb) file exists, then the
        # measure name is not included in the XML. Therefore, the measure_name will need to be abstracted by
        # the name of the directory relative to where the test_results are.
        if measure_name.include? File::SEPARATOR
          parts = measure_name.split(File::SEPARATOR)
          if parts.last == 'measure.rb'
            measure_names << parts[-2]
          end
        elsif measure_name == 'measure.rb'
          # the measure name must be extracted from the directory
          parts = file.split(File::SEPARATOR)
          measure_names << parts[-4]
        end
      end
    end

    @total_measures = measure_names.length

    # now go find the specific data about the measure
    measure_names.each do |measure_name|
      mhash = {}
      mhash['measure_issues'] = 0
      mhash['measure_info'] = 0
      mhash['measure_warnings'] = 0
      mhash['measure_errors'] = 0
      mhash['files'] = []

      cn = ''
      doc.elements.each('//checkstyle/file') do |rc_file|
        # Allow processing when the file is just the measure.rb
        if rc_file.attributes['name'] != 'measure.rb'
          parts = rc_file.attributes['name'].split(File::SEPARATOR)
          if !parts.include? measure_name
            next
          end
        end

        # Save off the file information
        fhash = {}
        if rc_file.attributes['name'].include? File::SEPARATOR
          fhash['file_name'] = rc_file.attributes['name'].split(File::SEPARATOR)[-1]
        else
          fhash['file_name'] = rc_file.attributes['name']
        end
        fhash['violations'] = []

        # get the class name out of the measure file! wow, okay... sure why not.
        if fhash['file_name'] == 'measure.rb'
          if File.exist? rc_file.attributes['name']
            File.readlines(rc_file.attributes['name']).each do |line|
              if (line.include? 'class') && line.split(' ')[0] == 'class'
                cn = line.split(' ')[1].gsub(/_?[tT]est\z/, '')
                break
              end
            end
          else
            puts "Could not find measure to parse for extracting class name in Rubocop. PWD: #{Dir.pwd} filename: #{rc_file.attributes['name']}"
          end
        end

        @file_issues = 0
        @file_info = 0
        @file_warnings = 0
        @file_errors = 0

        violations = []
        rc_file.elements.each('error') do |rc_error|
          @file_issues += 1
          case rc_error.attributes['severity']
          when 'info'
            @file_info += 1
          when 'warning'
            @file_warnings += 1
          when 'error'
            @file_errors += 1
          end
          violations << {
            line: rc_error.attributes['line'],
            column: rc_error.attributes['column'],
            severity: rc_error.attributes['severity'],
            message: rc_error.attributes['message']
          }
        end
        fhash['issues'] = @file_issues
        fhash['info'] = @file_info
        fhash['warning'] = @file_warnings
        fhash['error'] = @file_errors
        fhash['violations'] = violations

        mhash['measure_issues'] += @file_issues
        mhash['measure_info'] += @file_info
        mhash['measure_warnings'] += @file_warnings
        mhash['measure_errors'] += @file_errors

        @total_issues += @file_issues
        @total_info += @file_info
        @total_warnings += @file_warnings
        @total_errors += @file_errors

        mhash['files'] << fhash
      end
      @by_measure[cn] = mhash
    end
  end

  puts "Total files: #{@total_files}"
  puts "Total issues: #{@total_issues} (#{@total_info} info, #{@total_warnings} warnings, #{@total_errors} errors)"

  @error_status = true if @total_errors > 0
end

#to_fileObject



170
171
172
173
174
175
176
177
178
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 170

def to_file
  # save as a json and have something else parse it/plot it.
  res_hash = to_hash
  @summary = res_hash
  FileUtils.mkdir_p "#{@path_to_results}/" unless Dir.exist? "#{@path_to_results}/"
  File.open("#{@path_to_results}/rubocop.json", 'w') do |file|
    file << JSON.pretty_generate(res_hash)
  end
end

#to_hashObject



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/openstudio_measure_tester/rubocop_result.rb', line 157

def to_hash
  results = {}
  results['total_measures'] = @total_measures
  results['total_files'] = @total_files
  results['total_issues'] = @total_issues
  results['total_info'] = @total_info
  results['total_warnings'] = @total_warnings
  results['total_errors'] = @total_errors
  results['by_measure'] = @by_measure

  results
end