Class: OpenStudioMeasureTester::OpenStudioTestingResult

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results_dir, test_results_dir, orig_results_dir = nil) ⇒ OpenStudioTestingResult

Pass in the results_dir where all the results are stored

Parameters:

  • results_dir (String)

    : Directory where the results are scattered. Typically the root dir or where rake was executed

  • test_results_dir (String)

    : Where the final results are to be stored

  • orig_results_dir (String) (defaults to: nil)

    : Optional directory where results are sometimes thrown into that need to be moved (coverage and minitest)



16
17
18
19
20
21
22
23
24
25
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
52
53
# File 'lib/openstudio_measure_tester/openstudio_testing_result.rb', line 16

def initialize(results_dir, test_results_dir, orig_results_dir = nil)
  @results_dir = results_dir
  @test_results_dir = test_results_dir
  @orig_results_dir = orig_results_dir
  @results = {}

  puts "results_dir is #{@results_dir}"
  puts "test_results_dir is #{@test_results_dir}"
  puts "orig_results_dir is #{@orig_results_dir}"

  # get the repository info
  repo_name = 'unknown'
  current_branch = 'unknown'
  sha = 'unknown'
  begin
    g = Git.open(Dir.pwd)
    config = g.config
    repo_name = config['remote.origin.url'] ? config['remote.origin.url'].split('/').last.chomp('.git') : nil
    current_branch = g.branch.name || nil
    logs = g.log
    sha = nil
    if !logs.empty?
      sha = logs.first.sha
    end
  rescue StandardError => e
    puts 'Could not find .git for measure(s), will not be able to report git information'
  end

  # check if the results data already exist, and if so, then load the file now to keep the results
  load_results

  # add/overwrite repo info in results
  @results['repo_name'] = repo_name
  @results['current_branch'] = current_branch
  @results['sha'] = sha

  aggregate_results
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



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

def results
  @results
end

Instance Method Details

#aggregate_resultsObject



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

def aggregate_results
  # openstudio style is stored in the correct location (the test_results directory)
  # OpenStudio Style will have already run, so just grab the results out of the directory and jam into
  # the @results hash
  filename = "#{@test_results_dir}/openstudio_style/openstudio_style.json"
  if File.exist? filename
    puts 'Found OpenStudio Style results, parsing'
    @results['openstudio_style'] = JSON.parse(File.read(filename))
  end

  filename = "#{@test_results_dir}/rubocop/rubocop.json"
  if File.exist? filename
    @results['rubocop'] = JSON.parse(File.read(filename))
  elsif Dir.exist? "#{@test_results_dir}/rubocop"
    # Parse the rubocop results if they were not cached
    rc = OpenStudioMeasureTester::RubocopResult.new("#{@test_results_dir}/rubocop")
    @results['rubocop'] = rc.summary
  end

  # don't copy if the directories are the same
  if @test_results_dir != @orig_results_dir
    # coverage
    if Dir.exist? "#{@orig_results_dir}/coverage"
      puts 'Found Coverage results, parsing'
      FileUtils.rm_rf "#{@test_results_dir}/coverage" if Dir.exist? "#{@test_results_dir}/coverage"
      FileUtils.cp_r "#{@orig_results_dir}/coverage/.", "#{@test_results_dir}/coverage"
      FileUtils.rm_rf "#{@orig_results_dir}/coverage" if Dir.exist? "#{@orig_results_dir}/coverage"

      cov = OpenStudioMeasureTester::Coverage.new("#{@test_results_dir}/coverage")
      cov.parse_results
      @results['coverage'] = cov.to_hash
    end

    # minitest
    if Dir.exist?("#{@orig_results_dir}/test/html_reports") || Dir.exist?("#{@orig_results_dir}/test/reports")
      puts 'Found Minitest Results, parsing'
      # Do not delete the compatibility directory which is generated when the test is run
      FileUtils.rm_rf "#{@test_results_dir}/minitest/html_reports" if Dir.exist? "#{@test_results_dir}/minitest/html_reports"
      FileUtils.rm_rf "#{@test_results_dir}/minitest/reports" if Dir.exist? "#{@test_results_dir}/minitest/reports"

      FileUtils.mkdir_p "#{@test_results_dir}/minitest"

      # Copy the files over in case the folder is locked.
      if Dir.exist?("#{@orig_results_dir}/test/html_reports")
        puts 'Moving Minitest HTML results to results directory'
        FileUtils.cp_r "#{@orig_results_dir}/test/html_reports/.", "#{@test_results_dir}/minitest/html_reports"
      end

      if Dir.exist?("#{@orig_results_dir}/test/reports")
        puts 'Moving Minitest XML results to results directory'
        FileUtils.cp_r "#{@orig_results_dir}/test/reports/.", "#{@test_results_dir}/minitest/reports"
      end

      # Delete the test folder if it is empty
      FileUtils.rm_rf "#{@orig_results_dir}/test" if Dir.exist?("#{@orig_results_dir}/test") && Dir.entries("#{@orig_results_dir}/test").size == 2

      # Load in the data into the minitest object
      mr = OpenStudioMeasureTester::MinitestResult.new("#{@test_results_dir}/minitest")
      @results['minitest'] = mr.summary
    end
  end
end

#exit_codeObject

Return the exit code based on some arbitrary limit across all the tests



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/openstudio_measure_tester/openstudio_testing_result.rb', line 134

def exit_code
  # there must be no unit test failures
  # pp @results
  final_exit_code = 0
  # more than 10 errors per file on average
  if @results['rubocop'] && (@results['rubocop']['total_errors'].nonzero? && @results['rubocop']['total_files'].nonzero?)
    status = @results['rubocop']['total_errors'] / @results['rubocop']['total_files']
    if status > 10
      puts "More than 10 RuboCop errors per file found. Found #{status}"
      final_exit_code = 1
    end
  end

  if @results['openstudio_style']
    total_files = @results['openstudio_style']['by_measure'].count
    status = @results['openstudio_style']['total_errors'] / total_files
    if status > 10
      puts "More than 10 OpenStudio Style errors found per file. Found #{status}"
      final_exit_code = 1
    end

    status = @results['openstudio_style']['total_warnings'] / total_files
    if status > 25
      puts "More than 25 OpenStudio Style warnings found per file, reporting as error. Found #{status}"
      final_exit_code = 1
    end
  end

  if @results['minitest'] && (@results['minitest'][:total_errors] > 0 || @results['minitest'][:total_failures] > 0)
    puts 'Unit Test (Minitest) errors/failures found.'
    final_exit_code = 1
  end

  # if @results['coverage']
  #   status = @results['coverage']['total_percent_coverage']
  #   if status < 70
  #     puts "Code coverage is less than 70%, raising error. Coverage was #{status}"
  #     final_exit_code = 1
  #   end
  # end

  # Since the data are relative to the directory from which it has been run, then just show from current dir (.)
  puts 'Open ./test_results/dashboard/index.html to view measure testing dashboard.'

  return final_exit_code
end

#load_resultsObject



118
119
120
121
122
123
124
125
# File 'lib/openstudio_measure_tester/openstudio_testing_result.rb', line 118

def load_results
  filename = "#{@test_results_dir}/combined_results.json"
  begin
    @results = JSON.parse(File.read(filename)) if File.exist? filename
  rescue StandardError
    @results = {}
  end
end

#save_resultsObject



127
128
129
130
131
# File 'lib/openstudio_measure_tester/openstudio_testing_result.rb', line 127

def save_results
  File.open("#{@test_results_dir}/combined_results.json", 'w') do |file|
    file << JSON.pretty_generate(@results)
  end
end