Skip to content

Commit af2f32a

Browse files
committed
macos_test_runner: add attr post_action_determines_exit_code
1 parent ffb7cd0 commit af2f32a

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

apple/testing/default_runner/macos_test_runner.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ def _get_template_substitutions(
5757
*,
5858
xctestrun_template,
5959
pre_action_binary,
60-
post_action_binary):
60+
post_action_binary,
61+
post_action_determines_exit_code):
6162
"""Returns the template substitutions for this runner."""
6263
subs = {
6364
"xctestrun_template": xctestrun_template.short_path,
6465
"pre_action_binary": pre_action_binary,
6566
"post_action_binary": post_action_binary,
67+
"post_action_determines_exit_code": post_action_determines_exit_code,
6668
}
6769

6870
return {"%(" + k + ")s": subs[k] for k in subs}
@@ -101,8 +103,10 @@ def _macos_test_runner_impl(ctx):
101103
pre_action_binary = ctx.executable.pre_action.short_path
102104
runfiles = runfiles.merge(ctx.attr.pre_action[DefaultInfo].default_runfiles)
103105

106+
post_action_determines_exit_code = False
104107
if ctx.executable.post_action:
105108
post_action_binary = ctx.executable.post_action.short_path
109+
post_action_determines_exit_code = ctx.attr.post_action_determines_exit_code
106110
runfiles = runfiles.merge(ctx.attr.post_action[DefaultInfo].default_runfiles)
107111

108112
ctx.actions.expand_template(
@@ -112,6 +116,7 @@ def _macos_test_runner_impl(ctx):
112116
xctestrun_template = preprocessed_xctestrun_template,
113117
pre_action_binary = pre_action_binary,
114118
post_action_binary = post_action_binary,
119+
post_action_determines_exit_code = "true" if post_action_determines_exit_code else "false",
115120
),
116121
)
117122

@@ -139,6 +144,12 @@ A binary to run prior to test execution. Sets any environment variables availabl
139144
cfg = "exec",
140145
doc = """
141146
A binary to run following test execution. Runs after testing but before test result handling and coverage processing. Sets the `$TEST_EXIT_CODE`, `$TEST_LOG_FILE`, and `$TEST_XCRESULT_BUNDLE_PATH` environment variables, in addition to any other variables available to the test runner.
147+
""",
148+
),
149+
"post_action_determines_exit_code": attr.bool(
150+
default = False,
151+
doc = """
152+
When true, the exit code of the test run will be set to the exit code of the post action. This is useful for tests that need to fail the test run based on their own criteria.
142153
""",
143154
),
144155
"_test_template": attr.label(

apple/testing/default_runner/macos_test_runner.template.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,23 @@ xcodebuild test-without-building \
157157

158158
# Run a post-action binary, if provided.
159159
post_action_binary=%(post_action_binary)s
160+
post_action_determines_exit_code="%(post_action_determines_exit_code)s"
161+
post_action_exit_code=0
160162
TEST_EXIT_CODE=$test_exit_code \
161163
TEST_LOG_FILE="$testlog" \
162164
TEST_XCRESULT_BUNDLE_PATH="$result_bundle_path" \
163-
"$post_action_binary"
165+
"$post_action_binary" || post_action_exit_code=$?
164166

165-
if [[ "$test_exit_code" -ne 0 ]]; then
166-
echo "error: tests exited with '$test_exit_code'" >&2
167-
exit "$test_exit_code"
167+
if [[ "$post_action_determines_exit_code" == true ]]; then
168+
if [[ "$post_action_exit_code" -ne 0 ]]; then
169+
echo "error: post_action exited with '$post_action_exit_code'" >&2
170+
exit "$post_action_exit_code"
171+
fi
172+
else
173+
if [[ "$test_exit_code" -ne 0 ]]; then
174+
echo "error: tests exited with '$test_exit_code'" >&2
175+
exit "$test_exit_code"
176+
fi
168177
fi
169178

170179
if [[ "${COVERAGE:-}" -ne 1 ]]; then

test/macos_test_runner_unit_test.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ macos_test_runner(
7676
pre_action = ":pre_action",
7777
post_action = ":post_action",
7878
)
79+
80+
81+
genrule(
82+
name = "post_action_soft_fail_gen",
83+
executable = True,
84+
outs = ["post_action_soft_fail.bash"],
85+
cmd = """
86+
echo 'echo "POST-ACTION: Soft failing." && exit 0' > \$@
87+
""",
88+
)
89+
90+
sh_binary(
91+
name = "post_action_soft_fail",
92+
srcs = [":post_action_soft_fail_gen"],
93+
)
94+
95+
macos_test_runner(
96+
name = "macos_runner_with_soft_fail",
97+
post_action = ":post_action_soft_fail",
98+
post_action_determines_exit_code = True,
99+
)
79100
EOF
80101
}
81102

@@ -281,6 +302,14 @@ macos_unit_test(
281302
minimum_os_version = "${MIN_OS_MACOS}",
282303
runner = ":macos_runner",
283304
)
305+
306+
macos_unit_test(
307+
name = 'SoftFailingUnitTest',
308+
infoplists = ["FailUnitTest-Info.plist"],
309+
deps = [":fail_unit_test_lib"],
310+
minimum_os_version = "${MIN_OS_MACOS}",
311+
runner = ":macos_runner_with_soft_fail",
312+
)
284313
EOF
285314
}
286315

@@ -518,6 +547,17 @@ function test_macos_unit_test_fail() {
518547
expect_log "Executed 1 test, with 1 failure"
519548
}
520549

550+
function test_macos_unit_test_soft_fail() {
551+
create_runners
552+
create_macos_unit_tests
553+
do_macos_test //macos:SoftFailingUnitTest || fail "should pass"
554+
555+
expect_log "Test Suite 'FailingUnitTest' failed"
556+
expect_log "Test Suite 'SoftFailingUnitTest.xctest' failed"
557+
expect_log "Executed 1 test, with 1 failure"
558+
expect_log "POST-ACTION: Soft failing."
559+
}
560+
521561
function test_macos_unit_test_with_filter() {
522562
create_runners
523563
create_macos_unit_tests

0 commit comments

Comments
 (0)