Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize "\r\n" to "\n" to ensure ^ and $ match line boundaries #92

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ proc-macro2 = { version = "1.0", features = ["span-locations"] }
toml = "0.5"
url = "2.0"
regex = { version = "1.3", default-features = false, features = ["std", "unicode"] }

[dev-dependencies]
tempfile = "3.1"
18 changes: 18 additions & 0 deletions src/contains_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,22 @@ mod tests {
)
}

#[test]
fn line_boundaries() {
// The regex crate doesn't treat \r\n as a line boundary
// (https://github.com/rust-lang/regex/issues/244), so
// version-sync makes sure to normalize \r\n to \n when
// reading files.
use std::io::Write;
let mut file = tempfile::NamedTempFile::new().unwrap();

println!("Path: {}", file.path().to_str().unwrap());

file.write_all(b"first line\r\nsecond line\r\nthird line\r\n")
.unwrap();
assert_eq!(
check_contains_regex(file.path().to_str().unwrap(), "^second line$", "", ""),
Ok(())
)
}
}
6 changes: 4 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ where
buf
}

/// Return all data from `path`.
/// Return all data from `path`. Line boundaries are normalized from
/// "\r\n" to "\n" to make sure "^" and "$" will match them. See
/// https://github.com/rust-lang/regex/issues/244 for details.
pub fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path)?;
let mut buf = String::new();
file.read_to_string(&mut buf)?;
Ok(buf)
Ok(buf.replace("\r\n", "\n"))
}

/// Indent every line in text by four spaces.
Expand Down