blob: 0a4e95a1e5623ed1f848dd09705b392c668b2e25 [file] [log] [blame]
# Copyright 2019 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_library.gni")
import("//build/dart/toolchain.gni")
import("//topaz/runtime/dart/dart_kernel.gni")
# Defines a flutter test suite
#
# Parameters
#
# sources (required)
# The list of test files, which must be within source_dir.
#
# source_dir (optional)
# Directory containing the test sources. Defaults to "test".
# Note: this cannot be set to ".".
#
# deps (optional)
# List of labels for Dart libraries this suite depends on.
#
# disable_analysis (optional)
# Prevents analysis from being run on this target.
#
# Example of usage:
#
# flutter_test("baz_test") {
# deps = [
# "//foo/baz",
# "//third_party/dart-pkg/pub/test",
# ]
# }
if (current_toolchain == dart_toolchain) {
template("flutter_test") {
assert(defined(invoker.sources),
"flutter_test() requires 'sources' be defined")
if (defined(invoker.source_dir)) {
assert(invoker.source_dir != ".",
"Cannot set source_dir to '.' because it breaks code coverage.")
}
_main_target_name = target_name
_library_target_name = "${target_name}_library"
_sources_dir = "test"
if (defined(invoker.source_dir)) {
_sources_dir = invoker.source_dir
}
dart_library(_library_target_name) {
forward_variables_from(invoker,
[
"deps",
"disable_analysis",
])
infer_package_name = true
sources_required = false
source_dir = _sources_dir
sources = invoker.sources
}
_dot_packages_file = "$target_gen_dir/$_library_target_name.packages"
_fuchsia_tester_label = "//third_party/dart-pkg/git/flutter/packages/flutter_tools:fuchsia_tester($host_toolchain)"
_fuchsia_tester_out_dir =
get_label_info(_fuchsia_tester_label, "root_out_dir")
_fuchsia_tester_bin = "$_fuchsia_tester_out_dir/dart-tools/fuchsia_tester"
_flutter_shell_label =
"//third_party/flutter/shell/testing/($host_toolchain)"
_flutter_shell_out_dir =
get_label_info(_flutter_shell_label, "root_out_dir")
_flutter_shell_bin = "$_flutter_shell_out_dir/flutter_tester"
_precompiled_kernel_target_names = []
_tests_json = []
_tests_filename = "$target_gen_dir/tests.json"
foreach(_source_file, invoker.sources) {
_source_path = "$_sources_dir/$_source_file"
_test_target =
exec_script("//topaz/runtime/dart/gen_flutter_test_target_name.py",
[
"--file",
_source_file,
],
"trim string")
if (_test_target != "") {
_test_target_name = "${_main_target_name}_${_test_target}"
_kernel_target_name = "${_test_target_name}_dill"
_bootstrap_target_name = "${_test_target_name}_bootstrap"
_bootstrap_file_name =
"${target_gen_dir}/${_bootstrap_target_name}.dart"
action(_bootstrap_target_name) {
script = "$root_out_dir/dart-tools/build_test_bootstrap"
outputs = [
_bootstrap_file_name,
]
rebased_source = rebase_path(_source_path, target_gen_dir)
args = [
"--output",
rebase_path(_bootstrap_file_name),
"--test-name",
"$rebased_source",
]
deps = [
"//topaz/runtime/dart:build_test_bootstrap",
]
}
dart_kernel(_kernel_target_name) {
platform_name = "flutter_runner"
disable_analysis = true
main_dart = _bootstrap_file_name
args = []
# TODO(tvolkert): Change to flutter platform once libraries.json works
# in Fuchsia
# platform_deps = [
# "//third_party/flutter/lib/snapshot:strong_platform",
# ]
# platform_path = "$root_out_dir/flutter_patched_sdk"
platform_deps =
[ "//topaz/runtime/flutter_runner/kernel:kernel_platform_files" ]
platform_path = "$root_out_dir/flutter_runner_patched_sdk"
sources = [
_bootstrap_file_name,
]
sources_required = false
non_dart_deps = [ ":$_bootstrap_target_name" ]
deps = [
":$_library_target_name",
"//third_party/dart-pkg/git/flutter/packages/flutter_test",
"//third_party/dart-pkg/pub/stream_channel",
"//third_party/dart-pkg/pub/test",
]
}
_precompiled_kernel_target_names += [ ":${_kernel_target_name}_kernel" ]
_tests_json += [
{
source = rebase_path(_bootstrap_file_name)
dill =
rebase_path("$target_gen_dir/${_kernel_target_name}_kernel.dil")
},
]
}
write_file(_tests_filename, _tests_json, "json")
}
action(_main_target_name) {
_invocation_file = "$target_gen_dir/$target_name"
script = "//topaz/runtime/dart/gen_flutter_test_invocation.py"
testonly = true
outputs = [
_invocation_file,
]
inputs = [
_dot_packages_file,
_bootstrap_file_name,
_fuchsia_tester_bin,
_flutter_shell_bin,
_tests_filename,
]
args = [
"--out",
rebase_path(_invocation_file),
"--tests",
rebase_path(_tests_filename),
"--source-dir",
rebase_path(_sources_dir),
"--dot-packages",
rebase_path(_dot_packages_file),
"--test-runner",
rebase_path(_fuchsia_tester_bin),
"--flutter-shell",
rebase_path(_flutter_shell_bin),
"--icudtl",
rebase_path("//third_party/icu/common/icudtl.dat"),
"--sdk-root",
rebase_path("$root_out_dir/flutter_runner_patched_sdk"),
]
deps = [
":$_library_target_name",
":$_bootstrap_target_name",
":${_kernel_target_name}_kernel",
_flutter_shell_label,
_fuchsia_tester_label,
] + _precompiled_kernel_target_names
metadata = {
test_runtime_deps = [ rebase_path(_invocation_file, root_build_dir) ]
}
}
}
} else {
# Not the Dart toolchain.
template("flutter_test") {
_main_target_name = target_name
_invocation_file = "$target_gen_dir/$target_name"
if (is_linux || is_mac) {
# TODO(IN-819): Delete renaming when dart tests are no longer run out of one
# directory
_invocation_file = "$root_out_dir/$target_name"
}
action(_main_target_name) {
script = "//topaz/runtime/dart/gen_flutter_test_bundle_invocation.py"
testonly = true
not_needed(invoker, "*")
outputs = [
_invocation_file,
]
_dart_target_gen_dir =
get_label_info(":bogus($dart_toolchain)", "target_gen_dir")
_delegate_file = "$_dart_target_gen_dir/$target_name"
args = [
"--out",
rebase_path(_invocation_file),
"--test",
rebase_path(_delegate_file),
]
deps = [
":$_main_target_name($dart_toolchain)",
]
if (is_linux || is_mac) {
# TODO(IN-819): Temporary measure to support the fx workflow, which is to
# take a dependency on tests.json, to which this metadata will be
# supplementary. Once flutter tests are contributing test spec metadata,
# all relevant information will be in the former and this is to be
# deleted.
metadata = {
pseudo_test_spec = [
{
test = {
location = rebase_path(_invocation_file, root_build_dir)
os = current_os
}
},
]
}
}
}
}
}