From fa40f4d6aa4ee9cba5a33aee11ae32e1c1f3296a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Sun, 21 Nov 2021 22:04:44 +0100 Subject: [PATCH] Add some unit tests for path expansion --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index adace4e..6d103f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,46 @@ use repo::{ const GIT_MAIN_WORKTREE_DIRECTORY: &str = ".git-main-working-tree"; +#[cfg(test)] +mod tests { + use super::*; + + fn setup() { + std::env::set_var("HOME", "/home/test"); + } + + #[test] + fn check_expand_tilde() { + setup(); + assert_eq!( + expand_path(Path::new("~/file")), + Path::new("/home/test/file") + ); + } + + #[test] + fn check_expand_invalid_tilde() { + setup(); + assert_eq!( + expand_path(Path::new("/home/~/file")), + Path::new("/home/~/file") + ); + } + + #[test] + fn check_expand_home() { + setup(); + assert_eq!( + expand_path(Path::new("$HOME/file")), + Path::new("/home/test/file") + ); + assert_eq!( + expand_path(Path::new("${HOME}/file")), + Path::new("/home/test/file") + ); + } +} + fn path_as_string(path: &Path) -> String { path.to_path_buf().into_os_string().into_string().unwrap() }