Skip to main content

aft/
windows_shell.rs

1//! Shared Windows shell selection for foreground and background bash commands.
2//!
3//! Mirrors OpenCode's resolver:
4//!   1. `$SHELL` env var (typically points at git-bash on Windows dev setups).
5//!   2. `pwsh.exe` (PowerShell 7+).
6//!   3. `powershell.exe` (Windows PowerShell 5.1).
7//!   4. Git-for-Windows `bash.exe` discovered next to `git` on PATH (catches
8//!      users who installed Git for Windows but never set `$SHELL`).
9//!   5. `cmd.exe` (universal floor — always reachable on every Windows SKU).
10//!
11//! POSIX shells (bash, sh, zsh, ksh, dash) are invoked as `<shell> -c <cmd>`
12//! the same way Unix does. PowerShell variants take their `-Command` shape;
13//! cmd.exe takes `/D /C`.
14//!
15//! Compiled on all platforms so the cross-platform retry-decision unit
16//! tests in `commands::bash::try_spawn_with_fallback` (test-only — see the
17//! Windows foreground bash path in `crate::commands::bash`) can run on
18//! macOS/Linux dev machines. The production Windows background spawn path
19//! at `bash_background::registry::detached_shell_command_for` is the live
20//! caller.
21
22#![cfg_attr(not(windows), allow(dead_code))]
23
24use std::path::{Path, PathBuf};
25use std::process::Command;
26use std::sync::OnceLock;
27
28/// POSIX shells that can be invoked as `<shell> -c <command>`. Matches
29/// OpenCode's `POSIX` set in `packages/opencode/src/shell/shell.ts`.
30const POSIX_NAMES: &[&str] = &["bash", "sh", "zsh", "ksh", "dash"];
31
32#[derive(Clone, Debug, PartialEq, Eq)]
33pub(crate) enum WindowsShell {
34    /// PowerShell 7+ (cross-platform). Supports `&&` pipeline operator.
35    Pwsh,
36    /// Windows PowerShell 5.1 (legacy, still default on most Windows desktops
37    /// but **absent on Windows 11 IoT Enterprise LTSC SKUs** — issue #27).
38    /// Does NOT support `&&` in pipelines (PS 7+ only feature).
39    Powershell,
40    /// `cmd.exe` — the universal fallback. Present on every Windows SKU.
41    /// Supports `&&` and `||` natively. Lacks PowerShell's piping/cmdlets but
42    /// handles bash-style chained shell invocations correctly.
43    Cmd,
44    /// User-supplied POSIX shell — typically Git for Windows' bash.exe,
45    /// resolved either from `$SHELL` or auto-detected next to `git` on PATH.
46    /// Invoked as `<binary> -c <command>` exactly like a Unix shell, so
47    /// agents that emit bash-syntax commands (`cmd /c "foo"`, `find . -name`,
48    /// quoting with backslash-escapes, etc.) work the same way they would
49    /// in a real bash session. The string is the absolute path to the binary.
50    Posix(PathBuf),
51}
52
53impl WindowsShell {
54    /// Binary path to spawn. PowerShell/cmd variants resolve via PATH lookup;
55    /// `Posix` carries an already-absolute path resolved at candidate-build
56    /// time so we don't accidentally pick a different bash.exe later.
57    pub(crate) fn binary(&self) -> std::borrow::Cow<'_, str> {
58        match self {
59            WindowsShell::Pwsh => std::borrow::Cow::Borrowed("pwsh.exe"),
60            WindowsShell::Powershell => std::borrow::Cow::Borrowed("powershell.exe"),
61            WindowsShell::Cmd => std::borrow::Cow::Borrowed("cmd.exe"),
62            WindowsShell::Posix(path) => std::borrow::Cow::Owned(path.display().to_string()),
63        }
64    }
65
66    /// Argument vector to pass alongside the user's command string.
67    /// PowerShell variants take `-Command <string>`; cmd takes `/D /C <string>`
68    /// (`/D` disables AutoRun macros that could otherwise inject env-trust
69    /// behavior into our isolated invocation); POSIX shells take `-c <string>`.
70    pub(crate) fn args<'a>(&'a self, command: &'a str) -> Vec<&'a str> {
71        match self {
72            WindowsShell::Pwsh | WindowsShell::Powershell => vec![
73                "-NoLogo",
74                "-NoProfile",
75                "-NonInteractive",
76                "-ExecutionPolicy",
77                "Bypass",
78                "-Command",
79                command,
80            ],
81            WindowsShell::Cmd => vec!["/D", "/C", command],
82            WindowsShell::Posix(_) => vec!["-c", command],
83        }
84    }
85
86    /// Args for invoking a wrapper file under a PTY-attached shell.
87    /// Returns owned strings so callers can pass temporary wrapper paths.
88    pub(crate) fn pty_wrapper_args(&self, wrapper_path: &Path) -> Vec<String> {
89        match self {
90            WindowsShell::Cmd => vec!["/c".into(), wrapper_path.display().to_string()],
91            WindowsShell::Pwsh | WindowsShell::Powershell => vec![
92                "-NoProfile".into(),
93                "-NonInteractive".into(),
94                "-File".into(),
95                wrapper_path.display().to_string(),
96            ],
97            WindowsShell::Posix(_) => vec![wrapper_path.display().to_string()],
98        }
99    }
100
101    /// Build a `Command` that runs an inline background wrapper script.
102    ///
103    /// Production background bash now writes wrappers to `.bat` / `.ps1` temp
104    /// files and invokes those files directly, so paths containing `!` remain
105    /// literal and cmd.exe does not have to parse a long inline wrapper. This
106    /// helper is retained for tests that exercise the legacy shell-arg shape.
107    #[allow(dead_code)]
108    pub(crate) fn bg_command(&self, wrapper: &str) -> Command {
109        let binary = self.binary();
110        let mut cmd = Command::new(binary.as_ref());
111        // PowerShell variants accept the wrapper string directly via
112        // `-Command`; the shell's `-Command` parser is generally happy
113        // with embedded quotes when the script doesn't contain literal
114        // `"` (we use only single quotes in the PS wrapper for that
115        // reason — see `wrapper_script` for `Pwsh|Powershell`).
116        //
117        // For cmd.exe the wrapper contains `cmd_quote`-quoted paths
118        // which CAN survive cmd's /C parser, but only if we add `/S`
119        // to enable simple-quote-stripping mode. Even with /S the
120        // interaction with Rust's std-lib argument quoting is fragile,
121        // so we rely on `args()` for cmd and live with the constraints.
122        //
123        // `/D` skips AutoRun macros; `/S` enables simple quote-stripping.
124        //
125        // POSIX shells (git-bash etc.) take `-c <wrapper>` and execute
126        // the wrapper as a normal shell script — the wrapper's `trap` and
127        // `printf "$?"` mechanics are POSIX-standard, so no special flags.
128        match self {
129            WindowsShell::Pwsh | WindowsShell::Powershell => {
130                cmd.args(self.args(wrapper));
131            }
132            WindowsShell::Cmd => {
133                cmd.args(["/D", "/S", "/C", wrapper]);
134            }
135            WindowsShell::Posix(_) => {
136                cmd.args(["-c", wrapper]);
137            }
138        }
139        cmd
140    }
141
142    /// Wrap a background command so shell termination writes an exit marker.
143    /// The marker is written via temp-file + rename for PowerShell variants and
144    /// via `move /Y` for cmd.exe, matching the Unix background wrapper contract.
145    pub(crate) fn wrapper_script(&self, command: &str, exit_path: &Path) -> String {
146        match self {
147            WindowsShell::Pwsh | WindowsShell::Powershell => {
148                let exit_path = powershell_single_quote(&exit_path.display().to_string());
149                let command = powershell_single_quote(command);
150                // The wrapper itself runs as a PowerShell script (invoked
151                // via `pwsh -File <path>` by `detached_shell_command_for`),
152                // so we execute the user command directly with `Invoke-Expression`
153                // instead of nesting another shell. Earlier versions wrapped
154                // the user command in an inner `& 'pwsh.exe' -Command ...`
155                // which caused PS-on-PS recursion that silently produced
156                // empty output on Windows 11 (likely a console-host issue
157                // with nested non-interactive pwsh sessions).
158                //
159                // CRITICAL: this script must contain NO literal double-quote
160                // characters. Inner `"` would break the outer-shell parse on
161                // some Windows configurations even with `-File`. We use only
162                // single-quoted strings and string concat (`+`) for any
163                // interpolation needs.
164                format!(
165                    concat!(
166                        "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); ",
167                        "$OutputEncoding = [Console]::OutputEncoding; ",
168                        "$exitPath = {exit_path}; ",
169                        "$tmpPath = $exitPath + '.tmp.' + $PID; ",
170                        "$global:LASTEXITCODE = $null; ",
171                        "Invoke-Expression {command}; ",
172                        "$success = $?; ",
173                        "$nativeCode = $global:LASTEXITCODE; ",
174                        "if ($null -ne $nativeCode) {{ $code = [int]$nativeCode }} ",
175                        "elseif ($success) {{ $code = 0 }} ",
176                        "else {{ $code = 1 }}; ",
177                        "[System.IO.File]::WriteAllText($tmpPath, [string]$code); ",
178                        "Move-Item -LiteralPath $tmpPath -Destination $exitPath -Force; ",
179                        "exit $code"
180                    ),
181                    exit_path = exit_path,
182                    command = command
183                )
184            }
185            WindowsShell::Cmd => {
186                // This body is written to a `.bat` file and invoked as
187                // `cmd /D /C <wrapper.bat>`. Batch files expand `%ERRORLEVEL%`
188                // per line, so we do not need `/V:ON` delayed expansion; paths
189                // containing literal `!` survive unchanged.
190                let tmp_path = format!("{}.tmp", exit_path.display());
191                format!(
192                    concat!(
193                        "@echo off\r\n",
194                        "{command}\r\n",
195                        "set CODE=%ERRORLEVEL%\r\n",
196                        "echo %CODE% > {tmp}\r\n",
197                        "move /Y {tmp} {exit} > nul\r\n",
198                        "exit /B %CODE%\r\n"
199                    ),
200                    command = command,
201                    tmp = cmd_quote(&tmp_path),
202                    exit = cmd_quote(&exit_path.display().to_string())
203                )
204            }
205            WindowsShell::Posix(shell_path) => {
206                // git-bash and friends speak POSIX, so the same temp-file +
207                // mv pattern the Unix bg-bash wrapper uses applies here. The
208                // wrapper writes the user command's $? to a temp file and
209                // atomically renames it into place so partial writes are
210                // never observable. Single-quote the user command to defang
211                // any embedded `;`, `&`, or `$` — POSIX single-quotes don't
212                // interpret anything except `'` itself, which we escape via
213                // the `'\''` close-and-reopen idiom.
214                let exit_str = exit_path.display().to_string();
215                let tmp_path = format!("{}.tmp", exit_str);
216                format!(
217                    "{} -c {} ; printf '%s' \"$?\" > {} && mv {} {}",
218                    posix_single_quote(&shell_path.display().to_string()),
219                    posix_single_quote(command),
220                    posix_single_quote(&tmp_path),
221                    posix_single_quote(&tmp_path),
222                    posix_single_quote(&exit_str),
223                )
224            }
225        }
226    }
227
228    pub(crate) fn wrapper_script_bytes(&self, command: &str, exit_path: &Path) -> Vec<u8> {
229        let script = self.wrapper_script(command, exit_path);
230        match self {
231            WindowsShell::Pwsh | WindowsShell::Powershell => {
232                let mut bytes = vec![0xEF, 0xBB, 0xBF];
233                bytes.extend_from_slice(script.as_bytes());
234                bytes
235            }
236            WindowsShell::Cmd | WindowsShell::Posix(_) => script.into_bytes(),
237        }
238    }
239}
240
241/// All Windows shells that the PATH probe believes are reachable, returned
242/// in priority order. Always non-empty on Windows because cmd.exe is the
243/// floor. Order:
244///
245///   1. `$SHELL` env var (typically points at git-bash on Windows dev setups).
246///   2. `pwsh.exe`.
247///   3. `powershell.exe`.
248///   4. Git-for-Windows `bash.exe` discovered next to `git` on PATH.
249///   5. `cmd.exe`.
250///
251/// Used by the foreground bash spawn site to retry with the next candidate
252/// if the first one fails to spawn at runtime. Cached after the first
253/// resolve.
254pub(crate) fn shell_candidates() -> Vec<WindowsShell> {
255    static CACHED: OnceLock<Vec<WindowsShell>> = OnceLock::new();
256    CACHED
257        .get_or_init(|| {
258            shell_candidates_with(
259                |binary| which::which(binary).ok(),
260                || std::env::var_os("SHELL").map(PathBuf::from),
261            )
262        })
263        .clone()
264}
265
266/// Test seam for [`shell_candidates`]. The two closures let unit tests inject
267/// a fake `which`-like resolver and a fake `$SHELL` env value.
268///
269/// `which_for(binary)` should return `Some(absolute_path)` if the binary is
270/// reachable, `None` otherwise — matching the contract of `which::which`.
271pub(crate) fn shell_candidates_with<W, S>(which_for: W, shell_env: S) -> Vec<WindowsShell>
272where
273    W: Fn(&str) -> Option<PathBuf>,
274    S: FnOnce() -> Option<PathBuf>,
275{
276    let mut candidates: Vec<WindowsShell> = Vec::with_capacity(5);
277
278    // 1. $SHELL env var — typically points at git-bash on Windows dev
279    //    setups (`/c/Program Files/Git/bin/bash.exe` style or a normal
280    //    Windows path). Mirrors OpenCode's preferred() resolution.
281    //    Only honored when the named binary is recognized as POSIX
282    //    (bash/sh/zsh/ksh/dash) — we don't want SHELL=cmd.exe pinning us
283    //    to cmd when the user already gets cmd as the floor candidate.
284    if let Some(shell_path) = shell_env() {
285        if let Some(resolved) = resolve_user_shell(&shell_path, &which_for) {
286            crate::slog_info!(
287                "bash candidate: $SHELL = {} (POSIX, invoked as -c)",
288                resolved.display()
289            );
290            candidates.push(WindowsShell::Posix(resolved));
291        }
292    }
293
294    // 2-3. PowerShell variants.
295    if which_for("pwsh.exe").is_some() {
296        crate::slog_info!(
297            "bash candidate: pwsh.exe (PowerShell 7+; supports && pipeline operator)"
298        );
299        candidates.push(WindowsShell::Pwsh);
300    }
301    if which_for("powershell.exe").is_some() {
302        crate::slog_info!("bash candidate: powershell.exe (Windows PowerShell 5.1; && in pipelines unsupported, will surface as parse error)");
303        candidates.push(WindowsShell::Powershell);
304    }
305
306    // 4. Git for Windows auto-detect — find bash.exe next to git on PATH.
307    //    Catches the common case of "user installed Git for Windows but
308    //    didn't set $SHELL". Skipped when $SHELL already produced a POSIX
309    //    candidate (no point adding the same git-bash twice).
310    let already_posix = candidates
311        .iter()
312        .any(|c| matches!(c, WindowsShell::Posix(_)));
313    if !already_posix {
314        if let Some(git_bash) = locate_git_bash(&which_for) {
315            crate::slog_info!(
316                "bash candidate: git-bash auto-detected at {} (POSIX, invoked as -c)",
317                git_bash.display()
318            );
319            candidates.push(WindowsShell::Posix(git_bash));
320        }
321    }
322
323    // 5. cmd.exe is always added as the floor, regardless of PATH probe
324    //    result. It lives in a Windows search-path location that PATH
325    //    inheritance issues, ASR rules, and sandboxing generally cannot
326    //    remove. Without this floor, foreground bash retry would have
327    //    nowhere to fall back to when other shells fail to spawn at runtime.
328    candidates.push(WindowsShell::Cmd);
329
330    let only_cmd = candidates.len() == 1;
331    if only_cmd {
332        crate::slog_warn!(
333            "No bash, PowerShell, or git-bash is reachable from this \
334         aft process — using cmd.exe only. This can occur even when \
335         PowerShell is installed if PATH inheritance is restricted, \
336         antivirus / AppLocker / Defender ASR rules block PowerShell as a \
337         child process, or you're on a stripped Windows SKU. Bash-style \
338         commands using && and || still work; PowerShell-only cmdlets and \
339         POSIX-only commands will not. Details: \
340         https://github.com/cortexkit/aft/issues/27"
341        );
342    }
343    candidates
344}
345
346/// Resolve a `$SHELL` value into an absolute path to a POSIX shell binary,
347/// or `None` if the value is unusable on Windows. Handles three input
348/// shapes that show up in the wild:
349///
350///   - Full Windows path: `C:\Program Files\Git\bin\bash.exe`
351///   - MSYS/git-bash style: `/c/Program Files/Git/bin/bash.exe` or `/usr/bin/bash`
352///   - Bare name: `bash` or `bash.exe` (resolve via `which`)
353///
354/// Returns `None` if the resolved binary's filename isn't in `POSIX_NAMES`,
355/// so that someone with `SHELL=cmd.exe` doesn't accidentally pin us to a
356/// `Posix(cmd.exe)` invocation that breaks the `-c` contract.
357fn resolve_user_shell<W>(raw: &Path, which_for: &W) -> Option<PathBuf>
358where
359    W: Fn(&str) -> Option<PathBuf>,
360{
361    // Convert MSYS-style /c/foo/bar paths to C:\foo\bar so std::fs::metadata
362    // and Command::new can find them. Pure Windows paths and POSIX paths on
363    // a MSYS root pass through with /-to-\ normalization.
364    let resolved = normalize_shell_path(raw);
365
366    // If the (possibly-normalized) path is absolute and exists on disk,
367    // use it as-is. Otherwise treat it as a bare name and try PATH lookup.
368    let candidate = if resolved.is_absolute() && resolved.exists() {
369        resolved
370    } else {
371        let name = resolved.file_name()?.to_str()?.to_string();
372        which_for(&name)?
373    };
374
375    if !is_posix_shell_name(&candidate) {
376        crate::slog_info!(
377            "$SHELL points at {} which isn't a recognized POSIX shell; \
378         falling back to PowerShell/cmd resolution.",
379            candidate.display()
380        );
381        return None;
382    }
383    Some(candidate)
384}
385
386/// Look for git-bash next to `git` on PATH. Mirrors OpenCode's `gitbash()`:
387/// resolves `git`, then checks `<git_dir>/../../bin/bash.exe`. Returns
388/// `None` if git isn't on PATH, the expected bash.exe doesn't exist, or
389/// the file is empty.
390fn locate_git_bash<W>(which_for: &W) -> Option<PathBuf>
391where
392    W: Fn(&str) -> Option<PathBuf>,
393{
394    let git = which_for("git.exe").or_else(|| which_for("git"))?;
395    // git.exe typically lives at <install>/cmd/git.exe; bash.exe lives at
396    // <install>/bin/bash.exe. The two `parent()` calls walk up from
397    // `cmd/git.exe` to `<install>`, then we descend into `bin/bash.exe`.
398    let candidate = git.parent()?.parent()?.join("bin").join("bash.exe");
399    let metadata = std::fs::metadata(&candidate).ok()?;
400    if metadata.len() == 0 {
401        return None;
402    }
403    Some(candidate)
404}
405
406/// Normalize an MSYS / git-bash POSIX path to a Windows path, leaving
407/// already-Windows paths and bare names alone. This mirrors the relevant
408/// subset of OpenCode's `windowsPath()` for `$SHELL` values.
409fn normalize_shell_path(raw: &Path) -> PathBuf {
410    let s = raw.to_string_lossy();
411
412    // MSYS drive-letter form: /c/Foo/Bar  ->  C:\Foo\Bar
413    if let Some(rest) = s.strip_prefix('/') {
414        if let Some((drive, after)) = rest.split_once('/') {
415            if drive.len() == 1
416                && drive
417                    .chars()
418                    .next()
419                    .is_some_and(|c| c.is_ascii_alphabetic())
420            {
421                let drive_upper = drive.to_uppercase();
422                let win = format!("{}:\\{}", drive_upper, after.replace('/', "\\"));
423                return PathBuf::from(win);
424            }
425        }
426    }
427
428    PathBuf::from(s.as_ref())
429}
430
431/// True when the file name (without extension) is in `POSIX_NAMES`.
432fn is_posix_shell_name(path: &Path) -> bool {
433    let stem = path
434        .file_stem()
435        .and_then(|s| s.to_str())
436        .unwrap_or("")
437        .to_lowercase();
438    POSIX_NAMES.iter().any(|name| *name == stem)
439}
440
441fn powershell_single_quote(value: &str) -> String {
442    format!("'{}'", value.replace('\'', "''"))
443}
444
445/// Single-quote a value for POSIX `sh -c`, escaping inner single quotes via
446/// the standard `'\''` close-and-reopen idiom. Used by the bg-bash wrapper
447/// for [`WindowsShell::Posix`] (git-bash) and matches the Unix wrapper's
448/// quoting contract.
449#[cfg_attr(not(windows), allow(dead_code))]
450fn posix_single_quote(value: &str) -> String {
451    format!("'{}'", value.replace('\'', "'\\''"))
452}
453
454// Used by `wrapper_script` for `WindowsShell::Cmd`; that wrapper is
455// only invoked from `bash_background::registry::detached_shell_command_for`
456// which is `#[cfg(windows)]`. The function compiles on all platforms so
457// `wrapper_script` stays cross-platform-testable.
458#[cfg_attr(not(windows), allow(dead_code))]
459fn cmd_quote(value: &str) -> String {
460    format!("\"{}\"", value.replace('"', "\"\""))
461}
462
463#[cfg(test)]
464mod tests {
465    use super::*;
466
467    /// Helper: build a `which`-like closure that returns Some for the
468    /// listed binaries (mapping each to a synthetic absolute path) and
469    /// None for everything else. The synthetic path layout matches a
470    /// realistic Git for Windows install when `git.exe` is present,
471    /// so [`locate_git_bash`] can synthesize a sibling bash.exe path —
472    /// but the returned path won't exist on disk, so `locate_git_bash`
473    /// will bail at the metadata check, which is what the no-Posix-via-
474    /// auto-detect tests actually want.
475    fn fake_which(binaries: Vec<&'static str>) -> impl Fn(&str) -> Option<PathBuf> {
476        move |query| {
477            if binaries.contains(&query) {
478                match query {
479                    "git.exe" | "git" => Some(PathBuf::from(r"C:\Program Files\Git\cmd\git.exe")),
480                    _ => Some(PathBuf::from(format!(r"C:\fake\{}", query))),
481                }
482            } else {
483                None
484            }
485        }
486    }
487
488    #[test]
489    fn pty_wrapper_args_are_owned_and_shell_specific() {
490        let wrapper = PathBuf::from(r"C:\tmp\task.bat");
491        assert_eq!(
492            WindowsShell::Cmd.pty_wrapper_args(&wrapper),
493            vec!["/c".to_string(), wrapper.display().to_string()]
494        );
495        assert_eq!(
496            WindowsShell::Pwsh.pty_wrapper_args(&wrapper),
497            vec![
498                "-NoProfile".to_string(),
499                "-NonInteractive".to_string(),
500                "-File".to_string(),
501                wrapper.display().to_string(),
502            ]
503        );
504        assert_eq!(
505            WindowsShell::Posix(PathBuf::from(r"C:\Git\bin\bash.exe")).pty_wrapper_args(&wrapper),
506            vec![wrapper.display().to_string()]
507        );
508    }
509
510    // ---------------------------------------------------------------
511    // Fix for user report: $SHELL must be respected on Windows so
512    // git-bash (and other POSIX shells) can run agent-emitted bash
513    // syntax instead of getting routed to PowerShell where escaping
514    // breaks. Mirrors OpenCode's behavior.
515    // ---------------------------------------------------------------
516
517    #[test]
518    fn user_shell_pointing_at_bash_wins_over_powershell() {
519        // SHELL=C:\Program Files\Git\bin\bash.exe
520        // pwsh.exe also reachable.
521        // Expect: Posix(bash.exe) is the first candidate, pwsh second.
522        let tmp = tempfile::tempdir().expect("tempdir");
523        let bash = tmp.path().join("bash.exe");
524        std::fs::write(&bash, b"shebang").unwrap();
525
526        let candidates = shell_candidates_with(fake_which(vec!["pwsh.exe"]), || Some(bash.clone()));
527
528        assert!(matches!(candidates[0], WindowsShell::Posix(_)));
529        if let WindowsShell::Posix(p) = &candidates[0] {
530            assert_eq!(p, &bash);
531        }
532        assert_eq!(candidates[1], WindowsShell::Pwsh);
533    }
534
535    #[test]
536    fn user_shell_pointing_at_non_posix_binary_is_ignored() {
537        // SHELL=C:\Windows\System32\cmd.exe — not in POSIX_NAMES, so
538        // we should fall back to PowerShell/cmd resolution.
539        let tmp = tempfile::tempdir().expect("tempdir");
540        let cmd = tmp.path().join("cmd.exe");
541        std::fs::write(&cmd, b"").unwrap();
542
543        let candidates = shell_candidates_with(fake_which(vec!["pwsh.exe"]), || Some(cmd));
544
545        // No Posix candidate; pwsh wins.
546        assert!(!candidates
547            .iter()
548            .any(|c| matches!(c, WindowsShell::Posix(_))));
549        assert_eq!(candidates[0], WindowsShell::Pwsh);
550    }
551
552    #[test]
553    fn user_shell_msys_drive_letter_path_is_normalized() {
554        // SHELL=/c/Program Files/Git/bin/bash.exe — git-bash style.
555        // Without normalization this won't exist at all, so the
556        // resolver should at least *try* the normalized form before
557        // falling through.
558        //
559        // We can't easily fake an existing file at C:\... in a unit
560        // test, so we directly assert the normalization output here.
561        let raw = PathBuf::from("/c/Program Files/Git/bin/bash.exe");
562        let normalized = normalize_shell_path(&raw);
563        assert_eq!(
564            normalized,
565            PathBuf::from(r"C:\Program Files\Git\bin\bash.exe")
566        );
567    }
568
569    #[test]
570    fn user_shell_already_windows_path_passes_through() {
571        let raw = PathBuf::from(r"C:\Program Files\Git\bin\bash.exe");
572        let normalized = normalize_shell_path(&raw);
573        assert_eq!(normalized, raw);
574    }
575
576    /// Note: this test runs on every platform but uses platform-native
577    /// path separators because `Path::file_stem()` only recognizes the
578    /// host OS's separator. On macOS/Linux that means a forward-slash
579    /// fake path (`/fake/bash`); on Windows the equivalent backslash
580    /// path. The production code only runs on Windows where backslash
581    /// works correctly, so the test's job is to verify the resolution
582    /// flow, not the path syntax.
583    #[test]
584    fn user_shell_bare_name_resolves_via_which() {
585        // SHELL=bash → not absolute → which("bash") returns the fake
586        // resolver's path → recognized as POSIX.
587        #[cfg(unix)]
588        let expected = PathBuf::from("/fake/bash");
589        #[cfg(windows)]
590        let expected = PathBuf::from(r"C:\fake\bash");
591
592        // Pre-translate the fake_which return so it uses the host's
593        // separator. We can't share fake_which here because that helper
594        // is hard-coded to Windows-style paths.
595        let expected_clone = expected.clone();
596        let which_for = move |query: &str| -> Option<PathBuf> {
597            if query == "bash" {
598                Some(expected_clone.clone())
599            } else {
600                None
601            }
602        };
603
604        let candidates = shell_candidates_with(which_for, || Some(PathBuf::from("bash")));
605        assert!(
606            matches!(&candidates[0], WindowsShell::Posix(p) if p == &expected),
607            "expected Posix({}) as first candidate, got {:?}",
608            expected.display(),
609            candidates
610        );
611    }
612
613    #[test]
614    fn no_user_shell_and_no_git_falls_back_to_pwsh_powershell_cmd() {
615        let candidates =
616            shell_candidates_with(fake_which(vec!["pwsh.exe", "powershell.exe"]), || None);
617        assert_eq!(candidates.len(), 3);
618        assert_eq!(candidates[0], WindowsShell::Pwsh);
619        assert_eq!(candidates[1], WindowsShell::Powershell);
620        assert_eq!(candidates[2], WindowsShell::Cmd);
621    }
622
623    #[test]
624    fn cmd_is_always_the_floor() {
625        // Nothing reachable, no $SHELL — only cmd.exe should be in the list.
626        let candidates = shell_candidates_with(|_| None, || None);
627        assert_eq!(candidates, vec![WindowsShell::Cmd]);
628    }
629
630    // ---------------------------------------------------------------
631    // git-bash auto-detect: when $SHELL is unset but the user installed
632    // Git for Windows, we should still pick up the bundled bash.exe.
633    // ---------------------------------------------------------------
634
635    #[test]
636    fn git_bash_auto_detect_when_shell_unset() {
637        let tmp = tempfile::tempdir().expect("tempdir");
638        // Mirror the Git for Windows layout: <root>/cmd/git.exe and
639        // <root>/bin/bash.exe.
640        std::fs::create_dir_all(tmp.path().join("cmd")).unwrap();
641        std::fs::create_dir_all(tmp.path().join("bin")).unwrap();
642        let git = tmp.path().join("cmd").join("git.exe");
643        std::fs::write(&git, b"git").unwrap();
644        let bash = tmp.path().join("bin").join("bash.exe");
645        std::fs::write(&bash, b"shebang").unwrap();
646
647        let which = |query: &str| -> Option<PathBuf> {
648            match query {
649                "git.exe" | "git" => Some(git.clone()),
650                _ => None,
651            }
652        };
653        let candidates = shell_candidates_with(which, || None);
654
655        // First candidate is the auto-detected git-bash.
656        assert!(matches!(&candidates[0], WindowsShell::Posix(p) if p == &bash));
657        // cmd.exe is still the floor.
658        assert_eq!(*candidates.last().unwrap(), WindowsShell::Cmd);
659    }
660
661    #[test]
662    fn git_bash_skipped_when_user_shell_already_posix() {
663        // $SHELL points at git-bash → no need to auto-detect a second
664        // POSIX candidate. The candidate list should not contain two
665        // Posix entries.
666        let tmp = tempfile::tempdir().expect("tempdir");
667        let bash = tmp.path().join("bash.exe");
668        std::fs::write(&bash, b"shebang").unwrap();
669
670        let candidates = shell_candidates_with(
671            // git is reachable, but git-bash should NOT be added because
672            // we already have a Posix from $SHELL.
673            |query: &str| match query {
674                "git.exe" | "git" => Some(PathBuf::from(r"C:\Program Files\Git\cmd\git.exe")),
675                _ => None,
676            },
677            || Some(bash.clone()),
678        );
679
680        let posix_count = candidates
681            .iter()
682            .filter(|c| matches!(c, WindowsShell::Posix(_)))
683            .count();
684        assert_eq!(
685            posix_count, 1,
686            "exactly one Posix candidate when $SHELL is already set: got {:?}",
687            candidates
688        );
689    }
690
691    // ---------------------------------------------------------------
692    // Spawn-shape tests: Posix(bash) must be invoked as `bash -c <cmd>`
693    // exactly the way Unix bash works.
694    // ---------------------------------------------------------------
695
696    #[test]
697    fn posix_shell_uses_dash_c_invocation() {
698        let bash = PathBuf::from(r"C:\Program Files\Git\bin\bash.exe");
699        let shell = WindowsShell::Posix(bash);
700        let args = shell.args("ls -la /tmp");
701        assert_eq!(args, vec!["-c", "ls -la /tmp"]);
702    }
703
704    #[test]
705    fn posix_shell_binary_returns_full_path() {
706        let bash = PathBuf::from(r"C:\Program Files\Git\bin\bash.exe");
707        let shell = WindowsShell::Posix(bash.clone());
708        assert_eq!(shell.binary().as_ref(), &bash.display().to_string());
709    }
710
711    #[test]
712    fn pwsh_args_unchanged() {
713        // Regression guard: refactor must not have altered PowerShell
714        // arg shape.
715        let shell = WindowsShell::Pwsh;
716        let args = shell.args("Get-ChildItem");
717        assert_eq!(
718            args,
719            vec![
720                "-NoLogo",
721                "-NoProfile",
722                "-NonInteractive",
723                "-ExecutionPolicy",
724                "Bypass",
725                "-Command",
726                "Get-ChildItem"
727            ]
728        );
729    }
730
731    #[test]
732    fn cmd_args_unchanged() {
733        let shell = WindowsShell::Cmd;
734        let args = shell.args("dir");
735        assert_eq!(args, vec!["/D", "/C", "dir"]);
736    }
737
738    // ---------------------------------------------------------------
739    // POSIX wrapper script: bg-bash exit-marker contract for git-bash.
740    // ---------------------------------------------------------------
741
742    #[test]
743    fn posix_wrapper_writes_exit_marker_atomically() {
744        let bash = PathBuf::from(r"C:\Program Files\Git\bin\bash.exe");
745        let shell = WindowsShell::Posix(bash);
746        let script = shell.wrapper_script("echo hi", Path::new(r"C:\Temp\bash.exit"));
747        // The F2 wrapper invokes the resolved shell path directly (not a bare
748        // `sh -c`), so users get bash semantics (`[[ ]]`, arrays, pipefail)
749        // rather than dash. It then captures `$?` via `printf` into a tmp file
750        // and `mv`s atomically into place.
751        assert!(
752            script.contains(r"'C:\Program Files\Git\bin\bash.exe' -c 'echo hi'"),
753            "wrapper must invoke the resolved shell directly: {script}",
754        );
755        assert!(script.contains("printf '%s' \"$?\""), "{script}");
756        assert!(script.contains("mv "), "{script}");
757        assert!(script.contains(r"C:\Temp\bash.exit"), "{script}");
758        assert!(script.contains(r"C:\Temp\bash.exit.tmp"), "{script}");
759    }
760
761    #[test]
762    fn posix_wrapper_escapes_embedded_single_quotes() {
763        // User command contains a single quote — wrapper must use the
764        // standard `'\''` close-and-reopen idiom.
765        let bash = PathBuf::from(r"C:\Program Files\Git\bin\bash.exe");
766        let shell = WindowsShell::Posix(bash);
767        let script = shell.wrapper_script("echo 'hi'", Path::new(r"C:\Temp\bash.exit"));
768        assert!(
769            script.contains(r"'echo '\''hi'\'''"),
770            "embedded single quote must be escaped: got {script}"
771        );
772    }
773
774    // ---------------------------------------------------------------
775    // is_posix_shell_name: case-insensitive, .exe-tolerant lookup.
776    // ---------------------------------------------------------------
777
778    #[test]
779    fn is_posix_shell_name_recognizes_known_shells() {
780        for name in ["bash", "BASH", "bash.exe", "Bash.Exe", "sh", "zsh.exe"] {
781            assert!(
782                is_posix_shell_name(Path::new(name)),
783                "{name} should be POSIX"
784            );
785        }
786    }
787
788    #[test]
789    fn is_posix_shell_name_rejects_non_posix() {
790        for name in ["cmd.exe", "powershell.exe", "pwsh.exe", "fish", "nu.exe"] {
791            assert!(
792                !is_posix_shell_name(Path::new(name)),
793                "{name} must NOT be POSIX"
794            );
795        }
796    }
797}