Class: TestCenter::Helper::TestCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/test_center/helper/test_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TestCollector

Returns a new instance of TestCollector.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 11

def initialize(options)
  unless options[:xctestrun] || options[:derived_data_path]
    options[:derived_data_path] = default_derived_data_path(options)
  end
  @xctestrun_path = options[:xctestrun] || derived_testrun_path(options[:derived_data_path], options[:scheme])
  unless @xctestrun_path && File.exist?(@xctestrun_path)
    FastlaneCore::UI.user_error!("Error: cannot find xctestrun file '#{@xctestrun_path}'")
  end
  @only_testing = options[:only_testing] || only_testing_from_testplan(options)
  if @only_testing.kind_of?(String)
    @only_testing = @only_testing.split(',')
  end
  @skip_testing = options[:skip_testing]
  @invocation_based_tests = options[:invocation_based_tests]
  @batch_count = options[:batch_count]
  if @batch_count == 1 && options[:parallel_testrun_count] > 1
    @batch_count = options[:parallel_testrun_count]
  end

  @swift_test_prefix = options[:swift_test_prefix]
end

Instance Attribute Details

#only_testingObject (readonly)

Returns the value of attribute only_testing.



9
10
11
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 9

def only_testing
  @only_testing
end

#xctestrun_pathObject (readonly)

Returns the value of attribute xctestrun_path.



8
9
10
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 8

def xctestrun_path
  @xctestrun_path
end

Instance Method Details

#default_derived_data_path(options) ⇒ Object



63
64
65
66
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 63

def default_derived_data_path(options)
  project_derived_data_path = Scan.project.build_settings(key: "BUILT_PRODUCTS_DIR")
  File.expand_path("../../..", project_derived_data_path)
end

#derived_testrun_path(derived_data_path, scheme) ⇒ Object



68
69
70
71
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 68

def derived_testrun_path(derived_data_path, scheme)
  xctestrun_files = Dir.glob("#{derived_data_path}/Build/Products/*.xctestrun")
  xctestrun_files.sort { |f1, f2| File.mtime(f1) <=> File.mtime(f2) }.last
end

#expand_short_testidentifiers_to_tests(testables_tests) ⇒ Object



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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 108

def expand_short_testidentifiers_to_tests(testables_tests)
  # Remember, testable_tests is of the format:
  # {
  #   'testable1' => [
  #     'testsuite1/testcase1',
  #     'testsuite1/testcase2',
  #     'testsuite2/testcase1',
  #     'testsuite2/testcase2',
  #     ...
  #     'testsuiteN/testcase1', ... 'testsuiteN/testcaseM'
  #   ],
  #   ...
  #   'testableO' => [
  #     'testsuite1/testcase1',
  #     'testsuite1/testcase2',
  #     'testsuite2/testcase1',
  #     'testsuite2/testcase2',
  #     ...
  #     'testsuiteN/testcase1', ... 'testsuiteN/testcaseM'
  #   ]
  # }
  return if @invocation_based_tests

  # iterate among all the test identifers for each testable
  # A test identifier is seperated into components by '/'
  # if a test identifier has only 2 separators, it probably is
  # 'testable/testsuite' (but it could be 'testsuite/testcase' )
  all_known_tests = nil
  known_tests = []
  testables_tests.each do |testable, tests|
    tests.each_with_index do |test, index|
      test_components = test.split('/')
      is_full_test_identifier = (test_components.size == 3)
      next if is_full_test_identifier

      all_known_tests ||= xctestrun_known_tests.clone

      testsuite = ''
      if test_components.size == 1
        if test_components[0] == testable
          # The following || [] is just in case the user provided multiple
          # test targets or there are no actual tests found.
          testables_tests[testable][index] = all_known_tests[testable] || []
          all_known_tests.delete(testable)
          next
        end

        testsuite = test_components[0]
      else
        testsuite = test_components[1]
      end

      testables_tests[testable][index], all_known_tests[testable] = all_known_tests[testable].partition do |known_test|
        known_test.split('/')[1] == testsuite
      end
    end
    testables_tests[testable].flatten!
  end
end

#only_testing_from_testplan(options) ⇒ Object



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
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 33

def only_testing_from_testplan(options)
  return unless options[:testplan] && options[:scheme]

  config = FastlaneCore::Configuration.create(
    Fastlane::Actions::TestplansFromSchemeAction.available_options,
    {
      workspace: options[:workspace],
      xcodeproj: options[:project],
      scheme: options[:scheme]
    }
  )
  testplans = Fastlane::Actions::TestplansFromSchemeAction.run(config)
  FastlaneCore::UI.verbose("TestCollector found testplans: #{testplans}")
  testplan = testplans.find do |testplan_path|
    %r{(.*/?#{ options[:testplan] })\.xctestplan}.match?(testplan_path)
  end
  FastlaneCore::UI.verbose("  using :testplan option, #{options[:testplan]}, using found one: #{testplan}")

  return if testplan.nil?

  config = FastlaneCore::Configuration.create(
    Fastlane::Actions::TestOptionsFromTestplanAction.available_options,
    {
      testplan: testplan
    }
  )
  test_options = Fastlane::Actions::TestOptionsFromTestplanAction.run(config)
  return test_options[:only_testing]
end

#only_testing_to_testables_testsObject



84
85
86
87
88
89
90
91
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 84

def only_testing_to_testables_tests
  tests = Hash.new { |h, k| h[k] = [] }
  @only_testing.sort.each do |test_identifier|
    testable = test_identifier.split('/', 2)[0]
    tests[testable] << test_identifier
  end
  tests
end

#test_batchesObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 191

def test_batches
  if @batches.nil?
    @batches = []
    testables.each do |testable|
      testable_tests = testables_tests[testable]
      next if testable_tests.empty?

      if @batch_count > 1
        slice_count = [(testable_tests.length / @batch_count.to_f).ceil, 1].max
        testable_tests.each_slice(slice_count).to_a.each do |tests_batch|
          @batches << tests_batch
        end
      else
        @batches << testable_tests
      end
    end
  end

  @batches
end

#testablesObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 73

def testables
  unless @testables
    if @only_testing
      @testables ||= only_testing_to_testables_tests.keys
    else
      @testables = xctestrun_known_tests.keys
    end
  end
  @testables
end

#testables_testsObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 168

def testables_tests
  unless @testables_tests
    if @only_testing
      @testables_tests = only_testing_to_testables_tests
      expand_short_testidentifiers_to_tests(@testables_tests)
    else
      @testables_tests = xctestrun_known_tests
      if @skip_testing
        skipped_testable_tests = Hash.new { |h, k| h[k] = [] }
        @skip_testing.sort.each do |skipped_test_identifier|
          testable = skipped_test_identifier.split('/', 2)[0]
          skipped_testable_tests[testable] << skipped_test_identifier
        end
        @testables_tests.each_key do |testable|
          @testables_tests[testable] -= skipped_testable_tests[testable]
        end
      end
    end
  end

  @testables_tests
end

#xctestrun_known_testsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/test_center/helper/test_collector.rb', line 93

def xctestrun_known_tests
  unless @known_tests
    config = FastlaneCore::Configuration.create(
      ::Fastlane::Actions::TestsFromXctestrunAction.available_options,
      {
        xctestrun: @xctestrun_path,
        invocation_based_tests: @invocation_based_tests,
        swift_test_prefix: @swift_test_prefix
      }
    )
    @known_tests = ::Fastlane::Actions::TestsFromXctestrunAction.run(config)
  end
  @known_tests
end