blob: 11dcf050c7bdff57912b4c7a91d94e1f009b6f20 [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("//build/dart/dart_component.gni")
import("//build/dart/dart_library.gni")
import("//build/flutter/internal/flutter_dart_component.gni")
# Defines a component which runs tests in the flutter/dart test harness
#
# Bundles a set of `package:test` tests into a single Fuchsia application
# with generated helper code to invoke the tests appropriately. The resulting
# application can take Dart test arguments; pass --help to see full options.
# Some options (e.g. Dart test platform selectors) are not supported.
#
# Note: this target is intended to be called by the corresponding
# flutter_test_component and dart_test_component instead of being called
# directly.
#
# Parameters
#
# manifest (required)
# The path to the component manifest
# Type: path
#
# test_runtime_shard (optional)
# The path to the dart test shard.
# Type: path
#
# sources (required)
# The list of test sources. These sources must be within the test/ directory.
# Type: List of paths
#
# language_version (optional)
# Specify the Dart language version to use for this test.
# Defaults to "2.8".
#
# component_name (optional)
# The name of the component to test.
# Type: String
# Default: target_name
#
# build_cfg (required)
# A description of how to build this component. This object needs
# to contain the following variables:
# runtime_meta: a path to the partial cmx file containing the runner
# runtime_meta_v2: a path to the partial cml file containing the runner
# platform_name: either 'dart_runner' or 'flutter_runner'
# is_aot: a boolean indicating if this is an AOT build
# is_product: a boolean indicating if this is a product build
# enable_asserts: whether we should enable asserts when compiling
#
# deps
# visibility
# non_dart_deps
template("flutter_dart_test_component") {
assert(defined(invoker.sources),
"flutter_dart_test_component() requires 'sources' be defined")
assert(defined(invoker.manifest),
"flutter_dart_test_component() requires 'manifest' be defined")
assert(defined(invoker.build_cfg),
"flutter_dart_test_component() requires 'build_cfg' be defined")
_build_cfg = invoker.build_cfg
_test_files = invoker.sources
_package_root = "."
if (defined(invoker.package_root)) {
_package_root = invoker.package_root
}
_pubspec = "${_package_root}/pubspec.yaml"
if (defined(invoker.pubspec)) {
_pubspec = invoker.pubspec
}
_test_package_name = string_replace(target_name, "-", "_") + "_test_package"
# Create a package containing all of the original test sources
_test_package_target_name = "${target_name}_test_library"
dart_library(_test_package_target_name) {
forward_variables_from(invoker,
[
"language_version",
"deps",
"non_dart_deps",
"null_safe",
])
testonly = true
visibility = [ ":*" ]
sources = _test_files
package_name = _test_package_name
package_root = "${_package_root}/test"
source_dir = "."
pubspec = _pubspec
if (defined(invoker.options_file)) {
options_file = invoker.options_file
}
# TODO(fxbug.dev/71902): replace the enclosing dart_library with an analysis
# target when they are decoupled.
#
# Skip source verification because the purpose of this target is to run
# analysis only. `dart_library` expects all sources under `source_dir` to be
# included in `sources`, and this doesn't apply to `dart_test` because it is
# valid to have multiple test files for different tests in the same dir.
disable_source_verification = true
}
# Generate a main which will call all of the test targets
_dart_gen_dir = get_label_info(":bogus($dart_toolchain)", "target_gen_dir")
_generate_test_main_target_name = target_name + "_test_main"
_generated_test_package = "${_test_package_name}_generated"
_generated_package_root =
"${_dart_gen_dir}/${target_name}/${_generated_test_package}"
_generated_test_main = "${_generated_package_root}/lib/main.dart"
action(_generate_test_main_target_name) {
script = "//build/flutter/internal/gen_test_component_main.py"
outputs = [ _generated_test_main ]
sources = []
args = [
"--out",
rebase_path(_generated_test_main, root_build_dir),
"--package",
_test_package_name,
]
foreach(_source, _test_files) {
args += [
"--source",
_source,
]
# The source needs to be added relative to the test/ directory since that
# is where the build system expects the file to exist whereas the dart
# compiler expects the source to be within the test directory since we are
# using test/ as the package root.
sources += [ "${_package_root}/test/${_source}" ]
}
testonly = true
visibility = [ ":*" ]
if (defined(invoker.non_dart_deps)) {
deps = invoker.non_dart_deps
}
}
# Create a package containing the main
_generated_package_target_name = "${target_name}_test_library_generated"
dart_library(_generated_package_target_name) {
forward_variables_from(invoker, [ "language_version" ])
testonly = true
visibility = [ ":*" ]
sources = [ "main.dart" ]
package_name = _generated_test_package
package_root = _generated_package_root
pubspec = _pubspec
# speed up compilation times by skipping these checks
disable_analysis = true
deps = [
":$_test_package_target_name",
"//src/flutter/lib/fuchsia_test_helper",
]
non_dart_deps = [ ":$_generate_test_main_target_name($target_toolchain)" ]
if (defined(invoker.non_dart_deps)) {
non_dart_deps += invoker.non_dart_deps
}
}
_component_deps = [
":$_generated_package_target_name",
_build_cfg.runner_dep,
]
if (defined(invoker.deps)) {
_component_deps += invoker.deps
}
flutter_dart_component(target_name) {
forward_variables_from(invoker,
[
"manifest",
"visibility",
"language_version",
"component_name",
"build_cfg",
"test_runtime_shard",
])
main_package = _generated_test_package
main_dart = "main.dart"
testonly = true
deps = _component_deps
}
}