mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
tests: move tests of Ui
from tests/
into ui
module
The tests only test the `Ui` type and don't need anything from `testutils`, so it seems they should be treated as unit tests.
This commit is contained in:
parent
781307ddbd
commit
90edd670d9
2 changed files with 129 additions and 141 deletions
129
src/ui.rs
129
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"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
141
tests/test_ui.rs
141
tests/test_ui.rs
|
@ -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"))
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue