Skip to content

Commit

Permalink
Add target app bundle for Apple XCUI tests
Browse files Browse the repository at this point in the history
Summary:
Reported in https://fb.workplace.com/groups/1373371236362107/posts/2130750520624171/?comment_id=2140998562932700

XCUI tests use a UI test target app (the app to test).

This target app is similar to the test host app in terms of dependency and bundle:
- Added a sub target to build the app bundle

Looking at the tpx code:
https://www.internalfb.com/code/fbsource/[5796a9bd4962e1afd8b1dc8fe7abc8e8200527b8]/fbcode/testinfra/tpx/tpx-buck/src/translator/apple_test.rs?lines=185-191
- Added the label tpx:apple_test:buck2:uiTest, so tpx knows it's a XCUI test.
- Set the env var TARGET_APP_BUNDLE

Finally, I fixed the TpxUITests with the correct values for test_host_app and ui_test_target_app. These tests weren't working (they were failing with the error "Failure: No target application path specified via test configuration" -- now they succeed.

Differential Revision: D56968361

fbshipit-source-id: 23778755a4c706a353ac61ba834d7262ae3c506b
  • Loading branch information
maru authored and facebook-github-bot committed May 6, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5b9e98c commit 26d4425
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions apple/apple_test.bzl
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@ def apple_test_impl(ctx: AnalysisContext) -> [list[Provider], Promise]:

test_host_app_bundle = _get_test_host_app_bundle(ctx)
test_host_app_binary = _get_test_host_app_binary(ctx, test_host_app_bundle)
ui_test_target_app_bundle = _get_ui_test_target_app_bundle(ctx)

objc_bridging_header_flags = [
# Disable bridging header -> PCH compilation to mitigate an issue in Xcode 13 beta.
@@ -170,17 +171,18 @@ def apple_test_impl(ctx: AnalysisContext) -> [list[Provider], Promise]:
)
sub_targets[DSYM_SUBTARGET] = [DefaultInfo(default_output = dsym_artifact)]

# If the test has a test host, add a subtarget to build the test host app bundle.
# If the test has a test host and a ui test target, add the subtargets to build the app bundles.
sub_targets["test-host"] = [DefaultInfo(default_output = test_host_app_bundle)] if test_host_app_bundle else [DefaultInfo()]
sub_targets["ui-test-target"] = [DefaultInfo(default_output = ui_test_target_app_bundle)] if ui_test_target_app_bundle else [DefaultInfo()]

sub_targets[DWARF_AND_DSYM_SUBTARGET] = [
DefaultInfo(default_output = xctest_bundle, other_outputs = [dsym_artifact]),
_get_test_info(ctx, xctest_bundle, test_host_app_bundle, dsym_artifact),
_get_test_info(ctx, xctest_bundle, test_host_app_bundle, dsym_artifact, ui_test_target_app_bundle),
]

return [
DefaultInfo(default_output = xctest_bundle, sub_targets = sub_targets),
_get_test_info(ctx, xctest_bundle, test_host_app_bundle),
_get_test_info(ctx, xctest_bundle, test_host_app_bundle, ui_test_target_app_bundle = ui_test_target_app_bundle),
cxx_library_output.xcode_data_info,
cxx_library_output.cxx_compilationdb_info,
]
@@ -190,7 +192,7 @@ def apple_test_impl(ctx: AnalysisContext) -> [list[Provider], Promise]:
else:
return get_apple_test_providers([])

def _get_test_info(ctx: AnalysisContext, xctest_bundle: Artifact, test_host_app_bundle: Artifact | None, dsym_artifact: Artifact | None = None) -> Provider:
def _get_test_info(ctx: AnalysisContext, xctest_bundle: Artifact, test_host_app_bundle: Artifact | None, dsym_artifact: Artifact | None = None, ui_test_target_app_bundle: Artifact | None = None) -> Provider:
# When interacting with Tpx, we just pass our various inputs via env vars,
# since Tpx basiclaly wants structured output for this.

@@ -203,6 +205,10 @@ def _get_test_info(ctx: AnalysisContext, xctest_bundle: Artifact, test_host_app_
env["HOST_APP_BUNDLE"] = test_host_app_bundle
tpx_label = "tpx:apple_test:buck2:appTest"

if ui_test_target_app_bundle != None:
env["TARGET_APP_BUNDLE"] = ui_test_target_app_bundle
tpx_label = "tpx:apple_test:buck2:uiTest"

labels = ctx.attrs.labels + [tpx_label]
labels.append(tpx_label)

@@ -269,6 +275,17 @@ def _get_test_host_app_binary(ctx: AnalysisContext, test_host_app_bundle: Artifa
parts.append(ctx.attrs.test_host_app[AppleBundleInfo].binary_name)
return cmd_args(parts, delimiter = "/")

def _get_ui_test_target_app_bundle(ctx: AnalysisContext) -> Artifact | None:
""" Get the bundle for the ui test target app, if one exists for this test. """
if ctx.attrs.ui_test_target_app:
# Copy the ui test target app bundle into test's output directory
original_bundle = ctx.attrs.ui_test_target_app[AppleBundleInfo].bundle
ui_test_target_app_bundle = ctx.actions.declare_output(original_bundle.basename)
ctx.actions.copy_file(ui_test_target_app_bundle, original_bundle)
return ui_test_target_app_bundle

return None

def _get_bundle_loader_flags(binary: [cmd_args, None]) -> list[typing.Any]:
if binary:
# During linking we need to link the test shared lib against the test host binary. The

0 comments on commit 26d4425

Please sign in to comment.