use std::path::Path; use gix_testtools::Result; #[test] fn extract_interpreter() -> gix_testtools::Result { let root = gix_testtools::scripted_fixture_read_only("win_path_lookup.sh")?; assert_eq!( gix_command::extract_interpreter(&root.join("b").join("exe")), Some(gix_command::shebang::Data { interpreter: Path::new("/b/exe").into(), args: vec![] }) ); Ok(()) } mod shebang { mod parse { use gix_command::shebang; fn parse(input: &str) -> Option { shebang::parse(input.into()) } fn exe(name: &str) -> Option { shebang::Data { interpreter: name.into(), args: Vec::new(), } .into() } fn exe_arg(name: &str, arg: &str) -> Option { shebang::Data { interpreter: name.into(), args: shell_words::split(arg) .expect("can parse") .into_iter() .map(Into::into) .collect(), } .into() } #[test] fn valid() { assert_eq!(parse("#!/bin/sh"), exe("/bin/sh")); assert_eq!(parse("#!/bin/sh "), exe("/bin/sh"), "trim trailing whitespace"); assert_eq!( parse("#!/bin/sh\t\nother"), exe("/bin/sh"), "trimming works for tabs as well" ); assert_eq!( parse(r"#!\bin\sh"), exe(r"\bin\sh"), "backslashes are recognized as path separator" ); assert_eq!( parse("#!C:\\Program Files\\shell.exe\r\nsome stuff"), exe(r"C:\Program Files\shell.exe"), "absolute windows paths are fine" ); assert_eq!( parse("#!/bin/sh -i -o -u\nunrelated content"), exe_arg("/bin/sh", "-i -o -u"), "argument splitting happens as well" ); assert_eq!( parse("#!/bin/sh -o\nunrelated content"), exe_arg("/bin/sh", "-o"), "single arguments are OK too" ); assert_eq!( parse("#!/bin/exe anything goes\nunrelated content"), exe_arg("/bin/exe", "anything goes"), "any argument works" ); #[cfg(unix)] { use std::os::unix::ffi::OsStrExt; use bstr::ByteSlice; assert_eq!( shebang::parse(b"#!/bin/sh -x \xC3\x28\x41 -y ".as_bstr()), Some(shebang::Data { interpreter: "/bin/sh".into(), args: vec![std::ffi::OsStr::from_bytes(b"-x \xC3\x28\x41 -y").to_owned()] }), "illformed UTF8 in the arguments leads to them not being split - useful in case it's just one path or so" ); assert_eq!( shebang::parse(b"#!/bin/\xC3\x28\x41 ".as_bstr()), Some(shebang::Data { interpreter: std::ffi::OsStr::from_bytes(b"/bin/\xC3\x28\x41").to_owned().into(), args: vec![] }), "illformed UTF8 in the executable path is fine as well" ); } } #[test] fn invalid() { assert_eq!(parse(""), None); assert_eq!(parse("missing shebang"), None); assert_eq!(parse("#!missing-slash"), None); assert_eq!( parse("/bin/sh"), None, "shebang missing, even though a valid path is given" ); } } } mod context { use gix_command::Context; fn winfix(expected: impl Into) -> String { // Unclear why it's not debug-printing the env on windows. if cfg!(windows) { "\"\"".into() } else { expected.into() } } #[test] fn git_dir_sets_git_dir_env_and_cwd() { let ctx = Context { git_dir: Some(".".into()), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!(format!("{cmd:?}"), winfix(r#"GIT_DIR="." """#)); } #[test] fn worktree_dir_sets_env_only() { let ctx = Context { worktree_dir: Some(".".into()), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!(format!("{cmd:?}"), winfix(r#"GIT_WORK_TREE="." """#)); } #[test] fn no_replace_objects_sets_env_only() { for value in [false, true] { let expected = usize::from(value); let ctx = Context { no_replace_objects: Some(value), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!( format!("{cmd:?}"), winfix(format!(r#"GIT_NO_REPLACE_OBJECTS="{expected}" """#)) ); } } #[test] fn ref_namespace_sets_env_only() { let ctx = Context { ref_namespace: Some("namespace".into()), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!(format!("{cmd:?}"), winfix(r#"GIT_NAMESPACE="namespace" """#)); } #[test] fn literal_pathspecs_sets_env_only() { for value in [false, true] { let expected = usize::from(value); let ctx = Context { literal_pathspecs: Some(value), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!( format!("{cmd:?}"), winfix(format!(r#"GIT_LITERAL_PATHSPECS="{expected}" """#)) ); } } #[test] fn glob_pathspecs_sets_env_only() { for (value, expected) in [ (false, r#"GIT_NOGLOB_PATHSPECS="1""#), (true, r#"GIT_GLOB_PATHSPECS="1""#), ] { let ctx = Context { glob_pathspecs: Some(value), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!(format!("{cmd:?}"), winfix(format!(r#"{expected} """#))); } } #[test] fn icase_pathspecs_sets_env_only() { for value in [false, true] { let expected = usize::from(value); let ctx = Context { icase_pathspecs: Some(value), ..Default::default() }; let cmd = std::process::Command::from(gix_command::prepare("").with_context(ctx)); assert_eq!( format!("{cmd:?}"), winfix(format!(r#"GIT_ICASE_PATHSPECS="{expected}" """#)) ); } } } mod prepare { use std::sync::LazyLock; static SH: LazyLock<&'static str> = LazyLock::new(|| { gix_path::env::shell() .to_str() .expect("`prepare` tests must be run where 'sh' path is valid Unicode") }); // The basename of the default shell, used as the `command_name` operand // after `-c