mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
fix: convert CRLF to LF when comparing files
This commit is contained in:
parent
7a5404953c
commit
c0ff9f1c25
1 changed files with 6 additions and 1 deletions
|
@ -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))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue