| # 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/dart_package_config.gni") |
| import("//build/dart/kernel/dart_kernel.gni") |
| import("//build/dart/toolchain.gni") |
| import("//build/testing/test_spec.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. |
| # |
| # null_safe (optional) |
| # A flag that enables null safety check in dart libraries. |
| # |
| # 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" |
| _copy_target_name = "${target_name}_copy" |
| _snapshot_target_name = "${target_name}_snapshot" |
| |
| _source_dir = "test" |
| if (defined(invoker.source_dir)) { |
| _source_dir = invoker.source_dir |
| } |
| |
| _dart_deps = [] |
| if (defined(invoker.deps)) { |
| _dart_deps += invoker.deps |
| } |
| |
| dart_library(_library_target_name) { |
| forward_variables_from(invoker, |
| [ |
| "disable_analysis", |
| "null_safe", |
| ]) |
| |
| package_name = _main_target_name |
| |
| # We want to mimic the package_root being in place of the source_dir. Dart |
| # does not allow multiple packages to share the same package_root so we |
| # do this so our /test directories can live along side out /lib directories |
| # which is how most dart packages are structured in out tree. |
| package_root = rebase_path(_source_dir, ".") |
| source_dir = "." |
| |
| sources = invoker.sources |
| |
| deps = _dart_deps |
| |
| # We include the pubspec and analysis options files which are at the |
| # original package root since we are effectively changing the package_root |
| # for this library. |
| pubspec = "pubspec.yaml" |
| |
| testonly = true |
| |
| # 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 |
| } |
| |
| _packages_file = "$target_gen_dir/${target_name}_package_config.json" |
| _dart_deps += [ |
| ":$_library_target_name", |
| "//third_party/dart-pkg/git/flutter/packages/flutter_test", |
| "//third_party/dart-pkg/git/flutter/packages/flutter_tools", |
| ] |
| _package_config_target_name = "${target_name}_package_config" |
| |
| dart_package_config(_package_config_target_name) { |
| testonly = true |
| deps = _dart_deps |
| outputs = [ _packages_file ] |
| } |
| |
| # The binary does not depend on the runtime-mode, using debug is fine. |
| _flutter_tester_label = "//src/flutter:flutter_tester" |
| _flutter_tester_gen_dir = |
| get_label_info(_flutter_tester_label, "target_gen_dir") |
| _flutter_tester_bin = "${_flutter_tester_gen_dir}/flutter_tester" |
| |
| _precompiled_kernel_target_names = [] |
| _tests_json = [] |
| _tests_filename = "$target_gen_dir/tests.json" |
| |
| _test_runtime_deps = [ _tests_filename ] |
| |
| foreach(_source_file, invoker.sources) { |
| _source_path = "$_source_dir/$_source_file" |
| _trimmed_source = string_replace(_source_file, "_test.dart", "") |
| if (_source_file != _trimmed_source) { |
| _trimmed_source = string_replace(_trimmed_source, ".", "_") |
| _trimmed_source = string_replace(_trimmed_source, "/", "_") |
| _test_target_name = "${_main_target_name}_${_trimmed_source}" |
| _kernel_target_name = "${_test_target_name}_dill" |
| _bootstrap_target_name = "${_test_target_name}_bootstrap" |
| _pubspec_target_name = "${_test_target_name}_pubspec" |
| |
| _bootstrap_filename = "$target_gen_dir/${_bootstrap_target_name}.dart" |
| _dill_filename = "$target_gen_dir/__untraced_dart_kernel__/${_kernel_target_name}_kernel.dil" |
| |
| action(_bootstrap_target_name) { |
| script = "$root_out_dir/dart-tools/build_test_bootstrap" |
| inputs = [ "$root_gen_dir/build/flutter/internal/build_test_bootstrap/build_test_bootstrap.snapshot" ] |
| outputs = [ _bootstrap_filename ] |
| |
| rebased_source = rebase_path(_source_path, target_gen_dir) |
| args = [ |
| "--output", |
| rebase_path(_bootstrap_filename, root_build_dir), |
| "--test-name", |
| "$rebased_source", |
| ] |
| |
| deps = [ |
| "//build/flutter/internal/build_test_bootstrap", |
| "//build/flutter/internal/build_test_bootstrap:build_test_bootstrap_snapshot", |
| ] |
| } |
| |
| # Dart requires each package to have a unique package_root. This |
| # will copy the pubspec into a unique directory for each source |
| # allowing us to set that directory as the package_root. |
| copy(_pubspec_target_name) { |
| sources = [ "pubspec.yaml" ] |
| outputs = [ "${target_gen_dir}/${_test_target_name}/pubspec.yaml" ] |
| } |
| |
| dart_kernel(_kernel_target_name) { |
| testonly = true |
| platform_name = "flutter_runner" |
| packages_path = _packages_file |
| main_dart_file = _bootstrap_filename |
| |
| product = false |
| is_aot = false |
| |
| # Per dnfield@, flutter unit tests should always be run with asserts |
| # enabled in debug mode |
| is_debug = true |
| |
| # By default the dart_kernel will not link the current platform.dill but |
| # when running host tests this will fail because the sdk patched for |
| # fuchsia will be used. |
| link_platform = true |
| |
| deps = [ |
| ":$_bootstrap_target_name", |
| ":$_library_target_name", |
| ":$_package_config_target_name", |
| ":$_pubspec_target_name", |
| "//third_party/dart-pkg/git/flutter/packages/flutter_test", |
| "//third_party/dart-pkg/pub/clock", |
| "//third_party/dart-pkg/pub/fake_async", |
| "//third_party/dart-pkg/pub/stack_trace", |
| "//third_party/dart-pkg/pub/stream_channel", |
| "//third_party/dart-pkg/pub/test", |
| "//third_party/dart-pkg/pub/test_api", |
| ] |
| } |
| |
| _precompiled_kernel_target_names += [ ":${_kernel_target_name}_kernel" ] |
| |
| _tests_json += [ |
| { |
| source = rebase_path(_bootstrap_filename, root_build_dir) |
| dill = rebase_path(_dill_filename, root_build_dir) |
| }, |
| ] |
| _test_runtime_deps += [ |
| _bootstrap_filename, |
| _dill_filename, |
| ] |
| } |
| } |
| |
| write_file(_tests_filename, _tests_json, "json") |
| |
| # Copies resources to the build directory so that it may be archived |
| # with the test and the rest of the test's dependencies, so that the |
| # archiving happens with respect to the build directory. |
| _data_dir = "$target_gen_dir/${_main_target_name}_data" |
| _icudtl_file = "$_data_dir/icudtl.dat" |
| _dart_binary = "$_data_dir/dart" |
| copy(_copy_target_name) { |
| sources = [ |
| "//prebuilt/third_party/flutter/$host_cpu/deps/icudtl.dat", |
| prebuilt_dart, |
| ] |
| outputs = [ "$_data_dir/{{source_file_part}}" ] |
| } |
| |
| # Creates a snapshot file of the fuchsia tester, which allows the test to |
| # be invoked hermetically. |
| _snapshot = "$target_gen_dir/${_main_target_name}.snapshot" |
| _flutter_tools_label = "//third_party/dart-pkg/git/flutter/packages/flutter_tools:flutter_tools" |
| _main_file = "//third_party/dart-pkg/git/flutter/packages/flutter_tools/bin/fuchsia_tester.dart" |
| |
| action(_snapshot_target_name) { |
| testonly = true |
| depfile = "${_snapshot}.d" |
| |
| outputs = [ _snapshot ] |
| |
| # Dart writes absolute paths to depfiles, convert them to relative. |
| # See more information in https://fxbug.dev/75451. |
| script = "//build/depfile_path_to_relative.py" |
| inputs = [ _dart_binary ] |
| |
| args = [ |
| "--depfile=" + rebase_path(depfile, root_build_dir), |
| "--", |
| rebase_path(_dart_binary, root_build_dir), |
| "--verbosity=warning", |
| "--snapshot=" + rebase_path(_snapshot, root_build_dir), |
| "--snapshot-depfile=" + rebase_path(depfile, root_build_dir), |
| "--packages=" + rebase_path(_packages_file, root_build_dir), |
| rebase_path(_main_file, root_build_dir), |
| ] |
| |
| deps = dart_sdk_deps + [ |
| ":$_copy_target_name", |
| ":$_package_config_target_name", |
| "$_flutter_tools_label", |
| ] |
| } |
| |
| _invocation_file = "$target_gen_dir/$target_name" |
| |
| # _invocation_params encapsulates the parameters to pass to the |
| # invocation-generating action below. The utility lies in being able to |
| # construct the actions args and metadata at the same time. |
| _invocation_params = [ |
| { |
| flag = "--wd" |
| |
| # TODO(crbug.com/gn/56): Rebasing root_build_dir alone yields a path |
| # component that leaves root_build_dir, preventing portability. |
| path = "$root_build_dir/dummy/.." |
| base = get_path_info(_invocation_file, "dir") |
| }, |
| { |
| flag = "--out" |
| path = _invocation_file |
| base = root_build_dir |
| }, |
| { |
| flag = "--dart" |
| path = _dart_binary |
| base = root_build_dir |
| }, |
| { |
| flag = "--snapshot" |
| path = _snapshot |
| base = root_build_dir |
| }, |
| { |
| flag = "--tests" |
| path = _tests_filename |
| base = root_build_dir |
| }, |
| { |
| flag = "--dot-packages" |
| path = _packages_file |
| base = root_build_dir |
| }, |
| { |
| flag = "--flutter-shell" |
| path = _flutter_tester_bin |
| base = root_build_dir |
| }, |
| { |
| flag = "--icudtl" |
| path = _icudtl_file |
| base = root_build_dir |
| }, |
| { |
| flag = "--sdk-root" |
| path = "$root_out_dir/flutter_runner_patched_sdk" |
| base = root_build_dir |
| }, |
| ] |
| |
| action(_main_target_name) { |
| script = "//build/flutter/internal/gen_flutter_test_invocation.py" |
| testonly = true |
| outputs = [ _invocation_file ] |
| |
| inputs = [ |
| _packages_file, |
| _bootstrap_filename, |
| _flutter_tester_bin, |
| _tests_filename, |
| ] |
| |
| args = [] |
| foreach(param, _invocation_params) { |
| args += [ |
| param.flag, |
| rebase_path(param.path, param.base), |
| ] |
| if (param.flag != "--wd") { |
| _test_runtime_deps += [ param.path ] |
| } |
| } |
| |
| deps = [ |
| ":$_bootstrap_target_name", |
| ":$_copy_target_name", |
| ":$_library_target_name", |
| ":$_package_config_target_name", |
| ":$_snapshot_target_name", |
| ":${_kernel_target_name}_kernel", |
| _flutter_tester_label, |
| ] + _precompiled_kernel_target_names |
| |
| metadata = { |
| test_runtime_deps = _test_runtime_deps |
| } |
| } |
| } |
| } else { |
| # Not the Dart toolchain. |
| template("flutter_test") { |
| _main_target_name = target_name |
| _spec_target_name = "${target_name}_spec" |
| _invocation_file = "$target_gen_dir/$target_name" |
| |
| if (is_linux || is_mac) { |
| test_spec(_spec_target_name) { |
| target = get_label_info(":$_main_target_name", "label_with_toolchain") |
| path = _invocation_file |
| |
| deps = [ ":$_main_target_name($dart_toolchain)" ] |
| } |
| } else { |
| not_needed([ "_spec_target_name" ]) |
| } |
| |
| action(_main_target_name) { |
| script = "//build/flutter/internal/gen_flutter_test_bundle_invocation.py" |
| testonly = true |
| not_needed(invoker, "*") |
| |
| outputs = [ _invocation_file ] |
| |
| _dart_target_gen_dir = |
| get_label_info(":anything($dart_toolchain)", "target_gen_dir") |
| _delegate_file = "$_dart_target_gen_dir/$_main_target_name" |
| |
| args = [ |
| "--wd", |
| |
| # TODO(crbug.com/gn/56): Rebasing root_build_dir alone yields a path |
| # component that leaves root_build_dir, preventing portability. |
| rebase_path("$root_build_dir/dummy/..", |
| get_path_info(_invocation_file, "dir")), |
| "--out", |
| rebase_path(_invocation_file, root_build_dir), |
| "--test", |
| rebase_path(_delegate_file, root_build_dir), |
| ] |
| |
| deps = [ ":$_main_target_name($dart_toolchain)" ] |
| |
| if (is_linux || is_mac) { |
| data_deps = [ ":$_spec_target_name" ] |
| } |
| } |
| } |
| } |