use super::*;
#[cfg(windows)]
#[test]
fn test_display_user_drops_the_extended_length_prefix() {
assert_eq!(
Path::new(r"\\?\C:\Users\me\proj").display_user(),
r"C:\Users\me\proj"
);
assert_eq!(
Path::new(r"C:\Users\me\proj").display_user(),
r"C:\Users\me\proj"
);
assert_eq!(
Path::new(r"\\server\share\proj").display_user(),
r"\\server\share\proj"
);
assert_eq!(
Path::new(r"\\?\UNC\server\share").display_user(),
r"\\server\share"
);
assert_eq!(
Path::new(r"\\?\UNC\wsl.localhost\Ubuntu\home\me\proj").display_user(),
r"\\wsl.localhost\Ubuntu\home\me\proj"
);
assert_eq!(Path::new(r"\\.\COM1").display_user(), r"\\.\COM1");
assert_eq!(
Path::new(r"\\?\C:\proj\CON").display_user(),
r"\\?\C:\proj\CON"
);
}
#[cfg(windows)]
#[test]
fn test_display_user_keeps_the_prefix_when_unc_needs_it() {
assert_eq!(
Path::new(r"\\?\UNC\server\share\a/b").display_user(),
r"\\?\UNC\server\share\a/b"
);
let long = format!(r"\\?\UNC\server\share\{}", "d".repeat(260));
assert_eq!(Path::new(&long).display_user(), long);
assert_eq!(
Path::new(r"\\?\UNC\server\share\proj.").display_user(),
r"\\?\UNC\server\share\proj."
);
assert_eq!(
Path::new(r"\\?\UNC\server\share\proj ").display_user(),
r"\\?\UNC\server\share\proj "
);
}
#[test]
fn test_settle_display_separators() {
#[cfg(windows)]
{
assert_eq!(
settle_display_separators(r"C:/Users/me\proj\.git/hooks".to_string()),
r"C:\Users\me\proj\.git\hooks"
);
assert_eq!(
Path::new("C:/Users/me").display_user(),
"C:/Users/me",
"display_user must not settle separators"
);
}
#[cfg(not(windows))]
{
assert_eq!(settle_display_separators(r"/a/b\c".to_string()), r"/a/b\c");
}
}
#[cfg(not(windows))]
#[test]
fn test_display_user_leaves_unix_paths_alone() {
assert_eq!(Path::new("/usr/local/bin").display_user(), "/usr/local/bin");
assert_eq!(Path::new(r"weird\name").display_user(), r"weird\name");
assert_eq!(Path::new(r"/a/b\c").display_user(), r"/a/b\c");
if dirs::HOME.as_os_str() != "/" {
assert_eq!(dirs::HOME.join("proj").display_user(), "~/proj");
}
}
#[test]
fn test_is_posix_shell_program() {
assert!(is_posix_shell_program(Path::new("bash")));
assert!(is_posix_shell_program(Path::new("bash.exe")));
assert!(is_posix_shell_program(Path::new("BASH.EXE")));
assert!(is_posix_shell_program(Path::new(
r"C:\Program Files\Git\bin\bash.exe"
)));
assert!(is_posix_shell_program(Path::new("/usr/bin/bash")));
assert!(is_posix_shell_program(Path::new("sh")));
assert!(is_posix_shell_program(Path::new("zsh")));
assert!(is_posix_shell_program(Path::new("fish")));
assert!(is_posix_shell_program(Path::new("ash")));
assert!(!is_posix_shell_program(Path::new("cmd")));
assert!(!is_posix_shell_program(Path::new("cmd.exe")));
assert!(!is_posix_shell_program(Path::new("powershell")));
assert!(!is_posix_shell_program(Path::new("pwsh.exe")));
assert!(!is_posix_shell_program(Path::new("rustc")));
assert!(!is_posix_shell_program(Path::new("")));
}
#[test]
fn test_command_mode_script_payload() {
let payload = |p: &str| command_mode_script_payload(Path::new(p));
for posix in [
"bash",
"sh",
"zsh",
"ksh",
"dash",
"ash",
"/usr/bin/bash",
"BASH.EXE",
] {
assert_eq!(payload(posix), Some(r#""$0" "$@""#), "{posix}");
}
for fish in ["fish", "fish.exe", "/usr/bin/fish"] {
assert_eq!(payload(fish), Some("$argv"), "{fish}");
}
for other in ["cmd", "cmd.exe", "pwsh", "powershell.exe", "rustc", ""] {
assert_eq!(payload(other), None, "{other}");
}
}
#[test]
fn test_is_cmd_shell_program() {
assert!(is_cmd_shell_program(Path::new("cmd")));
assert!(is_cmd_shell_program(Path::new("cmd.exe")));
assert!(is_cmd_shell_program(Path::new("CMD.EXE")));
assert!(is_cmd_shell_program(Path::new(
r"C:\Windows\System32\cmd.exe"
)));
assert!(!is_cmd_shell_program(Path::new("bash")));
assert!(!is_cmd_shell_program(Path::new("bash.exe")));
assert!(!is_cmd_shell_program(Path::new("powershell")));
assert!(!is_cmd_shell_program(Path::new("pwsh.exe")));
assert!(!is_cmd_shell_program(Path::new("cmd.com")));
assert!(!is_cmd_shell_program(Path::new("")));
}
#[test]
fn test_is_powershell_program() {
assert!(is_powershell_program(Path::new("pwsh")));
assert!(is_powershell_program(Path::new("pwsh.exe")));
assert!(is_powershell_program(Path::new("PWSH.EXE")));
assert!(is_powershell_program(Path::new("powershell")));
assert!(is_powershell_program(Path::new("powershell.exe")));
assert!(is_powershell_program(Path::new(
r"C:\Program Files\PowerShell\7\pwsh.exe"
)));
assert!(!is_powershell_program(Path::new("cmd")));
assert!(!is_powershell_program(Path::new("bash")));
assert!(!is_powershell_program(Path::new("")));
}
#[test]
fn test_inject_powershell_no_profile() {
let inject = |args: &[&str]| {
let mut v = sv(args);
inject_powershell_no_profile(&mut v);
v
};
assert_eq!(
inject(&["pwsh", "-Command"]),
sv(&["pwsh", "-NoProfile", "-Command"])
);
assert_eq!(
inject(&["powershell", "-c"]),
sv(&["powershell", "-NoProfile", "-c"])
);
assert_eq!(
inject(&["pwsh.exe", "-NoLogo", "-Command"]),
sv(&["pwsh.exe", "-NoProfile", "-NoLogo", "-Command"])
);
assert_eq!(inject(&["cmd", "/c"]), sv(&["cmd", "/c"]));
assert_eq!(inject(&["bash", "-c"]), sv(&["bash", "-c"]));
assert_eq!(inject(&[]), sv(&[]));
assert_eq!(
inject(&["pwsh", "-NoProfile", "-Command"]),
sv(&["pwsh", "-NoProfile", "-Command"])
);
assert_eq!(
inject(&["pwsh", "-noprofile", "-c"]),
sv(&["pwsh", "-noprofile", "-c"])
);
assert_eq!(inject(&["pwsh", "-nop", "-c"]), sv(&["pwsh", "-nop", "-c"]));
assert_eq!(
inject(&["pwsh", "/NoProfile", "-c"]),
sv(&["pwsh", "/NoProfile", "-c"])
);
assert_eq!(
inject(&["pwsh", "-Command", "-nop"]),
sv(&["pwsh", "-NoProfile", "-Command", "-nop"])
);
assert_eq!(
inject(&["pwsh", "-File", "script.ps1", "-nop"]),
sv(&["pwsh", "-NoProfile", "-File", "script.ps1", "-nop"])
);
assert_eq!(
inject(&["pwsh", "-NoProfileLoadTime", "-c"]),
sv(&["pwsh", "-NoProfile", "-NoProfileLoadTime", "-c"])
);
assert_eq!(
inject(&["pwsh", "-NoLogo", "-c"]),
sv(&["pwsh", "-NoProfile", "-NoLogo", "-c"])
);
}
#[test]
fn test_cmd_verbatim_args() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(&[c()], r#"uv run python -c "import x""#, &[]),
sv(&["/s", "/c", r#""uv run python -c "import x"""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo hi", &[]),
sv(&["/s", "/c", r#""echo hi""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "proxy", &["a b".to_string(), "c".to_string()]),
sv(&["/s", "/c", r#""proxy "a b" c""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "type", &[r".\test dir\file.txt".to_string()]),
sv(&["/s", "/c", r#""type ".\test dir\file.txt"""#])
);
assert_eq!(
cmd_verbatim_args(&["/s".to_string(), c()], "echo hi", &[]),
sv(&["/s", "/c", r#""echo hi""#])
);
}
#[test]
fn test_positional_cmd_command() {
let args = sv(&["/C", r#"echo init && "C:\a b\mise.exe" run "dev""#]);
assert_eq!(
positional_cmd_command(&args),
Some((&args[..1], args[1].as_str()))
);
let args = sv(&["/d", "/s", "/k", r#"echo "a b""#]);
assert_eq!(
positional_cmd_command(&args),
Some((&args[..3], args[3].as_str()))
);
assert_eq!(
positional_cmd_command(&sv(&["/c", r"C:\Program Files\app.exe"])),
None
);
assert_eq!(
positional_cmd_command(&sv(&["/c", r#"echo "one""#, "two"])),
None
);
assert_eq!(positional_cmd_command(&sv(&["x", "/c", r#""a""#])), None);
assert_eq!(positional_cmd_command(&sv(&[r#""a""#])), None);
assert_eq!(positional_cmd_command(&[]), None);
}
#[test]
fn test_quote_arg_for_cmd_body() {
assert_eq!(quote_arg_for_cmd_body("plain"), "plain");
assert_eq!(quote_arg_for_cmd_body("a b"), r#""a b""#);
assert_eq!(quote_arg_for_cmd_body(""), r#""""#);
assert_eq!(quote_arg_for_cmd_body(r#"a"b"#), r#""a\"b""#);
assert_eq!(quote_arg_for_cmd_body(r"a b\"), r#""a b\\""#);
assert_eq!(quote_arg_for_cmd_body(r"a\b c"), r#""a\b c""#);
assert_eq!(quote_arg_for_cmd_body("a&b"), r#""a&b""#);
assert_eq!(quote_arg_for_cmd_body("foo|bar"), r#""foo|bar""#);
assert_eq!(quote_arg_for_cmd_body("a>b"), r#""a>b""#);
}
#[test]
#[cfg(windows)]
fn test_cmd_verbatim_command() {
let c = cmd_verbatim_command("cmd", &["/c".to_string()], r#"echo "a b""#).unwrap();
assert_eq!(c.get_program().to_str(), Some("cmd"));
let args: Vec<String> = c
.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect();
assert_eq!(
args,
vec![
"/s".to_string(),
"/c".to_string(),
r#""echo "a b"""#.to_string()
]
);
assert!(cmd_verbatim_command("cmd", &["/k".to_string()], "echo hi").is_some());
assert!(cmd_verbatim_command("bash", &["-c".to_string()], "echo hi").is_none());
assert!(cmd_verbatim_command("cmd", &[], "echo hi").is_none());
}
#[test]
fn test_cmd_verbatim_args_joins_multiline_scripts() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(
&[c()],
"echo one || exit /b 1\necho two || exit /b 1\n",
&[]
),
sv(&[
"/s",
"/c",
r#""echo one || exit /b 1&echo two || exit /b 1""#
])
);
assert_eq!(
cmd_verbatim_args(
&[c()],
"rem setup\r\n\r\n :: note\r\nfor %i in (a b) do echo %i\r\n echo done\r\n",
&[]
),
sv(&["/s", "/c", r#""for %i in (a b) do echo %i&echo done""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo a ^\nb\necho c", &["x y".to_string()]),
sv(&["/s", "/c", r#""echo a b&echo c "x y"""#])
);
}
#[test]
fn test_cmd_verbatim_args_multiline_blocks_and_carets() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(
&[c()],
"if exist foo (\n echo yes\n echo more\n) else (\n echo no\n)\necho after",
&[]
),
sv(&[
"/s",
"/c",
r#""if exist foo (echo yes&echo more) else (echo no)&echo after""#
])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo value^^", &[]),
sv(&["/s", "/c", r#""echo value^^""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo value^^\necho next", &[]),
sv(&["/s", "/c", r#""echo value^^&echo next""#])
);
}
#[test]
fn test_cmd_verbatim_args_blank_lines_and_escaped_parens() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(&[c()], "\necho ready\n", &[]),
sv(&["/s", "/c", r#""echo ready""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo ^(\necho next", &[]),
sv(&["/s", "/c", r#""echo ^(&echo next""#])
);
}
#[test]
fn test_cmd_verbatim_args_comments_inside_blocks() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(
&[c()],
"if exist foo (\n rem note\n\n :: more\n echo yes\n)",
&[]
),
sv(&["/s", "/c", r#""if exist foo (echo yes)""#])
);
}
#[test]
fn test_cmd_verbatim_args_continuations_and_single_line_whitespace() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(&[c()], "echo ^\nrem hello\nexit /b 3", &[]),
sv(&["/s", "/c", r#""echo rem hello&exit /b 3""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo value ", &[]),
sv(&["/s", "/c", r#""echo value ""#])
);
}
#[test]
fn test_cmd_verbatim_args_separator_adds_no_whitespace() {
assert_eq!(
cmd_verbatim_args(
&["/c".to_string()],
"set TOOL_DIR=C:\\tools\necho %TOOL_DIR%",
&[]
),
sv(&["/s", "/c", r#""set TOOL_DIR=C:\tools&echo %TOOL_DIR%""#])
);
}
#[test]
fn test_cmd_verbatim_args_echo_paren_is_not_a_block() {
assert_eq!(
cmd_verbatim_args(&["/c".to_string()], "echo(\nexit /b 1", &[]),
sv(&["/s", "/c", r#""echo(&exit /b 1""#])
);
assert_eq!(
cmd_verbatim_args(
&["/c".to_string()],
"@if 1==1 (\necho a\n)\nfor %i in (a) do (\necho %i\n)",
&[]
),
sv(&[
"/s",
"/c",
r#""@if 1==1 (echo a)&for %i in (a) do (echo %i)""#
])
);
}
#[test]
fn test_cmd_verbatim_args_block_after_another_command() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(&[c()], "echo before & if 1==1 (\necho inside\n)", &[]),
sv(&["/s", "/c", r#""echo before & if 1==1 (echo inside)""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo hello (\nexit /b 1", &[]),
sv(&["/s", "/c", r#""echo hello (&exit /b 1""#])
);
}
#[test]
fn test_cmd_verbatim_args_separator_inside_quotes_or_escaped() {
let c = || "/c".to_string();
assert_eq!(
cmd_verbatim_args(&[c()], "if \"a&b\"==\"a&b\" (\necho yes\n)", &[]),
sv(&["/s", "/c", r#""if "a&b"=="a&b" (echo yes)""#])
);
assert_eq!(
cmd_verbatim_args(&[c()], "echo a^& if (\nexit /b 1", &[]),
sv(&["/s", "/c", r#""echo a^& if (&exit /b 1""#])
);
}