Skip to content

Commit

Permalink
Require a main program (fix #2785)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Aug 2, 2023
1 parent 044b385 commit 27a4d57
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ EXTRA_DIST = $(DOC_FILES) $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) \
tests/setup tests/torture/input0.json \
tests/optional.test tests/man.test tests/manonig.test \
tests/jq.test tests/onig.test tests/base64.test \
tests/utf8-truncate.jq tests/jq-f-test.sh
tests/utf8-truncate.jq tests/jq-f-test.sh \
tests/no-main-program.jq tests/yes-main-program.jq

AM_DISTCHECK_CONFIGURE_FLAGS=--with-oniguruma=builtin

Expand Down
14 changes: 13 additions & 1 deletion docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,18 @@ sections:
output(s) of the filter are written to standard output, as a
sequence of newline-separated JSON data.
The simplest and most common filter (or jq program) is `.`,
which is the identity operator, copying the inputs of the jq
processor to the output stream. Because the default behavior of
the jq processor is to read JSON texts from the input stream,
and to pretty-print outputs, the `.` program's main use is to
validate and pretty-print the inputs. The jq programming
language is quite rich and allows for much more than just
validation and pretty-printing.
Note: it is important to mind the shell's quoting rules. As a
general rule it's best to always quote (with single-quote
characters) the jq program, as too many characters with special
characters on Unix shells) the jq program, as too many characters with special
meaning to jq are also shell meta-characters. For example, `jq
"foo"` will fail on most Unix shells because that will be the same
as `jq foo`, which will generally fail because `foo is not
Expand All @@ -100,6 +109,9 @@ sections:
* Powershell: `jq '.[\"foo\"]'`
* Windows command shell: `jq ".[\"foo\"]"`
Note: jq allows user-defined functions, but every jq program
must have a top-level expression.
You can affect how jq reads and writes its input and output
using some command-line options:
Expand Down
8 changes: 7 additions & 1 deletion jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ int load_program(jq_state *jq, struct locfile* src, block *out_block) {
if (nerrors)
return nerrors;

if (!block_has_main(program)) {
jq_report_error(jq, jv_string("jq: error: Top-level program not given (try \".\")"));
block_free(program);
return 1;
}

char* home = getenv("HOME");
if (home) { // silently ignore no $HOME
/* Import ~/.jq as a library named "" found in $HOME */
Expand Down
1 change: 1 addition & 0 deletions tests/no-main-program.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def a: .;
30 changes: 30 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,34 @@ if command -v script >/dev/null 2>&1; then
cmp $d/color $d/expect
fi

# #2785
if $VALGRIND $Q $JQ -n -f "$JQTESTDIR/no-main-program.jq" > $d/out 2>&1; then
echo "jq -f $JQTESTDIR/no-main-program.jq succeeded"
exit 1
else
EC=$?
if [ $EC -eq 1 ]; then
echo "jq -f $JQTESTDIR/no-main-program.jq failed with memory errors"
exit 1
fi
if [ $EC -ne 3 ]; then
echo "jq -f $JQTESTDIR/no-main-program.jq failed with wrong exit code ($EC)"
exit 1
fi
fi
cat > $d/expected <<EOF
jq: error: Top-level program not given (try ".")
jq: 1 compile error
EOF
if ! diff $d/expected $d/out; then
echo "jq -f $JQTESTDIR/no-main-program.jq failed but its error message is not the expected one"
exit 1
fi

if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then
echo "jq -f $JQTESTDIR/yes-main-program.jq failed"
exit 1
fi

exit 0
2 changes: 2 additions & 0 deletions tests/yes-main-program.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def a: .;
0

0 comments on commit 27a4d57

Please sign in to comment.