forked from rudderlabs/rudder-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xccov-to-generic.sh
96 lines (83 loc) · 2.73 KB
/
xccov-to-generic.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env bash
# xccov-to-generic.sh
# Rudder
#
# Created by Pallab Maiti on 29/01/23.
#
set -euo pipefail
function convert_file {
local xccovarchive_file="$1"
local file_name="$2"
local xccov_options="$3"
echo " <file path=\"$file_name\">"
xcrun xccov view $xccov_options --file "$file_name" "$xccovarchive_file" | \
sed -n '
s/^ *\([0-9][0-9]*\): 0.*$/ <lineToCover lineNumber="\1" covered="false"\/>/p;
s/^ *\([0-9][0-9]*\): [1-9].*$/ <lineToCover lineNumber="\1" covered="true"\/>/p
'
echo ' </file>'
}
function xccov_to_generic {
echo '<coverage version="1">'
for xccovarchive_file in "$@"; do
if [[ ! -d $xccovarchive_file ]]
then
echo "Coverage FILE NOT FOUND AT PATH: $xccovarchive_file" 1>&2;
exit 1
fi
if [ $sdk_version -gt 10 ]; then # Apply optimization
xccovarchive_file=$(optimize_format)
fi
local xccov_options=""
if [[ $xccovarchive_file == *".xcresult"* ]]; then
xccov_options="--archive"
fi
xcrun xccov view $xccov_options --file-list "$xccovarchive_file" | while read -r file_name; do
convert_file "$xccovarchive_file" "$file_name" "$xccov_options"
done
done
echo '</coverage>'
}
function check_sdk_version {
sdk_major_version=`xcrun --show-sdk-version | cut -d . -f 1`
if [ $? -ne 0 ]; then
echo 'Failed to execute xcrun show-sdk-version' 1>&2
exit -1
fi
echo $sdk_major_version
}
function cleanup_tmp_files {
rm -rf tmp.json
rm -rf tmp.xccovarchive
}
# Optimize coverage files conversion time by exporting to a clean xcodearchive directory
# Credits to silverhammermba on issue #68 for the suggestion
function optimize_format {
cleanup_tmp_files
xcrun xcresulttool get --format json --path "$xccovarchive_file" > tmp.json
if [ $? -ne 0 ]; then
echo 'Failed to execute xcrun xcresulttool get' 1>&2
exit -1
fi
# local reference=$(jq -r '.actions._values[2].actionResult.coverage.archiveRef.id._value' tmp.json)
local reference=$(jq -r '.actions._values[]|[.actionResult.coverage.archiveRef.id],._values' tmp.json | grep value | cut -d : -f 2 | cut -d \" -f 2)
if [ $? -ne 0 ]; then
echo 'Failed to execute jq (https://stedolan.github.io/jq/)' 1>&2
exit -1
fi
# $reference can be a list of IDs (from a merged .xcresult bundle of multiple test plans)
for test_ref in $reference; do
xcrun xcresulttool export --type directory --path "$xccovarchive_file" --id "$test_ref" --output-path tmp.xccovarchive
if [ $? -ne 0 ]; then
echo "Failed to execute xcrun xcresulttool export for reference ${test_ref}" 1>&2
exit -1
fi
done
echo "tmp.xccovarchive"
}
sdk_version=$(check_sdk_version)
if [ $? -ne 0 ]; then
exit -1
fi
xccov_to_generic "$@"
cleanup_tmp_files