blob: f5a1b94719a24f6ecf5b76fd2646bc90ba81a05e [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/python/python_binary.gni")
import("//build/testing/host_test.gni")
import("//build/testing/host_test_data.gni")
# Declares a host-side python test.
#
# Example
#
# ```
# python_host_test("my_host_test") {
# enable_mypy = true
# main_source = "my_host_test.py"
# sources = [
# "test_helpers_foo.py",
# "test_helpers_bar.py",
# ]
# libraries = [
# "//path/to/foo:lib",
# "//path/to/bar:lib",
# ]
# main_callable = "my_module.main"
# extra_args = [ "--a_very_useful_flag" ]
# }
# ```
#
# Parameters
#
# main_source (required)
# The .py file that will be interpreted.
# Type: path
#
# sources (optional)
# Other files that are used in the test.
# Type: list(path)
# Default: empty list
#
# libraries (optional)
# Paths to python_libraries this test imports.
# Type: list(string)
# Default: empty list
#
# test_dir (optional)
# Path to where the test executes.
# Type: string
# Default: "${target_out_dir}/${target_name}"
#
# extra_args (optional)
# Additional arguments to pass to the test.
# Type: list(string)
#
# main_callable (optional)
# Main callable, which serves as the entry point of the output zip archive.
# In the example above, this is "my_module.main".
# Type: string
# Default: unittest.main
#
# enable_mypy (optional)
# If true, enable MyPy type checking on the target and respective deps.
# Type: boolean
# Default: False
#
# test_data_deps (optional)
# The dependencies used only at runtime for this test.
#
# deps
# environments
# visibility
template("python_host_test") {
assert(defined(invoker.main_source), "main_source is required")
_python_binary_name = "${target_name}.pyz"
_python_binary_target = "${target_name}_python_binary"
python_binary(_python_binary_target) {
forward_variables_from(invoker,
[
"main_source",
"sources",
"deps",
"enable_mypy",
])
testonly = true
visibility = [ ":*" ]
output_name = _python_binary_name
main_callable = "unittest.main"
if (defined(invoker.main_callable)) {
main_callable = invoker.main_callable
}
if (!defined(deps)) {
deps = []
}
if (defined(invoker.libraries)) {
deps += invoker.libraries
}
}
_test_dir = "${target_out_dir}/${target_name}"
if (defined(invoker.test_dir)) {
_test_dir = invoker.test_dir
}
_host_test_data_target = "${target_name}_test_data"
host_test_data(_host_test_data_target) {
testonly = true
visibility = [ ":*" ]
sources = [ get_label_info(":${_python_binary_target}", "target_out_dir") +
"/${_python_binary_name}" ]
outputs = [ "${_test_dir}/${_python_binary_name}" ]
deps = [ ":${_python_binary_target}" ]
if (defined(invoker.deps)) {
deps += invoker.deps
}
if (!defined(data_deps)) {
data_deps = []
}
if (defined(invoker.test_data_deps)) {
data_deps += invoker.test_data_deps
}
}
host_test(target_name) {
forward_variables_from(invoker,
[
"environments",
"metadata",
"visibility",
])
binary_path = python_exe_src
args =
[ rebase_path("${_test_dir}/${_python_binary_name}", root_build_dir) ]
if (defined(invoker.extra_args)) {
args += invoker.extra_args
}
deps = [
":${_host_test_data_target}",
"//build/python:interpreter",
]
}
}