# 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" ]
  }

  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) {
    testonly = true
    forward_variables_from(invoker, "*")
    test_type = "unittest"
    deps = []
    external_deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (defined(invoker.external_deps)) {
      external_deps += invoker.external_deps
    }

    # Add static link library judgment logic below

    if (defined(invoker.rtti_compile_flag) && invoker.rtti_compile_flag) {
      external_deps += [ "googletest:gtest_rtti_main" ]
    } else {
      external_deps += [
        "googletest:gtest_main",
        "googletest:gtest",
      ]
    }
  }
}

template("ohos_moduletest") {
  _ohos_test(target_name) {
    testonly = true
    forward_variables_from(invoker, "*")
    test_type = "moduletest"
    deps = []
    external_deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (defined(invoker.external_deps)) {
      external_deps += invoker.external_deps
    }
    external_deps += [ "googletest:gtest_main" ]
  }
}

template("ohos_systemtest") {
  _ohos_test(target_name) {
    forward_variables_from(invoker, "*")
    test_type = "systemtest"
  }
}

template("ohos_performancetest") {
  _ohos_test(target_name) {
    testonly = true
    forward_variables_from(invoker, "*")
    test_type = "performance"
    deps = []
    external_deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    deps +=
        [ "//test/testfwk/developer_test/aw/cxx/hwext:performance_test_static" ]
    if (defined(invoker.external_deps)) {
      external_deps += invoker.external_deps
    }
    external_deps += [ "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) {
    testonly = true
    forward_variables_from(invoker, "*", [ "deps" ])
    test_type = "benchmark"

    deps = []
    external_deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (defined(invoker.external_deps)) {
      external_deps += invoker.external_deps
    }
    external_deps += [ "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"

  test_type = invoker.test_type

  write_file("$_test_output_dir/${target_name}_path.txt",
             get_label_info(":$target_name", "dir"),
             "string")

  _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

  if (defined(invoker.module_out_path)) {
    _test_output_dir =
        "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
  } else {
    _test_output_dir = "$root_out_dir/tests/${invoker.test_type}"
  }
  write_file("$_test_output_dir/${target_name}_path.txt",
             get_label_info(":$target_name", "dir"),
             "string")

  # 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,
    ]

    _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 = []
  _deps = [ "//build/rust/tests:original_libstd.so" ]
  ohos_unittest("$_target_name") {
    forward_variables_from(invoker, "*")
    deps += _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 = []
  _deps = [ "//build/rust/tests:original_libstd.so" ]
  ohos_systemtest("$_target_name") {
    forward_variables_from(invoker, "*")
    deps += _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}",
    ]
  }
}

#arkts1.2 api test template
ohos_ets_build_system_deps = "ets_frontend:ohos_ets_build_system"
if (path_exists("//developtools/ace_ets2bundle")) {
    ohos_ets_ui_plugins_deps = "ace_ets2bundle:ohos_ets_ui_plugins"
}

build_sdk_path = "$root_build_dir/ohos_ets"

ohos_ets_kits_deps = "sdk:ohos_ets_kits"
ohos_ets_api_deps = "sdk:ohos_ets_api"
ohos_ets_arkts_deps = "sdk:ohos_ets_arkts"

ohos_ets_api_path = "$root_build_dir/ohos_ets/api"
ohos_ets_arkts_path = "$root_build_dir/ohos_ets/arkts"
ohos_ets_kits_path = "$root_build_dir/ohos_ets/kits"

node_path = "//prebuilts/build-tools/common/nodejs/node-v18.20.1-linux-x64/bin"

template("ohos_arkts_unittest") {
  forward_variables_from(invoker, [
    "module_out_path",
    "test_files",
    "part_name",
    "subsystem_name",
    "sources",
    "external_deps",
  ])
  
  assert(defined(module_out_path), "module_out_path must be defined.")
  assert(defined(test_files), "test_files must be defined.")
  assert(defined(part_name), "part_name must be defined.")
  assert(defined(subsystem_name), "subsystem_name must be defined.")
  assert(defined(sources), "sources must be defined.")

  _deps = []
  if (defined(invoker.deps)) {
    _deps += invoker.deps
  }
  _external_deps = []
  if (defined(external_deps)) {
    _external_deps += external_deps
  }
  _arktsconfig = "arkcompiler/runtime_core/static_core/out/bin/arktsconfig.json"
  if (defined(invoker.arktsconfig)) {
    _arktsconfig = invoker.arktsconfig
  }
  
  _hap_name = target_name

  hypium_output_dir = "$root_out_dir/tests/arktstdd/hypium"

  test_output_dir = "$root_out_dir/tests/arktstdd/$module_out_path"

  _current_gn_dir = get_label_info(":$target_name", "dir")
  _target_path = "${_current_gn_dir}"

  _test_files_str = ""
  foreach(test_file, test_files) {
    _test_files_str = _test_files_str + "," + test_file
  }
  
  _sources_str = ""
  foreach(src, sources) {
    _sources_str = _sources_str + "," + src
  }

  action(target_name) {
    script = rebase_path("//build/ohos/testfwk/arkts_tdd_cases_build.py")
    outputs = [ "$target_out_dir/$target_name.out" ]
    ui_enable = "False"
    if (defined(invoker.ui_enable)) {
      ui_enable = invoker.ui_enable
    }

    args = [
      "--ui_enable",
      ui_enable,
    ]

    if (ui_enable == "True") {
      _arktsconfig = "$target_out_dir/arktsconfig_$target_name.json"
      assert(defined(invoker.base_url), "base_url is required!")
      _external_deps += [
        ohos_ets_api_deps,
        ohos_ets_arkts_deps,
        ohos_ets_build_system_deps,
        ohos_ets_kits_deps,
        ohos_ets_ui_plugins_deps,
      ]

      final_base_url = invoker.base_url
      if (final_base_url == "./arkui-preprocessed") {
         final_base_url = "//foundation/arkui/ace_engine/frameworks/bridge/arkts_frontend/koala_projects/arkoala-arkts/arkui-preprocessed"
      }

      args += [
        "--base_url",
        rebase_path("${final_base_url}"),
        "--build_sdk_path",
        rebase_path("$build_sdk_path"),
        "--memo_plugin",
        rebase_path(
            "$build_sdk_path/build-tools/ui-plugins/lib/memo-plugins/index.js"),
        "--entry_path",
        rebase_path(
            "$build_sdk_path/build-tools/driver/build-system/dist/entry.js"),
        "--env_path",
        rebase_path("$build_sdk_path/build-tools/ets2panda/lib"),
        "--node_path",
        rebase_path("$node_path"),
        "--scan_path",
        rebase_path("$ohos_ets_api_path"),
        rebase_path("$ohos_ets_arkts_path"),
        rebase_path("$ohos_ets_kits_path"),
      ]

      base_path = "//foundation/arkui/ace_engine/frameworks/bridge/arkts_frontend/koala_projects/arkoala-arkts/"
      if (defined(invoker.files)) {
        file_paths = []
        foreach(path, invoker.files) {
          if (invoker.base_url == "./arkui-preprocessed") {
          file_paths += [ rebase_path(base_path + path) ]
          } else {
          file_paths += [ rebase_path(path) ]
          }
        }
        file_list_file = "${target_out_dir}/${target_name}_file_list.txt"
        write_file(file_list_file, file_paths)
        args += [ "--files", rebase_path(file_list_file) ]
      }

      if (defined(invoker.package)) {
        args += [
          "--package",
          invoker.package,
        ]
      }
      if (defined(invoker.paths_keys) && defined(invoker.paths_values)) {
        args += [ "--paths_keys" ] + invoker.paths_keys
        paths_values = []
        foreach(path, invoker.paths_values) {
          if (invoker.base_url == "./arkui-preprocessed") {
            paths_values += [ rebase_path(base_path + path) ]
          } else {
             paths_values += [ rebase_path(path) ]
          }
        }
        args += [ "--paths_values" ] + paths_values
      }
    }

    cache_path = "$target_out_dir/$target_name/cache"
    if (defined(invoker.cache_path)) {
      cache_path = invoker.cache_path
    }
    args += [
      "--cache_path",
      rebase_path(cache_path),
    ]

    build_out_path = test_output_dir
    dst_file = "$build_out_path/${target_name}.abc"
    args += [
      "--dst_file",
      rebase_path(dst_file),
    ]

    args += [
      "--test_files",
      _test_files_str,
      "--target_path",
      rebase_path(_target_path, root_build_dir),
      "--part_name",
      part_name,
      "--subsystem_name",
      subsystem_name,
      "--hap_name",
      _hap_name,
      "--output_dir",
      rebase_path(test_output_dir, root_build_dir),
      "--hypium_output_dir",
      rebase_path(hypium_output_dir, root_build_dir),
      "--sources",
      _sources_str,
      "--arktsconfig",
      rebase_path(_arktsconfig)
    ]
    deps = _deps
    external_deps = _external_deps
    testonly = true
  }
}