Skip to content

Commit

Permalink
add XCResult::ExportOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ainame committed Nov 24, 2021
1 parent 540384e commit 5c49bf7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
33 changes: 33 additions & 0 deletions lib/xcresult/export_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module XCResult
class ExportOptions
AVAILABLE_OPTIONS = %i[destination by_device by_locale].freeze

def initialize(**options)
raise ':destination option is required' unless options[:destination]

options.each do |key, value|
raise "Found unknown option #{key} - #{value}." unless AVAILABLE_OPTIONS.include?(key)
end

@options = options.dup
@destination = options.delete(:destination)
end

def output_directory(target_device_record:, action_testable_summary:)
output_directory = @destination

# keep the order of given options so that you can customize output directory path based on your needs
@options.each do |key, value|
if key == :by_device && value == true
output_directory = File.join(output_directory, target_device_record.model_name)
elsif key == :by_locale && value == true
locale = [action_testable_summary.test_language, action_testable_summary.test_region].compact.join('_')
locale = 'UNKOWN' if locale.empty?
output_directory = File.join(output_directory, locale)
end
end

output_directory
end
end
end
13 changes: 7 additions & 6 deletions lib/xcresult/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'json'
require 'fileutils'

require_relative 'export_options'

module XCResult
class Parser
attr_accessor :path, :result_bundle_json, :actions_invocation_record
Expand Down Expand Up @@ -69,7 +71,7 @@ def export_xccovarchives(destination: nil)
end
end

def export_screenshots(destination: Dir.pwd, by_device: false, by_locale: false)
def export_screenshots(options = XCResult::ExportOptions.new(destination: Dir.pwd))
# Filter non test results; i.e build action
actions = actions_invocation_record.actions.select {|x| x.action_result.tests_ref }

Expand All @@ -89,11 +91,10 @@ def export_screenshots(destination: Dir.pwd, by_device: false, by_locale: false)
.flat_map(&:attachments)

# Prepare output directory for each tests
locale = [action_testable_summary.test_language, action_testable_summary.test_region].compact.join('_')
locale = 'UNKOWN' if locale.empty?
output_directory = destination
output_directory = File.join(output_directory, action.run_destination.target_device_record.model_name) if by_device
output_directory = File.join(output_directory, locale) if by_locale
output_directory = options.output_directory(
target_device_record: action.run_destination.target_device_record,
action_testable_summary: action_testable_summary
)
FileUtils.mkdir_p(output_directory) unless Dir.exist?(output_directory)

# Finally exports attachments
Expand Down

0 comments on commit 5c49bf7

Please sign in to comment.