summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanny Mösch <danny.moesch@icloud.com>2020-04-10 23:39:02 +0200
committerGitHub <noreply@github.com>2020-04-10 17:39:02 -0400
commitc8022e292f3ab2686843312f9c0a76987c3f1130 (patch)
tree6ac1dfdfe6d734d4313db568beab2a22133e9470
parentfd55fa098248605c36f30643abfb4e84d5794821 (diff)
Fix #68: Do not write empty content to file (#71)v0.7.3
-rw-r--r--src/input.rs8
-rw-r--r--tests/cli.rs17
2 files changed, 20 insertions, 5 deletions
diff --git a/src/input.rs b/src/input.rs
index 28adaf2..8a785d2 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -113,9 +113,11 @@ impl Replacer {
file.set_len(replaced.len() as u64)?;
file.set_permissions(meta.permissions())?;
- let mut mmap_target = unsafe { MmapMut::map_mut(&file)? };
- mmap_target.deref_mut().write_all(&replaced)?;
- mmap_target.flush_async()?;
+ if !replaced.is_empty() {
+ let mut mmap_target = unsafe { MmapMut::map_mut(&file)? };
+ mmap_target.deref_mut().write_all(&replaced)?;
+ mmap_target.flush_async()?;
+ }
drop(mmap_source);
drop(source);
diff --git a/tests/cli.rs b/tests/cli.rs
index 420eca4..7d95411 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -25,13 +25,26 @@ fn in_place() -> Result<()> {
}
#[test]
+fn in_place_with_empty_result_file() -> Result<()> {
+ let mut file = tempfile::NamedTempFile::new()?;
+ file.write(b"a7c")?;
+ let path = file.into_temp_path();
+
+ sd().args(&["a\\dc", "", path.to_str().unwrap()])
+ .assert()
+ .success();
+ assert_file(&path.to_path_buf(), "");
+
+ Ok(())
+}
+
+#[test]
fn replace_into_stdout() -> Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
file.write(b"abc123def")?;
#[rustfmt::skip]
- sd()
- .args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
+ sd().args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
.assert()
.success()
.stdout("def");