From d4613ff0029ea65f5c6895c3a6f8f5668dd1b55e Mon Sep 17 00:00:00 2001 From: Ale Sanchez Date: Mon, 26 Jan 2026 17:12:42 +0100 Subject: [PATCH] fix(shell): Make sidecars work in tests (fix #13767) (#3234) --- .changes/make-sidecars-work-in-tests.md | 6 +++ plugins/shell/src/process/mod.rs | 51 ++++++++++++++++--------- 2 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 .changes/make-sidecars-work-in-tests.md diff --git a/.changes/make-sidecars-work-in-tests.md b/.changes/make-sidecars-work-in-tests.md new file mode 100644 index 00000000..4f07cff8 --- /dev/null +++ b/.changes/make-sidecars-work-in-tests.md @@ -0,0 +1,6 @@ +--- +'shell': 'patch:bug' +'shell-js': 'patch:bug' +--- + +Make sidecars work in tests. Executable resolution is aware of the "deps" directory. \ No newline at end of file diff --git a/plugins/shell/src/process/mod.rs b/plugins/shell/src/process/mod.rs index 0c80f12d..3d29162d 100644 --- a/plugins/shell/src/process/mod.rs +++ b/plugins/shell/src/process/mod.rs @@ -118,27 +118,38 @@ pub struct Output { } fn relative_command_path(command: &Path) -> crate::Result { - match platform::current_exe()?.parent() { - #[cfg(windows)] - Some(exe_dir) => { - let mut command_path = exe_dir.join(command); - let already_exe = command_path.extension().is_some_and(|ext| ext == "exe"); - if !already_exe { - // do not use with_extension to retain dots in the command filename - command_path.as_mut_os_string().push(".exe"); - } - Ok(command_path) + let exe_path = platform::current_exe()?; + + let exe_dir = exe_path + .parent() + .ok_or(crate::Error::CurrentExeHasNoParent)?; + + // If a test is being run, the executable is in the "deps" directory, so we need to go up one level. + let base_dir = if exe_dir.ends_with("deps") { + exe_dir.parent().unwrap_or(exe_dir) + } else { + exe_dir + }; + + let mut command_path = base_dir.join(command); + + #[cfg(windows)] + { + let already_exe = command_path.extension().is_some_and(|ext| ext == "exe"); + if !already_exe { + // do not use with_extension to retain dots in the command filename + command_path.as_mut_os_string().push(".exe"); } - #[cfg(not(windows))] - Some(exe_dir) => { - let mut command_path = exe_dir.join(command); - if command_path.extension().is_some_and(|ext| ext == "exe") { - command_path.set_extension(""); - } - Ok(command_path) - } - None => Err(crate::Error::CurrentExeHasNoParent), } + + #[cfg(not(windows))] + { + if command_path.extension().is_some_and(|ext| ext == "exe") { + command_path.set_extension(""); + } + } + + Ok(command_path) } impl From for StdCommand { @@ -508,6 +519,8 @@ mod tests { .unwrap() .parent() .unwrap() + .parent() // Go up once more to get out of the "deps" directory + .unwrap() .to_owned(); assert_eq!( relative_command_path(Path::new("Tauri.Example")).unwrap(),