summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/reports/test_suite.rb
blob: 8bbf2e0f6cfe20b6225553cec54c110ceb3c04e0 (plain)
1
2
3
4
5
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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Reports
      class TestSuite
        attr_reader :name
        attr_reader :test_cases
        attr_reader :total_time
        attr_reader :suite_error

        def initialize(name = nil)
          @name = name
          @test_cases = {}
          @total_time = 0.0
          @duplicate_cases = []
        end

        def add_test_case(test_case)
          @duplicate_cases << test_case if existing_key?(test_case)

          @test_cases[test_case.status] ||= {}
          @test_cases[test_case.status][test_case.key] = test_case
          @total_time += test_case.execution_time
        end

        # rubocop: disable CodeReuse/ActiveRecord
        def total_count
          return 0 if suite_error

          test_cases.values.sum(&:count)
        end
        # rubocop: enable CodeReuse/ActiveRecord

        def total_status
          if suite_error
            TestCase::STATUS_ERROR
          elsif failed_count > 0 || error_count > 0
            TestCase::STATUS_FAILED
          else
            TestCase::STATUS_SUCCESS
          end
        end

        def with_attachment!
          @test_cases = @test_cases.extract!("failed")

          @test_cases.keep_if do |status, hash|
            hash.any? do |key, test_case|
              test_case.has_attachment?
            end
          end
        end

        TestCase::STATUS_TYPES.each do |status_type|
          define_method("#{status_type}") do
            return {} if suite_error || test_cases[status_type].nil?

            test_cases[status_type]
          end

          define_method("#{status_type}_count") do
            return 0 if suite_error || test_cases[status_type].nil?

            test_cases[status_type].length
          end
        end

        def set_suite_error(msg)
          @suite_error = msg
        end

        private

        def existing_key?(test_case)
          @test_cases[test_case.status]&.key?(test_case.key)
        end
      end
    end
  end
end