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

fix: Allow patches with empty files with multiple newlines or comments #5771

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugin/builtin/patchtransformer/PatchTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error {
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))

if (errSM == nil && errJson == nil) ||
(patchesSM != nil && patchesJson != nil) {
if ((errSM == nil && errJson == nil) ||
(patchesSM != nil && patchesJson != nil)) &&
(len(patchesSM) > 0 && len(patchesJson) > 0) {
return fmt.Errorf(
"illegally qualifies as both an SM and JSON patch: %s",
p.patchSource)
Expand Down
29 changes: 29 additions & 0 deletions plugin/builtin/patchtransformer/PatchTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,32 @@ spec:
name: test-deployment
`)
}

func TestPatchTransformerPatchEmpty(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchTransformer")
defer th.Reset()

th.RunTransformerAndCheckError(`
`, someDeploymentResources, func(t *testing.T, err error) {
if err != nil {
t.Fatalf("unexpected error")
}
})
}

func TestPatchTransformerPatchEmptyOnlyComments(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchTransformer")
defer th.Reset()

th.RunTransformerAndCheckError(`
# File with only comments

# Is a virtually empty yaml
`, someDeploymentResources, func(t *testing.T, err error) {
if err != nil {
t.Fatalf("unexpected error")
}
})
}