Skip to content

Commit 1340d1b

Browse files
committed
[sed]: requested adjustments
1 parent 9103232 commit 1340d1b

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

text/sed.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use std::{
2525
path::PathBuf,
2626
};
2727

28+
const DEFAULT_TMP_DIR: &str = "../target/tmp";
29+
2830
static ERE: Mutex<bool> = Mutex::new(false);
2931

3032
#[derive(Parser, Debug, Clone)]
@@ -1346,7 +1348,7 @@ fn print_multiline_binary(line: &str) {
13461348
}
13471349
}
13481350
}
1349-
} else if let Some(line) = line.strip_suffix(&['\n']) {
1351+
} else if let Some(line) = line.strip_suffix(['\n']) {
13501352
println!("{}$", line);
13511353
} else {
13521354
print!("{}$", line);
@@ -1416,6 +1418,16 @@ fn filter_comments(raw_script: impl AsRef<str>) -> String {
14161418
raw_script_without_comments
14171419
}
14181420

1421+
/// Get path to [`wfile`] in tmp dir
1422+
fn get_tmp_path(wfile: PathBuf) -> PathBuf {
1423+
let mut tmp_path =
1424+
PathBuf::from(std::env::var("CARGO_TARGET_TMPDIR").unwrap_or(DEFAULT_TMP_DIR.to_string()));
1425+
1426+
tmp_path.extend(&wfile);
1427+
1428+
tmp_path
1429+
}
1430+
14191431
/// Contains [`Command`] sequence of all [`Sed`] session
14201432
/// that applied all to every line of input files
14211433
#[derive(Debug)]
@@ -1821,6 +1833,11 @@ fn execute_replace(
18211833
Some(wfile)
18221834
}) {
18231835
if replace && wfile.components().next().is_some() {
1836+
let mut wfile = wfile.clone();
1837+
if (cfg!(test) || cfg!(debug_assertions)) && !wfile.exists() {
1838+
wfile = get_tmp_path(wfile);
1839+
}
1840+
18241841
if let Ok(mut file) = std::fs::OpenOptions::new()
18251842
.append(true)
18261843
.create(true)
@@ -2213,6 +2230,11 @@ impl Sed {
22132230
}
22142231

22152232
fn execute_w(&mut self, wfile: PathBuf) -> Result<(), SedError> {
2233+
let mut wfile = wfile.clone();
2234+
if (cfg!(test) || cfg!(debug_assertions)) && !wfile.exists() {
2235+
wfile = get_tmp_path(wfile);
2236+
}
2237+
22162238
let _ = match std::fs::OpenOptions::new()
22172239
.append(true)
22182240
.create(true)

text/tests/sed/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,23 +1825,29 @@ mod tests {
18251825
fn test_w() {
18261826
let test_data = [
18271827
// correct
1828-
("w ./tests/sed/assets/newfile", "abc\ncdf\n", "abc\ncdf\n", ""),
1828+
("w ./newfile", "abc\ncdf\n", "abc\ncdf\n", ""),
18291829
("w atyfv", "abc\ncdf\n", "abc\ncdf\n", ""),
18301830
("w./tests/sed/assets/r", "", "", ""),
1831-
("w./tests/sed/assets/newfile", "a\n", "a\n", ""),
1831+
("w./newfile", "a\n", "a\n", ""),
18321832
("w ; h", "abc\ncdf\n", "abc\ncdf\n", ""),
18331833
// wrong
1834+
(
1835+
"w ./dir/newfile",
1836+
"abc\ncdf\n",
1837+
"",
1838+
"sed: read stdin: can't find '../target/tmp/./dir/newfile': no such file or directory (os error 2)\n",
1839+
),
18341840
(
18351841
"w./tests/s\x04ed/assets/abc",
18361842
"a\n",
18371843
"",
1838-
"sed: read stdin: can't find './tests/s\u{4}ed/assets/abc': no such file or directory (os error 2)\n",
1844+
"sed: read stdin: can't find '../target/tmp/./tests/s\u{4}ed/assets/abc': no such file or directory (os error 2)\n",
18391845
),
18401846
(
18411847
"w./tests/ard/assets/abc",
18421848
"a\n",
18431849
"",
1844-
"sed: read stdin: can't find './tests/ard/assets/abc': no such file or directory (os error 2)\n",
1850+
"sed: read stdin: can't find '../target/tmp/./tests/ard/assets/abc': no such file or directory (os error 2)\n",
18451851
),
18461852
];
18471853

0 commit comments

Comments
 (0)