Skip to content
Myr

[Tests] use a better JSON parsing implementation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jordan Harband · committed by Jordan Harband · parent aa427ad · Browse files · 2 files+137 added−95 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+137 added−95 removed

  • tests:+137 added−95 removed

2 files+137 added−95 removed

modified:test/fast/Unit tests/nvm_process_nvmrc+2 added-2 removed
12 unmodified lines
13for f in ../../../test/fixtures/nvmrc/test/fixtures/valid/*; do14 STDOUT="$(nvm_process_nvmrc $f/.nvmrc 2>/dev/null)"15 EXIT_CODE="$(nvm_process_nvmrc $f/.nvmrc >/dev/null 2>/dev/null; echo $?)"1617 EXPECTED="$(extract_value node "$(parse_json "$(cat "$f/expected.json")")")"17 EXPECTED="$(nvm_json_extract node < "${f}/expected.json" | tr -d '"')"1819 [ "${EXIT_CODE}" = "0" ] || die "$(basename "${f}"): expected exit code of 0 but got ${EXIT_CODE}"2021 [ "${STDOUT}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDOUT of \`${EXPECTED}\` but got \`${STDOUT}\`"
3 unmodified lines
25 STDOUT="$(nvm_process_nvmrc $f/.nvmrc 2>/dev/null)"26 STDERR="$(nvm_process_nvmrc $f/.nvmrc 2>&1 >/dev/null | awk '{if(NR > 8) print $0}' | strip_colors)"27 EXIT_CODE="$(nvm_process_nvmrc $f/.nvmrc >/dev/null 2>/dev/null; echo $?)"2829 EXPECTED="$(parse_json "$(cat "$f/expected.json")" | sed 's/^[0-9]*="//;s/"$//')"29 EXPECTED="$(nvm_json_extract < "${f}/expected.json" | tr -d '"')"3031 [ "${EXIT_CODE}" != "0" ] || die "$(basename "${f}"): expected exit code of 'not 0' but got ${EXIT_CODE}"3233 [ "${STDERR}" = "${EXPECTED}" ] || die "$(basename "${f}"): expected STDERR of \`${EXPECTED}\` but got \`${STDERR}\`"
1 unmodified line
modified:test/common.sh+135 added-93 removed
100 unmodified lines
101 kill %2;102 return $EXIT_CODE103}104105parse_json() {106 local json107 json="$1"108 local key109 key=""110 local value111 value=""112 local output113 output=""114 local in_key115 in_key=0116 local in_value117 in_value=0118 local in_string119 in_string=0120 local escaped121 escaped=0122 local buffer123 buffer=""124 local char125 local len126 len=${#json}127 local arr_index128 arr_index=0129 local in_array130 in_array=0105132 for ((i = 0; i < len; i++)); do133 char="${json:i:1}"106# JSON parsing from https://gist.github.com/assaf/ee377a186371e2e269a7107nvm_json_throw() {108 nvm_err "$*"109 exit 1110}111135 if [ "$in_string" -eq 1 ]; then136 if [ "$escaped" -eq 1 ]; then137 buffer="$buffer$char"138 escaped=0139 elif [ "$char" = "\\" ]; then140 escaped=1141 elif [ "$char" = "\"" ]; then142 in_string=0143 if [ "$in_key" -eq 1 ]; then144 key="$buffer"145 buffer=""146 in_key=0147 elif [ "$in_value" -eq 1 ]; then148 value="$buffer"149 buffer=""150 output="$output$key=\"$value\"\n"151 in_value=0152 elif [ "$in_array" -eq 1 ]; then153 value="$buffer"154 buffer=""155 output="$output$arr_index=\"$value\"\n"156 arr_index=$((arr_index + 1))157 fi158 else159 buffer="$buffer$char"160 fi161 continue162 fi112nvm_json_awk_egrep() {113 local pattern_string114 pattern_string="${1}"115164 case "$char" in165 "\"")166 in_string=1167 buffer=""168 if [ "$in_value" -eq 0 ] && [ "$in_array" -eq 0 ]; then169 in_key=1170 fi171 ;;172 ":")173 in_value=1174 ;;175 ",")176 if [ "$in_value" -eq 1 ]; then177 in_value=0178 fi179 ;;180 "[")181 in_array=1182 ;;183 "]")184 in_array=0185 ;;186 "{" | "}")187 ;;188 *)189 if [ "$in_value" -eq 1 ] && [ "$char" != " " ] && [ "$char" != "\n" ] && [ "$char" != "\t" ]; then190 buffer="$buffer$char"191 fi192 ;;193 esac194 done116 awk '{117 while ($0) {118 start=match($0, pattern);119 token=substr($0, start, RLENGTH);120 print token;121 $0=substr($0, start+RLENGTH);122 }123 }' "pattern=${pattern_string}"124}125196 printf "%b" "$output"126nvm_json_tokenize() {127 local GREP128 GREP='grep -Eao'129130 local ESCAPE131 local CHAR132133 # if echo "test string" | grep -Eo "test" > /dev/null 2>&1; then134 # ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'135 # CHAR='[^[:cntrl:]"\\]'136 # else137 GREP=nvm_json_awk_egrep138 ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'139 CHAR='[^[:cntrl:]"\\\\]'140 # fi141142 local STRING143 STRING="\"${CHAR}*(${ESCAPE}${CHAR}*)*\""144 local NUMBER145 NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'146 local KEYWORD147 KEYWORD='null|false|true'148 local SPACE149 SPACE='[[:space:]]+'150151 $GREP "${STRING}|${NUMBER}|${KEYWORD}|${SPACE}|." | TERM=dumb grep -Ev "^${SPACE}$"152}153199extract_value() {154_json_parse_array() {155 local index=0156 local ary=''157 read -r token158 case "$token" in159 ']') ;;160 *)161 while :; do162 _json_parse_value "${1}" "${index}"163 index=$((index+1))164 ary="${ary}${value}"165 read -r token166 case "${token}" in167 ']') break ;;168 ',') ary="${ary}," ;;169 *) nvm_json_throw "EXPECTED , or ] GOT ${token:-EOF}" ;;170 esac171 read -r token172 done173 ;;174 esac175 :176}177178_json_parse_object() {179 local key201 key="$1"202 local parsed203 parsed="$2"204 echo "$parsed" | grep "^$key=" | cut -d'=' -f2 | tr -d '"'180 local obj=''181 read -r token182 case "$token" in183 '}') ;;184 *)185 while :; do186 case "${token}" in187 '"'*'"') key="${token}" ;;188 *) nvm_json_throw "EXPECTED string GOT ${token:-EOF}" ;;189 esac190 read -r token191 case "${token}" in192 ':') ;;193 *) nvm_json_throw "EXPECTED : GOT ${token:-EOF}" ;;194 esac195 read -r token196 _json_parse_value "${1}" "${key}"197 obj="${obj}${key}:${value}"198 read -r token199 case "${token}" in200 '}') break ;;201 ',') obj="${obj}," ;;202 *) nvm_json_throw "EXPECTED , or } GOT ${token:-EOF}" ;;203 esac204 read -r token205 done206 ;;207 esac208 :209}210211_json_parse_value() {212 local jpath="${1:+$1,}$2"213 local isleaf=0214 local isempty=0215 local print=0216217 case "$token" in218 '{') _json_parse_object "${jpath}" ;;219 '[') _json_parse_array "${jpath}" ;;220 # At this point, the only valid single-character tokens are digits.221 ''|[!0-9]) nvm_json_throw "EXPECTED value GOT >${token:-EOF}<" ;;222 *)223 value=$token224 isleaf=1225 [ "${value}" = '""' ] && isempty=1226 ;;227 esac228229 [ "${value}" = '' ] && return230 [ "${isleaf}" -eq 1 ] && [ $isempty -eq 0 ] && print=1231 [ "${print}" -eq 1 ] && printf "[%s]\t%s\n" "${jpath}" "${value}"232 :233}234235_json_parse() {236 read -r token237 _json_parse_value238 read -r token239 case "${token}" in240 '') ;;241 *) nvm_json_throw "EXPECTED EOF GOT >${token}<" ;;242 esac243}244245nvm_json_extract() {246 nvm_json_tokenize | _json_parse | grep -e "${1}" | awk '{print $2 $3}'247}