blob: e1ed1e475061f0c2f4ce256b433cffac450018d6 [file] [log] [blame]
# Copyright 2018 The Chromium 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("//third_party/closure_compiler/compile_js.gni")
# Describes a list of js_library targets that will each have an html file
# written listing all its (flattened) js dependencies, for loading as a test.
#
# A companion group target with a "_type_check" suffix is also generated with
# this template. This depends on a list of js_type_check(..) targets -- one for
# each test -- that will type check the test js file and its dependency subtree.
#
# Must be declared after the js_library targets it depends on.
#
# Note js_type_check(..) is a no-op unless `closure_compile` is set in gn args.
#
# Variables:
# deps:
# List of js_library targets to depend on
#
# mocks:
# An optional list of .js files to load before any other scripts
#
# Example:
# js_unit_tests("folder_tests") {
# deps = [
# ":foo_unittest",
# ":bar_unittest",
# ":baz_unittest",
# ]
# mocks = [ "my_mocks.js" ]
# }
# group("closure_compile") {
# # ...
# ":folder_tests_type_check".
# }
# test("browser_tests") {
# # ...
# data_deps += [ "//folder:folder_tests" ]
# }
template("js_unit_tests") {
html_gen_target_name = target_name + "_html_gen"
action_foreach(html_gen_target_name) {
script_path = "//third_party/closure_compiler"
script = "$script_path/js_unit_test.py"
forward_variables_from(invoker,
[
"deps",
"mocks",
])
testonly = true
sources = []
foreach(dep, deps) {
sources += get_target_outputs(dep)
}
outputs = [
"$target_gen_dir/{{source_name_part}}.html",
]
args = [ "--output" ] + rebase_path(outputs, root_build_dir)
args += [ "--input" ] + [ "{{source}}" ]
# js_unit_test.py needs to walk .js_library dependency files relative to the
# gen_dir, but we'd like to reference the corresponding .js source files
# in the source dir, not the gen dir. Pass a prefix to remap --input args.
args += [
"--include_prefix",
rebase_path("//", "{{root_gen_dir}}", "//"),
]
if (defined(mocks)) {
args += [ "--mocks" ] + rebase_path(mocks, root_build_dir)
data = mocks
}
}
type_check_deps = []
foreach(dep, invoker.deps) {
type_check_target_name = target_name + "_" + dep + "_type_check"
type_check_deps += [ ":$type_check_target_name" ]
js_type_check(type_check_target_name) {
testonly = true
forward_variables_from(invoker, [ "closure_flags" ])
deps = [
dep,
]
}
}
type_check_group_name = target_name + "_type_check"
group(type_check_group_name) {
testonly = true
deps = type_check_deps
}
group(target_name) {
data = get_target_outputs(":$html_gen_target_name")
testonly = true
deps = [
":$html_gen_target_name",
]
}
}
# Simple wrapper around js_library to describe a unit test.
template("js_unittest") {
js_library(target_name) {
testonly = true
# Forward everything that js_library forwards except testonly (omitting that
# causes gn to emit "Assignment had no effect" if it's added redundantly).
forward_variables_from(invoker,
[
"deps",
"externs_list",
"extra_deps",
"extra_sources",
"sources",
"visibility",
])
}
}