diff --git a/src/ui.rs b/src/ui.rs index 324e4e525..986b67f79 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -206,3 +206,132 @@ pub fn relative_path(mut from: &Path, to: &Path) -> PathBuf { } result } + +#[cfg(test)] +mod tests { + use std::io::Cursor; + + use super::*; + + #[test] + fn parse_file_path_wc_in_cwd() { + let temp_dir = tempfile::tempdir().unwrap(); + let cwd_path = temp_dir.path().join("repo"); + let wc_path = cwd_path.clone(); + let mut unused_stdout_buf = vec![]; + let mut unused_stderr_buf = vec![]; + let unused_stdout = Box::new(Cursor::new(&mut unused_stdout_buf)); + let unused_stderr = Box::new(Cursor::new(&mut unused_stderr_buf)); + let ui = Ui::new( + cwd_path, + unused_stdout, + unused_stderr, + false, + UserSettings::default(), + ); + + assert_eq!(ui.parse_file_path(&wc_path, ""), Ok(RepoPath::root())); + assert_eq!(ui.parse_file_path(&wc_path, "."), Ok(RepoPath::root())); + assert_eq!( + ui.parse_file_path(&wc_path, "file"), + Ok(RepoPath::from_internal_string("file")) + ); + // Both slash and the platform's separator are allowed + assert_eq!( + ui.parse_file_path(&wc_path, &format!("dir{}file", std::path::MAIN_SEPARATOR)), + Ok(RepoPath::from_internal_string("dir/file")) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "dir/file"), + Ok(RepoPath::from_internal_string("dir/file")) + ); + assert_eq!( + ui.parse_file_path(&wc_path, ".."), + Err(FilePathParseError::InputNotInRepo("..".to_string())) + ); + // TODO: handle these cases: + // assert_eq!(ui.parse_file_path(&cwd_path, "../repo"), + // Ok(RepoPath::root())); assert_eq!(ui.parse_file_path(&cwd_path, + // "../repo/file"), Ok(RepoPath::from_internal_string("file"))); + } + + #[test] + fn parse_file_path_wc_in_cwd_parent() { + let temp_dir = tempfile::tempdir().unwrap(); + let cwd_path = temp_dir.path().join("dir"); + let wc_path = cwd_path.parent().unwrap().to_path_buf(); + let mut unused_stdout_buf = vec![]; + let mut unused_stderr_buf = vec![]; + let unused_stdout = Box::new(Cursor::new(&mut unused_stdout_buf)); + let unused_stderr = Box::new(Cursor::new(&mut unused_stderr_buf)); + let ui = Ui::new( + cwd_path, + unused_stdout, + unused_stderr, + false, + UserSettings::default(), + ); + + assert_eq!( + ui.parse_file_path(&wc_path, ""), + Ok(RepoPath::from_internal_string("dir")) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "."), + Ok(RepoPath::from_internal_string("dir")) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "file"), + Ok(RepoPath::from_internal_string("dir/file")) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "subdir/file"), + Ok(RepoPath::from_internal_string("dir/subdir/file")) + ); + assert_eq!(ui.parse_file_path(&wc_path, ".."), Ok(RepoPath::root())); + assert_eq!( + ui.parse_file_path(&wc_path, "../.."), + Err(FilePathParseError::InputNotInRepo("../..".to_string())) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "../other-dir/file"), + Ok(RepoPath::from_internal_string("other-dir/file")) + ); + } + + #[test] + fn parse_file_path_wc_in_cwd_child() { + let temp_dir = tempfile::tempdir().unwrap(); + let cwd_path = temp_dir.path().join("cwd"); + let wc_path = cwd_path.join("repo"); + let mut unused_stdout_buf = vec![]; + let mut unused_stderr_buf = vec![]; + let unused_stdout = Box::new(Cursor::new(&mut unused_stdout_buf)); + let unused_stderr = Box::new(Cursor::new(&mut unused_stderr_buf)); + let ui = Ui::new( + cwd_path, + unused_stdout, + unused_stderr, + false, + UserSettings::default(), + ); + + assert_eq!( + ui.parse_file_path(&wc_path, ""), + Err(FilePathParseError::InputNotInRepo("".to_string())) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "not-repo"), + Err(FilePathParseError::InputNotInRepo("not-repo".to_string())) + ); + assert_eq!(ui.parse_file_path(&wc_path, "repo"), Ok(RepoPath::root())); + assert_eq!( + ui.parse_file_path(&wc_path, "repo/file"), + Ok(RepoPath::from_internal_string("file")) + ); + assert_eq!( + ui.parse_file_path(&wc_path, "repo/dir/file"), + Ok(RepoPath::from_internal_string("dir/file")) + ); + } +} diff --git a/tests/test_ui.rs b/tests/test_ui.rs deleted file mode 100644 index 51a615754..000000000 --- a/tests/test_ui.rs +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2021 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::io::Cursor; - -use jujutsu::ui::{FilePathParseError, Ui}; -use jujutsu_lib::repo_path::RepoPath; -use jujutsu_lib::testutils::user_settings; - -#[test] -fn test_parse_file_path_wc_in_cwd() { - let temp_dir = tempfile::tempdir().unwrap(); - let cwd_path = temp_dir.path().join("repo"); - let wc_path = cwd_path.clone(); - let mut unused_stdout_buf = vec![]; - let mut unused_stderr_buf = vec![]; - let unused_stdout = Box::new(Cursor::new(&mut unused_stdout_buf)); - let unused_stderr = Box::new(Cursor::new(&mut unused_stderr_buf)); - let ui = Ui::new( - cwd_path, - unused_stdout, - unused_stderr, - false, - user_settings(), - ); - - assert_eq!(ui.parse_file_path(&wc_path, ""), Ok(RepoPath::root())); - assert_eq!(ui.parse_file_path(&wc_path, "."), Ok(RepoPath::root())); - assert_eq!( - ui.parse_file_path(&wc_path, "file"), - Ok(RepoPath::from_internal_string("file")) - ); - // Both slash and the platform's separator are allowed - assert_eq!( - ui.parse_file_path(&wc_path, &format!("dir{}file", std::path::MAIN_SEPARATOR)), - Ok(RepoPath::from_internal_string("dir/file")) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "dir/file"), - Ok(RepoPath::from_internal_string("dir/file")) - ); - assert_eq!( - ui.parse_file_path(&wc_path, ".."), - Err(FilePathParseError::InputNotInRepo("..".to_string())) - ); - // TODO: handle these cases: - // assert_eq!(ui.parse_file_path(&cwd_path, "../repo"), - // Ok(RepoPath::root())); assert_eq!(ui.parse_file_path(&cwd_path, - // "../repo/file"), Ok(RepoPath::from_internal_string("file"))); -} - -#[test] -fn test_parse_file_path_wc_in_cwd_parent() { - let temp_dir = tempfile::tempdir().unwrap(); - let cwd_path = temp_dir.path().join("dir"); - let wc_path = cwd_path.parent().unwrap().to_path_buf(); - let mut unused_stdout_buf = vec![]; - let mut unused_stderr_buf = vec![]; - let unused_stdout = Box::new(Cursor::new(&mut unused_stdout_buf)); - let unused_stderr = Box::new(Cursor::new(&mut unused_stderr_buf)); - let ui = Ui::new( - cwd_path, - unused_stdout, - unused_stderr, - false, - user_settings(), - ); - - assert_eq!( - ui.parse_file_path(&wc_path, ""), - Ok(RepoPath::from_internal_string("dir")) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "."), - Ok(RepoPath::from_internal_string("dir")) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "file"), - Ok(RepoPath::from_internal_string("dir/file")) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "subdir/file"), - Ok(RepoPath::from_internal_string("dir/subdir/file")) - ); - assert_eq!(ui.parse_file_path(&wc_path, ".."), Ok(RepoPath::root())); - assert_eq!( - ui.parse_file_path(&wc_path, "../.."), - Err(FilePathParseError::InputNotInRepo("../..".to_string())) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "../other-dir/file"), - Ok(RepoPath::from_internal_string("other-dir/file")) - ); -} - -#[test] -fn test_parse_file_path_wc_in_cwd_child() { - let temp_dir = tempfile::tempdir().unwrap(); - let cwd_path = temp_dir.path().join("cwd"); - let wc_path = cwd_path.join("repo"); - let mut unused_stdout_buf = vec![]; - let mut unused_stderr_buf = vec![]; - let unused_stdout = Box::new(Cursor::new(&mut unused_stdout_buf)); - let unused_stderr = Box::new(Cursor::new(&mut unused_stderr_buf)); - let ui = Ui::new( - cwd_path, - unused_stdout, - unused_stderr, - false, - user_settings(), - ); - - assert_eq!( - ui.parse_file_path(&wc_path, ""), - Err(FilePathParseError::InputNotInRepo("".to_string())) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "not-repo"), - Err(FilePathParseError::InputNotInRepo("not-repo".to_string())) - ); - assert_eq!(ui.parse_file_path(&wc_path, "repo"), Ok(RepoPath::root())); - assert_eq!( - ui.parse_file_path(&wc_path, "repo/file"), - Ok(RepoPath::from_internal_string("file")) - ); - assert_eq!( - ui.parse_file_path(&wc_path, "repo/dir/file"), - Ok(RepoPath::from_internal_string("dir/file")) - ); -}