fix: convert CRLF to LF when comparing files

This commit is contained in:
umarcor 2019-03-18 21:12:13 +01:00
parent 7a5404953c
commit c0ff9f1c25

View file

@ -17,6 +17,11 @@ func init() {
initCmd.SetOutput(new(bytes.Buffer)) initCmd.SetOutput(new(bytes.Buffer))
} }
// ensureLF converts any \r\n to \n
func ensureLF(content []byte) []byte {
return bytes.Replace(content, []byte("\r\n"), []byte("\n"), -1)
}
// compareFiles compares the content of files with pathA and pathB. // compareFiles compares the content of files with pathA and pathB.
// If contents are equal, it returns nil. // If contents are equal, it returns nil.
// If not, it returns which files are not equal // If not, it returns which files are not equal
@ -30,7 +35,7 @@ func compareFiles(pathA, pathB string) error {
if err != nil { if err != nil {
return err return err
} }
if !bytes.Equal(contentA, contentB) { if !bytes.Equal(ensureLF(contentA), ensureLF(contentB)) {
output := new(bytes.Buffer) output := new(bytes.Buffer)
output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", pathA, pathB)) output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", pathA, pathB))