Fix regression on ~/.jq being a directory
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Muh Muhten · committed by Nico Williams · parent 30afc86 · Browse files · 6 files·+34 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+34 added−29 removed
- tests:+13 added−7 removed
- other:+21 added−22 removed
Provenance
- Commit
141bb78- Tree
a956d49- Author
Muh Muhten <muh.muhten@gmail.com>- Committer
Nico Williams <nico@cryptonector.com>- Parent
30afc86
6 files changed+34 added−29 removed
modified:src/linker.c+16 added-13 removed
24 unmodified lines25 char **names;26 block *defs;27 uint64_t ct;28 };29-static int load_library(jq_state *jq, jv lib_path, int is_data, int raw,29+static int load_library(jq_state *jq, jv lib_path,30+ int is_data, int raw, int optional,31 const char *as, block *out_block,32 struct lib_loading_state *lib_state);33 34 static int path_is_relative(jv p) {244 unmodified lines279 280 if (is_data) {281 // Can't reuse data libs because the wrong name is bound282 block dep_def_block;282- nerrors += load_library(jq, resolved, is_data, raw, as_str, &dep_def_block, lib_state);283+ nerrors += load_library(jq, resolved, is_data, raw, optional, as_str, &dep_def_block, lib_state);284 if (nerrors == 0) {285 // Bind as both $data::data and $data for backward compatibility vs common sense286 bk = block_bind_library(dep_def_block, bk, OP_IS_CALL_PSEUDO, as_str);287 bk = block_bind_library(dep_def_block, bk, OP_IS_CALL_PSEUDO, NULL);10 unmodified lines298 // Bind the library to the program299 bk = block_bind_library(lib_state->defs[state_idx], bk, OP_IS_CALL_PSEUDO, as_str);300 } else { // Not found. Add it to the table before binding.301 block dep_def_block = gen_noop();301- nerrors += load_library(jq, resolved, is_data, raw, as_str, &dep_def_block, lib_state);302+ nerrors += load_library(jq, resolved, is_data, raw, optional, as_str, &dep_def_block, lib_state);303 // resolved has been freed304 if (nerrors == 0) {305 // Bind the library to the program306 bk = block_bind_library(dep_def_block, bk, OP_IS_CALL_PSEUDO, as_str);10 unmodified lines317 }318 319 // Loads the library at lib_path into lib_state, putting the library's defs320 // into *out_block320-static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, const char *as, block *out_block, struct lib_loading_state *lib_state) {321+static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, int optional, const char *as, block *out_block, struct lib_loading_state *lib_state) {322 int nerrors = 0;323 struct locfile* src = NULL;324 block program;325 jv data;2 unmodified lines328 else329 data = jv_load_file(jv_string_value(lib_path), 1);330 int state_idx;331 if (!jv_is_valid(data)) {332+ program = gen_noop();333+ if (!optional) {331- if (jv_invalid_has_msg(jv_copy(data)))332- data = jv_invalid_get_msg(data);333- else334- data = jv_string("unknown error");335- jq_report_error(jq, jv_string_fmt("jq: error loading data file %s: %s\n", jv_string_value(lib_path), jv_string_value(data)));336- nerrors++;334+ if (jv_invalid_has_msg(jv_copy(data)))335+ data = jv_invalid_get_msg(data);336+ else337+ data = jv_string("unknown error");338+ jq_report_error(jq, jv_string_fmt("jq: error loading data file %s: %s\n", jv_string_value(lib_path), jv_string_value(data)));339+ nerrors++;340+ }341 goto out;342 } else if (is_data) {343 // import "foo" as $bar;344 program = gen_const_global(jv_copy(data), as);345 } else {346 // import "foo" as bar;347 src = locfile_init(jq, jv_string_value(lib_path), jv_string_value(data), jv_string_length_bytes(jv_copy(data)));348 nerrors += jq_parse_library(src, &program);349+ locfile_free(src);350 if (nerrors == 0) {351 char *lib_origin = strdup(jv_string_value(lib_path));352 nerrors += process_dependencies(jq, jq_get_jq_origin(jq),353 jv_string(dirname(lib_origin)),6 unmodified lines360 lib_state->names = jv_mem_realloc(lib_state->names, lib_state->ct * sizeof(const char *));361 lib_state->defs = jv_mem_realloc(lib_state->defs, lib_state->ct * sizeof(block));362 lib_state->names[state_idx] = strdup(jv_string_value(lib_path));363 lib_state->defs[state_idx] = program;359- *out_block = program;360- if (src)361- locfile_free(src);364 out:365+ *out_block = program;366 jv_free(lib_path);367 jv_free(data);368 return nerrors;369 }64 unmodified linesadded:tests/modules/home1/.jq+5 added
1+def foo: "baz";2+def f: "wat";3+def f: "foo";4+def g: "bar";5+def fg: f+g;added:tests/modules/home2/.jq/g.jq+1 added
1+def g: 1;deleted:tests/modules/.jq-5 removed
1-def foo: "baz";2-def f: "wat";3-def f: "foo";4-def g: "bar";5-def fg: f+g;modified:tests/shtest+7 added-2 removed
216 unmodified lines217 ## Test library/module system218 219 # Check handling of ~/.jq; these can't move into jq_test.c yet because220 # they depend on $HOME221-if [ "$(HOME="$mods" $VALGRIND $Q $JQ -nr fg)" != foobar ]; then221+if [ "$(HOME="$mods/home1" $VALGRIND $Q $JQ -nr fg)" != foobar ]; then222 echo "Bug #479 appears to be back" 1>&2223 exit 1224 fi225 226-if [ $(HOME="$mods" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -gt 3 ]; then226+if [ $(HOME="$mods/home1" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -ne 3 ]; then227 echo "Binding too many defs into program" 1>&2228 exit 1229 fi230 231+if ! HOME="$mods/home2" $VALGRIND $Q $JQ -n 'include "g"; empty'; then232+ echo "Mishanding directory ~/.jq" 1>&2233+ exit 1234+fi235+236 cd "$JQBASEDIR" # so that relative library paths are guaranteed correct237 if ! $VALGRIND $Q $JQ -L ./tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then238 echo "Issue #817 regression?" 1>&2239 exit 196 unmodified linesmodified:Makefile.am+5 added-9 removed
168 unmodified lines169 docs/build_manpage.py docs/README.md jq.1.prebuilt170 171 EXTRA_DIST = $(DOC_FILES) $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) \172 jq.1.prebuilt jq.spec src/lexer.c src/lexer.h src/parser.c \173- src/parser.h src/version.h src/builtin.jq \174- scripts/version tests/jq.test tests/modules/.jq \173+ src/parser.h src/version.h src/builtin.jq scripts/version \174+ tests/base64.test tests/jq-f-test.sh tests/jq.test \175 tests/modules/a.jq tests/modules/b/b.jq tests/modules/c/c.jq \176 tests/modules/c/d.jq tests/modules/data.json \177+ tests/modules/home1/.jq tests/modules/home2/.jq/g.jq \178 tests/modules/lib/jq/e/e.jq tests/modules/lib/jq/f.jq \179 tests/modules/syntaxerror/syntaxerror.jq \180 tests/modules/test_bind_order.jq \181 tests/modules/test_bind_order0.jq \182 tests/modules/test_bind_order1.jq \183 tests/modules/test_bind_order2.jq tests/onig.supp \183- tests/onig.test tests/setup tests/torture/input0.json \184- tests/optional.test tests/optionaltest \185- tests/utf8-truncate.jq tests/utf8test \186- tests/base64.test tests/base64test \187- tests/jq-f-test.sh tests/shtest184+ tests/onig.test tests/optional.test tests/setup \185+ tests/torture/input0.json tests/utf8-truncate.jq186 189-190-187 # README.md is expected in Github projects, good stuff in it, so we'll188 # distribute it and install it with the package in the doc directory.189 docdir = ${datadir}/doc/${PACKAGE}190 dist_doc_DATA = README.md COPYING AUTHORS README28 unmodified lines