From 38d26960586bdb3e851d4705275ecc08613b68fe Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Thu, 15 Dec 2022 19:14:52 +0530 Subject: [PATCH] Break updateFileFixInfo function into getResourceFileFix and addResourceFileFix functions --- core/pkg/fixhandler/fixhandler.go | 25 ++++++++++++++----------- core/pkg/fixhandler/fixhandler_test.go | 7 ++++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index 4be4d682..3aabf011 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -193,15 +193,15 @@ func (h *FixHandler) ApplyChanges(resourcesToFix []ResourceFixInfo) (int, []erro resourceFilePath := resourceToFix.FilePath if _, pathExistsInMap := filePathFixInfo[resourceFilePath]; !pathExistsInMap { - contentToAdd := make([]contentToAdd, 0) - linesToRemove := make([]linesToRemove, 0) filePathFixInfo[resourceFilePath] = &fileFixInfo{ - contentToAdd: contentToAdd, - linesToRemove: linesToRemove, + contentToAdd: make([]contentToAdd, 0), + linesToRemove: make([]linesToRemove, 0), } } - if err := h.updateFileFixInfo(resourceFilePath, singleExpression, resourceToFix.DocumentIndex, filePathFixInfo[resourceFilePath]); err != nil { + contentsToAdd, linesToRemove, err := h.getResourceFileFix(resourceFilePath, singleExpression, resourceToFix.DocumentIndex) + + if err != nil { errors = append(errors, fmt.Errorf("failed to fix resource [Name: '%s', Kind: '%s'] in '%s': %w ", resourceToFix.Resource.GetName(), @@ -209,6 +209,7 @@ func (h *FixHandler) ApplyChanges(resourcesToFix []ResourceFixInfo) (int, []erro resourceToFix.FilePath, err)) } else { + h.addResourceFileFix(contentsToAdd, linesToRemove, filePathFixInfo[resourceFilePath]) updatedFiles[resourceToFix.FilePath] = true } } @@ -235,11 +236,11 @@ func (h *FixHandler) getFilePathAndIndex(filePathWithIndex string) (filePath str } } -func (h *FixHandler) updateFileFixInfo(filePath string, yamlExpression string, documentIdx int, fileFixInfo *fileFixInfo) error { +func (h *FixHandler) getResourceFileFix(filePath string, yamlExpression string, documentIdx int) (*[]contentToAdd, *[]linesToRemove, error) { originalYamlNode := (*constructDecodedYaml(filePath))[documentIdx] fixedYamlNodes, err := constructFixedYamlNodes(filePath, yamlExpression) if err != nil { - return err + return nil, nil, err } fixedYamlNode := (*fixedYamlNodes)[documentIdx] @@ -249,16 +250,18 @@ func (h *FixHandler) updateFileFixInfo(filePath string, yamlExpression string, d contentsToAdd, linesToRemove := getFixInfo(originalList, fixedList) - for _, content := range *contentsToAdd { + return contentsToAdd, linesToRemove, nil + +} + +func (h *FixHandler) addResourceFileFix(contentToAdd *[]contentToAdd, linesToRemove *[]linesToRemove, fileFixInfo *fileFixInfo) { + for _, content := range *contentToAdd { fileFixInfo.addContent(content) } for _, lines := range *linesToRemove { fileFixInfo.addLinesToRemove(lines) } - - return nil - } func (h *FixHandler) applyFixToFiles(filePathFixInfo map[string]*fileFixInfo) error { diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 59552aea..b7ef9e03 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -75,7 +75,12 @@ func testDirectoryApplyFixHelper(t *testing.T, yamlExpressions *[][]string, dire fixInfo := filePathFixInfo[filePath] for idx, yamlExpression := range (*yamlExpressions)[scenario-1] { - h.updateFileFixInfo(filePath, yamlExpression, idx, fixInfo) + contentToAdd, linesToRemove, err := h.getResourceFileFix(filePath, yamlExpression, idx) + + if err == nil { + h.addResourceFileFix(contentToAdd, linesToRemove, fixInfo) + } + } err = h.applyFixToFiles(filePathFixInfo)