Class: Fastlane::Actions::TestsFromXctestrunAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



172
173
174
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 172

def self.authors
  ["lyndsey-ferguson/lyndseydf"]
end

.available_optionsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 103

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :xctestrun,
      env_name: "FL_SUPPRESS_TESTS_FROM_XCTESTRUN_FILE",
      description: "The xctestrun file to use to find where the xctest bundle file is for test retrieval",
      verify_block: proc do |path|
        UI.user_error!("Error: cannot find the xctestrun file '#{path}'") unless File.exist?(path)
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :invocation_based_tests,
      description: "Set to true If your test suit have invocation based tests like Kiwi",
      type: Boolean,
      is_string: false,
      default_value: false,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :swift_test_prefix,
      description: "The prefix used to find test methods. In standard XCTests, this is `test`. If you are using Quick with Swift, set this to `spec`",
      default_value: "test",
      optional: true,
      verify_block: proc do |swift_test_prefix|
        UI.user_error!("Error: swift_test_prefix must be non-nil and non-empty") if swift_test_prefix.nil? || swift_test_prefix.empty?
      end
    )
  ]
end

.categoryObject



176
177
178
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 176

def self.category
  :testing
end

.descriptionObject

:nocov:



99
100
101
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 99

def self.description
  "️️☑️ Retrieves all of the tests from xctest bundles referenced by the xctestrun file"
end

.example_codeObject



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
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 137

def self.example_code
  [
    "
    require 'fastlane/actions/scan'

    lane :split_tests do
      scan(
        build_for_testing: true,
        workspace: File.absolute_path('../AtomicBoy/AtomicBoy.xcworkspace'),
        scheme: 'AtomicBoy'
      )
      derived_data_path = Scan.config[:derived_data_path]
      xctestrun_file = Dir.glob(\"\#{derived_data_path}/Build/Products/*.xctestrun\").first
      tests = tests_from_xctestrun(xctestrun: xctestrun_file).values.flatten.shuffle
      slice_size = (tests.size/4.0).ceil
      tests.each_slice(slice_size).each_with_index do |inner_array, index|
        File.write(\"test_output/batch\#{index}.txt\", inner_array.join(','))
      end
    end

    lane :run_split_tests do |options|
      batch_file = File.join('test_output', \"batch\#{options[:batch_index]}.txt\")
      only_testing = File.read(batch_file).split(',')
      multi_scan(
        workspace: File.absolute_path('../AtomicBoy/AtomicBoy.xcworkspace'),
        scheme: 'AtomicBoy',
        try_count: 3,
        fail_build: false,
        only_testing: only_testing
      )
    end
    "
  ]
end

.ignoredTestablesObject



90
91
92
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 90

def self.ignoredTestables
  return ['__xctestrun_metadata__']
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 180

def self.is_supported?(platform)
  %i[ios mac].include?(platform)
end

.return_valueObject



133
134
135
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 133

def self.return_value
  "A Hash of testable => tests, where testable is the name of the test target and tests is an array of test identifiers"
end

.run(params) ⇒ Object



6
7
8
9
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 6

def self.run(params)
  UI.verbose("Getting tests from xctestrun file at '#{params[:xctestrun]}'")
  return xctestrun_tests(params[:xctestrun], params[:invocation_based_tests], swift_test_prefix: params[:swift_test_prefix])
end

.subtract_skipped_tests_from_test_identifiers(test_identifiers, skipped_test_identifiers) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 64

def self.subtract_skipped_tests_from_test_identifiers(test_identifiers, skipped_test_identifiers)
  skipped_tests_identifiers = []
  skipped_testsuites = []
  skipped_test_identifiers.each do |skipped_test|
    if skipped_test.split('/').size > 1
      skipped_tests_identifiers << skipped_test
    else
      skipped_testsuites << skipped_test
    end
  end
  skipped_testsuites.each do |skipped_testsuite|
    derived_skipped_tests = test_identifiers.select do |test_identifier|
      test_identifier.start_with?(skipped_testsuite)
    end
    skipped_tests_identifiers.concat(derived_skipped_tests)
  end

  UI.verbose("Removing skipped tests: #{skipped_tests_identifiers.join("\n\t")}")
  test_identifiers.reject { |test_identifier| skipped_tests_identifiers.include?(test_identifier) }
end

.xctest_bundle_path(xctestrun_rootpath, xctestrun_config) ⇒ Object



85
86
87
88
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 85

def self.xctest_bundle_path(xctestrun_rootpath, xctestrun_config)
  xctest_host_path = xctestrun_config['TestHostPath'].sub('__TESTROOT__', xctestrun_rootpath)
  xctestrun_config['TestBundlePath'].sub('__TESTHOST__', xctest_host_path).sub('__TESTROOT__', xctestrun_rootpath)
end

.xctestrun_tests(xctestrun_path, invocation_based_tests, swift_test_prefix: "test") ⇒ Object



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
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 11

def self.xctestrun_tests(xctestrun_path, invocation_based_tests, swift_test_prefix: "test")
  xctestrun = Plist.parse_xml(xctestrun_path)
  xctestrun_rootpath = File.dirname(xctestrun_path)
  xctestrun_version = xctestrun.fetch('__xctestrun_metadata__', Hash.new).fetch('FormatVersion', 1)

  test_targets = []
  if xctestrun_version == 1
    xctestrun.each do |testable_name, test_target_config|
      next if ignoredTestables.include? testable_name
      test_target_config['TestableName'] = testable_name
      test_targets << test_target_config
    end
  else
    test_configurations = xctestrun['TestConfigurations']
    test_configurations.each do |configuration|
      configuration['TestTargets'].each do |test_target|
        test_target['TestableName'] = test_target['BlueprintName']
        test_targets << test_target
      end
    end
  end

  tests = Hash.new([])
  test_targets.each do |xctestrun_config|
    testable_name = xctestrun_config['TestableName']
    xctest_path = xctest_bundle_path(xctestrun_rootpath, xctestrun_config)
    test_identifiers = []
    if xctestrun_config.key?('OnlyTestIdentifiers')
      test_identifiers = xctestrun_config['OnlyTestIdentifiers']
      UI.verbose("Identifiers after adding onlytest tests: #{test_identifiers.join("\n\t")}")
    else
      test_identifiers = XCTestList.tests(xctest_path, swift_test_prefix: swift_test_prefix)
      UI.verbose("Found the following tests: #{test_identifiers.join("\n\t")}")
    end
    if xctestrun_config.key?('SkipTestIdentifiers')
      test_identifiers = subtract_skipped_tests_from_test_identifiers(
        test_identifiers,
        xctestrun_config['SkipTestIdentifiers']
      )
      UI.verbose("Identifiers after removing skipped tests: #{test_identifiers.join("\n\t")}")
    end
    if test_identifiers.empty? && !invocation_based_tests
      UI.error("No tests found in '#{xctest_path}'!")
      UI.important("Is the Build Setting, `ENABLE_TESTABILITY` enabled for the test target #{testable_name}?")
      UI.message("If your Swift test method names use a prefix other than `test`, consider setting `:swift_test_prefix`.")
    end
    tests[testable_name] = test_identifiers.map do |test_identifier|
      "#{testable_name}/#{test_identifier}"
    end
  end
  tests
end