Skip to content

Commit

Permalink
Merge pull request #173 from mininny/master
Browse files Browse the repository at this point in the history
Add merging multiple xccovreport
  • Loading branch information
Josh Holtz authored Sep 24, 2020
2 parents 91f0a1b + 3409e81 commit abea21b
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions lib/xcov/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ def parse_xccoverage
# If no xccov direct path, use the old derived data path method
if xccov_file_direct_paths.nil?
extension = Xcov.config[:legacy_support] ? "xccoverage" : "xccovreport"

test_logs_path = derived_data_path + "Logs/Test/"
xccoverage_files = Dir["#{test_logs_path}*.#{extension}", "#{test_logs_path}*.xcresult/*/action.#{extension}"].sort_by { |filename| File.mtime(filename) }.reverse

UI.important("Derived content from #{Dir["#{test_logs_path}/*"]}")

xccoverage_files = Dir["#{test_logs_path}*.#{extension}", "#{test_logs_path}*.xcresult/*/action.#{extension}"]
if xccoverage_files.empty?
xcresult_paths = Dir["#{test_logs_path}*.xcresult"].sort_by { |filename| File.mtime(filename) }.reverse
xcresult_paths = Dir["#{test_logs_path}*.xcresult"]
xcresult_paths.each do |xcresult_path|
xcresults_to_parse_and_export << xcresult_path
end
Expand All @@ -77,6 +79,7 @@ def parse_xccoverage
# Iterates over xcresults
# Exports .xccovarchives
# Exports .xccovreports and collects the paths
# Merge .xccovreports if multiple exists and return merged report
unless xcresults_to_parse_and_export.empty?
xccoverage_files = process_xcresults!(xcresults_to_parse_and_export)
end
Expand All @@ -88,6 +91,7 @@ def parse_xccoverage

# Convert .xccoverage file to json
ide_foundation_path = Xcov.config[:legacy_support] ? nil : Xcov.config[:ideFoundationPath]
xccoverage_files = xccoverage_files.sort_by {|filename| File.mtime(filename)}.reverse
json_report = Xcov::Core::Parser.parse(xccoverage_files.first, Xcov.config[:output_directory], ide_foundation_path)
ErrorHandler.handle_error("UnableToParseXccoverageFile") if json_report.nil?

Expand Down Expand Up @@ -204,30 +208,66 @@ def xccov_file_direct_paths
def process_xcresults!(xcresult_paths)
output_path = Xcov.config[:output_directory]
FileUtils.mkdir_p(output_path)

return xcresult_paths.flat_map do |xcresult_path|

result_path = ""
index = 0

xcresult_paths.flat_map do |xcresult_path|
begin
parser = XCResult::Parser.new(path: xcresult_path)

# Exporting to same directory as xcresult
archive_paths = parser.export_xccovarchives(destination: output_path)
report_paths = parser.export_xccovreports(destination: output_path)

# Informating user of export paths
archive_paths.each do |path|
UI.important("Copying .xccovarchive to #{path}")
tmp_archive_paths = parser.export_xccovarchives(destination: output_path)
tmp_report_paths = parser.export_xccovreports(destination: output_path)

# Rename each file with global index
tmp_report_paths.each_with_index do |item, i|
File.rename(tmp_archive_paths[i], "#{output_path}/xccovarchive-#{index + i}.xccovarchive")
File.rename(item, "#{output_path}/xccovreport-#{index + i}.xccovreport")
index += 1
end
report_paths.each do |path|
UI.important("Copying .xccovreport to #{path}")
end

report_paths
rescue
UI.error("Error occured while exporting xccovreport from xcresult '#{xcresult_path}'")
UI.error("Make sure you have both Xcode 11 selected and pointing to the correct xcresult file")
UI.crash!("Failed to export xccovreport from xcresult'")
end
end

# Grab paths from the directory instead of parser
report_paths = Dir["#{output_path}/*.xccovreport"]
archive_paths = Dir["#{output_path}/*.xccovarchive"]

# Merge coverage reports
if report_paths.length > 1 then
# Creating array of paths for merging
paths = ""
for i in 0..report_paths.length
paths += " #{report_paths[i]} #{archive_paths[i]}"
end

UI.important("Merging multiple coverage reports with #{paths}")
if system ( "xcrun xccov merge --outReport #{output_path}/out.xccovreport --outArchive #{output_path}/out.xccovarchive #{paths}" ) then
result_path = "#{output_path}/out.xccovreport"
else
UI.error("Error occured during merging multiple coverage reports")
end
end

if result_path == "" then
# Informating user of export paths
archive_paths.each do |path|
UI.important("Copying .xccovarchive to #{path}")
end
report_paths.each do |path|
UI.important("Copying .xccovreport to #{path}")
end

# Return array of report_paths if coverage reports were not merged
return report_paths
else
# Return merged xccovreport
return [result_path]
end
end
end
end

0 comments on commit abea21b

Please sign in to comment.