common.sh
This file
Path
test/common.sh
306 lines · 6.9 KiB · Shell
Last changed by
[Fix] use `command` to bypass `curl`/`wget` shell functions and aliasesJordan Harbandbefore adoption04fef13
History of this fileTake it
The walk stopped after 200 commits: lines older than 5c12680 point at it.
[Tests] add `try`/`try_err` helpers; convert tests to use themJordan Harbandbefore adoption14d01c6
lines 1–26why, one step back1# Runs a command once and captures stdout and exit code.2# Suppresses xtrace in the subshell. Discards stderr.3#4# Sets: CAPTURED_STDOUT, CAPTURED_EXIT_CODE5#6# Usage:7# try nvm_version current8# [ "$CAPTURED_STDOUT" = "v20.0.0" ] || die "wrong output"9# [ "$CAPTURED_EXIT_CODE" = 0 ] || die "wrong exit code"10try() {11 CAPTURED_STDOUT="$(set +x; "$@" 2>/dev/null)" && CAPTURED_EXIT_CODE=0 || CAPTURED_EXIT_CODE=$?12}1314# Runs a command once and captures stderr and exit code.15# Suppresses xtrace in the subshell. Discards stdout.16#17# Sets: CAPTURED_STDERR, CAPTURED_EXIT_CODE18#19# Usage:20# try_err nvm_alias21# [ "$CAPTURED_STDERR" = "An alias is required." ] || die "wrong error"22# [ "$CAPTURED_EXIT_CODE" = 1 ] || die "wrong exit code"23try_err() {24 CAPTURED_STDERR="$(set +x; "$@" 2>&1 >/dev/null)" && CAPTURED_EXIT_CODE=0 || CAPTURED_EXIT_CODE=$?25}26[Dev Deps] update `markdown-link-check`Jordan Harbandbefore adoption5c12680
lines 27–30why, one step back27assert_ok() {28 local FUNCTION=$129 shift30[tests] remove double-substitution in assert_ok and assert_not_okАндрій Шовкошитнийbefore adoption20d52b6
line 31why, one step back31 "$FUNCTION" "$@" || die '"'"$FUNCTION $@"'" should have succeeded, but failed'[Dev Deps] update `markdown-link-check`Jordan Harbandbefore adoption5c12680
lines 32–37why, one step back32}3334assert_not_ok() {35 local FUNCTION=$136 shift37[tests] remove double-substitution in assert_ok and assert_not_okАндрій Шовкошитнийbefore adoption20d52b6
line 38why, one step back38 ! "$FUNCTION" "$@" || die '"'"$FUNCTION $@"'" should have failed, but succeeded'[Dev Deps] update `markdown-link-check`Jordan Harbandbefore adoption5c12680
lines 39–48why, one step back39}4041strip_colors() {42 while read -r line; do43 echo "$line" | LC_ALL=C command sed 's/\[[ -?]*[@-~]//g'44 done45}4647make_echo() {48 echo "#!/bin/sh" > "$1"[tests] remove double-substitution in assert_ok and assert_not_okАндрій Шовкошитнийbefore adoption20d52b6
line 49why, one step back49 echo "echo \"${2}\"" >> "$1"[Dev Deps] update `markdown-link-check`Jordan Harbandbefore adoption5c12680
lines 50–52why, one step back50 chmod a+x "$1"51}52[Fix] use `command` to bypass `curl`/`wget` shell functions and aliasesJordan Harbandbefore adoption04fef13
lines 53–85why, one step back53# `command curl` bypasses shell functions and aliases, so tests that mock curl54# must provide an actual executable on the PATH: this creates one in the given55# directory. Invoked as `curl -V`, it prints ${VERSION_MESSAGE} (exported here56# on the mock's behalf); any other invocation runs the body given as the57# second argument, or fails.58make_fake_curl() {59 local FAKE_BIN_DIR60 FAKE_BIN_DIR="${1-}"61 [ -n "${FAKE_BIN_DIR}" ] || return 16263 local FAKE_CURL_BODY64 FAKE_CURL_BODY="${2-}"65 if [ -z "${FAKE_CURL_BODY}" ]; then66 FAKE_CURL_BODY='echo >&2 "This fake curl only takes one parameter, -V"67exit 1'68 fi6970 mkdir -p "${FAKE_BIN_DIR}" || return 27172 {73 echo '#!/bin/sh'74 echo 'if [ "$#" -eq 1 ] && [ "$1" = "-V" ]; then'75 echo ' echo "${VERSION_MESSAGE}"'76 echo ' exit 0'77 echo 'fi'78 printf '%s\n' "${FAKE_CURL_BODY}"79 } > "${FAKE_BIN_DIR}/curl"80 chmod +x "${FAKE_BIN_DIR}/curl"8182 # the fake curl reads VERSION_MESSAGE from the environment83 export VERSION_MESSAGE84}85[Dev Deps] update `markdown-link-check`Jordan Harbandbefore adoption5c12680
lines 86–306why, one step back86make_fake_node() {87 local VERSION88 VERSION="${1-}"89 [ -n "${VERSION}" ] || return 19091 local FORMATTED_VERSION92 FORMATTED_VERSION="$(nvm_format_version "${VERSION}")"9394 local BIN_PATH95 BIN_PATH="$(nvm_version_path "${FORMATTED_VERSION}")/bin"96 [ "${BIN_PATH}" != "/bin" ] || {97 echo >&2 'nvm_version_path was empty'98 return 599 }100101 mkdir -p "${BIN_PATH}" || {102 echo >&2 'unable to make bin dir'103 return 2104 }105106 make_echo "${BIN_PATH}/node" "${VERSION}" || {107 echo >&2 'unable to make fake node bin'108 return 3109 }110111 nvm_is_version_installed "${FORMATTED_VERSION}" || {112 echo >&2 'fake node is not installed'113 return 4114 }115}116117make_fake_iojs() {118 local VERSION119 VERSION="${1-}"120 [ -n "${VERSION}" ] || return 1121122 local FORMATTED_VERSION123 FORMATTED_VERSION="$(nvm_format_version "iojs-${VERSION}")"124125 local BIN_PATH126 BIN_PATH="$(nvm_version_path "${FORMATTED_VERSION}")/bin"127 [ "${BIN_PATH}" != "/bin" ] || {128 echo >&2 'nvm_version_path was empty'129 return 5130 }131132 mkdir -p "${BIN_PATH}" || {133 echo >&2 'unable to make bin dir'134 return 2135 }136137 make_echo "${BIN_PATH}/node" "${VERSION}" || {138 echo >&2 'unable to make fake node bin'139 return 3140 }141 make_echo "${BIN_PATH}/iojs" "${VERSION}" || {142 echo >&2 'unable to make fake iojs bin'143 return 3144 }145146 nvm_is_version_installed "${FORMATTED_VERSION}" || {147 echo >&2 'fake iojs is not installed'148 return 4149 }150}151152watch() {153 $@ &154 local JOB155 JOB=$!156 while true; do sleep 15; >&2 echo '* ping *'; done &157 wait $JOB;158 local EXIT_CODE159 EXIT_CODE=$?160 kill %2;161 return $EXIT_CODE162}163164165# JSON parsing from https://gist.github.com/assaf/ee377a186371e2e269a7166nvm_json_throw() {167 nvm_err "$*"168 exit 1169}170171nvm_json_awk_egrep() {172 local pattern_string173 pattern_string="${1}"174175 awk '{176 while ($0) {177 start=match($0, pattern);178 token=substr($0, start, RLENGTH);179 print token;180 $0=substr($0, start+RLENGTH);181 }182 }' "pattern=${pattern_string}"183}184185nvm_json_tokenize() {186 local GREP187 GREP='grep -Eao'188189 local ESCAPE190 local CHAR191192 # if echo "test string" | grep -Eo "test" > /dev/null 2>&1; then193 # ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'194 # CHAR='[^[:cntrl:]"\\]'195 # else196 GREP=nvm_json_awk_egrep197 ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'198 CHAR='[^[:cntrl:]"\\\\]'199 # fi200201 local STRING202 STRING="\"${CHAR}*(${ESCAPE}${CHAR}*)*\""203 local NUMBER204 NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'205 local KEYWORD206 KEYWORD='null|false|true'207 local SPACE208 SPACE='[[:space:]]+'209210 $GREP "${STRING}|${NUMBER}|${KEYWORD}|${SPACE}|." | TERM=dumb grep -Ev "^${SPACE}$"211}212213_json_parse_array() {214 local index=0215 local ary=''216 read -r token217 case "$token" in218 ']') ;;219 *)220 while :; do221 _json_parse_value "${1}" "${index}"222 index=$((index+1))223 ary="${ary}${value}"224 read -r token225 case "${token}" in226 ']') break ;;227 ',') ary="${ary}," ;;228 *) nvm_json_throw "EXPECTED , or ] GOT ${token:-EOF}" ;;229 esac230 read -r token231 done232 ;;233 esac234 :235}236237_json_parse_object() {238 local key239 local obj=''240 read -r token241 case "$token" in242 '}') ;;243 *)244 while :; do245 case "${token}" in246 '"'*'"') key="${token}" ;;247 *) nvm_json_throw "EXPECTED string GOT ${token:-EOF}" ;;248 esac249 read -r token250 case "${token}" in251 ':') ;;252 *) nvm_json_throw "EXPECTED : GOT ${token:-EOF}" ;;253 esac254 read -r token255 _json_parse_value "${1}" "${key}"256 obj="${obj}${key}:${value}"257 read -r token258 case "${token}" in259 '}') break ;;260 ',') obj="${obj}," ;;261 *) nvm_json_throw "EXPECTED , or } GOT ${token:-EOF}" ;;262 esac263 read -r token264 done265 ;;266 esac267 :268}269270_json_parse_value() {271 local jpath="${1:+$1,}$2"272 local isleaf=0273 local isempty=0274 local print=0275276 case "$token" in277 '{') _json_parse_object "${jpath}" ;;278 '[') _json_parse_array "${jpath}" ;;279 # At this point, the only valid single-character tokens are digits.280 ''|[!0-9]) nvm_json_throw "EXPECTED value GOT >${token:-EOF}<" ;;281 *)282 value=$token283 isleaf=1284 [ "${value}" = '""' ] && isempty=1285 ;;286 esac287288 [ "${value}" = '' ] && return289 [ "${isleaf}" -eq 1 ] && [ $isempty -eq 0 ] && print=1290 [ "${print}" -eq 1 ] && printf "[%s]\t%s\n" "${jpath}" "${value}"291 :292}293294_json_parse() {295 read -r token296 _json_parse_value297 read -r token298 case "${token}" in299 '') ;;300 *) nvm_json_throw "EXPECTED EOF GOT >${token}<" ;;301 esac302}303304nvm_json_extract() {305 nvm_json_tokenize | _json_parse | grep -e "${1}" | awk '{print $2 $3}'306}