# Copyright (c) 2021 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import("//build/config/clang/clang.gni") import("//build/config/python.gni") import("//build/ohos.gni") import("//build/ohos_var.gni") import("//build/templates/cxx/cxx.gni") if (!is_arkui_x) { import("//build/rust/rustc_toolchain.gni") } template("_testcase_resources") { assert(defined(invoker.testcase_target_name)) assert(defined(invoker.test_output_dir)) assert(defined(invoker.module_out_path)) _deps = [] if (defined(invoker.deps)) { _deps += invoker.deps } action_with_pydeps(target_name) { if (defined(invoker.testonly)) { testonly = invoker.testonly } deps = _deps inputs = [] script = "//build/ohos/testfwk/testcase_resource_copy.py" output_file = "$target_out_dir/$target_name.json" outputs = [ output_file ] depfile = "$target_gen_dir/$target_name.d" args = [] if (defined(invoker.resource_config_file)) { args += [ "--resource-config-file", rebase_path(invoker.resource_config_file, root_build_dir), ] inputs += [ invoker.resource_config_file ] } args += [ "--depfile", rebase_path(depfile, root_build_dir), "--testcase-target-name", invoker.testcase_target_name, "--part-build-out-path", rebase_path(root_out_dir, root_build_dir), "--resource-output-path", rebase_path(invoker.test_output_dir + "/resource", root_build_dir), "--module-out-path", invoker.module_out_path, "--output-file", rebase_path(output_file, root_build_dir), ] } } # ohos test template template("_ohos_test") { assert(defined(invoker.test_type), "test_type is required.") assert(defined(invoker.module_out_path)) _deps = [] if (defined(invoker.deps)) { _deps += invoker.deps } if (defined(invoker.crate_type)) { _deps += [ "//build/rust:libstd.dylib.so", "//build/rust:libtest.dylib.so", ] } test_output_dir = "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}" # generate module list file in gn stage if (!skip_generate_module_list_file) { _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py" _arguments = [ "--target", target_name, "--target_label", get_label_info(":$target_name", "label_with_toolchain"), "--source_dir", rebase_path(get_label_info(":$target_name", "dir"), root_build_dir), "--test_type", invoker.test_type, ] _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf" _arguments += [ "--output_dir", rebase_path(test_output_dir, root_build_dir), "--module_list_file", rebase_path(_module_list_file, root_build_dir), ] exec_script(_gen_module_list_script, _arguments) } # copy testcase resource testcase_target_name = target_name _testcase_resources("${testcase_target_name}_resource_copy") { if (defined(invoker.resource_config_file)) { resource_config_file = invoker.resource_config_file } module_out_path = invoker.module_out_path deps = _deps testonly = true } # copy fuzz config file if (defined(invoker.fuzz_config_file)) { fuzz_config_file = invoker.fuzz_config_file action("${target_name}_copy_fuzz_config_file") { script = "//build/ohos/testfwk/fuzz_config_file_copy.py" inputs = [ fuzz_config_file ] args = [] args += [ "--fuzz-config-file-path", rebase_path(fuzz_config_file, root_build_dir), "--fuzz-config-file-output-path", rebase_path(root_out_dir + "/tests/res", root_build_dir), ] outputs = [ "$target_out_dir/${target_name}_result.txt" ] } } _has_sources = defined(invoker.sources) && invoker.sources != [] if (_has_sources) { _c_sources_file = "$target_gen_dir/$target_name.sources" write_file(_c_sources_file, rebase_path(invoker.sources, root_build_dir)) } write_file("$test_output_dir/${target_name}_path.txt", get_label_info(":$target_name", "dir"), "string") ohos_executable(target_name) { forward_variables_from(invoker, "*", [ "test_type", "module_out_path", "visibility", ]) forward_variables_from(invoker, [ "visibility" ]) if (!defined(deps)) { deps = [] } deps += [ ":${testcase_target_name}_resource_copy" ] if (defined(invoker.fuzz_config_file)) { deps += [ ":${target_name}_copy_fuzz_config_file" ] } subsystem_name = "tests" part_name = invoker.test_type ohos_test = true testonly = true output_name = "$target_name" } } template("ohos_unittest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "unittest" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } deps += [ "//third_party/googletest:gtest_main" ] } } template("ohos_moduletest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "moduletest" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } deps += [ "//third_party/googletest:gtest_main" ] } } template("ohos_systemtest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "systemtest" } } template("ohos_performancetest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "performance" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } deps += [ "//test/testfwk/developer_test/aw/cxx/hwext:performance_test_static", "//third_party/googletest:gtest_main", ] } } template("ohos_securitytest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "security" } } template("ohos_reliabilitytest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "reliability" } } template("ohos_distributedtest") { _ohos_test(target_name) { forward_variables_from(invoker, "*") test_type = "distributedtest" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } deps += [ "//test/testfwk/developer_test/aw/cxx/distributed:distributedtest_lib", ] } } template("ohos_fuzztest") { # Judge the compliation library. # Do the FUZZ compliation if the compliation library is the musl. if (use_musl) { _ohos_test(target_name) { forward_variables_from(invoker, "*", [ "deps", "cflags", ]) test_type = "fuzztest" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } cflags = [] if (defined(invoker.cflags)) { cflags += invoker.cflags } cflags += [ "-fno-sanitize-coverage=trace-pc-guard,edge,trace-cmp,indirect-calls,8bit-counters", "-fsanitize=fuzzer", ] if (target_cpu == "arm64") { libs = [ "$clang_lib_base_path/aarch64-linux-ohos/libclang_rt.fuzzer.a" ] } else if (target_cpu == "x86_64") { libs = [ "$clang_lib_base_path/x86_64-linux-ohos/libclang_rt.fuzzer.a" ] } else { libs = [ "$clang_lib_base_path/arm-linux-ohos/libclang_rt.fuzzer.a" ] } } } else { group(target_name) { not_needed(invoker, "*") } } } template("ohos_benchmarktest") { _ohos_test(target_name) { forward_variables_from(invoker, "*", [ "deps" ]) test_type = "benchmark" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } deps += [ "//third_party/benchmark:benchmark" ] } } template("_test_py_file_copy") { assert(defined(invoker.sources), "sources is required.") assert(defined(invoker.target_base_dir)) assert(defined(invoker.copy_output_dir)) action_with_pydeps(target_name) { forward_variables_from(invoker, [ "sources", "testonly", "visibility", ]) script = "//build/ohos/testfwk/test_py_file_copy.py" deps = [] if (defined(invoker.deps)) { deps += invoker.deps } depfile = "$target_gen_dir/$target_name.d" outfile = "$target_out_dir/$target_name.out" outputs = [ outfile ] args = [ "--target-base-dir", rebase_path(invoker.target_base_dir, root_build_dir), "--copy-output-dir", rebase_path(invoker.copy_output_dir, root_build_dir), "--outfile", rebase_path(outfile, root_build_dir), "--depfile", rebase_path(depfile, root_build_dir), "--source-files", ] args += rebase_path(sources, root_build_dir) } } # python test template template("_ohos_test_py") { assert(defined(invoker.test_type), "test_type is required.") assert(defined(invoker.sources), "sources is required.") _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py" _arguments = [ "--target", target_name, "--target_label", get_label_info(":$target_name", "label_with_toolchain"), "--source_dir", rebase_path(get_label_info(":$target_name", "dir"), root_build_dir), "--test_type", invoker.test_type, ] if (defined(invoker.module_out_path)) { test_output_dir = "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}" _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf" } else { test_output_dir = "$root_out_dir/tests/${invoker.test_type}" _module_list_file = "$root_out_dir/module_list_files/$target_name.mlf" } _arguments += [ "--output_dir", rebase_path(test_output_dir, root_build_dir), "--module_list_file", rebase_path(_module_list_file, root_build_dir), ] exec_script(_gen_module_list_script, _arguments) _test_py_file_copy(target_name) { testonly = true target_base_dir = get_label_info(":$target_name", "dir") copy_output_dir = test_output_dir sources = get_path_info(invoker.sources, "abspath") } } template("ohos_unittest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "unittest" } } template("ohos_moduletest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "moduletest" } } template("ohos_systemtest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "systemtest" } } template("ohos_performancetest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "performance" } } template("ohos_securitytest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "security" } } template("ohos_reliabilitytest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "reliability" } } template("ohos_distributedtest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "distributedtest" } } template("ohos_fuzztest_py") { _ohos_test_py(target_name) { forward_variables_from(invoker, "*") test_type = "fuzztest" } } #js api test template template("ohos_js_test") { assert(defined(invoker.test_type), "test_type must be defined.") assert(defined(invoker.hap_profile), "hap_profile must be defined.") assert(defined(invoker.module_out_path), "module_out_path must be defined.") # generate module list file in gn stage _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py" _arguments = [ "--target", target_name, "--target_label", get_label_info(":$target_name", "label_with_toolchain"), "--source_dir", rebase_path(get_label_info(":$target_name", "dir"), root_build_dir), "--test_type", invoker.test_type, ] _test_output_dir = "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}" _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf" _arguments += [ "--output_dir", rebase_path(_test_output_dir, root_build_dir), "--module_list_file", rebase_path(_module_list_file, root_build_dir), ] exec_script(_gen_module_list_script, _arguments) # copy testcase resource testcase_target_name = target_name _testcase_resources("${testcase_target_name}_resource_copy") { if (defined(invoker.resource_config_file)) { resource_config_file = invoker.resource_config_file } module_out_path = invoker.module_out_path test_output_dir = _test_output_dir } _suite_path = get_label_info(":$target_name", "dir") _template_path = "//test/testfwk/developer_test/libs/js_template" _target_path = "${_template_path}/$target_name" _target_js_copy = "${target_name}__js_copy" action_with_pydeps(_target_js_copy) { script = "//build/ohos/testfwk/test_js_file_copy.py" outputs = [ "$target_out_dir/$target_name.out" ] args = [ "--suite_path", rebase_path(_suite_path, root_build_dir), "--template_path", rebase_path(_template_path, root_build_dir), "--target_path", rebase_path(_target_path, root_build_dir), "--test_output_dir", rebase_path(_test_output_dir, root_build_dir), "--target_name", testcase_target_name, ] deps = [ ":${testcase_target_name}_resource_copy" ] } _target_js_assets = "${target_name}__intl_js_assets" ohos_js_assets(_target_js_assets) { source_dir = "${_target_path}/src/main/js/default" deps = [ ":$_target_js_copy" ] } _target_resources = "${target_name}__resources" ohos_resources(_target_resources) { sources = [ "${_target_path}/src/main/resources" ] deps = [ ":$_target_js_assets" ] hap_profile = invoker.hap_profile } ohos_hap(target_name) { forward_variables_from(invoker, "*", [ "test_type", "module_out_path", "visibility", ]) forward_variables_from(invoker, [ "visibility" ]) deps = [ ":$_target_js_assets", ":$_target_resources", ] final_hap_path = "$_test_output_dir/$target_name.hap" js_build_mode = "debug" testonly = true } } template("ohos_js_unittest") { ohos_js_test(target_name) { forward_variables_from(invoker, "*") test_type = "unittest" } } template("ohos_js_stage_test") { if (defined(invoker.part_name)) { _part_name = invoker.part_name } if (defined(invoker.subsystem_name)) { _subsystem_name = invoker.subsystem_name } if (defined(invoker.test_type)) { _test_type = invoker.test_type } _build_part_boolean = true # generate module list file in gn stage if (!skip_generate_module_list_file) { _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py" _arguments = [ "--target", target_name, "--target_label", get_label_info(":$target_name", "label_with_toolchain"), "--source_dir", rebase_path(get_label_info(":$target_name", "dir"), root_build_dir), "--test_type", _test_type, ] _test_output_dir = "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}" _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf" _arguments += [ "--output_dir", rebase_path(_test_output_dir, root_build_dir), "--module_list_file", rebase_path(_module_list_file, root_build_dir), ] exec_script(_gen_module_list_script, _arguments) } assert(defined(invoker.hap_name), "hap_name is required in target ${target_name}") assert(!defined(invoker.final_hap_path), "please use hap_name instead of final_hap_path") _hap_name = invoker.hap_name _final_hap_path = "" _target_name = "" _final_hap_path = "$root_out_dir/tests/haps/${invoker.module_out_path}/${_hap_name}.hap" _target_name = "module_${target_name}" _archive_filename = "${_hap_name}.hap" if (_build_part_boolean == true) { target("ohos_hap", _target_name) { forward_variables_from(invoker, "*") final_hap_path = _final_hap_path testonly = true } } else { print(tmp_subsystem_part + " is not build") print(_target_name) if (defined(invoker.certificate_profile)) { print(invoker.certificate_profile) } if (defined(invoker.ets2abc)) { print(invoker.ets2abc) } if (defined(invoker.deps)) { print(invoker.deps) } if (defined(invoker.hap_profile)) { print(invoker.hap_profile) } if (defined(invoker.testonly)) { print(invoker.testonly) } } _deps = [ ":module_${target_name}" ] _archive_testfile = "$root_out_dir/tests/$_test_type/${invoker.module_out_path}/${_archive_filename}" _arguments = [] _arguments = [ "--build_target_name", target_name, "--project_path", rebase_path("."), "--archive_testfile", rebase_path("${_archive_testfile}"), "--final_hap_path", rebase_path("${_final_hap_path}"), "--test_type", _test_type, "--module_out_path", invoker.module_out_path, ] if (_subsystem_name != "") { _arguments += [ "--subsystem_name", _subsystem_name, ] } if (_part_name != "") { _arguments += [ "--part_name", _part_name, ] } action(target_name) { deps = _deps script = rebase_path("//build/ohos/testfwk/test_js_stage_file_copy.py") args = _arguments outputs = [ _archive_testfile ] testonly = true } } template("ohos_js_stage_unittest") { ohos_js_stage_test(target_name) { forward_variables_from(invoker, "*") test_type = "unittest" } } template("ohos_rust_unittest") { _target_name = target_name _rustflags = [] _public_deps = [ "//build/rust/tests:original_libstd.so" ] ohos_unittest("$_target_name") { forward_variables_from(invoker, "*") public_deps = _public_deps if (!defined(invoker.crate_name)) { crate_name = _target_name } crate_type = "bin" if (defined(invoker.crate_type)) { assert(invoker.crate_type == crate_type, "crate_type should be $crate_type or use default value.") } if (defined(invoker.rustc_lints)) { rustc_lints = invoker.rustc_lints } if (defined(invoker.clippy_lints)) { clippy_lints = invoker.clippy_lints } if (!defined(rustc_lints) && !defined(clippy_lints)) { file_path = get_path_info(get_path_info(invoker.sources, "dir"), "abspath") file_path_split = string_split(file_path[0], "/") source_dir_begin = file_path_split[2] if (source_dir_begin == "openharmony") { _rustflags += allowAllLints } else if (source_dir_begin == "prebuilts") { _rustflags += allowAllLints } else if (source_dir_begin == "vendor") { _rustflags += rustcVendorLints _rustflags += clippyVendorLints } else if (source_dir_begin == "device") { _rustflags += rustcVendorLints _rustflags += clippyVendorLints } else { _rustflags += rustcOhosLints _rustflags += clippyOhosLints } } if (defined(rustc_lints)) { if (invoker.rustc_lints == "openharmony") { _rustflags += rustcOhosLints } else if (rustc_lints == "vendor") { _rustflags += rustcVendorLints } else if (rustc_lints == "none") { _rustflags += allowAllLints } } if (defined(clippy_lints)) { if (invoker.clippy_lints == "openharmony") { _rustflags += clippyOhosLints } else if (clippy_lints == "vendor") { _rustflags += clippyVendorLints } else if (clippy_lints == "none") { _rustflags += allowAllLints } } if (!defined(rustflags)) { rustflags = _rustflags } else { rustflags += _rustflags } edition = rust_default_edition if (defined(invoker.edition)) { edition = invoker.edition } rustflags += [ "--cfg", "feature=\"test\"", "--test", "-Csymbol-mangling-version=v0", "--edition=${edition}", ] } } template("ohos_rust_systemtest") { _target_name = target_name _rustflags = [] _public_deps = [ "//build/rust/tests:original_libstd.so" ] ohos_systemtest("$_target_name") { forward_variables_from(invoker, "*") public_deps = _public_deps if (!defined(invoker.crate_name)) { crate_name = _target_name } crate_type = "bin" if (defined(invoker.crate_type)) { assert(invoker.crate_type == crate_type, "crate_type should be $crate_type or use default value.") } if (defined(invoker.rustc_lints)) { rustc_lints = invoker.rustc_lints } if (defined(invoker.clippy_lints)) { clippy_lints = invoker.clippy_lints } if (!defined(rustc_lints) && !defined(clippy_lints)) { file_path = get_path_info(get_path_info(invoker.sources, "dir"), "abspath") file_path_split = string_split(file_path[0], "/") source_dir_begin = file_path_split[2] if (source_dir_begin == "openharmony") { _rustflags += allowAllLints } else if (source_dir_begin == "prebuilts") { _rustflags += allowAllLints } else if (source_dir_begin == "vendor") { _rustflags += rustcVendorLints _rustflags += clippyVendorLints } else if (source_dir_begin == "device") { _rustflags += rustcVendorLints _rustflags += clippyVendorLints } else { _rustflags += rustcOhosLints _rustflags += clippyOhosLints } } if (defined(rustc_lints)) { if (invoker.rustc_lints == "openharmony") { _rustflags += rustcOhosLints } else if (rustc_lints == "vendor") { _rustflags += rustcVendorLints } else if (rustc_lints == "none") { _rustflags += allowAllLints } } if (defined(clippy_lints)) { if (invoker.clippy_lints == "openharmony") { _rustflags += clippyOhosLints } else if (clippy_lints == "vendor") { _rustflags += clippyVendorLints } else if (clippy_lints == "none") { _rustflags += allowAllLints } } if (!defined(rustflags)) { rustflags = _rustflags } else { rustflags += _rustflags } edition = rust_default_edition if (defined(invoker.edition)) { edition = invoker.edition } rustflags += [ "--cfg", "feature=\"test\"", "--test", "-Csymbol-mangling-version=v0", "--edition=${edition}", ] } }