Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests to use slurpfile and rawfile arguments #3123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jadutter
Copy link

Fixed it so that jq can use --slurpfile and --rawfile when running tests.
I think it's useful to be able to reference a file containing some data instead of needing to repeatedly copy-paste the same input to different test cases. Particularly since if the input is a larger object, then it's difficult to read since it needs to be all on one line in the test file.

Simple example

Given the following files

raw.csv

id,name
1,Alice
2,Bob

slurp.json

{"foo":"bar"}

and this test file

myjq.test

$raw|split("\n")|.[1]|split(",")|.[1]
null
"Alice"

$slurp|.[0]|.foo
null
"bar"

map(($slurp|.[0]|.foo)+.)
["ista","yonx"]
["barista","baryonx"]

then this command

jq --rawfile raw ./raw.csv --slurpfile slurp ./slurp.json  --run-tests ./myjq.test

should output

Test #1: '$raw|split("\n")|.[1]|split(",")|.[1]' at line number 1
Test #2: '$slurp|.[0]|.foo' at line number 5
Test #3: 'map(($slurp|.[0]|.foo)+.)' at line number 9
3 of 3 tests passed (0 malformed, 0 skipped)

@wader
Copy link
Member

wader commented May 29, 2024

I've thought about some kind of binding support in the .test format, either inline or read from a file. Could that be an alternative?

Can't come up with a neat syntax now but something like this:

# read json value inline
%%BIND $input
{
   "a": 123
}

.a
$input
123

type
$input
"object"

# read json value from file.json
%%BIND $input file.json
...

@wader
Copy link
Member

wader commented Jun 3, 2024

Doh i got it wrong, probably more like:

# read json value inline
%%BIND $input
{
   "a": 123
}

$input | .a
null
123

$input | type
null
"object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants