Skip to content

Commit

Permalink
Exit code 0 on successful patch
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed Feb 9, 2025
1 parent 8521e7e commit 81a04f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
18 changes: 2 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,11 @@ func printPatch(p, a string, metadata []jd.Metadata) {
out = bNode.Json(metadata...)
}
if *output == "" {
if out == "" {
os.Exit(0)
}
fmt.Print(out)
os.Exit(1)
} else {
if out == "" {
os.Exit(0)
}
ioutil.WriteFile(*output, []byte(out), 0644)
os.Exit(1)
}
os.Exit(0)
}

func printPatchV2(p, a string, options []v2.Option) {
Expand Down Expand Up @@ -497,18 +490,11 @@ func printPatchV2(p, a string, options []v2.Option) {
out = bNode.Json(options...)
}
if *output == "" {
if out == "" {
os.Exit(0)
}
fmt.Print(out)
os.Exit(1)
} else {
if out == "" {
os.Exit(0)
}
ioutil.WriteFile(*output, []byte(out), 0644)
os.Exit(1)
}
os.Exit(0)
}

func printTranslation(a string) {
Expand Down
49 changes: 39 additions & 10 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ func TestMain(t *testing.T) {
main()
}

ref := func(s string) *string {
return &s
}

testCases := []struct {
name string
files map[string]string
args []string
exitCode int
out string
out *string
outFile string
}{{
name: "no diff",
Expand All @@ -34,7 +38,7 @@ func TestMain(t *testing.T) {
"b.json": `{"foo":"bar"}`,
},
args: []string{"a.json", "b.json"},
out: "",
out: ref(""),
exitCode: 0,
}, {
name: "diff",
Expand All @@ -43,11 +47,11 @@ func TestMain(t *testing.T) {
"b.json": `{"foo":"baz"}`,
},
args: []string{"a.json", "b.json"},
out: s(
out: ref(s(
`@ ["foo"]`,
`- "bar"`,
`+ "baz"`,
),
)),
exitCode: 1,
}, {
name: "no diff in patch mode",
Expand All @@ -56,7 +60,7 @@ func TestMain(t *testing.T) {
"b.json": `{}`,
},
args: []string{"-f", "patch", "a.json", "b.json"},
out: `[]`,
out: ref(`[]`),
exitCode: 0,
}, {
name: "no diff in merge mode",
Expand All @@ -65,7 +69,7 @@ func TestMain(t *testing.T) {
"b.json": `{}`,
},
args: []string{"-f", "merge", "a.json", "b.json"},
out: `{}`,
out: ref(`{}`),
exitCode: 0,
}, {
name: "diff in patch mode",
Expand All @@ -74,7 +78,7 @@ func TestMain(t *testing.T) {
"b.json": `{"foo":"baz"}`,
},
args: []string{"-f", "patch", "a.json", "b.json"},
out: `[{"op":"test","path":"/foo","value":"bar"},{"op":"remove","path":"/foo","value":"bar"},{"op":"add","path":"/foo","value":"baz"}]`,
out: ref(`[{"op":"test","path":"/foo","value":"bar"},{"op":"remove","path":"/foo","value":"bar"},{"op":"add","path":"/foo","value":"baz"}]`),
exitCode: 1,
}, {
name: "diff in merge mode",
Expand All @@ -83,8 +87,33 @@ func TestMain(t *testing.T) {
"b.json": `{"foo":"baz"}`,
},
args: []string{"-f", "merge", "a.json", "b.json"},
out: `{"foo":"baz"}`,
out: ref(`{"foo":"baz"}`),
exitCode: 1,
}, {
name: "exit 0 on successful patch",
files: map[string]string{
"a.json": `{"foo":"bar"}`,
"patch": `
@ ["foo"]
- "bar"
+ "baz"
`,
},
args: []string{"-p", "patch", "a.json"},
out: ref(`{"foo":"baz"}`),
exitCode: 0,
}, {
name: "exit 1 on unsuccessful patch",
files: map[string]string{
"a.json": `{"foo":"bar"}`,
"patch": `
@ ["foo"]
- "zap"
+ "baz"
`,
},
args: []string{"-p", "patch", "a.json"},
exitCode: 2,
}}

testName := t.Name()
Expand Down Expand Up @@ -116,8 +145,8 @@ func TestMain(t *testing.T) {
cmd := exec.Command(os.Args[0], "-test.run", testName)
cmd.Env = append(os.Environ(), jdFlags+"="+strings.Join(args, " "))
out, _ := cmd.CombinedOutput()
if string(out) != tc.out {
t.Errorf("wanted out %q. got %q", tc.out, string(out))
if tc.out != nil && string(out) != *tc.out {
t.Errorf("wanted out %q. got %q", *tc.out, string(out))
}
if exitCode := cmd.ProcessState.ExitCode(); exitCode != tc.exitCode {
t.Errorf("wanted exit code %v. got %v", tc.exitCode, exitCode)
Expand Down

0 comments on commit 81a04f5

Please sign in to comment.