Skip to content

Commit d20fc44

Browse files
maxwellEbrentleyjonesaaronsky
authored
Execute coverage logic (if enabled) prior to post action binary in xc… (#2803)
…testrunner This allows consumers in post action binary to act upon coverage results (if they are enabled) --------- Signed-off-by: Maxwell Elliott <[email protected]> Co-authored-by: Brentley Jones <[email protected]> Co-authored-by: Aaron Sky <[email protected]>
1 parent bdd83d9 commit d20fc44

File tree

1 file changed

+85
-82
lines changed

1 file changed

+85
-82
lines changed

apple/testing/default_runner/ios_xctestrun_runner.template.sh

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,83 @@ else
553553
2>&1 | tee -i "$testlog" || test_exit_code=$?
554554
fi
555555

556+
if [[ "${COVERAGE:-}" -eq 1 || "${APPLE_COVERAGE:-}" -eq 1 ]]; then
557+
profdata="$test_tmp_dir/$simulator_id/Coverage.profdata"
558+
if [[ "$should_use_xcodebuild" == false ]]; then
559+
profdata="$test_tmp_dir/coverage.profdata"
560+
fi
561+
562+
if [[ "${COLLECT_PROFDATA:-0}" == "1" && -f "$profdata" ]]; then
563+
cp -R "$profdata" "$TEST_UNDECLARED_OUTPUTS_DIR"
564+
fi
565+
566+
if [[ "$should_use_xcodebuild" == false ]]; then
567+
xcrun llvm-profdata merge "$profraw" --output "$profdata"
568+
fi
569+
570+
lcov_args=(
571+
-instr-profile "$profdata"
572+
-ignore-filename-regex='.*external/.+'
573+
-path-equivalence=".,$PWD"
574+
)
575+
has_binary=false
576+
IFS=";"
577+
arch=$(uname -m)
578+
for binary in $TEST_BINARIES_FOR_LLVM_COV; do
579+
if [[ "$has_binary" == false ]]; then
580+
lcov_args+=("${binary}")
581+
has_binary=true
582+
if ! file "$binary" | grep -q "$arch"; then
583+
arch=x86_64
584+
fi
585+
else
586+
lcov_args+=(-object "${binary}")
587+
fi
588+
589+
lcov_args+=("-arch=$arch")
590+
done
591+
592+
llvm_coverage_manifest="$COVERAGE_MANIFEST"
593+
readonly provided_coverage_manifest="%(test_coverage_manifest)s"
594+
if [[ -s "${provided_coverage_manifest:-}" ]]; then
595+
llvm_coverage_manifest="$provided_coverage_manifest"
596+
fi
597+
598+
readonly error_file="$test_tmp_dir/llvm-cov-error.txt"
599+
llvm_cov_status=0
600+
xcrun llvm-cov \
601+
export \
602+
-format lcov \
603+
"${lcov_args[@]}" \
604+
@"$llvm_coverage_manifest" \
605+
> "$COVERAGE_OUTPUT_FILE" \
606+
2> "$error_file" \
607+
|| llvm_cov_status=$?
608+
609+
# Error ourselves if lcov outputs warnings, such as if we misconfigure
610+
# something and the file path of one of the covered files doesn't exist
611+
if [[ -s "$error_file" || "$llvm_cov_status" -ne 0 ]]; then
612+
echo "error: while exporting coverage report" >&2
613+
cat "$error_file" >&2
614+
fi
615+
616+
if [[ -n "${COVERAGE_PRODUCE_JSON:-}" ]]; then
617+
llvm_cov_json_export_status=0
618+
xcrun llvm-cov \
619+
export \
620+
-format text \
621+
"${lcov_args[@]}" \
622+
@"$llvm_coverage_manifest" \
623+
> "$TEST_UNDECLARED_OUTPUTS_DIR/coverage.json" \
624+
2> "$error_file" \
625+
|| llvm_cov_json_export_status=$?
626+
if [[ -s "$error_file" || "$llvm_cov_json_export_status" -ne 0 ]]; then
627+
echo "error: while exporting json coverage report" >&2
628+
cat "$error_file" >&2
629+
fi
630+
fi
631+
fi
632+
556633
# Run a post-action binary, if provided.
557634
post_action_binary=%(post_action_binary)s
558635
post_action_determines_exit_code="%(post_action_determines_exit_code)s"
@@ -562,6 +639,8 @@ if [[ -n "${result_bundle_path:-}" ]]; then
562639
TEST_LOG_FILE="$testlog" \
563640
SIMULATOR_UDID="$simulator_id" \
564641
TEST_XCRESULT_BUNDLE_PATH="$result_bundle_path" \
642+
LLVM_COV_EXIT_CODE="${llvm_cov_status:-0}" \
643+
LLVM_COV_JSON_EXPORT_EXIT_CODE="${llvm_cov_json_export_status:-0}" \
565644
"$post_action_binary" || post_action_exit_code=$?
566645
else
567646
TEST_EXIT_CODE=$test_exit_code \
@@ -591,15 +670,6 @@ if [[ "$reuse_simulator" == false ]]; then
591670
xcrun simctl delete "$simulator_id"
592671
fi
593672

594-
profdata="$test_tmp_dir/$simulator_id/Coverage.profdata"
595-
if [[ "$should_use_xcodebuild" == false ]]; then
596-
profdata="$test_tmp_dir/coverage.profdata"
597-
fi
598-
599-
if [[ "${COLLECT_PROFDATA:-0}" == "1" && -f "$profdata" ]]; then
600-
cp -R "$profdata" "$TEST_UNDECLARED_OUTPUTS_DIR"
601-
fi
602-
603673
if [[ "$post_action_determines_exit_code" == true ]]; then
604674
if [[ "$post_action_exit_code" -ne 0 ]]; then
605675
echo "error: post_action exited with '$post_action_exit_code'" >&2
@@ -666,81 +736,14 @@ then
666736
exit 1
667737
fi
668738

669-
if [[ "${COVERAGE:-}" -ne 1 || "${APPLE_COVERAGE:-}" -ne 1 ]]; then
670-
# Normal tests run without coverage
671-
if [[ -f "${TEST_PREMATURE_EXIT_FILE:-}" ]]; then
672-
rm -f "$TEST_PREMATURE_EXIT_FILE"
673-
fi
674-
675-
exit 0
676-
fi
677-
678-
if [[ "$should_use_xcodebuild" == false ]]; then
679-
xcrun llvm-profdata merge "$profraw" --output "$profdata"
739+
if [[ "${llvm_cov_status:-0}" -ne 0 ]]; then
740+
echo "error: exporting coverage report failed" >&2
741+
exit "$llvm_cov_status"
680742
fi
681743

682-
lcov_args=(
683-
-instr-profile "$profdata"
684-
-ignore-filename-regex='.*external/.+'
685-
-path-equivalence=".,$PWD"
686-
)
687-
has_binary=false
688-
IFS=";"
689-
arch=$(uname -m)
690-
for binary in $TEST_BINARIES_FOR_LLVM_COV; do
691-
if [[ "$has_binary" == false ]]; then
692-
lcov_args+=("${binary}")
693-
has_binary=true
694-
if ! file "$binary" | grep -q "$arch"; then
695-
arch=x86_64
696-
fi
697-
else
698-
lcov_args+=(-object "${binary}")
699-
fi
700-
701-
lcov_args+=("-arch=$arch")
702-
done
703-
704-
llvm_coverage_manifest="$COVERAGE_MANIFEST"
705-
readonly provided_coverage_manifest="%(test_coverage_manifest)s"
706-
if [[ -s "${provided_coverage_manifest:-}" ]]; then
707-
llvm_coverage_manifest="$provided_coverage_manifest"
708-
fi
709-
710-
readonly error_file="$test_tmp_dir/llvm-cov-error.txt"
711-
llvm_cov_status=0
712-
xcrun llvm-cov \
713-
export \
714-
-format lcov \
715-
"${lcov_args[@]}" \
716-
@"$llvm_coverage_manifest" \
717-
> "$COVERAGE_OUTPUT_FILE" \
718-
2> "$error_file" \
719-
|| llvm_cov_status=$?
720-
721-
# Error ourselves if lcov outputs warnings, such as if we misconfigure
722-
# something and the file path of one of the covered files doesn't exist
723-
if [[ -s "$error_file" || "$llvm_cov_status" -ne 0 ]]; then
724-
echo "error: while exporting coverage report" >&2
725-
cat "$error_file" >&2
726-
exit 1
727-
fi
728-
729-
if [[ -n "${COVERAGE_PRODUCE_JSON:-}" ]]; then
730-
llvm_cov_json_export_status=0
731-
xcrun llvm-cov \
732-
export \
733-
-format text \
734-
"${lcov_args[@]}" \
735-
@"$llvm_coverage_manifest" \
736-
> "$TEST_UNDECLARED_OUTPUTS_DIR/coverage.json" \
737-
2> "$error_file" \
738-
|| llvm_cov_json_export_status=$?
739-
if [[ -s "$error_file" || "$llvm_cov_json_export_status" -ne 0 ]]; then
740-
echo "error: while exporting json coverage report" >&2
741-
cat "$error_file" >&2
742-
exit 1
743-
fi
744+
if [[ "${llvm_cov_json_export_status:-0}" -ne 0 ]]; then
745+
echo "error: exporting json coverage report failed" >&2
746+
exit "$llvm_cov_json_export_status"
744747
fi
745748

746749
if [[ -f "${TEST_PREMATURE_EXIT_FILE:-}" ]]; then

0 commit comments

Comments
 (0)