Skip to content

Commit

Permalink
Add tests for --arg, --argjson, and similar options
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny authored and nicowilliams committed Jul 22, 2023
1 parent 649d6b5 commit c9c45d7
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,49 @@ grep "Expected string key after '{', not '\\['" $d/err > /dev/null
echo '{"x":"y",["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true
grep "Expected string key after ',' in object, not '\\['" $d/err > /dev/null

# --arg, --argjson, $ARGS.named
$VALGRIND $JQ -n -c --arg foo 1 --argjson bar 2 '{$foo, $bar} | ., . == $ARGS.named' > $d/out
printf '{"foo":"1","bar":2}\ntrue\n' > $d/expected
cmp $d/out $d/expected

# --slurpfile, --rawfile
$VALGRIND $JQ -n --slurpfile foo $JQBASEDIR/tests/modules/data.json \
--rawfile bar $JQBASEDIR/tests/modules/data.json '{$foo, $bar}' > $d/out
cat > $d/expected <<'EOF'
{
"foo": [
{
"this": "is a test",
"that": "is too"
}
],
"bar": "{\n \"this\": \"is a test\",\n \"that\": \"is too\"\n}\n"
}
EOF
cmp $d/out $d/expected

# --args, --jsonargs, $ARGS.positional
$VALGRIND $JQ -n -c --args '$ARGS.positional' foo bar baz > $d/out
printf '["foo","bar","baz"]\n' > $d/expected
cmp $d/out $d/expected
$VALGRIND $JQ -n -c --jsonargs '$ARGS.positional' null true '[]' '{}' > $d/out
printf '[null,true,[],{}]\n' > $d/expected
cmp $d/out $d/expected
$VALGRIND $JQ -n -c '$ARGS.positional' --args foo 1 --jsonargs 2 '{}' --args 3 4 > $d/out
printf '["foo","1",2,{},"3","4"]\n' > $d/expected
cmp $d/out $d/expected
$VALGRIND $JQ -n -c '$ARGS.positional' --args --jsonargs > $d/out
printf '[]\n' > $d/expected
cmp $d/out $d/expected

## Regression test for issue #2572 assert when using --jsonargs and invalid JSON
$JQ -n --jsonargs null invalid && EC=$? || EC=$?
$VALGRIND $JQ -n --jsonargs null invalid && EC=$? || EC=$?
if [ "$EC" -ne 2 ]; then
echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
exit 1
fi
# this tests the args_done code path "--"
$JQ -n --jsonargs null -- invalid && EC=$? || EC=$?
$VALGRIND $JQ -n --jsonargs null -- invalid && EC=$? || EC=$?
if [ "$EC" -ne 2 ]; then
echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
exit 1
Expand Down

0 comments on commit c9c45d7

Please sign in to comment.