Skip to content

Commit 54c4684

Browse files
committed
Add test support for --define=apple.experimental.tree_artifact_outputs=1.
See #49 RELNOTES: None PiperOrigin-RevId: 236717030
1 parent a8f24ae commit 54c4684

File tree

3 files changed

+94
-39
lines changed

3 files changed

+94
-39
lines changed

apple/internal/testing/apple_test_rules.bzl

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ These are internal rules not to be used outside of the
1818
@build_bazel_rules_apple//apple package.
1919
"""
2020

21+
load(
22+
"@build_bazel_rules_apple//apple/internal:experimental.bzl",
23+
"is_experimental_tree_artifact_enabled",
24+
)
2125
load(
2226
"@build_bazel_rules_apple//apple/internal:file_support.bzl",
2327
"file_support",
@@ -376,15 +380,15 @@ def _apple_ui_test_attributes():
376380
},
377381
)
378382

379-
def _get_template_substitutions(ctx, test_type):
383+
def _get_template_substitutions(test_type, test_bundle, test_host = None):
380384
"""Dictionary with the substitutions to be applied to the template script."""
381385
subs = {}
382386

383-
if ctx.attr.test_host:
384-
subs["test_host_path"] = ctx.attr.test_host[AppleBundleInfo].archive.short_path
387+
if test_host:
388+
subs["test_host_path"] = test_host.short_path
385389
else:
386390
subs["test_host_path"] = ""
387-
subs["test_bundle_path"] = ctx.outputs.test_bundle.short_path
391+
subs["test_bundle_path"] = test_bundle.short_path
388392
subs["test_type"] = test_type.upper()
389393

390394
return {"%(" + k + ")s": subs[k] for k in subs}
@@ -418,11 +422,11 @@ def _apple_test_impl(ctx, test_type):
418422
runner.test_environment,
419423
)
420424

421-
direct_runfiles = [ctx.outputs.test_bundle]
425+
direct_runfiles = []
422426
transitive_runfiles = []
423-
test_host = ctx.attr.test_host
424-
if test_host:
425-
direct_runfiles.append(test_host[AppleBundleInfo].archive)
427+
428+
direct_outputs = []
429+
transitive_outputs = []
426430

427431
if ctx.configuration.coverage_enabled:
428432
test_environment = dicts.add(
@@ -439,34 +443,50 @@ def _apple_test_impl(ctx, test_type):
439443
transitive_runfiles.append(ctx.attr._mcov.files)
440444
transitive_runfiles.append(ctx.attr._apple_coverage_support.files)
441445

442-
file_support.symlink(
443-
ctx,
444-
ctx.attr.test_bundle[AppleBundleInfo].archive,
445-
ctx.outputs.test_bundle,
446-
)
446+
if is_experimental_tree_artifact_enabled(ctx):
447+
ctx.actions.write(
448+
output = ctx.outputs.test_bundle,
449+
content = "test",
450+
)
451+
test_bundle = ctx.attr.test_bundle[AppleBundleInfo].archive
452+
else:
453+
file_support.symlink(
454+
ctx,
455+
ctx.attr.test_bundle[AppleBundleInfo].archive,
456+
ctx.outputs.test_bundle,
457+
)
458+
test_bundle = ctx.outputs.test_bundle
459+
460+
direct_outputs.append(test_bundle)
461+
direct_runfiles.append(test_bundle)
462+
463+
test_host = ctx.attr.test_host
464+
test_host_archive = None
465+
if test_host:
466+
test_host_archive = test_host[AppleBundleInfo].archive
467+
direct_runfiles.append(test_host_archive)
447468

448469
executable = ctx.actions.declare_file("%s" % ctx.label.name)
449470
ctx.actions.expand_template(
450471
template = runner.test_runner_template,
451472
output = executable,
452-
substitutions = _get_template_substitutions(ctx, test_type),
473+
substitutions = _get_template_substitutions(
474+
test_type,
475+
test_bundle,
476+
test_host = test_host_archive,
477+
),
453478
)
479+
direct_outputs.append(executable)
454480

455481
# Add required data into the runfiles to make it available during test
456482
# execution.
457483
for data_dep in ctx.attr.data:
458484
transitive_runfiles.append(data_dep.files)
459485

460-
transitive_outputs = []
461486
extra_outputs_provider = ctx.attr.test_bundle[AppleExtraOutputsInfo]
462487
if extra_outputs_provider:
463488
transitive_outputs.append(extra_outputs_provider.files)
464489

465-
outputs = depset(
466-
direct = [ctx.outputs.test_bundle, executable],
467-
transitive = transitive_outputs,
468-
)
469-
470490
extra_providers = []
471491

472492
# TODO(b/110264170): Repropagate the provider that makes the dSYM bundle
@@ -485,7 +505,7 @@ def _apple_test_impl(ctx, test_type):
485505
testing.TestEnvironment(test_environment),
486506
DefaultInfo(
487507
executable = executable,
488-
files = outputs,
508+
files = depset(direct_outputs, transitive = transitive_outputs),
489509
runfiles = ctx.runfiles(
490510
files = direct_runfiles,
491511
transitive_files = depset(transitive = transitive_runfiles),

apple/testing/default_runner/ios_test_runner.template.sh

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,38 @@ TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/test_runner_work_dir.XXXXXX")"
3030
runner_flags+=("--work_dir=${TMP_DIR}")
3131

3232
TEST_BUNDLE_PATH="%(test_bundle_path)s"
33-
TEST_BUNDLE_NAME=$(basename_without_extension "${TEST_BUNDLE_PATH}")
34-
TEST_BUNDLE_TMP_DIR="${TMP_DIR}/${TEST_BUNDLE_NAME}"
35-
unzip -qq -d "${TEST_BUNDLE_TMP_DIR}" "${TEST_BUNDLE_PATH}"
36-
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_TMP_DIR}/${TEST_BUNDLE_NAME}.xctest")
33+
34+
if [[ "$TEST_BUNDLE_PATH" == *.xctest ]]; then
35+
# Need to copy the bundle outside of the Bazel execroot since the test runner
36+
# needs to make some modifications to its contents.
37+
# TODO(kaipi): Improve xctestrunner to account for Bazel permissions.
38+
cp -R "$TEST_BUNDLE_PATH" "$TMP_DIR"
39+
chmod -R 777 "${TMP_DIR}/$(basename "$TEST_BUNDLE_PATH")"
40+
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_PATH}")
41+
else
42+
TEST_BUNDLE_NAME=$(basename_without_extension "${TEST_BUNDLE_PATH}")
43+
TEST_BUNDLE_TMP_DIR="${TMP_DIR}/${TEST_BUNDLE_NAME}"
44+
unzip -qq -d "${TEST_BUNDLE_TMP_DIR}" "${TEST_BUNDLE_PATH}"
45+
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_TMP_DIR}/${TEST_BUNDLE_NAME}.xctest")
46+
fi
47+
3748

3849
TEST_HOST_PATH="%(test_host_path)s"
50+
3951
if [[ -n "$TEST_HOST_PATH" ]]; then
40-
TEST_HOST_NAME=$(basename_without_extension "${TEST_HOST_PATH}")
41-
TEST_HOST_TMP_DIR="${TMP_DIR}/${TEST_HOST_NAME}"
42-
unzip -qq -d "${TEST_HOST_TMP_DIR}" "${TEST_HOST_PATH}"
43-
runner_flags+=("--app_under_test_path=${TEST_HOST_TMP_DIR}/Payload/${TEST_HOST_NAME}.app")
52+
if [[ "$TEST_HOST_PATH" == *.app ]]; then
53+
# Need to copy the bundle outside of the Bazel execroot since the test
54+
# runner needs to make some modifications to its contents.
55+
# TODO(kaipi): Improve xctestrunner to account for Bazel permissions.
56+
cp -R "$TEST_HOST_PATH" "$TMP_DIR"
57+
chmod -R 777 "${TMP_DIR}/$(basename "$TEST_HOST_PATH")"
58+
runner_flags+=("--app_under_test_path=${TMP_DIR}/$(basename "$TEST_HOST_PATH")")
59+
else
60+
TEST_HOST_NAME=$(basename_without_extension "${TEST_HOST_PATH}")
61+
TEST_HOST_TMP_DIR="${TMP_DIR}/${TEST_HOST_NAME}"
62+
unzip -qq -d "${TEST_HOST_TMP_DIR}" "${TEST_HOST_PATH}"
63+
runner_flags+=("--app_under_test_path=${TEST_HOST_TMP_DIR}/Payload/${TEST_HOST_NAME}.app")
64+
fi
4465
fi
4566

4667
if [[ -n "${TEST_UNDECLARED_OUTPUTS_DIR}" ]]; then

apple/testing/default_runner/macos_test_runner.template.sh

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,39 @@ BAZEL_XCTESTRUN_TEMPLATE=%(xctestrun_template)s
4444
TEST_TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/test_tmp_dir.XXXXXX")"
4545
trap 'rm -rf "${TEST_TMP_DIR}"' ERR EXIT
4646

47+
TEST_BUNDLE_PATH="%(test_bundle_path)s"
48+
TEST_BUNDLE_NAME=$(basename_without_extension "$TEST_BUNDLE_PATH")
49+
50+
if [[ "$TEST_BUNDLE_PATH" == *.xctest ]]; then
51+
cp -R "$TEST_BUNDLE_PATH" "$TEST_TMP_DIR"
52+
# Need to modify permissions as Bazel will set all files to non-writable, and
53+
# Xcode's test runner requires the files to be writable.
54+
chmod -R 777 "$TEST_TMP_DIR/$TEST_BUNDLE_NAME.xctest"
55+
else
56+
unzip -qq -d "${TEST_TMP_DIR}" "${TEST_BUNDLE_PATH}"
57+
fi
58+
4759
# In case there is no test host, TEST_HOST_PATH will be empty. TEST_BUNDLE_PATH
4860
# will always be populated.
4961
TEST_HOST_PATH="%(test_host_path)s"
50-
TEST_BUNDLE_PATH="%(test_bundle_path)s"
5162

52-
# This is the equivalent of retrieving the test and test host's target name,
53-
# as the target name is set for the bundle and binary names.
54-
TEST_HOST_NAME=$(basename_without_extension "$TEST_HOST_PATH")
55-
TEST_BUNDLE_NAME=$(basename_without_extension "$TEST_BUNDLE_PATH")
63+
if [[ -n "$TEST_HOST_PATH" ]]; then
64+
TEST_HOST_NAME=$(basename_without_extension "$TEST_HOST_PATH")
5665

57-
# Extract the test bundle into the temporary directory.
58-
unzip -qq -d "${TEST_TMP_DIR}" "${TEST_BUNDLE_PATH}"
66+
if [[ "$TEST_HOST_PATH" == *.app ]]; then
67+
cp -R "$TEST_HOST_PATH" "$TEST_TMP_DIR"
68+
# Need to modify permissions as Bazel will set all files to non-writable,
69+
# and Xcode's test runner requires the files to be writable.
70+
chmod -R 777 "$TEST_TMP_DIR/$TEST_HOST_NAME.app"
71+
else
72+
unzip -qq -d "${TEST_TMP_DIR}" "${TEST_HOST_PATH}"
73+
fi
74+
fi
5975

6076
# List of substitutions for the xctestrun template. This list is different
6177
# depending on whether the test is running with or without a test host.
6278
XCTESTRUN_TEST_BUNDLE_PATH="__TESTROOT__/$TEST_BUNDLE_NAME.xctest"
63-
if [[ -n "$TEST_HOST_NAME" ]]; then
64-
# If there's a test host, unpack it.
65-
unzip -qq -d "$TEST_TMP_DIR" "$TEST_HOST_PATH"
79+
if [[ -n "$TEST_HOST_PATH" ]]; then
6680
XCTESTRUN_TEST_HOST_PATH="__TESTROOT__/$TEST_HOST_NAME.app"
6781
XCTESTRUN_TEST_HOST_BASED=true
6882
XCTESTRUN_TEST_HOST_BINARY="__TESTHOST__/Contents/MacOS/$TEST_HOST_NAME"

0 commit comments

Comments
 (0)