Fixed: Comments and empty lines at the head are excluded

This commit is contained in:
suhasgumma
2022-12-09 18:55:31 +05:30
parent 48516b891f
commit fa03a9dae3
4 changed files with 71 additions and 4 deletions

View File

@@ -216,13 +216,22 @@ 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 := getDecodedYaml(filePath)
fixedYamlNode := getFixedYamlNode(filePath, yamlExpression)
originalList := getDFSOrder(originalYamlNode)
fixedList := getDFSOrder(fixedYamlNode)
contentToAdd, linesToRemove := getFixInfo(originalList, fixedList)
err := applyFixesToFile(filePath, contentToAdd, linesToRemove)
err = applyFixesToFile(filePath, contentToAdd, linesToRemove, contentAtHead)
return err
}

View File

@@ -77,7 +77,9 @@ func testDirectoryApplyFixHelper(t *testing.T, yamlExpressions *[]string, direct
// Get fixed Yaml file contents
fixedFileContent, err := ioutil.ReadFile(fixedFilePath)
assert.Equal(t, tempFileContent, fixedFileContent)
errorMessage := fmt.Sprintf("Content of fixed %s doesn't match content of %s in %s", originalFile, fixedFile, directoryPath)
assert.Equal(t, tempFileContent, fixedFileContent, errorMessage)
}
}

View File

@@ -184,7 +184,7 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) {
return updatedOriginalTracker, updatedFixedTracker
}
func applyFixesToFile(filePath string, lineAndContentsToAdd *[]ContentToAdd, linesToRemove *[]ContentToRemove) (cmdError error) {
func applyFixesToFile(filePath string, lineAndContentsToAdd *[]ContentToAdd, linesToRemove *[]ContentToRemove, contentAtHead string) error {
linesSlice, err := getLinesSlice(filePath)
if err != nil {
@@ -212,6 +212,9 @@ func applyFixesToFile(filePath string, lineAndContentsToAdd *[]ContentToAdd, lin
writer := bufio.NewWriter(file)
lineIdx, lineToAddIdx := 0, 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] == "*" {

View File

@@ -177,6 +177,7 @@ func enocodeIntoYaml(parentNode *yaml.Node, dfsOrder *[]NodeInfo, tracker int) (
}
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
encoder.SetIndent(2)
errorEncoding := encoder.Encode(parentForContent)
if errorEncoding != nil {
return "", fmt.Errorf("Error debugging node, %v", errorEncoding.Error())
@@ -238,13 +239,65 @@ func isEmptyLineOrComment(lineContent string) bool {
return false
}
// Truncates the comments and empty lines at the top of the file and
// returns the truncated content
func truncateContentAtHead(filePath string) (string, error) {
var contentAtHead string
linesSlice, err := getLinesSlice(filePath)
if err != nil {
return "", err
}
if err := os.Truncate(filePath, 0); err != nil {
return "", err
}
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
return "", err
}
defer func() error {
if err := file.Close(); err != nil {
return err
}
return nil
}()
lineIdx := 0
for lineIdx < len(linesSlice) {
if isEmptyLineOrComment(linesSlice[lineIdx]) {
contentAtHead += (linesSlice[lineIdx] + "\n")
lineIdx += 1
} else {
break
}
}
writer := bufio.NewWriter(file)
for lineIdx < len(linesSlice) {
_, err = writer.WriteString(linesSlice[lineIdx] + "\n")
if err != nil {
return "", err
}
lineIdx += 1
}
writer.Flush()
return contentAtHead, nil
}
// Get the lines of existing yaml in a slice
func getLinesSlice(filePath string) ([]string, error) {
lineSlice := make([]string, 0)
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
logger.L().Fatal(fmt.Sprintf("Cannot open file %s", filePath))
return nil, err
}
defer file.Close()