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

Code before:
function maybe_run() {
  if ${DRY_RUN}; then
    log_warning -e "# DRY_RUN:\\n  % ${*}"
  else
    run "${@}"
  fi
}

# modules
# returns the list of all modules in the project, not including the tools,
# as they are not considered to be added to the bill for materials.
function modules() {
  modules=(
    "${ROOT_MODULE}/api/v3"
    "${ROOT_MODULE}/pkg/v3"
    "${ROOT_MODULE}/client/pkg/v3"
    "${ROOT_MODULE}/client/v3"
    "${ROOT_MODULE}/server/v3"
    "${ROOT_MODULE}/etcdutl/v3"
    "${ROOT_MODULE}/etcdctl/v3"
    "${ROOT_MODULE}/tests/v3"
    "${ROOT_MODULE}/v3")
  echo "${modules[@]}"
}

# Receives a variable name to populate with workspace relative modules.
# Uses eval for bash 3.2 compatibility (macOS default) instead of nameref.
function load_workspace_relative_modules() {
  local varname=$1
  local _temp_modules=()
  while IFS= read -r line; do _temp_modules+=("$line"); done < <(
    go work edit -json | jq -r '.Use[].DiskPath + "/..."'
  )
  eval "${varname}=(\"\${_temp_modules[@]}\")"
}

# Receives a variable name to populate with workspace relative modules, not
# including the tools, as they are not considered to be added to the bill for materials.
# Uses eval for bash 3.2 compatibility (macOS default) instead of nameref.
function load_workspace_relative_modules_for_bom() {
  local varname=$1
  local modules=()
  load_workspace_relative_modules modules
  local _temp_bom_modules=()
  for module in "${modules[@]}"; do
    if [[ ! "${module}" =~ ^./tools ]]; then
      _temp_bom_modules+=("${module}")
    fi
  done
  eval "${varname}=(\"\${_temp_bom_modules[@]}\")"
}

Review comment (bug):
@ahrtr Do we aim to support MacOS bash 3.2? 

As I look into supporting it, there seems to be more to change needed as we are using features that is available in 4.x onwards (please see https://github.com/etcd-io/etcd/pull/21067)

I am not an expert of bash, so I might be wrong here. Just clarifying that we are indeed supporting vanilla bash on MacOS moving forward!

Thanks

Code after:
function maybe_run() {
  if ${DRY_RUN}; then
    log_warning -e "# DRY_RUN:\\n  % ${*}"
  else
    run "${@}"
  fi
}

# modules
# returns the list of all modules in the project, not including the tools,
# as they are not considered to be added to the bill for materials.
function modules() {
  modules=(
    "${ROOT_MODULE}/api/v3"
    "${ROOT_MODULE}/pkg/v3"
    "${ROOT_MODULE}/client/pkg/v3"
    "${ROOT_MODULE}/client/v3"
    "${ROOT_MODULE}/server/v3"
    "${ROOT_MODULE}/etcdutl/v3"
    "${ROOT_MODULE}/etcdctl/v3"
    "${ROOT_MODULE}/tests/v3"
    "${ROOT_MODULE}/v3")
  echo "${modules[@]}"
}

# Receives a reference to an array variable, and returns the workspace relative modules.
function load_workspace_relative_modules() {
  local -n _relative_modules=$1
  while IFS= read -r line; do _relative_modules+=("$line"); done < <(
    go work edit -json | jq -r '.Use[].DiskPath + "/..."'
  )
}

# Receives a reference to an array variable, and returns the workspace relative modules, not
# including the tools, as they are not considered to be added to the bill for materials.
function load_workspace_relative_modules_for_bom() {
  local -n relative_modules_for_bom=$1
  local modules=()
  load_workspace_relative_modules modules
  for module in "${modules[@]}"; do
    if [[ ! "${module}" =~ ^./tools ]]; then
      relative_modules_for_bom+=("${module}")
    fi
  done
}

#  run_for_all_workspace_modules [cmd]
#  run given command across all workspace modules
#  (unless the set is limited using ${PKG} or / ${USERMOD})
function run_for_all_workspace_modules {
  local pkg="${PKG:-./...}"