Skip to content

Commit b65cf0c

Browse files
authored
New tests + fixes (bashtools#12)
1 parent f47ab04 commit b65cf0c

17 files changed

+53
-13
lines changed

JSONPath.sh

+11-13
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ main() {
4646

4747
[[ $MULTIPASS -eq 1 ]] && {
4848
# replace filter expression with index sequence
49-
SET=$(sed -rn 's/.*,([0-9]+)[],].*/\1/p' $PASSFILE | tr '\n' ,)
49+
SET=$(sed -rn 's/.*[[,"]+([0-9]+)[],].*/\1/p' $PASSFILE | tr '\n' ,)
5050
SET=${SET%,}
5151
QUERY=$(echo $QUERY | sed "s/?(@[^)]\+)/$SET/")
52-
[[ $DEBUG -eq 1 ]] && echo "QUERY=$QUERY"
52+
[[ $DEBUG -eq 1 ]] && echo "QUERY=$QUERY" >/dev/stderr
5353
reset
5454
continue
5555
}
@@ -120,15 +120,13 @@ usage() {
120120
echo "Usage: JSONPath.sh [-b] [j] [-h] [-f FILE] [pattern]"
121121
echo
122122
echo "pattern - the JSONPath query. Defaults to '$.*' if not supplied."
123-
#echo "-s - Remove escaping of the solidus symbol (straight slash)."
124123
echo "-b - Brief. Only show values."
125124
echo "-j - JSON output."
126125
echo "-u - Strip unnecessary leading path elements."
127126
echo "-i - Case insensitive."
128127
echo "-p - Pass-through to the JSON parser."
129128
echo "-w - Match whole words only (for filter script expression)."
130129
echo "-f FILE - Read a FILE instead of stdin."
131-
#echo "-n - No-head. Do not show nodes that have no path (lines that start with [])."
132130
echo "-h - This help text."
133131
echo
134132
}
@@ -245,7 +243,6 @@ tokenize_path () {
245243
CHAR='[^[:cntrl:]"\\]'
246244
else
247245
GREP=awk_egrep
248-
#CHAR='[^[:cntrl:]"\\\\]'
249246
fi
250247

251248
local WILDCARD='\*'
@@ -265,10 +262,10 @@ tokenize_path () {
265262
$GREP "$INDEX|$STRING|$WORD|$WILDCARD|$FILTER|$DEEPSCAN|$SET|$INDEXALL|." | \
266263
egrep -v "^$SPACE$|^\\.$|^\[$|^\]$|^'$|^\\\$$|^\)$")
267264
[[ $DEBUG -eq 1 ]] && {
268-
echo "egrep -o '$INDEX|$STRING|$WORD|$WILDCARD|$FILTER|$DEEPSCAN|$SET|$INDEXALL|.'"
265+
echo "egrep -o '$INDEX|$STRING|$WORD|$WILDCARD|$FILTER|$DEEPSCAN|$SET|$INDEXALL|.'" >/dev/stderr
269266
echo -n "TOKENISED QUERY="; echo "$QUERY" | \
270267
$GREP "$INDEX|$STRING|$WORD|$WILDCARD|$FILTER|$DEEPSCAN|$SET|$INDEXALL|." | \
271-
egrep -v "^$SPACE$|^\\.$|^\[$|^\]$|^'$|^\\\$$|^\)$"
268+
egrep -v "^$SPACE$|^\\.$|^\[$|^\]$|^'$|^\\\$$|^\)$" >/dev/stderr
272269
}
273270
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
274271
}
@@ -396,7 +393,6 @@ create_filter() {
396393
else
397394
[[ $i -gt 0 ]] && comma=","
398395
fi
399-
#idx=$(echo "${PATHTOKENS[i]}" | tr -d "[]")
400396
query+="$comma$a"
401397
fi
402398
comma=","
@@ -410,7 +406,7 @@ create_filter() {
410406
done
411407

412408
[[ -z $FILTER ]] && FILTER="$query[],]"
413-
[[ $DEBUG -eq 1 ]] && echo "FILTER=$FILTER"
409+
[[ $DEBUG -eq 1 ]] && echo "FILTER=$FILTER" >/dev/stderr
414410
}
415411

416412
# ---------------------------------------------------------------------------
@@ -566,7 +562,7 @@ indexmatcher() {
566562

567563
[[ $DEBUG -eq 1 ]] && {
568564
for i in `seq 0 $((${#INDEXMATCH_QUERY[*]}-1))`; do
569-
echo "INDEXMATCH_QUERY[$i]=${INDEXMATCH_QUERY[i]}"
565+
echo "INDEXMATCH_QUERY[$i]=${INDEXMATCH_QUERY[i]}" >/dev/stderr
570566
done
571567
}
572568

@@ -591,11 +587,11 @@ indexmatcher() {
591587
[[ $i -eq $((${#INDEXMATCH_QUERY[*]}-1)) ]] && {
592588
if [[ $step -gt 1 ]]; then
593589
[[ $(((num[i]-a)%step)) -eq 0 ]] && {
594-
[[ $DEBUG -eq 1 ]] && echo -n "($a,$b,${num[i]}) "
590+
[[ $DEBUG -eq 1 ]] && echo -n "($a,$b,${num[i]}) " >/dev/stderr
595591
echo "$line"
596592
}
597593
else
598-
[[ $DEBUG -eq 1 ]] && echo -n "($a,$b,${num[i]}) "
594+
[[ $DEBUG -eq 1 ]] && echo -n "($a,$b,${num[i]}) " >/dev/stderr
599595
echo "$line"
600596
fi
601597
}
@@ -814,7 +810,9 @@ filter() {
814810
[[ $NOCASE -eq 1 ]] && opts+="-i"
815811
[[ $WHOLEWORD -eq 1 ]] && opts+=" -w"
816812
if [[ -z $OPERATOR ]]; then
813+
[[ $MULTIPASS -eq 1 ]] && FILTER="$FILTER[\"]?$"
817814
egrep $opts "$FILTER"
815+
[[ $DEBUG -eq 1 ]] && echo "FILTER=$FILTER" >/dev/stderr
818816
else
819817
egrep $opts "$FILTER" | \
820818
while read line; do
@@ -837,7 +835,7 @@ filter() {
837835
[[ "${v,,}" < "${RHS,,}" ]] && echo "$line"
838836
;;
839837
esac
840-
done #< <(egrep $opts "$FILTER")
838+
done
841839
fi
842840
}
843841

all-docker-tests.sh

+13
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,25 @@ cp test/docker/Dockerfile-centos test/docker/Dockerfile
2121

2222
c=$(grep 'test(s) failed' $log)
2323

24+
# Debian
25+
export IMAGE=json-path-debian-bash
26+
cp test/docker/Dockerfile-debian test/docker/Dockerfile
27+
./test/docker/wrap_in_docker.sh ./all-tests.sh | tee $log
28+
29+
d=$(grep 'test(s) failed' $log)
30+
31+
# Cleanup
2432
rm $log
33+
rm test/docker/Dockerfile
2534

35+
# Results
2636
echo
2737
echo "Fedora tests"
2838
echo $a
2939
echo "Ubuntu tests"
3040
echo $b
3141
echo "Centos tests"
3242
echo $c
43+
echo "Debian tests"
44+
echo $c
45+

test/docker/Dockerfile-debian

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM ubuntu
2+
3+
RUN apt-get update && apt-get -y install python gawk

test/valid/cburgmer.github.io.argp1

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$[2]

test/valid/cburgmer.github.io.argp2

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$[0]

test/valid/cburgmer.github.io.argp3

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$[1:10]

test/valid/cburgmer.github.io.argp4

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$[1:8]

test/valid/cburgmer.github.io.argp5

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$[1:2]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$[?(@.key==42)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{"key": 0},
3+
{"key": 42},
4+
{"key": -1},
5+
{"key": 41},
6+
{"key": 43},
7+
{"key": 42.0001},
8+
{"key": 41.9999},
9+
{"key": 100},
10+
{"some": "value"}
11+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1,"key"] 42

test/valid/cburgmer.github.io.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["first", "second", "third"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[2] "third"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[0] "first"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1] "second"
2+
[2] "third"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1] "second"
2+
[2] "third"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1] "second"

0 commit comments

Comments
 (0)