From 1340d1bd9d65f9cc257ffff7535fbe322153bb60 Mon Sep 17 00:00:00 2001 From: wandalen Date: Fri, 11 Apr 2025 12:44:57 +0300 Subject: [PATCH 1/2] [sed]: requested adjustments --- text/sed.rs | 24 +++++++++++++++++++++++- text/tests/sed/mod.rs | 14 ++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/text/sed.rs b/text/sed.rs index 7c4376c98..6e3a78361 100644 --- a/text/sed.rs +++ b/text/sed.rs @@ -25,6 +25,8 @@ use std::{ path::PathBuf, }; +const DEFAULT_TMP_DIR: &str = "../target/tmp"; + static ERE: Mutex = Mutex::new(false); #[derive(Parser, Debug, Clone)] @@ -1346,7 +1348,7 @@ fn print_multiline_binary(line: &str) { } } } - } else if let Some(line) = line.strip_suffix(&['\n']) { + } else if let Some(line) = line.strip_suffix(['\n']) { println!("{}$", line); } else { print!("{}$", line); @@ -1416,6 +1418,16 @@ fn filter_comments(raw_script: impl AsRef) -> String { raw_script_without_comments } +/// Get path to [`wfile`] in tmp dir +fn get_tmp_path(wfile: PathBuf) -> PathBuf { + let mut tmp_path = + PathBuf::from(std::env::var("CARGO_TARGET_TMPDIR").unwrap_or(DEFAULT_TMP_DIR.to_string())); + + tmp_path.extend(&wfile); + + tmp_path +} + /// Contains [`Command`] sequence of all [`Sed`] session /// that applied all to every line of input files #[derive(Debug)] @@ -1821,6 +1833,11 @@ fn execute_replace( Some(wfile) }) { if replace && wfile.components().next().is_some() { + let mut wfile = wfile.clone(); + if (cfg!(test) || cfg!(debug_assertions)) && !wfile.exists() { + wfile = get_tmp_path(wfile); + } + if let Ok(mut file) = std::fs::OpenOptions::new() .append(true) .create(true) @@ -2213,6 +2230,11 @@ impl Sed { } fn execute_w(&mut self, wfile: PathBuf) -> Result<(), SedError> { + let mut wfile = wfile.clone(); + if (cfg!(test) || cfg!(debug_assertions)) && !wfile.exists() { + wfile = get_tmp_path(wfile); + } + let _ = match std::fs::OpenOptions::new() .append(true) .create(true) diff --git a/text/tests/sed/mod.rs b/text/tests/sed/mod.rs index 8f4048d51..83ac08213 100644 --- a/text/tests/sed/mod.rs +++ b/text/tests/sed/mod.rs @@ -1825,23 +1825,29 @@ mod tests { fn test_w() { let test_data = [ // correct - ("w ./tests/sed/assets/newfile", "abc\ncdf\n", "abc\ncdf\n", ""), + ("w ./newfile", "abc\ncdf\n", "abc\ncdf\n", ""), ("w atyfv", "abc\ncdf\n", "abc\ncdf\n", ""), ("w./tests/sed/assets/r", "", "", ""), - ("w./tests/sed/assets/newfile", "a\n", "a\n", ""), + ("w./newfile", "a\n", "a\n", ""), ("w ; h", "abc\ncdf\n", "abc\ncdf\n", ""), // wrong + ( + "w ./dir/newfile", + "abc\ncdf\n", + "", + "sed: read stdin: can't find '../target/tmp/./dir/newfile': no such file or directory (os error 2)\n", + ), ( "w./tests/s\x04ed/assets/abc", "a\n", "", - "sed: read stdin: can't find './tests/s\u{4}ed/assets/abc': no such file or directory (os error 2)\n", + "sed: read stdin: can't find '../target/tmp/./tests/s\u{4}ed/assets/abc': no such file or directory (os error 2)\n", ), ( "w./tests/ard/assets/abc", "a\n", "", - "sed: read stdin: can't find './tests/ard/assets/abc': no such file or directory (os error 2)\n", + "sed: read stdin: can't find '../target/tmp/./tests/ard/assets/abc': no such file or directory (os error 2)\n", ), ]; From 315c8e451121cfed62050ac184f70a30e55f5edb Mon Sep 17 00:00:00 2001 From: wandalen Date: Sat, 12 Apr 2025 11:00:37 +0300 Subject: [PATCH 2/2] [sed]: fix regression --- text/sed.rs | 12 ++++++++++-- text/tests/sed/mod.rs | 9 ++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/text/sed.rs b/text/sed.rs index 6e3a78361..0d3feef16 100644 --- a/text/sed.rs +++ b/text/sed.rs @@ -1834,7 +1834,11 @@ fn execute_replace( }) { if replace && wfile.components().next().is_some() { let mut wfile = wfile.clone(); - if (cfg!(test) || cfg!(debug_assertions)) && !wfile.exists() { + if !(wfile.is_absolute() + || wfile.starts_with("./") + || wfile.starts_with("../") + || wfile.exists()) + { wfile = get_tmp_path(wfile); } @@ -2231,7 +2235,11 @@ impl Sed { fn execute_w(&mut self, wfile: PathBuf) -> Result<(), SedError> { let mut wfile = wfile.clone(); - if (cfg!(test) || cfg!(debug_assertions)) && !wfile.exists() { + if !(wfile.is_absolute() + || wfile.starts_with("./") + || wfile.starts_with("../") + || wfile.exists()) + { wfile = get_tmp_path(wfile); } diff --git a/text/tests/sed/mod.rs b/text/tests/sed/mod.rs index 83ac08213..c0aff379b 100644 --- a/text/tests/sed/mod.rs +++ b/text/tests/sed/mod.rs @@ -1825,29 +1825,28 @@ mod tests { fn test_w() { let test_data = [ // correct - ("w ./newfile", "abc\ncdf\n", "abc\ncdf\n", ""), ("w atyfv", "abc\ncdf\n", "abc\ncdf\n", ""), ("w./tests/sed/assets/r", "", "", ""), - ("w./newfile", "a\n", "a\n", ""), + ("w newfile", "a\n", "a\n", ""), ("w ; h", "abc\ncdf\n", "abc\ncdf\n", ""), // wrong ( "w ./dir/newfile", "abc\ncdf\n", "", - "sed: read stdin: can't find '../target/tmp/./dir/newfile': no such file or directory (os error 2)\n", + "sed: read stdin: can't find './dir/newfile': no such file or directory (os error 2)\n", ), ( "w./tests/s\x04ed/assets/abc", "a\n", "", - "sed: read stdin: can't find '../target/tmp/./tests/s\u{4}ed/assets/abc': no such file or directory (os error 2)\n", + "sed: read stdin: can't find './tests/s\u{4}ed/assets/abc': no such file or directory (os error 2)\n", ), ( "w./tests/ard/assets/abc", "a\n", "", - "sed: read stdin: can't find '../target/tmp/./tests/ard/assets/abc': no such file or directory (os error 2)\n", + "sed: read stdin: can't find './tests/ard/assets/abc': no such file or directory (os error 2)\n", ), ];