From 6dac753a21791181a53b2bb9c95ae7ab397935e3 Mon Sep 17 00:00:00 2001 From: ainame Date: Fri, 23 Jul 2021 10:12:17 +0900 Subject: [PATCH] add XCResult::ExportOptions --- lib/xcresult/export_options.rb | 33 +++++++++++++++++++++++++++++++++ lib/xcresult/parser.rb | 13 +++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 lib/xcresult/export_options.rb diff --git a/lib/xcresult/export_options.rb b/lib/xcresult/export_options.rb new file mode 100644 index 0000000..f27a23d --- /dev/null +++ b/lib/xcresult/export_options.rb @@ -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 diff --git a/lib/xcresult/parser.rb b/lib/xcresult/parser.rb index 8e4a089..674cd45 100644 --- a/lib/xcresult/parser.rb +++ b/lib/xcresult/parser.rb @@ -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 @@ -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 } @@ -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