Class: OpenStudioMeasureTester::Coverage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_results) ⇒ Coverage



12
13
14
15
16
17
18
19
20
21
# File 'lib/openstudio_measure_tester/coverage.rb', line 12

def initialize(path_to_results)
  @path_to_results = path_to_results
  @total_percent_coverage = 0
  @total_lines = 0
  @total_relevant_lines = 0
  @total_covered_lines = 0
  @total_missed_lines = 0

  @measure_coverages = {}
end

Instance Attribute Details

#avg_hits_per_lineObject (readonly)

Returns the value of attribute avg_hits_per_line.



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

def avg_hits_per_line
  @avg_hits_per_line
end

#covered_linesObject (readonly)

Returns the value of attribute covered_lines.



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

def covered_lines
  @covered_lines
end

#measure_coveragesObject (readonly)

Returns the value of attribute measure_coverages.



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

def measure_coverages
  @measure_coverages
end

#missed_linesObject (readonly)

Returns the value of attribute missed_lines.



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

def missed_lines
  @missed_lines
end

#total_linesObject (readonly)

Returns the value of attribute total_lines.



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

def total_lines
  @total_lines
end

#total_percent_coverageObject (readonly)

Returns the value of attribute total_percent_coverage.



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

def total_percent_coverage
  @total_percent_coverage
end

#total_relevant_linesObject (readonly)

Returns the value of attribute total_relevant_lines.



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

def total_relevant_lines
  @total_relevant_lines
end

Instance Method Details

#parse_class_name(measure_file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openstudio_measure_tester/coverage.rb', line 23

def parse_class_name(measure_file)
  # grab the class name out of the parts
  unless File.exist? measure_file
    # magically try to find the path name by dropping the first element of array. This is
    # mainly needed for the testing framework as we are committing the .resultset.json which may
    # have been generated by any user, esp since the file names are fully qualified in the .resultset.json,
    puts "Trying to determine the file path of unknown measure #{measure_file}"
    measure_file = measure_file.split('/')[1..].join('/') until File.exist?(measure_file) || measure_file.split('/').empty?
  end

  # file should exist now. Read from the class name
  File.readlines(measure_file).each do |line|
    if (line.include? 'class') && line.split(' ')[0] == 'class'
      return line.match(/class\s(.*)\s</)[1]
    end
  end
end

#parse_resultsObject

Call this method to parse the results of the Coverage runs



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
# File 'lib/openstudio_measure_tester/coverage.rb', line 42

def parse_results
  file = "#{@path_to_results}/.resultset.json"

  unless File.exist? file
    puts "Could not find the results of coverage in #{file}"
    return false
  end

  puts 'Parsing coverage results'
  json_data = File.read(file)
  hash = JSON.parse(json_data)

  # pp hash

  # Sometimes the coverage is RSpec, sometimes MiniTest. Depends if running
  # tests in this Gem, or using this gem where MiniTest is already loaded.
  _, coverage_results = hash.first
  # puts "Coverage results are of type #{k}"

  # Find all measure names. Currently this is hardcoded to be the second to last element
  # of the file path name of the measure.rb. For example the key in the resultset.json file is
  # typically of the form:
  #     OpenStudio-measure-tester-gem/spec/test_measures/AddOverhangsByProjectionFactor/measure.rb"
  # which results in the measure name being 'AddOverhangsByProjectionFactor'
  # The measure names stores the mapping of the measure names to the key in the resultset.json
  # This requires two passes through the list of files, the first to grab the measure.rb files, which has
  # the classname, then to go through and grab all the other files that exist for the same measure class name.
  # Two passes because the order of the files in .resultset.json is not guaranteed to have measure.rb first.
  measure_maps = {}
  coverage_results['coverage'].each_key do |key|
    parts = key.split('/')
    if parts.last == 'measure.rb'
      class_name = parse_class_name(key)
      measure_maps[class_name] = {
        class_name:,
        root_path: File.dirname(key),
        files: [key]
      }
    end
  end

  # second pass, add other files.
  coverage_results['coverage'].each_key do |key|
    next if key.split('/').last == 'measure.rb'

    measure_maps.each do |m_key, m_value|
      if key.include? m_value[:root_path]
        measure_maps[m_key][:files] << key
      end
    end
  end

  # remap all this data to another format... seems like we could do this another way
  measure_maps.each do |class_name, measure_map|
    mhash = {}
    mhash['total_lines'] = 0
    mhash['relevant_lines'] = 0
    mhash['missed_lines'] = 0
    mhash['covered_lines'] = 0
    mhash['percent_coverage'] = 0
    mhash['files'] = []

    measure_map[:files].each do |test_file|
       = coverage_results['coverage'][test_file]
      fhash = {}
      # get the relative file path

      fhash['name'] = test_file.gsub("#{measure_map[:root_path]}/", '')
      fhash['total_lines'] = .size
      mhash['total_lines'] += fhash['total_lines']

      # remove nils from array
      .delete(nil)

      cov = .count { |x| x > 0 }
      fhash['percent_coverage'] = ((cov.to_f / .size) * 100).round(2)
      fhash['missed_lines'] = .size - cov
      fhash['relevant_lines'] = .size
      fhash['covered_lines'] = cov

      # measure-level totals
      mhash['relevant_lines'] += fhash['relevant_lines']
      mhash['covered_lines'] += fhash['covered_lines']
      mhash['missed_lines'] += fhash['missed_lines']
      mhash['files'] << fhash
    end

    mhash['percent_coverage'] = (mhash['covered_lines'].to_f / mhash['relevant_lines'] * 100).round(2)
    @measure_coverages[class_name] = mhash
    @total_lines += mhash['total_lines']
    @total_relevant_lines += mhash['relevant_lines']
    @total_covered_lines += mhash['covered_lines']
    @total_missed_lines += mhash['missed_lines']
  end

  # pp @measure_coverages
  lines = @total_relevant_lines # unnecessary but breaks formatting otherwise
  # lines can be zero if coverage doesn't run correctly
  if lines != 0
    @total_percent_coverage = (@total_covered_lines.to_f / lines * 100).round(2)
  end
  pp "Total Coverage: #{@total_percent_coverage}"

  return true
end

#save_resultsObject



161
162
163
164
165
166
# File 'lib/openstudio_measure_tester/coverage.rb', line 161

def save_results
  res_hash = to_hash
  File.open("#{@path_to_results}/coverage.json", 'w') do |file|
    file << JSON.pretty_generate(res_hash)
  end
end

#to_hashObject



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/openstudio_measure_tester/coverage.rb', line 148

def to_hash
  results = {}
  results['total_percent_coverage'] = @total_percent_coverage
  results['total_lines'] = @total_lines
  results['total_relevant_lines'] = @total_relevant_lines
  results['total_covered_lines'] = @total_covered_lines
  results['total_missed_lines'] = @total_missed_lines
  results['by_measure'] = @measure_coverages
  # pp results

  results
end