Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apple/testing/default_runner/ios_xctestrun_runner.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ When executed, the binary will have the following environment variables availabl
<ul>
<li>`SIMULATOR_UDID`: The UDID of the simulator to clean up. This will be the same UDID produced by the `create_simulator_action` and used to run the tests.</li>
<li>`SIMULATOR_REUSE_SIMULATOR`: Whether to an existing simulator was reused or if a new one was created. The value will be set to "1" if the `reuse_simulator` attribute is true, and unset otherwise. Whether or not this variable is respected should be treated as an implementation detail of the simulator cleanup tool.</li>
<li>`TEST_TIMEOUT_DETECTED`: Whether the test timed out. The value will be set to "1" if the test timed out (i.e., if the test runner received SIGTERM or SIGINT), and unset otherwise.</li>
</ul>
""",
),
Expand Down
31 changes: 26 additions & 5 deletions apple/testing/default_runner/ios_xctestrun_runner.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,33 @@ basename_without_extension() {
echo "${filename%.*}"
}

test_timeout_detected=""

cleanup() {
local exit_status=$?

# Clean up simulator if one was created.
if [[ -n "${simulator_id:-}" && "$simulator_id" != "unused" ]]; then
SIMULATOR_UDID="$simulator_id" \
SIMULATOR_REUSE_SIMULATOR="${reuse_simulator:-}" \
XCTESTRUN_RUNNER_PID="${BASHPID:-$$}" \
TEST_TIMEOUT_DETECTED="${test_timeout_detected:-}" \
"%(clean_up_simulator_action_binary)s"
fi

# Clean up temporary directory if appropriate.
if [[ -n "${test_tmp_dir:-}" && -z "${NO_CLEAN:-}" ]]; then
rm -rf "${test_tmp_dir}"
fi

exit $exit_status
}

trap 'cleanup' EXIT
trap 'test_timeout_detected=1; exit 143' INT TERM

test_tmp_dir="$(mktemp -d "${TEST_TMPDIR:-${TMPDIR:-/tmp}}/test_tmp_dir.XXXXXX")"
if [[ -z "${NO_CLEAN:-}" ]]; then
trap 'rm -rf "${test_tmp_dir}"' EXIT
else
if [[ -n "${NO_CLEAN:-}" ]]; then
test_tmp_dir="${TMPDIR:-/tmp}/test_tmp_dir"
rm -rf "$test_tmp_dir"
mkdir -p "$test_tmp_dir"
Expand Down Expand Up @@ -647,8 +670,6 @@ if [[
rm -r "$result_bundle_path"
fi

SIMULATOR_UDID="$simulator_id" SIMULATOR_REUSE_SIMULATOR="${reuse_simulator:-}" XCTESTRUN_RUNNER_PID="${BASHPID:-$$}" "%(clean_up_simulator_action_binary)s"

if [[ "$post_action_determines_exit_code" == true ]]; then
if [[ "$post_action_exit_code" -ne 0 ]]; then
echo "error: post_action exited with '$post_action_exit_code'" >&2
Expand Down
2 changes: 1 addition & 1 deletion apple/testing/default_runner/simulator_cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ -z "${SIMULATOR_UDID:-}" ]; then
exit 1
fi

if [ -z "${SIMULATOR_REUSE_SIMULATOR:-}" ]; then
if [ -z "${SIMULATOR_REUSE_SIMULATOR:-}" ] || [ -n "${TEST_TIMEOUT_DETECTED:-}" ]; then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the simulator is being shared between tests won't deleting it when a single test times out cause a bunch of unrelated failures?

# Delete will shutdown down the simulator if it's still currently running.
xcrun simctl delete "$SIMULATOR_UDID"
fi
2 changes: 1 addition & 1 deletion doc/rules-ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ in Xcode.
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="ios_xctestrun_runner-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="ios_xctestrun_runner-attachment_lifetime"></a>attachment_lifetime | Attachment lifetime to set in the xctestrun file when running the test bundle - `"keepNever"` (default), `"keepAlways"` or `"deleteOnSuccess"`. This affects presence of attachments in the XCResult output. This does not force using `xcodebuild` or an XCTestRun file but the value will be used in that case. | String | optional | `"keepNever"` |
| <a id="ios_xctestrun_runner-clean_up_simulator_action"></a>clean_up_simulator_action | A binary that cleans up any simulators created by the `create_simulator_action`. Runs after the `post_action`, regardless of test success or failure.<br><br>When executed, the binary will have the following environment variables available to it:<br><br><ul> <li>`SIMULATOR_UDID`: The UDID of the simulator to clean up. This will be the same UDID produced by the `create_simulator_action` and used to run the tests.</li> <li>`SIMULATOR_REUSE_SIMULATOR`: Whether to an existing simulator was reused or if a new one was created. The value will be set to "1" if the `reuse_simulator` attribute is true, and unset otherwise. Whether or not this variable is respected should be treated as an implementation detail of the simulator cleanup tool.</li> </ul> | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@rules_apple//apple/testing/default_runner:simulator_cleanup"` |
| <a id="ios_xctestrun_runner-clean_up_simulator_action"></a>clean_up_simulator_action | A binary that cleans up any simulators created by the `create_simulator_action`. Runs after the `post_action`, regardless of test success or failure.<br><br>When executed, the binary will have the following environment variables available to it:<br><br><ul> <li>`SIMULATOR_UDID`: The UDID of the simulator to clean up. This will be the same UDID produced by the `create_simulator_action` and used to run the tests.</li> <li>`SIMULATOR_REUSE_SIMULATOR`: Whether to an existing simulator was reused or if a new one was created. The value will be set to "1" if the `reuse_simulator` attribute is true, and unset otherwise. Whether or not this variable is respected should be treated as an implementation detail of the simulator cleanup tool.</li> <li>`TEST_TIMEOUT_DETECTED`: Whether the test timed out. The value will be set to "1" if the test timed out (i.e., if the test runner received SIGTERM or SIGINT), and unset otherwise.</li> </ul> | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@rules_apple//apple/testing/default_runner:simulator_cleanup"` |
| <a id="ios_xctestrun_runner-command_line_args"></a>command_line_args | CommandLineArguments to pass to xctestrun file when running the test bundle. This means it will always use `xcodebuild test-without-building` to run the test bundle. | List of strings | optional | `[]` |
| <a id="ios_xctestrun_runner-create_simulator_action"></a>create_simulator_action | A binary that produces a UDID for a simulator that matches the given device type and OS version. Runs before the `pre_action`. The UDID will be used to run the tests on the correct simulator. The binary must print only the UDID to stdout.<br><br>When executed, the binary will have the following environment variables available to it:<br><br><ul> <li>`SIMULATOR_DEVICE_TYPE`: The device type of the simulator to create. The supported types correspond to the output of `xcrun simctl list devicetypes`. E.g., iPhone 6, iPad Air. The value will either be the value of the `device_type` attribute, or the `ios_simulator_device` build setting.</li> <li>`SIMULATOR_OS_VERSION`: The OS version of the simulator to create. The supported OS versions correspond to the output of `xcrun simctl list runtimes`. E.g., 11.2, 9.3. The value will either be the value of the `os_version` attribute, or the `ios_simulator_version` build setting.</li> <li>`SIMULATOR_REUSE_SIMULATOR`: Whether to reuse an existing simulator or create a new one. The value will be set to "1" if the `reuse_simulator` attribute is true, and unset otherwise. Whether or not this variable is respected should be treated as an implementation detail of the simulator creator tool.</li> <li>`SIMULATOR_SDK_VERSION`: The SDK version of the simulator to create. The supported SDK builds correspond to the output of `xcrun simctl runtime match list`. E.g., 11.2, 9.3. The value will be derived from the `default_ios_sdk_version` for the current Xcode version.</li> </ul> | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@rules_apple//apple/testing/default_runner:simulator_creator"` |
| <a id="ios_xctestrun_runner-create_xcresult_bundle"></a>create_xcresult_bundle | Force the test runner to always create an XCResult bundle. This means it will always use `xcodebuild test-without-building` to run the test bundle. | Boolean | optional | `False` |
Expand Down