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



131
132
133
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 131

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

.available_optionsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 78

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
    )
  ]
end

.categoryObject



135
136
137
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 135

def self.category
  :testing
end

.descriptionObject

:nocov:



74
75
76
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 74

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

.example_codeObject



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

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

    UI.important(
      'example: ' \\
      'get list of tests that are referenced from an xctestrun file'
    )
    # build the tests so that we have a xctestrun file to parse
    scan(
      build_for_testing: true,
      workspace: File.absolute_path('../AtomicBoy/AtomicBoy.xcworkspace'),
      scheme: 'AtomicBoy'
    )

    # find the xctestrun file
    derived_data_path = Scan.config[:derived_data_path]
    xctestrun_file = Dir.glob(\"\#{derived_data_path}/Build/Products/*.xctestrun\").first

    # get the tests from the xctestrun file
    tests = tests_from_xctestrun(xctestrun: xctestrun_file)
    UI.header('xctestrun file contains the following tests')
    tests.values.flatten.each { |test_identifier| puts test_identifier }
    "
  ]
end

.ignoredTestablesObject



65
66
67
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 65

def self.ignoredTestables
  return ['__xctestrun_metadata__']
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 139

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

.return_valueObject



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

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])
end

.subtract_skipped_tests_from_test_identifiers(test_identifiers, skipped_test_identifiers) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 39

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



60
61
62
63
# File 'lib/fastlane/plugin/test_center/actions/tests_from_xctestrun.rb', line 60

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

def self.xctestrun_tests(xctestrun_path, invocation_based_tests)
  xctestrun = Plist.parse_xml(xctestrun_path)
  xctestrun_rootpath = File.dirname(xctestrun_path)
  tests = Hash.new([])
  xctestrun.each do |testable_name, xctestrun_config|
    next if ignoredTestables.include? testable_name

    xctest_path = xctest_bundle_path(xctestrun_rootpath, xctestrun_config)
    test_identifiers = XCTestList.tests(xctest_path)
    UI.verbose("Found the following tests: #{test_identifiers.join("\n\t")}")

    if xctestrun_config.key?('SkipTestIdentifiers')
      test_identifiers = subtract_skipped_tests_from_test_identifiers(
        test_identifiers,
        xctestrun_config['SkipTestIdentifiers']
      )
    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}?")
    end
    tests[testable_name] = test_identifiers.map do |test_identifier|
      "#{testable_name}/#{test_identifier}"
    end
  end
  tests
end