diff --git a/rust/bitbazaar/cli/mod.rs b/rust/bitbazaar/cli/mod.rs index c2c22254..8d0bc93a 100644 --- a/rust/bitbazaar/cli/mod.rs +++ b/rust/bitbazaar/cli/mod.rs @@ -25,11 +25,14 @@ mod tests { default_stdout_global_logging(tracing::Level::DEBUG).unwrap(); } + // Temp file: fn tf() -> String { // Using debug formatting to make sure escaped properly on windows: format!("{:?}", NamedTempFile::new().unwrap().path()) } + static GLOB_TD: Lazy = Lazy::new(|| tempfile::tempdir().unwrap()); + static HOME_DIR: Lazy = Lazy::new(|| { homedir::get_my_home() .unwrap() @@ -129,7 +132,7 @@ mod tests { // <-- redirection: // Write: #[case::redir_1(format!("echo foo > {fp} && rm {fp}", fp=tf()), "", 0, None, None, true)] - #[case::redir_2(format!("echo foo > {fp} && {CAT_CMD} {fp} && rm {fp}", fp=tf()), "foo", 0, None, None, true)] + #[case::redir_2(format!("cd {dp:?} && echo foo > file.txt && {CAT_CMD} file.txt", dp=GLOB_TD.path()), "foo", 0, None, None, true)] // Write and append together: #[case::redir_3( format!("echo foo >> {fp} && echo bar > {fp} && echo ree >> {fp} && {CAT_CMD} {fp} && rm {fp}", fp=tf()), diff --git a/rust/bitbazaar/cli/redirect.rs b/rust/bitbazaar/cli/redirect.rs index 9116a0d0..651459b8 100644 --- a/rust/bitbazaar/cli/redirect.rs +++ b/rust/bitbazaar/cli/redirect.rs @@ -1,5 +1,6 @@ use std::{ io::{Read, Write}, + path::PathBuf, process, }; @@ -20,23 +21,23 @@ pub fn handle_redirect( Ok(match redirect { ast::Redirect::Write(fd, name) => { let dest = Target::new(shell, name)?.set_write(); - Data::new(last_out, fd)?.submit(dest)? + Data::new(last_out, fd)?.submit(shell, dest)? } ast::Redirect::Append(fd, name) => { let dest = Target::new(shell, name)?.set_write().set_append(); - Data::new(last_out, fd)?.submit(dest)? + Data::new(last_out, fd)?.submit(shell, dest)? } ast::Redirect::DupWrite(fd, name) => { let dest = Target::new(shell, name)?.set_write(); - Data::new(last_out, fd)?.submit(dest)? + Data::new(last_out, fd)?.submit(shell, dest)? } ast::Redirect::Read(fd, name) => { let dest = Target::new(shell, name)?.set_read(); - Data::new(last_out, fd)?.submit(dest)? + Data::new(last_out, fd)?.submit(shell, dest)? } ast::Redirect::DupRead(fd, name) => { let dest = Target::new(shell, name)?.set_read(); - Data::new(last_out, fd)?.submit(dest)? + Data::new(last_out, fd)?.submit(shell, dest)? } ast::Redirect::ReadWrite(..) => { return Err(err!( @@ -203,7 +204,7 @@ impl Data { }) } - fn submit(self, dest: Target) -> Result { + fn submit(self, shell: &Shell, dest: Target) -> Result { let mut conc = ConcreteOutput::default(); match dest.variant { @@ -243,7 +244,15 @@ impl Data { opts.append(true); } - let mut file = opts.open(name).change_context(ShellErr::InternalError)?; + // Make sure relative to current dir if it is a relative path: + let mut filepath = PathBuf::from(name); + if filepath.is_relative() { + filepath = shell.active_dir()?.join(filepath) + } + + let mut file = opts + .open(filepath) + .change_context(ShellErr::InternalError)?; if dest.write { self.write(&mut file)?;