Skip to content

Commit a4f5dcb

Browse files
committed
Add more options
1 parent 3808edd commit a4f5dcb

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

lib/xcresult/export_options.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
module XCResult
22
class ExportOptions
3-
AVAILABLE_OPTIONS = %i[destination by_device by_locale].freeze
3+
AVAILABLE_OPTIONS = %i[destination by_device by_locale by_region by_language by_os_version by_test_name].freeze
44

5+
# A initializer of ExportOptions class. The order of options that are prefixed with `by_` is important.
6+
# `XCResult::Parser#export_screenshots` will create nested directories by the order you give.
7+
#
8+
# @option [String] destination The base directory path of exported items
9+
# @option [Boolean] by_device If true, a nested directory under destination will be made based on device model name
10+
# @option [Boolean] by_locale If true, a nested directory under destination will be made based on locale; i.e. "en_US"
11+
# @option [Boolean] by_region If true, a nested directory under destination will be made based on region
12+
# @option [Boolean] by_language If true, a nested directory under destination will be made based on language
13+
# @option [Boolean] by_os_version If true, a nested directory under destination will be made based on OS version
14+
# @option [Boolean] by_test_name If true, a nested directory under destination will be made based on test name in TestPlan
515
def initialize(**options)
616
raise ':destination option is required' unless options[:destination]
717

@@ -13,17 +23,26 @@ def initialize(**options)
1323
@destination = options.delete(:destination)
1424
end
1525

16-
def output_directory(target_device_record:, action_testable_summary:)
26+
def output_directory(target_device_record:, action_test_plan_summary:, action_testable_summary:)
1727
output_directory = @destination
1828

1929
# keep the order of given options so that you can customize output directory path based on your needs
20-
@options.each do |key, value|
21-
if key == :by_device && value == true
30+
@options.select { |key, value| key.to_s.start_with?('by_') && value == true }.each do |key, _|
31+
case key
32+
when :by_device
2233
output_directory = File.join(output_directory, target_device_record.model_name)
23-
elsif key == :by_locale && value == true
34+
when :by_language
35+
output_directory = File.join(output_directory, action_testable_summary.test_language)
36+
when :by_region
37+
output_directory = File.join(output_directory, action_testable_summary.test_region)
38+
when :by_locale
2439
locale = [action_testable_summary.test_language, action_testable_summary.test_region].compact.join('_')
2540
locale = 'UNKOWN' if locale.empty?
2641
output_directory = File.join(output_directory, locale)
42+
when :by_os_version
43+
output_directory = File.join(output_directory, target_device_record.operating_system_version)
44+
when :by_test_name
45+
output_directory = File.join(output_directory, action_test_plan_summary.name)
2746
end
2847
end
2948

lib/xcresult/parser.rb

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,34 @@ def export_screenshots(options = XCResult::ExportOptions.new(destination: Dir.pw
7878
# Iterate through each action as it represents run destination (testing device)
7979
actions.each do |action|
8080
action_test_plan_run_summaries = action.action_result.tests_ref.load_object(from: path)
81-
action_testable_summaries = action_test_plan_run_summaries.summaries.flat_map(&:testable_summaries)
82-
83-
# Iterate thorugh each action_testable_summary as it represents testing conditions such as region and language
84-
action_testable_summaries.each do |action_testable_summary|
85-
# Collect all attachments from a testing condition
86-
attachments = action_testable_summary.all_tests
87-
.map(&:summary_ref)
88-
.map { |ref| ref.load_object(from: path) }
89-
.flat_map(&:activity_summaries)
90-
.flat_map(&:subactivities)
91-
.flat_map(&:attachments)
92-
93-
# Prepare output directory for each tests
94-
output_directory = options.output_directory(
95-
target_device_record: action.run_destination.target_device_record,
96-
action_testable_summary: action_testable_summary
97-
)
98-
FileUtils.mkdir_p(output_directory) unless Dir.exist?(output_directory)
99-
100-
# Finally exports attachments
101-
attachments.each do |attachment|
102-
output_path = File.join(output_directory, attachment.filename)
103-
cmd = "xcrun xcresulttool export --path #{path} --id '#{attachment.payload_ref.id}' --output-path '#{output_path}' --type file"
104-
execute_cmd(cmd)
81+
action_test_plan_summaries = action_test_plan_run_summaries.summaries
82+
83+
# Iterate thorugh each action_test_plan_summaries as it represents test name
84+
action_test_plan_summaries.each do |action_test_plan_summary|
85+
# Iterate thorugh each action_testable_summary as it represents testing conditions such as region and language
86+
action_test_plan_summary.testable_summaries.each do |action_testable_summary|
87+
# Collect all attachments from a testing condition
88+
attachments = action_testable_summary.all_tests
89+
.map(&:summary_ref)
90+
.map { |ref| ref.load_object(from: path) }
91+
.flat_map(&:activity_summaries)
92+
.flat_map(&:subactivities)
93+
.flat_map(&:attachments)
94+
95+
# Prepare output directory for each tests
96+
output_directory = options.output_directory(
97+
target_device_record: action.run_destination.target_device_record,
98+
action_test_plan_summary: action_test_plan_summary,
99+
action_testable_summary: action_testable_summary
100+
)
101+
FileUtils.mkdir_p(output_directory) unless Dir.exist?(output_directory)
102+
103+
# Finally exports attachments
104+
attachments.each do |attachment|
105+
output_path = File.join(output_directory, attachment.filename)
106+
cmd = "xcrun xcresulttool export --path #{path} --id '#{attachment.payload_ref.id}' --output-path '#{output_path}' --type file"
107+
execute_cmd(cmd)
108+
end
105109
end
106110
end
107111
end

0 commit comments

Comments
 (0)