From f72cb215d70786bc0068b0f5f64ca2be3adbc2a9 Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Fri, 9 Dec 2022 22:45:55 +0530 Subject: [PATCH] Adjust Content Line --- core/pkg/fixhandler/fixhandler_test.go | 4 +-- core/pkg/fixhandler/yamlhandler.go | 38 +++++++++++++++++++------- core/pkg/fixhandler/yamlhelper.go | 1 + 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 59603a49..57155560 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -65,7 +65,7 @@ func testDirectoryApplyFixHelper(t *testing.T, yamlExpressions *[]string, direct // make changes to temp file h, _ := NewFixHandlerMock() - err = h.applyFixToFile(tempFile.Name(), (*yamlExpressions)[scenario]) + err = h.applyFixToFile(tempFile.Name(), (*yamlExpressions)[scenario-1]) assert.NoError(t, err) // Check temp file contents @@ -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, tempFileContent, fixedFileContent, errorMessage) + assert.Equal(t, string(tempFileContent), string(fixedFileContent), errorMessage) } } diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index f51b16d2..ab7efeb8 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -184,7 +184,21 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { return updatedOriginalTracker, updatedFixedTracker } -func applyFixesToFile(filePath string, lineAndContentsToAdd *[]ContentToAdd, linesToRemove *[]ContentToRemove, contentAtHead string) error { +// Line numbers are readjusted such that there are no empty lines or comment lines before them +func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) { + for contentIdx, content := range *contentToAdd { + line := content.Line + for idx := line - 1; idx >= 0; idx-- { + if isEmptyLineOrComment((*linesSlice)[idx]) { + (*contentToAdd)[contentIdx].Line -= 1 + } else { + break + } + } + } +} + +func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]ContentToRemove, contentAtHead string) error { linesSlice, err := getLinesSlice(filePath) if err != nil { @@ -210,32 +224,36 @@ func applyFixesToFile(filePath string, lineAndContentsToAdd *[]ContentToAdd, lin removeLines(linesToRemove, &linesSlice) writer := bufio.NewWriter(file) - lineIdx, lineToAddIdx := 0, 0 + lineIdx, lineToAddIdx := 1, 0 // Insert the comments and lines at the head removed initially. writer.WriteString(contentAtHead) - for lineToAddIdx < len(*lineAndContentsToAdd) { - for lineIdx <= (*lineAndContentsToAdd)[lineToAddIdx].Line { - if linesSlice[lineIdx] == "*" { + // 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) + + for lineToAddIdx < len(*contentToAdd) { + for lineIdx <= (*contentToAdd)[lineToAddIdx].Line { + if linesSlice[lineIdx-1] == "*" { continue } - _, err := writer.WriteString(linesSlice[lineIdx] + "\n") + _, err := writer.WriteString(linesSlice[lineIdx-1] + "\n") if err != nil { return err } lineIdx += 1 } - writeContentToAdd(writer, (*lineAndContentsToAdd)[lineToAddIdx].Content) + writeContentToAdd(writer, (*contentToAdd)[lineToAddIdx].Content) lineToAddIdx += 1 } - for lineIdx < len(linesSlice) { - if linesSlice[lineIdx] == "*" { + for lineIdx <= len(linesSlice) { + if linesSlice[lineIdx-1] == "*" { continue } - _, err := writer.WriteString(linesSlice[lineIdx] + "\n") + _, err := writer.WriteString(linesSlice[lineIdx-1] + "\n") if err != nil { return err } diff --git a/core/pkg/fixhandler/yamlhelper.go b/core/pkg/fixhandler/yamlhelper.go index 60b9135c..8e1ecb96 100644 --- a/core/pkg/fixhandler/yamlhelper.go +++ b/core/pkg/fixhandler/yamlhelper.go @@ -231,6 +231,7 @@ func removeLines(linesToRemove *[]ContentToRemove, linesSlice *[]string) { // Checks if the line is empty or a comment func isEmptyLineOrComment(lineContent string) bool { + lineContent = strings.TrimSpace(lineContent) if lineContent == "" { return true } else if lineContent[0:1] == "#" {