diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index ec00a975..4a38df56 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -217,13 +217,6 @@ func (h *FixHandler) getFilePathAndIndex(filePathWithIndex string) (filePath str func (h *FixHandler) applyFixToFile(filePath, yamlExpression string) (cmdError error) { - // While obtaining fixedYamlNode, comments and empty lines at the top are ignored. In order to deal with that, - // comments and empty lines are removed and they are inserted again when applying fixes to file. - contentAtHead, err := truncateContentAtHead(filePath) - - if err != nil { - logger.L().Fatal("Error truncating comments and empty lines at head") - } originalYamlNode := constructDecodedYaml(filePath) fixedYamlNode := constructFixedYamlNode(filePath, yamlExpression) @@ -232,7 +225,7 @@ func (h *FixHandler) applyFixToFile(filePath, yamlExpression string) (cmdError e contentToAdd, linesToRemove := getFixInfo(originalList, fixedList) - err = applyFixesToFile(filePath, contentToAdd, linesToRemove, contentAtHead) + err := applyFixesToFile(filePath, contentToAdd, linesToRemove) return err } diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index b8b08028..06cbaab0 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -79,7 +79,7 @@ func testDirectoryApplyFixHelper(t *testing.T, yamlExpressions *[]string, direct errorMessage := fmt.Sprintf("Content of fixed %s doesn't match content of %s in %s", originalFile, fixedFile, directoryPath) - assert.Equal(t, string(tempFileContent), string(fixedFileContent), errorMessage) + assert.Equal(t, string(fixedFileContent), string(tempFileContent), errorMessage) } } diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index d52f4ea6..a129a3f8 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -88,6 +88,12 @@ func matchNodes(nodeOne, nodeTwo *yaml.Node) int { } func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]LinesToRemove) { + + // While obtaining fixedYamlNode, comments and empty lines at the top are ignored. + // This causes a difference in Line numbers across the tree structure. In order to + // counter this, line numbers are adjusted in fixed list. + adjustFixedListLines(originalList, fixedList) + contentToAdd := make([]ContentToAdd, 0) linesToRemove := make([]LinesToRemove, 0) @@ -209,13 +215,14 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { return updatedOriginalTracker, updatedFixedTracker } -func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]LinesToRemove, contentAtHead string) error { +func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]LinesToRemove) error { + // Read contents of the file line by line and store in a list linesSlice, err := getLinesSlice(filePath) if err != nil { return err } - + // Clear the current content of file if err := os.Truncate(filePath, 0); err != nil { return err } @@ -237,9 +244,6 @@ func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemo writer := bufio.NewWriter(file) lineIdx, lineToAddIdx := 1, 0 - // Insert the comments and lines at the head removed initially. - writer.WriteString(contentAtHead) - // Ideally, new node is inserted at line before the next node in DFS order. But, when the previous line contains a // comment or empty line, we need to insert new nodes before them. adjustContentLines(contentToAdd, &linesSlice) diff --git a/core/pkg/fixhandler/yamlhelper.go b/core/pkg/fixhandler/yamlhelper.go index 502c48a1..85e674fb 100644 --- a/core/pkg/fixhandler/yamlhelper.go +++ b/core/pkg/fixhandler/yamlhelper.go @@ -45,6 +45,24 @@ func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) { } } +func adjustFixedListLines(originalList, fixedList *[]NodeInfo) { + differenceAtTop := (*originalList)[0].node.Line - (*fixedList)[0].node.Line + + if differenceAtTop <= 0 { + return + } + + for _, node := range *fixedList { + // Line numbers should not be changed for new nodes. + if node.node.Line != 0 { + node.node.Line += differenceAtTop + } + } + + return + +} + func constructDFSOrderHelper(node *yaml.Node, parent *yaml.Node, dfsOrder *[]NodeInfo, index int) { dfsNode := NodeInfo{ node: node,