I sometimes make changes that means I have to update all of the JSON files in examples/ folder. I thought that it’d be easy to just add an environment variable when I want the test to just overwrite the original file instead of reading it.
The problem is that it seems to 1) not have the rights to write files and 2) only has access to the files that were copied within the _build/.... path.
I guess I’m looking for a simple and clean alternative to make this work here, any ideas?
I guess I’m looking for a simple and clean alternative to make this work here, any ideas?
The JSON files contain input and expected output parts, is that correct? An idiomatic way to deal with updating files with dune is to use promotion. The idea is that your test executable, instead of doing the comparison, should emit a “corrected” version (with inputs being the same and outputs being the actual outputs from the system under test).
Then you’d attach a rule that does diffing and promotion between the JSON file in tree and the JSON file produced by your test executable.
Once this is in place, dune runtest will display any diffs (if any - and exit with corresponding exit code), and dune promote will apply the diff back to the JSON file in your source tree.
(you need to format JSON consistently for this to work)
On that note, it seems the Yojson pretty-printer changed formatting between versions 1 and 2, so it may be worth finding a formatter with a stable output like maybe jq
I would also argue that depending on a particular pretty printing of JSON is a fragile test to begin with since for most cases the exact formatting of JSON shouldn’t matter (e.g. swapped keys in objects, whitespace changes). I’d rather suggest a structural comparison in such case.
I thought that it’d be easy to just add an environment variable when I want the test to just overwrite the original file instead of reading it.
But it might be preferrable to stick to either something that consumes an existing format (say, third party test vectors) or something that makes promotion easier (e.g. ppx_expect).