From c8df4fa46b4695b3bf7402b28d4f22940335149e Mon Sep 17 00:00:00 2001 From: hetdagli234 Date: Fri, 16 Aug 2024 00:30:43 +0530 Subject: [PATCH] Handling both EOF and EOL cases --- crates/client/src/test_generator.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/client/src/test_generator.rs b/crates/client/src/test_generator.rs index c4792f51..6c07f794 100644 --- a/crates/client/src/test_generator.rs +++ b/crates/client/src/test_generator.rs @@ -698,10 +698,20 @@ impl TestGenerator { return; } } + // Check if the file ends with a newline + let mut file = File::open(&gitignore_path)?; + let mut buf = [0; 1]; + file.seek(io::SeekFrom::End(-1))?; + file.read_exact(&mut buf)?; + let file = OpenOptions::new().append(true).open(gitignore_path); if let Ok(mut file) = file { - writeln!(file, "\n{}", ignored_path)?; + if buf[0] == b'\n' { + writeln!(file, "{}", ignored_path)?; + } else { + writeln!(file, "\n{}", ignored_path)?; + } println!("{FINISH} [{GIT_IGNORE}] update with [{ignored_path}]"); } } else {