From: Shohei Wada Date: Fri, 6 Oct 2017 16:53:55 +0000 (+0900) Subject: Fix the test framework is failing in CRLF env X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=fe1619b548f0fd689653e5f03281264239035393;p=rust.git Fix the test framework is failing in CRLF env --- diff --git a/tests/system.rs b/tests/system.rs index 1bf85a4d0c1..a633c42d722 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -17,7 +17,9 @@ use std::collections::HashMap; use std::fs; use std::io::{self, BufRead, BufReader, Read}; +use std::iter::Peekable; use std::path::{Path, PathBuf}; +use std::str::Chars; use rustfmt::*; use rustfmt::filemap::{write_system_newlines, FileMap}; @@ -369,7 +371,8 @@ fn handle_result( let read_error = format!("Failed reading target {:?}", &target); f.read_to_string(&mut text).expect(&read_error); - if fmt_text != text { + // Ignore LF and CRLF difference for Windows. + if !string_eq_ignore_newline_repr(&fmt_text, &text) { let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE); assert!( !diff.is_empty(), @@ -430,3 +433,38 @@ fn rustfmt_diff_no_diff_test() { let diff = make_diff("a\nb\nc\nd", "a\nb\nc\nd", 3); assert_eq!(diff, vec![]); } + +// Compare strings without distinguishing between CRLF and LF +fn string_eq_ignore_newline_repr(left: &str, right: &str) -> bool { + let left = CharsIgnoreNewlineRepr(left.chars().peekable()); + let right = CharsIgnoreNewlineRepr(right.chars().peekable()); + left.eq(right) +} + +struct CharsIgnoreNewlineRepr<'a>(Peekable>); + +impl<'a> Iterator for CharsIgnoreNewlineRepr<'a> { + type Item = char; + fn next(&mut self) -> Option { + self.0.next().map(|c| if c == '\r' { + if *self.0.peek().unwrap_or(&'\0') == '\n' { + self.0.next(); + '\n' + } else { + '\r' + } + } else { + c + }) + } +} + +#[test] +fn string_eq_ignore_newline_repr_test() { + assert!(string_eq_ignore_newline_repr("", "")); + assert!(!string_eq_ignore_newline_repr("", "abc")); + assert!(!string_eq_ignore_newline_repr("abc", "")); + assert!(string_eq_ignore_newline_repr("a\nb\nc\rd", "a\nb\r\nc\rd")); + assert!(string_eq_ignore_newline_repr("a\r\n\r\n\r\nb", "a\n\n\nb")); + assert!(!string_eq_ignore_newline_repr("a\r\nbcd", "a\nbcdefghijk")); +}