File: test_helper.bash

package info (click to toggle)
bats 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,016 kB
  • sloc: sh: 4,351; makefile: 33; python: 28; xml: 3
file content (80 lines) | stat: -rw-r--r-- 2,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
emulate_bats_env() {
  export BATS_CWD="$PWD"
  export BATS_ROOT_PID=$$
  export BATS_RUN_TMPDIR
  BATS_RUN_TMPDIR=$(mktemp -d "${BATS_RUN_TMPDIR}/emulated-tmpdir-${BATS_ROOT_PID}-XXXXXX")
  REENTRANT_RUN_PRESERVE+=(BATS_CWD BATS_ROOT_PID BATS_RUN_TMPDIR)
  export BATS_LINE_REFERENCE_FORMAT=comma_line
  export BATS_BEGIN_CODE_QUOTE='`'
  export BATS_END_CODE_QUOTE="'"
  export BATS_CODE_QUOTE_STYLE="\`'"
}

fixtures() {
  FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/$1"
  # shellcheck disable=SC2034
  RELATIVE_FIXTURE_ROOT="${FIXTURE_ROOT#"$BATS_CWD"/}"
  if [[ $BATS_ROOT == "$BATS_CWD" ]]; then
    RELATIVE_BATS_ROOT=''
  else
    RELATIVE_BATS_ROOT=${BATS_ROOT#"$BATS_CWD"/}
  fi
  if [[ -n "$RELATIVE_BATS_ROOT" && "$RELATIVE_BATS_ROOT" != */ ]]; then
    RELATIVE_BATS_ROOT+=/
  fi
}

filter_control_sequences() {
  local status=0
  "$@" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' || status=$?
  return "$status"
}

if ! command -v tput >/dev/null; then
  tput() {
    printf '1000\n'
  }
  export -f tput
fi

emit_debug_output() {
  # shellcheck disable=SC2154
  printf '%s\n' 'output:' "$output" >&2
}

execute_with_unset_bats_vars() { # <command to execute...>
  for var_to_delete in "${!BATS_@}"; do
    for var_to_exclude in "${REENTRANT_RUN_PRESERVE[@]-}"; do
      # is this var excluded -> skip unset
      if [[ $var_to_delete == "$var_to_exclude" ]]; then
        continue 2
      fi
    done
    unset "$var_to_delete"
  done
  "$@"
}

REENTRANT_RUN_PRESERVE+=(BATS_SAVED_PATH BATS_ROOT BATS_TEST_TAGS BATS_PARALLEL_BINARY_NAME BATS_LIBDIR BATS_killer_pid)

# call run with all BATS_* variables purged from the environment
reentrant_run() { # <same args as run>
  # take up all args to run except the command,
  # to avoid having to deal with empty arrays in Bash 3
  local -a pre_command_args=()

  # remove all flags to run to leave the command in $@
  while [[ $1 == -* || $1 == ! ]]; do
    pre_command_args+=("$1")
    if [[ "$1" == -- ]]; then
      shift
      break
    fi
    shift
  done

  # put that here to ensure Bash 3 won't have to deal with empty pre_command_args
  pre_command_args+=(execute_with_unset_bats_vars)

  run "${pre_command_args[@]}" "$@"
}