diff --git a/src/notify.rs b/src/notify.rs index b03a3ff..b1e1684 100644 --- a/src/notify.rs +++ b/src/notify.rs @@ -170,18 +170,42 @@ mod tests { #[test] fn test_expand_tilde() { if let Some(home) = dirs::home_dir() { + // bare `~` -> home dir assert_eq!(expand_tilde("~"), home); - assert_eq!(expand_tilde("~/foo/bar.wav"), home.join("foo/bar.wav")); + // `~/sub/dir/file.wav` -> home joined with `sub/dir/file.wav` + assert_eq!(expand_tilde("~/sub/dir/file.wav"), home.join("sub/dir/file.wav")); } - assert_eq!(expand_tilde("/absolute/path.wav"), PathBuf::from("/absolute/path.wav")); - assert_eq!(expand_tilde("relative/path.wav"), PathBuf::from("relative/path.wav")); - assert_eq!(expand_tilde(" "), PathBuf::from("")); + // absolute path (`/etc/foo.wav`) -> unchanged + assert_eq!(expand_tilde("/etc/foo.wav"), PathBuf::from("/etc/foo.wav")); + // relative path (`foo/bar.wav`) -> unchanged + assert_eq!(expand_tilde("foo/bar.wav"), PathBuf::from("foo/bar.wav")); + // a tilde not at the start (`/opt/~/x.wav`) -> unchanged + assert_eq!(expand_tilde("/opt/~/x.wav"), PathBuf::from("/opt/~/x.wav")); + // `~username` style (`~bob/x.wav`) -> unchanged + assert_eq!(expand_tilde("~bob/x.wav"), PathBuf::from("~bob/x.wav")); + // leading/trailing whitespace is trimmed + if let Some(home) = dirs::home_dir() { + assert_eq!(expand_tilde(" ~ "), home); + assert_eq!(expand_tilde(" ~/sub/dir/file.wav "), home.join("sub/dir/file.wav")); + } + assert_eq!(expand_tilde(" /etc/foo.wav "), PathBuf::from("/etc/foo.wav")); + assert_eq!(expand_tilde(" foo/bar.wav "), PathBuf::from("foo/bar.wav")); } #[test] fn test_validate_custom_path() { + // empty and whitespace-only -> None assert_eq!(validate_custom_path(""), None); assert_eq!(validate_custom_path(" "), None); + // a path that does not exist -> Some(false) assert_eq!(validate_custom_path("/non/existent/file.wav"), Some(false)); + // a real existing file -> Some(true) + let existing_file = concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml"); + assert_eq!(validate_custom_path(existing_file), Some(true)); + // an existing directory -> Some(false) + let existing_dir = env!("CARGO_MANIFEST_DIR"); + assert_eq!(validate_custom_path(existing_dir), Some(false)); + // a `~`-prefixed path that resolves to a non-existent file -> Some(false) + assert_eq!(validate_custom_path("~/non/existent/file.wav"), Some(false)); } }