Skip to content

Commit

Permalink
interp: Cleanup stdin reading and add more option tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Nov 30, 2021
1 parent 97c7403 commit 8d442b8
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 31 deletions.
20 changes: 11 additions & 9 deletions pkg/interp/bufferrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,27 @@ func (of *openFile) ToBufferView() (BufferRange, error) {
// TODO: when to close? when bb loses all refs? need to use finalizer somehow?
func (i *Interp) _open(c interface{}, a []interface{}) interface{} {
var err error

var f fs.File
var path string
path, err = toString(c)
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}

var bEnd int64
var f fs.File
if path == "" || path == "-" {
switch c.(type) {
case nil:
path = "<stdin>"
f = i.os.Stdin()
} else {
default:
path, err = toString(c)
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
f, err = i.os.FS().Open(path)
if err != nil {
return err
}
}

var bEnd int64
var fRS io.ReadSeeker

fFI, err := f.Stat()
if err != nil {
f.Close()
Expand Down
8 changes: 0 additions & 8 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
def print: stdout;
def println: ., "\n" | stdout;
def debug:
( ((["DEBUG", .] | tojson), "\n" | stderr)
, .
);
def debug(f): . as $c | f | debug | $c;

def display($opts): _display(options($opts));
def display: display({});
def d($opts): display($opts);
Expand Down
9 changes: 9 additions & 0 deletions pkg/interp/internal.jq
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# is here to be defined as early as possible to allow debugging
def print: stdout;
def println: ., "\n" | stdout;
def debug:
( ((["DEBUG", .] | tojson), "\n" | stderr)
, .
);
def debug(f): . as $c | f | debug | $c;

def _global_var($k): _global_state[$k];
def _global_var($k; f): _global_state(_global_state | .[$k] |= f) | .[$k];

Expand Down
15 changes: 10 additions & 5 deletions pkg/interp/interp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def input:
| _input_filename(null) as $_
| $h
| try
# null input here means stdin
( open
| _input_filename($h) as $_
| _input_filename($h // "<stdin>") as $_
| .
)
catch
Expand Down Expand Up @@ -233,10 +234,12 @@ def _main:
),
expr_eval_path: $combined_opts.expr_file,
filenames: (
( if $combined_opts.expr_file then $rest
( if $combined_opts.filenames then $combined_opts.filenames
elif $combined_opts.expr_file then $rest
else $rest[1:]
end
| if . == [] then null end
# null means stdin
| if . == [] then [null] end
)
),
join_string: (
Expand Down Expand Up @@ -291,10 +294,12 @@ def _main:
elif $opts.show_formats then
_formats_list | println
elif
( ($rest | length) == 0 and
( $opts.filenames == [null] and
$opts.null_input == false and
($opts.repl | not) and
($opts.expr_file | not) and
$stdin.is_terminal and $stdout.is_terminal
$stdin.is_terminal and
$stdout.is_terminal
) then
( (( _usage($arg0), "\n") | stderr)
, null | halt_error(_exit_code_args_error)
Expand Down
8 changes: 4 additions & 4 deletions pkg/interp/options.jq
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def _opt_build_default_fixed:
decode_progress: (env.NO_DECODE_PROGRESS == null),
depth: 0,
expr: ".",
expr_file: null,
expr_eval_path: "arg",
filenames: ["-"],
expr_file: null,
filenames: null,
include_path: null,
join_string: "\n",
null_input: false,
Expand Down Expand Up @@ -108,14 +108,14 @@ def _opt_cli_arg_options:
color: (.color | _opt_toboolean),
colors: (.colors | _opt_tostring),
compact: (.compact | _opt_toboolean),
decode_file: (.decode_file | _opt_toarray(type == "string")),
decode_file: (.decode_file | _opt_toarray(_opt_is_string_pair)),
decode_format: (.decode_format | _opt_tostring),
decode_progress: (.decode_progress | _opt_toboolean),
depth: (.depth | _opt_tonumber),
display_bytes: (.display_bytes | _opt_tonumber),
expr: (.expr | _opt_tostring),
expr_file: (.expr_file | _opt_tostring),
filename: (.filenames | _opt_toarray(type == "string")),
filenames: (.filenames | _opt_toarray(type == "string")),
include_path: (.include_path | _opt_tostring),
join_string: (.join_string | _opt_tostring),
line_bytes: (.line_bytes | _opt_tonumber),
Expand Down
4 changes: 1 addition & 3 deletions pkg/interp/testdata/args.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ null> ^D
$ fq -i . /test.mp3
mp3> ^D
$ fq -n
exitcode: 2
stderr:
Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
null
$ fq -ni
null> ^D
$ fq -n 123
Expand Down
2 changes: 1 addition & 1 deletion pkg/interp/testdata/basic.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mp3> .headers[0].magic._format
null
mp3> ^D
$ fq -d raw .
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} - (raw)
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} <stdin> (raw)
0x0|61 62 63 0a| |abc.| | unknown0: raw bits
stdin:
abc
4 changes: 4 additions & 0 deletions pkg/interp/testdata/inputs.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ $ fq -d raw -n '(input,input,input,input) | todescription' /a /b /c
exitcode: 5
stderr:
error: break
$ fq -d raw input_filename
"<stdin>"
stdin:
test
$ fq -d raw input_filename /a /b /c
"/a"
"/b"
Expand Down
8 changes: 7 additions & 1 deletion pkg/interp/testdata/options.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ fq -n options
"expr_eval_path": "arg",
"expr_file": null,
"filenames": [
"-"
null
],
"include_path": null,
"join_string": "\n",
Expand Down Expand Up @@ -58,6 +58,8 @@ $ fq -o compact=true -n options.compact
true
$ fq -o compact=aaa -n options.compact
false
$ fq -n -o 'decode_file=[["a", "/test.mp3"]]' '$a | format'
"mp3"
$ fq -o decode_progress=false -n options.decode_progress
false
$ fq -o decode_progress=aaa -n options.decode_progress
Expand All @@ -70,10 +72,14 @@ $ fq -o display_bytes=10 -n options.display_bytes
10
$ fq -o display_bytes=true -n options.display_bytes
16
$ fq -n -o expr=123
123
$ fq -o expr_file=test.jq -n options.expr_file
exitcode: 2
stderr:
error: open testdata/test.jq: no such file or directory
$ fq -o 'filenames=["/test.mp3"]' format
"mp3"
$ fq -o include_path=path -n options.include_path
"path"
$ fq -o 'join_string=aaa\n' -n options.join_string
Expand Down

0 comments on commit 8d442b8

Please sign in to comment.