Class: ParallelTestsReport::GenerateReport

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

Instance Method Summary collapse

Instance Method Details

#generate_xml(errors, time_exceeding_examples, time_limit) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/parallel_tests_report/generate_report.rb', line 92

def generate_xml(errors, time_exceeding_examples, time_limit)
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.testsuite {
      time_exceeding_examples.each do |arr|
        classname = "#{arr["file_path"]}".sub(%r{\.[^/]*\Z}, "").gsub("/", ".").gsub(%r{\A\.+|\.+\Z}, "")
        xml.testcase("classname" => "#{classname}", "name" => "#{arr["full_description"]}", "file" => "#{arr["file_path"]}", "time" => "#{arr["run_time"]}") {
          xml.failure "Execution time is exceeding the threshold of #{time_limit} seconds"
        }
      end
      errors.each do |arr|
        file_path = arr[/(?<=An error occurred while loading ).*/]
        classname = "#{file_path}".sub(%r{\.[^/]*\Z}, "").gsub("/", ".").gsub(%r{\A\.+|\.+\Z}, "")
        xml.testcase("classname" => "#{classname}", "name" => "An error occurred while loading", "file" => "#{file_path}", "time" => "0.0") {
          xml.failure arr.gsub(/\e\[([;\d]+)?m/, "").gsub(/An error occurred while loading #{file_path}\n/, "")
        }
      end
    }
  end
  File.open('tmp/test-results/time_limit_exceeded.xml', 'w') do |file|
    file << builder.to_xml
  end
end

#start(time_limit, output) ⇒ Object



6
7
8
9
10
11
12
13
14
15
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
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
# File 'lib/parallel_tests_report/generate_report.rb', line 6

def start(time_limit, output)
  all_examples = []
  slowest_examples = []
  failed_examples = []
  time_exceeding_examples = []
  rerun_failed = []
  errors = []

  return if File.zero?(output)

  File.foreach(output) do |line|
    parallel_suite = JSON.parse(line)
    all_examples += parallel_suite["examples"]
    slowest_examples += parallel_suite["profile"]["examples"]
    failed_examples += parallel_suite["examples"].select {|ex| ex["status"] == "failed" }
    time_exceeding_examples += parallel_suite["examples"].select {|ex| ex["run_time"] >= time_limit}
    errors << parallel_suite["messages"][0] if parallel_suite["examples"].size == 0
  end

  if slowest_examples.size > 0
    slowest_examples = slowest_examples.sort_by do |ex|
      -ex["run_time"]
    end.first(20)
    puts "Top #{slowest_examples.size} slowest examples\n"
    slowest_examples.each do |ex|
      puts "\#{ex[\"full_description\"]}\n    \#{ex[\"run_time\"]} \#{\"seconds\"} \#{ex[\"file_path\"]} \#{ex[\"line_number\"]}\n      TEXT\n    end\n  end\n\n  if failed_examples.size > 0\n    puts \"\\nFailed Examples:\\n\"\n    failed_examples.each do |ex|\n      puts <<-TEXT\n=> \#{ex[\"full_description\"]}\n    \#{ex[\"run_time\"]} \#{\"seconds\"} \#{ex[\"file_path\"]} \#{ex[\"line_number\"]}\n      \#{ex[\"exception\"][\"message\"]}\n      TEXT\n      all_examples.each do |e|\n        rerun_failed << e[\"file_path\"].to_s if e[\"parallel_test_proessor\"] == ex[\"parallel_test_proessor\"] && !rerun_failed.include?(e[\"file_path\"])\n      end\n      str = \"\"\n      rerun_failed.each do |e|\n        str += e + \" \"\n      end\n      puts <<-TEXT\n\\n\\s\\sIn case the failure: \"\#{ex[\"full_description\"]}\" is due to random ordering, run the following command to isolate the minimal set of examples that reproduce the same failures:\n  `bundle exec rspec \#{str} --seed \#{ex['seed']} --bisect`\\n\n      TEXT\n      rerun_failed.clear\n    end\n  end\n\n  if errors.size > 0\n    puts \"\\Errors:\\n\"\n    errors.each do |err|\n      puts <<-TEXT\n      \#{err}\n      TEXT\n    end\n  end\n\n  if time_exceeding_examples.size > 0 || errors.size > 0\n    generate_xml(errors, time_exceeding_examples, time_limit)\n  end\n\n  if time_exceeding_examples.size > 0\n    puts \"\\nExecution time is exceeding the threshold of \#{@time_limit} seconds for following tests:\"\n    time_exceeding_examples.each do |ex|\n      puts <<-TEXT\n=> \#{ex[\"full_description\"]}: \#{ex[\"run_time\"]} \#{\"Seconds\"}\n      TEXT\n    end\n  else\n    puts \"Runtime check Passed.\"\n  end\n\n  if failed_examples.size > 0 || errors.size > 0 || time_exceeding_examples.size > 0\n    fail_message = \"Tests Failed\"\n    puts \"\\e[31m\#{fail_message}\\e[0m\"\n    exit 1\n  end\nend\n"