| # Copyright 2020 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| import("//build/testing/test_spec.gni") | |
| # Defines a test that runs on a Fuchsia device. | |
| # See: https://fuchsia.dev/fuchsia-src/development/components/build | |
| # | |
| # Invoking this template registers a component and a package such that running | |
| # the test would involve pushing the package to a Fuchsia device, launching the | |
| # component, and treating said component as a test. | |
| # | |
| # Example: | |
| # ``` | |
| # fuchsia_test("my-test") { | |
| # package = ":my-package" | |
| # component = ":my-component" | |
| # } | |
| # ``` | |
| # The above can be tested with `fx test fuchsia-pkg://fuchsia.com/my-package#meta/my-component.cmx`. | |
| # | |
| # Parameters | |
| # | |
| # package (required) | |
| # The `fuchsia_package()` that contains the test component. | |
| # Type: label | |
| # | |
| # package_name (optional) | |
| # The name of the above (`package_name` argument if specified). | |
| # Type: string | |
| # Default: get_label_info(package, "name") | |
| # | |
| # component (required) | |
| # The component that implements the test. | |
| # | |
| # Additional `test_spec()` parameters will be forwarded. | |
| template("fuchsia_test") { | |
| if (current_toolchain == default_toolchain) { | |
| assert( | |
| defined(invoker.package), | |
| "A `package` argument was missing when calling fuchsia_test($target_name)") | |
| assert( | |
| defined(invoker.component), | |
| "A `component` argument was missing when calling fuchsia_test($target_name)") | |
| package_name = get_label_info(invoker.package, "name") | |
| if (defined(invoker.package_name)) { | |
| package_name = invoker.package_name | |
| } | |
| manifest_name = get_target_outputs(invoker.component) | |
| manifest_name = get_path_info(manifest_name[0], "file") | |
| assert( | |
| get_path_info(manifest_name, "extension") == "cm", | |
| "`manifest_name` must end with '.cm'. Refer https://fxbug.dev/84888 for more information.") | |
| test_spec(target_name) { | |
| forward_variables_from(invoker, "*") | |
| target = get_label_info(invoker.package, "label_with_toolchain") | |
| build_rule = "fuchsia_test" | |
| package_out_dir = get_label_info(invoker.package, "target_out_dir") | |
| package_manifests = | |
| [ rebase_path("$package_out_dir/$package_name/package_manifest.json", | |
| root_build_dir) ] | |
| package_url = | |
| "fuchsia-pkg://fuchsia.com/$package_name#meta/$manifest_name" | |
| if (!defined(deps)) { | |
| deps = [] | |
| } | |
| deps += [ invoker.package ] | |
| } | |
| } else { | |
| group(target_name) { | |
| testonly = true | |
| deps = [ ":$target_name($default_toolchain)" ] | |
| forward_variables_from(invoker, [ "visibility" ]) | |
| } | |
| not_needed(invoker, "*") | |
| } | |
| } |