josephburnett / jd

JSON diff and patch
MIT License
826 stars 38 forks source link

Running with `-f patch` always exits with code `1` #62

Closed doctordesh closed 1 month ago

doctordesh commented 7 months ago

Hello,

I'm using your fantastic tool to run diffs in a refactoring project. Works wonders! Today though, I tried to use the patch format instead, and even on files that are equal the output is [] (which might be by design) but the exit code is always 1, regardless if there are problems or not.

> ./jd --version
jd version 1.7.1
> echo "{}" > file1.json
> echo "{}" > file2.json
> ./jd -f patch file1.json file2.json
[]
> echo $?
1
> ./jd file1.json file2.json
> echo $?
0
doctordesh commented 7 months ago

I would guess that this is the problem. You always create an empty slice. Which means that json.Marshal further down will at least output [].

Then the first and last if here will fail and cause os.Exit(1)

josephburnett commented 1 month ago

@doctordesh Yeah, it should definitely return 0 when there is no diff. However [] is a correct output for a noop JSON Patch. And {} is a correct output for a noop JSON Merge Patch (which I was not doing!). I've updated the diff functions to produce and parse the correct noop values for those formats. And changed the main.go to return the right exit code. I also started with a test in main_test.go to make sure this doesn't break in the future.

Thanks for the report!

Here is the change: https://github.com/josephburnett/jd/commit/6446402ed2cb998b23b0d0ff88952160fec47fc2. It will be included in the next release.