Class: OpenStudioMeasureTester::GithubActionsReport

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_results_directory) ⇒ GithubActionsReport



13
14
15
16
17
18
19
# File 'lib/openstudio_measure_tester/github_actions_report.rb', line 13

def initialize(test_results_directory)
  @test_results_directory = test_results_directory
  file = File.read("#{@test_results_directory}/combined_results.json")
  @hash = JSON.parse(file)
  @minitest_summary_table = nil
  @all_annotations = []
end

Instance Attribute Details

#all_annotationsObject (readonly)

Returns the value of attribute all_annotations.



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

def all_annotations
  @all_annotations
end

#minitest_summary_tableObject (readonly)

Returns the value of attribute minitest_summary_table.



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

def minitest_summary_table
  @minitest_summary_table
end

Instance Method Details

#hash_to_markdown(h, header0, header1) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/openstudio_measure_tester/github_actions_report.rb', line 28

def hash_to_markdown(h, header0, header1)
  n0 = [h.keys.map(&:to_s).map(&:size).max, header0.size].max
  n1 = [h.values.map(&:to_s).map(&:size).max, header1.size].max

  content = [
    "| #{header0.ljust(n0)} | #{header1.ljust(n1)} |",
    "| #{'-' * n0} | #{'-' * n1} |"
  ] + h.map { |k, v| "| #{k.ljust(n0)} | #{v.to_s.ljust(n1)} |" }
  return content.join("\n")
end

#make_minitest_annotationsObject



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

def make_minitest_annotations
  @all_annotations = []

  report_xmls = Dir["#{@test_results_directory}/minitest/reports/TEST-*.xml"]
  report_xmls.each do |report_xml|
    doc = REXML::Document.new(File.open(report_xml)).root
    testsuite_element = doc.elements['testsuite']
    filepath = testsuite_element.attributes['filepath']
    testsuite_element.elements.each('testcase') do |testcase|
      test_name = testcase.attributes['name']
      line = testcase.attributes['lineno'].to_i
      tested_class = testcase.attributes['classname']

      testcase.elements.each('failure') do |x|
        title = x.attributes['type']
        message = x.attributes['message']
        annot = "::error file=#{filepath},line=#{line},endLine=#{line + 1},title=#{title}::#{tested_class}.#{test_name}: #{message}"
        @all_annotations << annot
      end
      testcase.elements.each('error') do |x|
        title = x.attributes['type']
        message = x.attributes['message']
        annot = "::error file=#{filepath},line=#{line},endLine=#{line + 1},title=#{title}::#{message}"
        @all_annotations << annot
      end
      testcase.elements.each('skipped') do |x|
        title = x.attributes['type']
        message = x.attributes['message']
        annot = "::warning file=#{filepath},line=#{line},endLine=#{line + 1},title=#{title}::#{message}"
        @all_annotations << annot
      end
    end
  end
  @all_annotations.each { |a| puts a }
  nil
end

#make_minitest_step_summary_tableObject



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

def make_minitest_step_summary_table
  write_step_summary('## Minitest')
  write_step_summary('')

  total_tests = @hash['minitest']['total_tests']
  total_assertions = @hash['minitest']['total_assertions']
  total_errors = @hash['minitest']['total_errors']
  total_failures = @hash['minitest']['total_failures']
  total_skipped = @hash['minitest']['total_skipped']
  total_compatibility_errors = @hash['minitest']['total_compatibility_errors']
  total_load_errors = @hash['minitest'].fetch('total_load_errors', []).count

  passed = total_tests - (total_failures + total_errors + total_skipped)
  pct = passed.to_f / (total_tests - total_skipped)

  h = {
    'Total Tests' => total_tests,
    'Load Error' => total_load_errors,
    'Passed' => passed,
    'Success Rate' => format('%.2f%%', (pct * 100.0)),
    'Failures' => total_failures,
    'Errors' => total_errors,
    'Skipped' => total_skipped,
    'Incompatible' => total_compatibility_errors,
    'Total Assertions' => total_assertions
  }

  @minitest_summary_table = hash_to_markdown(h, 'Metric', 'Value')

  write_step_summary(@minitest_summary_table)
  write_step_summary('')
end

#write_step_summary(msg) ⇒ Object



21
22
23
24
25
26
# File 'lib/openstudio_measure_tester/github_actions_report.rb', line 21

def write_step_summary(msg)
  puts msg
  if !ENV['GITHUB_STEP_SUMMARY'].nil?
    File.write(ENV['GITHUB_STEP_SUMMARY'], "#{msg}\n", mode: 'a+')
  end
end