diff --git a/pkg/patch/strategicmerge.go b/pkg/patch/strategicmerge.go index 9042eb844..deec8980c 100644 --- a/pkg/patch/strategicmerge.go +++ b/pkg/patch/strategicmerge.go @@ -35,3 +35,20 @@ func Exist(patches []types.PatchStrategicMerge, path string) bool { } return false } + +// Delete deletes patches from a PatchStrategicMerge slice +func Delete(patches []types.PatchStrategicMerge, paths ...string) []types.PatchStrategicMerge { + // Convert paths into PatchStrategicMerge slice + convertedPath := make([]types.PatchStrategicMerge, len(paths)) + for i, p := range paths { + convertedPath[i] = types.PatchStrategicMerge(p) + } + + filteredPatches := make([]types.PatchStrategicMerge, 0, len(patches)) + for _, containedPatch := range patches { + if !Exist(convertedPath, string(containedPatch)) { + filteredPatches = append(filteredPatches, containedPatch) + } + } + return filteredPatches +} diff --git a/pkg/patch/strategicmerge_test.go b/pkg/patch/strategicmerge_test.go new file mode 100644 index 000000000..2c517df51 --- /dev/null +++ b/pkg/patch/strategicmerge_test.go @@ -0,0 +1,95 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package patch + +import ( + "sigs.k8s.io/kustomize/v3/pkg/types" + "testing" +) + +func buildPatchStrategicMergeSlice(patchStrings []string) []types.PatchStrategicMerge { + var patches []types.PatchStrategicMerge + for _, patchString := range patchStrings { + patches = append(patches, types.PatchStrategicMerge(patchString)) + } + return patches +} + +func TestAppend(t *testing.T) { + patchStrings := []string{"patch1.yaml", "patch2.yaml"} + patches := buildPatchStrategicMergeSlice(patchStrings) + + patches = Append(patches, "patch3.yaml") + + for i, k := range []string{"patch1.yaml", "patch2.yaml", "patch3.yaml"} { + if patches[i] != types.PatchStrategicMerge(k) { + t.Fatalf("patches[%d] must be %s, got %s", i, k, patches[i]) + } + } +} + +func TestExistTrue(t *testing.T) { + patchStrings := []string{"patch1.yaml", "patch2.yaml"} + patches := buildPatchStrategicMergeSlice(patchStrings) + + for _, patchString := range patchStrings { + if !Exist(patches, patchString) { + t.Fatalf("%s must exist", patchString) + } + } +} + +func TestExistFalse(t *testing.T) { + patchStrings := []string{"patch1.yaml", "patch2.yaml"} + patches := buildPatchStrategicMergeSlice(patchStrings) + + for _, patchString := range []string{"invalid1.yaml", "invalid2.yaml"} { + if Exist(patches, patchString) { + t.Fatalf("%s must not exist", patchString) + } + } +} + +func TestDelete(t *testing.T) { + patchStrings := []string{"patch1.yaml", "patch2.yaml"} + patches := buildPatchStrategicMergeSlice(patchStrings) + + patches = Delete(patches, "patch1.yaml") + + if Exist(patches, "patch1.yaml") { + t.Fatalf("patch1.yaml should be deleted") + } + if !Exist(patches, "patch2.yaml") { + t.Fatalf("patch2.yaml should exist") + } + if len(patches) != 1 { + t.Fatalf("Length of slice must be 1: actual %d", len(patches)) + } +} + +func TestDeleteMultiple(t *testing.T) { + patchStrings := []string{"patch1.yaml", "patch2.yaml"} + patches := buildPatchStrategicMergeSlice(patchStrings) + + patches = Delete(patches, "patch2.yaml", "patch4.yaml", "patch1.yaml", "patch3.yaml") + + for _, k := range patchStrings { + if Exist(patches, k) { + t.Fatalf("%s should be deleted", k) + } + } +}