Skip to content

Commit 31b3754

Browse files
authored
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 <[email protected]> * Add t.helper() at start of test function Signed-off-by: Julio Chana <[email protected]> --------- Signed-off-by: Julio Chana <[email protected]>
1 parent 2643c51 commit 31b3754

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

api/internal/builtins/PatchTransformer.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/builtin/patchtransformer/PatchTransformer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error {
5959
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
6060
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))
6161

62-
if (errSM == nil && errJson == nil) ||
63-
(patchesSM != nil && patchesJson != nil) {
62+
if ((errSM == nil && errJson == nil) ||
63+
(patchesSM != nil && patchesJson != nil)) &&
64+
(len(patchesSM) > 0 && len(patchesJson) > 0) {
6465
return fmt.Errorf(
6566
"illegally qualifies as both an SM and JSON patch: %s",
6667
p.patchSource)

plugin/builtin/patchtransformer/PatchTransformer_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,3 +1108,54 @@ spec:
11081108
name: test-deployment
11091109
`)
11101110
}
1111+
1112+
func TestPatchTransformerPatchEmpty(t *testing.T) {
1113+
th := kusttest_test.MakeEnhancedHarness(t).
1114+
PrepBuiltin("PatchTransformer")
1115+
defer th.Reset()
1116+
1117+
th.WriteF("patch.yaml", `
1118+
`)
1119+
1120+
th.RunTransformerAndCheckError(`
1121+
apiVersion: builtin
1122+
kind: PatchTransformer
1123+
metadata:
1124+
name: notImportantHere
1125+
path: patch.yaml
1126+
target:
1127+
name: myDeploy
1128+
`, someDeploymentResources, func(t *testing.T, err error) {
1129+
t.Helper()
1130+
if err != nil {
1131+
t.Fatalf("unexpected error")
1132+
}
1133+
})
1134+
}
1135+
1136+
func TestPatchTransformerPatchEmptyOnlyComments(t *testing.T) {
1137+
th := kusttest_test.MakeEnhancedHarness(t).
1138+
PrepBuiltin("PatchTransformer")
1139+
defer th.Reset()
1140+
1141+
th.WriteF("patch.yaml", `
1142+
# File with only comments
1143+
1144+
# Is a virtually empty yaml
1145+
`)
1146+
1147+
th.RunTransformerAndCheckError(`
1148+
apiVersion: builtin
1149+
kind: PatchTransformer
1150+
metadata:
1151+
name: notImportantHere
1152+
path: patch.yaml
1153+
target:
1154+
name: myDeploy
1155+
`, someDeploymentResources, func(t *testing.T, err error) {
1156+
t.Helper()
1157+
if err != nil {
1158+
t.Fatalf("unexpected error")
1159+
}
1160+
})
1161+
}

0 commit comments

Comments
 (0)