mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
fix: Allow patches with empty files with multiple newlines or comments (#5771)
* fix: Add test, when an empty patch file is given, it should not fail * fix: Add code so there's no error given if an empty file is given as a patch * chore: Generate plugin with pluginator * chore: fix tests Signed-off-by: Julio Chana <julio.chana@lokalise.com> * Add t.helper() at start of test function Signed-off-by: Julio Chana <julio.chana@lokalise.com> --------- Signed-off-by: Julio Chana <julio.chana@lokalise.com>
This commit is contained in:
@@ -56,8 +56,9 @@ func (p *PatchTransformerPlugin) Config(h *resmap.PluginHelpers, c []byte) error
|
|||||||
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
|
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
|
||||||
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))
|
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))
|
||||||
|
|
||||||
if (errSM == nil && errJson == nil) ||
|
if ((errSM == nil && errJson == nil) ||
|
||||||
(patchesSM != nil && patchesJson != nil) {
|
(patchesSM != nil && patchesJson != nil)) &&
|
||||||
|
(len(patchesSM) > 0 && len(patchesJson) > 0) {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"illegally qualifies as both an SM and JSON patch: %s",
|
"illegally qualifies as both an SM and JSON patch: %s",
|
||||||
p.patchSource)
|
p.patchSource)
|
||||||
|
|||||||
@@ -59,8 +59,9 @@ func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error {
|
|||||||
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
|
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
|
||||||
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))
|
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))
|
||||||
|
|
||||||
if (errSM == nil && errJson == nil) ||
|
if ((errSM == nil && errJson == nil) ||
|
||||||
(patchesSM != nil && patchesJson != nil) {
|
(patchesSM != nil && patchesJson != nil)) &&
|
||||||
|
(len(patchesSM) > 0 && len(patchesJson) > 0) {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"illegally qualifies as both an SM and JSON patch: %s",
|
"illegally qualifies as both an SM and JSON patch: %s",
|
||||||
p.patchSource)
|
p.patchSource)
|
||||||
|
|||||||
@@ -1108,3 +1108,54 @@ spec:
|
|||||||
name: test-deployment
|
name: test-deployment
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPatchTransformerPatchEmpty(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
|
PrepBuiltin("PatchTransformer")
|
||||||
|
defer th.Reset()
|
||||||
|
|
||||||
|
th.WriteF("patch.yaml", `
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.RunTransformerAndCheckError(`
|
||||||
|
apiVersion: builtin
|
||||||
|
kind: PatchTransformer
|
||||||
|
metadata:
|
||||||
|
name: notImportantHere
|
||||||
|
path: patch.yaml
|
||||||
|
target:
|
||||||
|
name: myDeploy
|
||||||
|
`, someDeploymentResources, func(t *testing.T, err error) {
|
||||||
|
t.Helper()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPatchTransformerPatchEmptyOnlyComments(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
|
PrepBuiltin("PatchTransformer")
|
||||||
|
defer th.Reset()
|
||||||
|
|
||||||
|
th.WriteF("patch.yaml", `
|
||||||
|
# File with only comments
|
||||||
|
|
||||||
|
# Is a virtually empty yaml
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.RunTransformerAndCheckError(`
|
||||||
|
apiVersion: builtin
|
||||||
|
kind: PatchTransformer
|
||||||
|
metadata:
|
||||||
|
name: notImportantHere
|
||||||
|
path: patch.yaml
|
||||||
|
target:
|
||||||
|
name: myDeploy
|
||||||
|
`, someDeploymentResources, func(t *testing.T, err error) {
|
||||||
|
t.Helper()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user