use std::{ffi::OsStr, path::PathBuf};
use super::*;
#[cfg(unix)]
#[test]
fn unix_editor_command_supports_arguments_and_quoted_paths() {
let parts = editor_parts(OsStr::new("'/opt/My Editor/bin/editor' --wait")).unwrap();
assert_eq!(
parts,
[
OsString::from("/opt/My Editor/bin/editor"),
OsString::from("--wait")
]
);
}
#[test]
fn existing_editor_path_is_not_split() {
let executable = tempfile::NamedTempFile::new().unwrap();
let path = executable.path().as_os_str();
assert_eq!(editor_parts(path).unwrap(), [path]);
}
#[cfg(windows)]
#[test]
fn windows_editor_command_preserves_paths_flags_and_empty_arguments() {
let parts = editor_parts(OsStr::new(
r#""C:\Program Files\Editor\editor.exe" --wait """#,
))
.unwrap();
assert_eq!(
parts,
[
OsString::from(r"C:\Program Files\Editor\editor.exe"),
OsString::from("--wait"),
OsString::new(),
]
);
}
#[cfg(windows)]
#[test]
fn windows_editor_command_preserves_unquoted_backslashes() {
let parts = editor_parts(OsStr::new(r"C:\tools\vim.exe --nofork")).unwrap();
assert_eq!(
parts,
[
OsString::from(r"C:\tools\vim.exe"),
OsString::from("--nofork"),
]
);
}
#[test]
fn editor_command_rejects_whitespace_only_values() {
let error = editor_command(OsStr::new(" ")).unwrap_err();
assert_eq!(error.to_string(), "editor command is empty");
}
#[test]
fn resolve_editor_prefers_visual_over_editor() {
let editor = resolve_editor(
Some(OsString::from("visual-editor")),
Some(OsString::from("fallback-editor")),
);
assert_eq!(editor, Some(OsString::from("visual-editor")));
}
#[test]
fn resolve_editor_requires_a_configured_editor() {
assert_eq!(resolve_editor(None, None), None);
assert_eq!(resolve_editor(Some(OsString::new()), None), None);
assert_eq!(resolve_editor(None, Some(OsString::new())), None);
}
#[test]
fn editor_parts_preserves_nonexistent_direct_path_shape() {
let parts = editor_parts(OsStr::new("/missing/editor --wait")).unwrap();
assert_eq!(
parts,
[OsString::from("/missing/editor"), OsString::from("--wait")]
);
assert_eq!(PathBuf::from(&parts[0]), PathBuf::from("/missing/editor"));
}