Skip to content

Commit

Permalink
Fix line endings (#19)
Browse files Browse the repository at this point in the history
* Setup test files

* Remove ending from file

* Fix file contents

* Update file contents

* Update test file contents

* Update test file contents

* Add test grep file

* WIP Setup test

* Setup write line endings test

* Work on using paths to get file names

* Figure out copying to directory

* Remove printing path

* Add running the replace

* Make separate file names

* Work on checking for EOL

* Print line ending check results

* Write failing end of line test

* Write new line if it exists
  • Loading branch information
robenkleene committed Jul 2, 2023
1 parent 198125e commit baea01e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ impl<'a> Writer<'a> {
let lines = mmap_source.lines()
.map(|l| l.expect("Error getting line"))
.collect();
let replaced = match self.patcher.patch(lines) {
let mut replaced = match self.patcher.patch(lines) {
Ok(replaced) => replaced,
Err(_) => panic!("Error patching lines"), // FIXME:
};
if mmap_source.ends_with("\n".as_bytes()) {
replaced.push_str("\n");
}

let target = tempfile::NamedTempFile::new_in(
self.path.parent()
Expand Down
50 changes: 50 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ mod cli {
use anyhow::Result;
use assert_cmd::Command;
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::io::Read;
use std::io::Seek;

fn rep() -> Command {
Command::cargo_bin("rep").expect("Error invoking rep")
Expand Down Expand Up @@ -62,4 +66,50 @@ mod cli {
.stdout(result);
Ok(())
}

#[test]
fn write_line_endings() -> Result<()> {
let input = fs::read_to_string("tests/data/line-endings/grep.txt").expect("Error reading input");
let ending_path = Path::new("tests/data/line-endings/ending.txt");
let noending_path = Path::new("tests/data/line-endings/noending.txt");
let ending_file_name = ending_path.file_name().expect("Error getting filename");
let noending_file_name = noending_path.file_name().expect("Error getting filename");
let tmp_dir = tempfile::tempdir()?;
let tmp_dir_path = tmp_dir.path();
let ending_path_dst = tmp_dir_path.join(ending_file_name);
let noending_path_dst = tmp_dir_path.join(noending_file_name);
fs::copy(ending_path, &ending_path_dst).expect("Error copying file");
fs::copy(noending_path, &noending_path_dst).expect("Error copying file");
rep()
.current_dir(tmp_dir_path)
.write_stdin(input)
.args(&["foo", "bar", "-w"])
.assert()
.success();
fn has_eol(path: &PathBuf) -> std::io::Result<bool> {
let mut file = File::open(&path)?;
let mut buffer = [0; 1];

match file.seek(std::io::SeekFrom::End(-1)) {
Ok(_) => (),
// Empty file
Err(_) => return Ok(false),
}

file.read(&mut buffer[..])?;
if buffer == [b'\n'] {
return Ok(true);
};
Ok(false)
}
let ending_result = has_eol(&ending_path.to_path_buf())?;
let noending_result = has_eol(&noending_path.to_path_buf())?;
let ending_dst_result = has_eol(&ending_path_dst.to_path_buf())?;
let noending_dst_result = has_eol(&noending_path_dst.to_path_buf())?;
assert!(ending_result);
assert!(!noending_result);
assert_eq!(ending_result, ending_dst_result);
assert_eq!(noending_result, noending_dst_result);
Ok(())
}
}
5 changes: 5 additions & 0 deletions tests/data/line-endings/ending.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
2 changes: 2 additions & 0 deletions tests/data/line-endings/grep.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
noending.txt:1:foo
ending.txt:1:foo
5 changes: 5 additions & 0 deletions tests/data/line-endings/noending.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0 comments on commit baea01e

Please sign in to comment.