blob: 6ec708e88e9dfcac9baa60db4db64d05973fc807 [file] [log] [blame]
# 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("fuchsia_component.gni")
import("fuchsia_test_package.gni")
# Defines a package that contains a component that launches a test.
# See: https://fuchsia.dev/fuchsia-src/development/components/build
#
# This template is a useful shortcut for defining classic unit tests for target
# devices.
# Pure unit tests don't require any special capabilities, so their component
# manifest can be generated by the template if a manifest is not specified.
#
# Example:
# ```
# executable("rot13_encoder_decoder_test") {
# sources = [ "rot13_encoder_decoder_test.cc" ]
# testonly = true
# }
#
# fuchsia_unittest_package("rot13-test") {
# executable_path = "bin/rot13_encoder_decoder_test"
# deps = [ ":rot13_encoder_decoder_test" ]
# }
# ```
#
# The above will generate a manifest and create a test with the launch URL:
# fuchsia-pkg://fuchsia.com/rot13-test#meta/rot13-test.cmx
#
# Parameters
#
# package_name (optional)
# The name of the package.
# Type: string
# Default: target_name
#
# manifest (required if `executable_path` is not specified)
# Specify a component manifest for the test.
# If a manifest is not specified, a trivial manifest will be generated.
# The generated manifest requests no special capabilities, which is good
# enough for "pure" unit tests.
# To view the generated manifest, see:
# `fx gn outputs out/default path/to/your:<target_name>_generated_manifest`
# Type: path
#
# executable_path (required if `manifest` is not specified)
# Specify the packaged path of the test executable.
# Example: "test/my_executable"
# This executable must be generated by one of the `deps`.
# This is used in generating the manifest.
# Type: string
#
# test_specs (optional)
# Additional test specifications to apply to the test defined above.
# See `test_spec.gni`.
# Type: scope
#
# deps
# visibility
template("fuchsia_unittest_package") {
assert(
defined(invoker.manifest) || defined(invoker.executable_path),
"Either `manifest` or `executable_path` must be specified when calling fuchsia_unittest_package($target_name)")
assert(
!(defined(invoker.manifest) && defined(invoker.executable_path)),
"Specifying `executable_path` has no effect when specifying `manifest` when calling fuchsia_unittest_package($target_name)")
if (!defined(invoker.manifest)) {
generated_target = "${target_name}_generated_manifest"
generated_file(generated_target) {
contents = {
program = {
binary = invoker.executable_path
}
sandbox = {
services = [ "fuchsia.logger.LogSink" ]
}
}
outputs = [ "$target_out_dir/${generated_target}.cmx" ]
output_conversion = "json"
testonly = true
visibility = [ ":*" ]
}
manifest = get_target_outputs(":$generated_target")
manifest = manifest[0]
}
component_target = "${target_name}_component"
fuchsia_component(component_target) {
forward_variables_from(invoker,
[
"deps",
"manifest",
])
component_name = invoker.target_name
testonly = true
visibility = [ ":*" ]
if (defined(generated_target)) {
if (!defined(deps)) {
deps = []
}
deps += [ ":$generated_target" ]
}
}
fuchsia_test_package(target_name) {
forward_variables_from(invoker,
[
"package_name",
"test_specs",
"visibility",
])
test_components = [ ":$component_target" ]
}
}