-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fastfile
170 lines (135 loc) · 4.57 KB
/
Fastfile
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
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
167
168
169
170
# frozen_string_literal: true
# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Actions.md
# can also be listed using the `fastlane actions` command
# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`
# This is the minimum version number required.
fastlane_version '2.210.1'
default_platform :ios
platform :ios do
before_all do
ensure_git_status_clean
# Use .xcode-version file
xcodes(select_for_current_build_only: true)
end
desc 'Run library tests'
lane :tests do
clean_result_files
clean_derived_data
iphone14 = Device.new('iOS', 'iPhone 15')
appletv = Device.new('tvOS', 'Apple TV')
devices = [iphone14, appletv]
scheme = swift_package_name
swift_package_tests = TestBuild.new(scheme, '-Package', false)
test_builds = [swift_package_tests]
# Run all tests on various simulators
srg_run_tests(devices, test_builds)
# Produce JUnit files for CI
srg_junit_files
end
after_all do
reset_git_repo(skip_clean: true)
end
error do
clean_build_artifacts
reset_git_repo(skip_clean: true, force: true)
end
end
Device = Struct.new :platform, :name
TestBuild = Struct.new :scheme, :scheme_suffix, :in_workspace
def swift_package_name
JSON.parse((sh 'swift package dump-package'))['name']
end
def clean_result_files
Dir['*.xml'].each { |file| File.delete(file) }
FileUtils.remove_entry('xcresult', true)
end
def clean_derived_data
Dir.chdir('..') { FileUtils.remove_entry(srg_xcodebuild_derived_data_path, true) }
end
def srg_run_tests(devices, test_builds)
devices.each do |device|
test_builds.each do |test_build|
srg_xcodebuild(device, test_build)
rescue StandardError => e
raise e unless e.message.include? '** TEST FAILED **'
UI.important("One or more tests failed on #{device.platform} (#{srg_xcodebuild_scheme(test_build)}). ⚠️")
end
end
end
def srg_xcodebuild(device, test_build)
xcodebuild(
test: true,
xcargs: srg_test_xcargs,
workspace: srg_xcodebuild_workspace(test_build),
scheme: srg_xcodebuild_scheme(test_build),
destination: srg_xcodebuild_destination(device),
result_bundle_path: srg_xcodebuild_result_bundle_path(device, test_build),
derivedDataPath: srg_xcodebuild_derived_data_path
)
end
def srg_test_xcargs
'-retry-tests-on-failure -testLanguage en -testRegion en-US'
end
def srg_xcodebuild_workspace(test_build)
test_build.in_workspace ? "Tests/#{srg_xcodebuild_scheme(test_build)}.xcworkspace" : nil
end
def srg_xcodebuild_scheme(test_build)
test_build.scheme + test_build.scheme_suffix
end
def srg_xcodebuild_destination(device)
"platform=#{device.platform} Simulator,name=#{device.name}"
end
def srg_xcodebuild_result_bundle_path(device, test_build)
"#{result_bundle_folder_path}#{test_build.scheme}-#{device.platform}"
end
def srg_xcodebuild_derived_data_path
'.build/DerivedData'
end
def srg_junit_files
srg_trainer
override_junit_test_suite_names
expose_junit_files
end
# Convert xcresults to JUnit files
def srg_trainer
trainer(
path: result_bundle_folder_path,
output_directory: './fastlane',
extension: raw_extension,
output_remove_retry_attempts: true,
fail_build: false
)
end
def result_bundle_folder_path
'./fastlane/xcresult/'
end
def raw_extension
'.rawjunit'
end
# Override JUnit test suite names to split iOS and tvOS test results
def override_junit_test_suite_names
Dir["*#{raw_extension}"].each do |file_name|
override_junit_test_suite_name(file_name)
end
end
def override_junit_test_suite_name(file_name)
platform = file_name.split('.').first.split('-').last
file = File.open(file_name, 'r')
xml = file.read.gsub('Tests" tests="', "-#{platform}\" tests=\"")
xml = xml.gsub('-tests" tests="', "-#{platform}\" tests=\"")
File.write(file_name, xml)
end
def expose_junit_files
Dir["*#{raw_extension}"].each do |file_name|
# Rename JUnit files with correct extension
new_file_name = file_name.gsub(raw_extension, '.xml')
File.rename(file_name, new_file_name) unless File.exist?(new_file_name)
end
end
# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Actions.md
# fastlane reports which actions are used
# No personal data is recorded. Learn more at https://github.com/fastlane/enhancer