Skip to content

Commit

Permalink
[scan] add option for zipping build productions (fastlane#12378)
Browse files Browse the repository at this point in the history
* Zip derived data after scan run

* Working on tests

* Zip up products

* Zipping contents of build/products directory instead of zipping product directory

* Added new option to zip method for contents_only

* Fix tests for windows

* Standard rubo stuff
  • Loading branch information
Josh Holtz authored Apr 30, 2018
1 parent aa29ce2 commit 77f359d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fastlane_core/lib/fastlane_core/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ def self.strip_ansi_colors(str)
str.gsub(/\e\[([;\d]+)?m/, '')
end

# Zips directory
def self.zip_directory(path, output_path, contents_only: false, print: true)
if contents_only
command = "cd '#{path}' && zip -r '#{output_path}' *"
else
containing_path = File.expand_path("..", path)
contents_path = File.basename(path)

command = "cd '#{containing_path}' && zip -r '#{output_path}' '#{contents_path}'"
end

UI.command(command) unless print
Helper.backticks(command, print: print)
end

# loading indicator
#

Expand Down
23 changes: 23 additions & 0 deletions fastlane_core/spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,28 @@
expect(FastlaneCore::Helper.xcode_version).to match(/^\d[\.\d]+$/)
end
end

describe "#zip_directory" do
let(:directory) { File.absolute_path('/tmp/directory') }
let(:directory_to_zip) { File.absolute_path('/tmp/directory/to_zip') }
let(:the_zip) { File.absolute_path('/tmp/thezip.zip') }

it "creates correct zip command with contents_only set to false with default print option (true)" do
expect(FastlaneCore::Helper).to receive(:backticks)
.with("cd '#{directory}' && zip -r '#{the_zip}' 'to_zip'", print: true)
.exactly(1).times

FastlaneCore::Helper.zip_directory(directory_to_zip, the_zip, contents_only: false)
end

it "creates correct zip command with contents_only set to true with print set to false" do
expect(FastlaneCore::Helper).to receive(:backticks)
.with("cd '#{directory_to_zip}' && zip -r '#{the_zip}' *", print: false)
.exactly(1).times
expect(FastlaneCore::UI).to receive(:command).exactly(1).times

FastlaneCore::Helper.zip_directory(directory_to_zip, the_zip, contents_only: true, print: false)
end
end
end
end
7 changes: 7 additions & 0 deletions scan/lib/scan/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ def self.available_options
env_name: "SCAN_DERIVED_DATA_PATH",
description: "The directory where build products and other derived data will go",
optional: true),
FastlaneCore::ConfigItem.new(key: :should_zip_build_products,
short_option: "-Z",
env_name: "SCAN_SHOULD_ZIP_BUILD_PRODUCTS",
description: "Should zip the derived data build products and place in output path?",
optional: true,
is_string: false,
default_value: false),
FastlaneCore::ConfigItem.new(key: :result_bundle,
short_option: "-z",
env_name: "SCAN_RESULT_BUNDLE",
Expand Down
19 changes: 19 additions & 0 deletions scan/lib/scan/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,30 @@ def handle_results(tests_exit_status)
UI.test_failure!("Test execution failed. Exit status: #{tests_exit_status}")
end

zip_build_products

if !Helper.ci? && Scan.cache[:open_html_report_path]
`open --hide '#{Scan.cache[:open_html_report_path]}'`
end
end

def zip_build_products
return unless Scan.config[:should_zip_build_products]

# Gets :derived_data_path/Build/Products directory for zipping zip
derived_data_path = Scan.config[:derived_data_path]
path = File.join(derived_data_path, "Build/Products")

# Gets absolute path of output directory
output_directory = File.absolute_path(Scan.config[:output_directory])
output_path = File.join(output_directory, "build_products.zip")

# Zips build products and moves it to output directory
UI.message("Zipping build products")
FastlaneCore::Helper.zip_directory(path, output_path, contents_only: true, print: false)
UI.message("Succesfully zipped build products: #{output_path}")
end

def test_results
temp_junit_report = Scan.cache[:temp_junit_report]
return File.read(temp_junit_report) if temp_junit_report && File.file?(temp_junit_report)
Expand Down
36 changes: 36 additions & 0 deletions scan/spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,41 @@
end
end
end

describe "#zip_build_products" do
it "doesn't zip data when :should_zip_build_products is false", requires_xcodebuild: true do
Scan.config = FastlaneCore::Configuration.create(Scan::Options.available_options, {
output_directory: '/tmp/scan_results',
project: './scan/examples/standard/app.xcodeproj',
should_zip_build_products: false
})

expect(FastlaneCore::Helper).to receive(:backticks).with(anything).exactly(0).times

scan = Scan::Runner.new
scan.zip_build_products
end

it "zips data when :should_zip_build_products is true", requires_xcodebuild: true do
Scan.config = FastlaneCore::Configuration.create(Scan::Options.available_options, {
output_directory: '/tmp/scan_results',
derived_data_path: '/tmp/derived_data/app',
project: './scan/examples/standard/app.xcodeproj',
should_zip_build_products: true
})

path = File.join(Scan.config[:derived_data_path], "Build/Products")
path = File.absolute_path(path)

output_path = File.absolute_path('/tmp/scan_results/build_products.zip')

expect(FastlaneCore::Helper).to receive(:backticks)
.with("cd '#{path}' && zip -r '#{output_path}' *", { print: false })
.exactly(1).times

scan = Scan::Runner.new
scan.zip_build_products
end
end
end
end

0 comments on commit 77f359d

Please sign in to comment.