chromium/build/config/ios/ios_test_runner_wrapper.gni

# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/coverage/coverage.gni")
import("//build/config/ios/ios_sdk.gni")
import("//build/util/generate_wrapper.gni")

# Invokes generate_wrapper to create an executable script wrapping iOS'
# run.py with baked in arguments. Only takes effect when test entry in
# gn_isolate_map.pyl is updated to type="generated_script" with script
# set to the wrapper output path.
#
# Arguments:
#
# clones
#   (optional) number of ios simulator clones to execute tests on
#   in parallel
#
# data
#   (optional, default [ "//ios/build/bots/scripts/" ]) list of files or
#   directories required to run target
#
# data_deps
#   (optional) list of target non-linked labels
#
# deps
#   (optional) list of files or directories required to run target
#
# executable_args
#   (optional) a list of string arguments to pass to run.py
#
# retries
#   (optional, default 3) number of retry attempts
#
# shards
#   (optional) number of ios simulator clones to execute tests in parallel. not
#   the same as swarming shards. Only used if clones is not provided. Will be
#   deprecated.
#
# wrapper_output_name
#   (optional, default "run_${target_name}") name of the wrapper script
#
template("ios_test_runner_wrapper") {
  generate_wrapper(target_name) {
    forward_variables_from(invoker,
                           [
                             "deps",
                             "retries",
                             "shards",
                             "wrapper_output_name",
                           ])
    testonly = true
    executable = "//testing/test_env.py"

    # iOS main test runner
    _runner_path =
        rebase_path("//ios/build/bots/scripts/run.py", root_build_dir)

    executable_args = [ "@WrappedPath(${_runner_path})" ]

    # arguments passed to run.py
    if (defined(invoker.executable_args)) {
      executable_args += invoker.executable_args
    }

    _rebased_mac_toolchain = rebase_path("//mac_toolchain", root_build_dir)
    _rebased_xcode_path = rebase_path("//Xcode.app", root_build_dir)
    _rebased_ios_runtime_cache_prefix =
        rebase_path("//Runtime-ios-", root_build_dir)

    # --out-dir argument is specified in gn_isolate_map.pyl because
    # ${ISOLATED_OUTDIR} doesn't get resolved through this wrapper.
    executable_args += [
      "--xcode-path",
      "@WrappedPath(${_rebased_xcode_path})",
      "--mac-toolchain-cmd",
      "@WrappedPath(${_rebased_mac_toolchain})",
      "--runtime-cache-prefix",
      "@WrappedPath(${_rebased_ios_runtime_cache_prefix})",
    ]

    # Default retries to 3
    if (!defined(retries)) {
      retries = 3
    }
    executable_args += [
      "--retries",
      "${retries}",
    ]

    # Clones is not required by test runner so only pass if defined.
    if (defined(clones)) {
      executable_args += [
        "--clones",
        "${clones}",
      ]
    }

    if (xcode_version_int >= 1400) {
      executable_args += [
        "--readline-timeout",
        "600",
      ]
    }

    data_deps = [ "//testing:test_scripts_shared" ]
    if (defined(invoker.data_deps)) {
      data_deps += invoker.data_deps
    }

    # test runner relies on iossim for simulator builds.
    if (target_environment == "simulator") {
      _rebased_root_build_dir = rebase_path("${root_build_dir}", root_build_dir)
      data_deps += [ "//testing/iossim" ]

      executable_args += [
        "--iossim",
        "@WrappedPath(${_rebased_root_build_dir}/iossim)",
      ]
    }

    if (use_clang_coverage) {
      executable_args += [ "--use-clang-coverage" ]
    }

    if (!is_debug) {
      executable_args += [ "--release" ]
    }

    # wrapper script output name and path
    if (!defined(wrapper_output_name)) {
      _wrapper_output_name = "run_${target_name}"
    } else {
      _wrapper_output_name = wrapper_output_name
    }

    wrapper_script = "${root_build_dir}/bin/${_wrapper_output_name}"

    data = []
    if (defined(invoker.data)) {
      data += invoker.data
    }
    data += [
      "//ios/build/bots/scripts/",
      "//ios/build/bots/scripts/plugin",

      # gRPC interface for iOS test plugin
      "//ios/testing/plugin",

      # exception reporting protos
      "//build/util/lib/proto",

      # Variations test utilities used by variations_runner script.
      "//testing/scripts/variations_seed_access_helper.py",
      "//testing/test_env.py",
    ]
  }
}