Repository: etcd-io/etcd
File: scripts/test_lib.sh (Shell)

Code before:
      args+=("${arg}")
    fi
  done

  # If JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
  local junit_report_dir=${JUNIT_REPORT_DIR:-${ARTIFACTS:-}}

  local go_test_grep_pattern=".*"
  if [[ -n "${junit_report_dir}" ]]; then
    # Show only summary lines by matching lines like "status package/test"
    go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
  fi

  if [[ -n "${junit_report_dir}" || "${VERBOSE:-}" == "1" ]]; then
    go_test_flags+=("-v" "-json")
  fi

  local cmd=(go test "${go_test_flags[@]}" "$@")

  local junit_filename_prefix
  junit_filename_prefix=$(get_junit_filename_prefix "${junit_report_dir}")

  if ! run env ETCD_VERIFY="${ETCD_VERIFY}" "${cmd[@]}" | tee ${junit_filename_prefix:+"${junit_filename_prefix}.stdout"} | grep --binary-files=text "${go_test_grep_pattern}" ; then
    produce_junit_xmlreport "${junit_filename_prefix}"
    return 2
  fi

  produce_junit_xmlreport "${junit_filename_prefix}"
}

#### Other ####

# tool_exists [tool] [instruction]
# Checks whether given [tool] is installed. In case of failure,
# prints a warning with installation [instruction] and returns !=0 code.
#
# WARNING: This depend on "any" version of the 'binary' that might be tricky
# from hermetic build perspective. For go binaries prefer 'tool_go_run'
function tool_exists {
  local tool="${1}"
  local instruction="${2}"
  if ! command -v "${tool}" >/dev/null; then
    log_warning "Tool: '${tool}' not found on PATH. ${instruction}"
    return 255
  fi
}

# tool_get_bin [tool] - returns absolute path to a tool binary (or returns error).
# This function is only used to run commands that are managed by tools/mod.
function tool_get_bin {
  local tool="$1"

Review comment (refactor):
Not sure why remove this for loop? Previously it may run test against a list of package, now you remove it? If you remove the for loop, do we still need to the variable packages (see line 389 or 385),

https://github.com/etcd-io/etcd/blob/30f9ac5264dfe0f647c382ebfd41c4c5cff5629a/scripts/test_lib.sh#L389


Code after:
    go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
  fi

  if [[ -n "${junit_report_dir}" || "${VERBOSE:-}" == "1" ]]; then
    go_test_flags+=("-v" "-json")
  fi

  local cmd=(go test "${go_test_flags[@]}" "$@")

  local junit_filename_prefix
  junit_filename_prefix=$(get_junit_filename_prefix "${junit_report_dir}")

  if ! run env ETCD_VERIFY="${ETCD_VERIFY}" "${cmd[@]}" | tee ${junit_filename_prefix:+"${junit_filename_prefix}.stdout"} | grep --binary-files=text "${go_test_grep_pattern}" ; then
    produce_junit_xmlreport "${junit_filename_prefix}"
    return 2
  fi

  produce_junit_xmlreport "${junit_filename_prefix}"
}

#### Other ####

# tool_exists [tool] [instruction]
# Checks whether given [tool] is installed. In case of failure,
# prints a warning with installation [instruction] and returns !=0 code.
#
# WARNING: This depend on "any" version of the 'binary' that might be tricky
# from hermetic build perspective. For go binaries prefer 'tool_go_run'
function tool_exists {
  local tool="${1}"
  local instruction="${2}"
  if ! command -v "${tool}" >/dev/null; then
    log_warning "Tool: '${tool}' not found on PATH. ${instruction}"
    return 255
  fi
}

# tool_get_bin [tool] - returns absolute path to a tool binary (or returns error).
# This function is only used to run commands that are managed by tools/mod.
function tool_get_bin {
  local tool="$1"
  local pkg_part="$1"
  if [[ "$tool" == *"@"* ]]; then
    pkg_part=$(echo "${tool}" | cut -d'@' -f1)
    # shellcheck disable=SC2086
    run go install ${GOBINARGS:-} "${tool}" || return 2
  else
    # shellcheck disable=SC2086
    run_for_module ./tools/mod run go install ${GOBINARGS:-} "${tool}" || return 2
  fi