diff --git a/pkg/patch/transformer/patchjson6902json.go b/pkg/patch/transformer/patchjson6902json.go index d4d6c0abb..9e65a290b 100644 --- a/pkg/patch/transformer/patchjson6902json.go +++ b/pkg/patch/transformer/patchjson6902json.go @@ -19,7 +19,7 @@ package transformer import ( "fmt" - "github.com/evanphx/json-patch" + jsonpatch "github.com/evanphx/json-patch" "github.com/pkg/errors" "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resmap" @@ -42,6 +42,11 @@ func newPatchJson6902JSONTransformer( id resid.ResId, rawOp []byte) (transformers.Transformer, error) { op := rawOp var err error + + if len(op) == 0 { + return nil, fmt.Errorf("json patch file is empty %v", id) + } + if !isJsonFormat(op) { // if it isn't JSON, try to parse it as YAML op, err = yaml.YAMLToJSON(rawOp) diff --git a/pkg/patch/transformer/patchjson6902json_test.go b/pkg/patch/transformer/patchjson6902json_test.go index 8e5c751cc..fbad24988 100644 --- a/pkg/patch/transformer/patchjson6902json_test.go +++ b/pkg/patch/transformer/patchjson6902json_test.go @@ -163,3 +163,20 @@ func TestJsonPatchJSONTransformer_UnHappyTransform(t *testing.T) { t.Fatalf("expected error didn't happen, but got %v", err) } } + +func TestJsonPatchJSONTransformer_EmptyPatchFile(t *testing.T) { + id := resid.NewResId(deploy, "deploy1") + operations := []byte(``) + + _, err := newPatchJson6902JSONTransformer(id, operations) + + if err == nil { + t.Fatalf("expected an error") + } + + if err != nil { + if !strings.HasPrefix(err.Error(), "json patch file is empty") { + t.Fatalf("expected %s, but got %v", "json patch file is empty", err) + } + } +}