Skip to content

Commit

Permalink
Add test support for --define=apple.experimental.tree_artifact_output…
Browse files Browse the repository at this point in the history
…s=1.

See #49

RELNOTES: None
PiperOrigin-RevId: 236717030
  • Loading branch information
sergiocampama committed Mar 4, 2019
1 parent a8f24ae commit 54c4684
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 39 deletions.
62 changes: 41 additions & 21 deletions apple/internal/testing/apple_test_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ These are internal rules not to be used outside of the
@build_bazel_rules_apple//apple package.
"""

load(
"@build_bazel_rules_apple//apple/internal:experimental.bzl",
"is_experimental_tree_artifact_enabled",
)
load(
"@build_bazel_rules_apple//apple/internal:file_support.bzl",
"file_support",
Expand Down Expand Up @@ -376,15 +380,15 @@ def _apple_ui_test_attributes():
},
)

def _get_template_substitutions(ctx, test_type):
def _get_template_substitutions(test_type, test_bundle, test_host = None):
"""Dictionary with the substitutions to be applied to the template script."""
subs = {}

if ctx.attr.test_host:
subs["test_host_path"] = ctx.attr.test_host[AppleBundleInfo].archive.short_path
if test_host:
subs["test_host_path"] = test_host.short_path
else:
subs["test_host_path"] = ""
subs["test_bundle_path"] = ctx.outputs.test_bundle.short_path
subs["test_bundle_path"] = test_bundle.short_path
subs["test_type"] = test_type.upper()

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

direct_runfiles = [ctx.outputs.test_bundle]
direct_runfiles = []
transitive_runfiles = []
test_host = ctx.attr.test_host
if test_host:
direct_runfiles.append(test_host[AppleBundleInfo].archive)

direct_outputs = []
transitive_outputs = []

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

file_support.symlink(
ctx,
ctx.attr.test_bundle[AppleBundleInfo].archive,
ctx.outputs.test_bundle,
)
if is_experimental_tree_artifact_enabled(ctx):
ctx.actions.write(
output = ctx.outputs.test_bundle,
content = "test",
)
test_bundle = ctx.attr.test_bundle[AppleBundleInfo].archive
else:
file_support.symlink(
ctx,
ctx.attr.test_bundle[AppleBundleInfo].archive,
ctx.outputs.test_bundle,
)
test_bundle = ctx.outputs.test_bundle

direct_outputs.append(test_bundle)
direct_runfiles.append(test_bundle)

test_host = ctx.attr.test_host
test_host_archive = None
if test_host:
test_host_archive = test_host[AppleBundleInfo].archive
direct_runfiles.append(test_host_archive)

executable = ctx.actions.declare_file("%s" % ctx.label.name)
ctx.actions.expand_template(
template = runner.test_runner_template,
output = executable,
substitutions = _get_template_substitutions(ctx, test_type),
substitutions = _get_template_substitutions(
test_type,
test_bundle,
test_host = test_host_archive,
),
)
direct_outputs.append(executable)

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

transitive_outputs = []
extra_outputs_provider = ctx.attr.test_bundle[AppleExtraOutputsInfo]
if extra_outputs_provider:
transitive_outputs.append(extra_outputs_provider.files)

outputs = depset(
direct = [ctx.outputs.test_bundle, executable],
transitive = transitive_outputs,
)

extra_providers = []

# TODO(b/110264170): Repropagate the provider that makes the dSYM bundle
Expand All @@ -485,7 +505,7 @@ def _apple_test_impl(ctx, test_type):
testing.TestEnvironment(test_environment),
DefaultInfo(
executable = executable,
files = outputs,
files = depset(direct_outputs, transitive = transitive_outputs),
runfiles = ctx.runfiles(
files = direct_runfiles,
transitive_files = depset(transitive = transitive_runfiles),
Expand Down
37 changes: 29 additions & 8 deletions apple/testing/default_runner/ios_test_runner.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,38 @@ TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/test_runner_work_dir.XXXXXX")"
runner_flags+=("--work_dir=${TMP_DIR}")

TEST_BUNDLE_PATH="%(test_bundle_path)s"
TEST_BUNDLE_NAME=$(basename_without_extension "${TEST_BUNDLE_PATH}")
TEST_BUNDLE_TMP_DIR="${TMP_DIR}/${TEST_BUNDLE_NAME}"
unzip -qq -d "${TEST_BUNDLE_TMP_DIR}" "${TEST_BUNDLE_PATH}"
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_TMP_DIR}/${TEST_BUNDLE_NAME}.xctest")

if [[ "$TEST_BUNDLE_PATH" == *.xctest ]]; then
# Need to copy the bundle outside of the Bazel execroot since the test runner
# needs to make some modifications to its contents.
# TODO(kaipi): Improve xctestrunner to account for Bazel permissions.
cp -R "$TEST_BUNDLE_PATH" "$TMP_DIR"
chmod -R 777 "${TMP_DIR}/$(basename "$TEST_BUNDLE_PATH")"
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_PATH}")
else
TEST_BUNDLE_NAME=$(basename_without_extension "${TEST_BUNDLE_PATH}")
TEST_BUNDLE_TMP_DIR="${TMP_DIR}/${TEST_BUNDLE_NAME}"
unzip -qq -d "${TEST_BUNDLE_TMP_DIR}" "${TEST_BUNDLE_PATH}"
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_TMP_DIR}/${TEST_BUNDLE_NAME}.xctest")
fi


TEST_HOST_PATH="%(test_host_path)s"

if [[ -n "$TEST_HOST_PATH" ]]; then
TEST_HOST_NAME=$(basename_without_extension "${TEST_HOST_PATH}")
TEST_HOST_TMP_DIR="${TMP_DIR}/${TEST_HOST_NAME}"
unzip -qq -d "${TEST_HOST_TMP_DIR}" "${TEST_HOST_PATH}"
runner_flags+=("--app_under_test_path=${TEST_HOST_TMP_DIR}/Payload/${TEST_HOST_NAME}.app")
if [[ "$TEST_HOST_PATH" == *.app ]]; then
# Need to copy the bundle outside of the Bazel execroot since the test
# runner needs to make some modifications to its contents.
# TODO(kaipi): Improve xctestrunner to account for Bazel permissions.
cp -R "$TEST_HOST_PATH" "$TMP_DIR"
chmod -R 777 "${TMP_DIR}/$(basename "$TEST_HOST_PATH")"
runner_flags+=("--app_under_test_path=${TMP_DIR}/$(basename "$TEST_HOST_PATH")")
else
TEST_HOST_NAME=$(basename_without_extension "${TEST_HOST_PATH}")
TEST_HOST_TMP_DIR="${TMP_DIR}/${TEST_HOST_NAME}"
unzip -qq -d "${TEST_HOST_TMP_DIR}" "${TEST_HOST_PATH}"
runner_flags+=("--app_under_test_path=${TEST_HOST_TMP_DIR}/Payload/${TEST_HOST_NAME}.app")
fi
fi

if [[ -n "${TEST_UNDECLARED_OUTPUTS_DIR}" ]]; then
Expand Down
34 changes: 24 additions & 10 deletions apple/testing/default_runner/macos_test_runner.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,39 @@ BAZEL_XCTESTRUN_TEMPLATE=%(xctestrun_template)s
TEST_TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/test_tmp_dir.XXXXXX")"
trap 'rm -rf "${TEST_TMP_DIR}"' ERR EXIT

TEST_BUNDLE_PATH="%(test_bundle_path)s"
TEST_BUNDLE_NAME=$(basename_without_extension "$TEST_BUNDLE_PATH")

if [[ "$TEST_BUNDLE_PATH" == *.xctest ]]; then
cp -R "$TEST_BUNDLE_PATH" "$TEST_TMP_DIR"
# Need to modify permissions as Bazel will set all files to non-writable, and
# Xcode's test runner requires the files to be writable.
chmod -R 777 "$TEST_TMP_DIR/$TEST_BUNDLE_NAME.xctest"
else
unzip -qq -d "${TEST_TMP_DIR}" "${TEST_BUNDLE_PATH}"
fi

# In case there is no test host, TEST_HOST_PATH will be empty. TEST_BUNDLE_PATH
# will always be populated.
TEST_HOST_PATH="%(test_host_path)s"
TEST_BUNDLE_PATH="%(test_bundle_path)s"

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

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

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

0 comments on commit 54c4684

Please sign in to comment.