diff --git a/src/commands/restore.rs b/src/commands/restore.rs index 86811d8..178ba94 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -67,7 +67,7 @@ pub fn restore_worktree(filter: Option<&Vec>, target_blobs: &Vec<(PathB let mut file_paths = util::integrate_paths(&input_paths); //根据用户输入整合存在的文件(绝对路径) file_paths.extend(deleted_files); //已删除的文件 - let index = Index::new(); + let index = Index::get_instance(); let store = store::Store::new(); for path in &file_paths { @@ -139,7 +139,6 @@ pub fn restore_index(filter: Option<&Vec>, target_blobs: &Vec<(PathBuf, } } } - index.save(); } /** 对于工作区中的新文件,若已跟踪,则删除;若未跟踪,则保留
@@ -227,7 +226,8 @@ mod test { test_util::ensure_no_file(&path); cmd::add(vec![], true, false); //add -A cmd::restore(vec![".".to_string()], Some("HEAD".to_string()), false, true); - assert!(Index::get_instance().is_empty()); + let index = Index::get_instance(); + assert!(index.get_tracked_files().is_empty()); } #[test] diff --git a/src/models/index.rs b/src/models/index.rs index 368d3fa..2a691b6 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -55,7 +55,7 @@ pub struct Index { impl Index { /// 从index文件加载 - pub fn new() -> Index { + fn new() -> Index { let mut index = Index::default(); index.load(); return index; @@ -67,7 +67,7 @@ impl Index { unsafe { &mut INSTANCE } } - /// 重置index 从文件重新加载,主要用于测试,防止单例模式的影响 + /// 重置index,主要用于测试,防止单例模式的影响 pub fn reload() { let index = Index::get_instance(); index.load(); @@ -224,14 +224,14 @@ mod tests { #[test] fn test_load() { test_util::setup_test_with_clean_mit(); - let index = Index::new(); + let index = Index::get_instance(); println!("{:?}", index); } #[test] fn test_save() { test_util::setup_test_with_clean_mit(); - let mut index = Index::new(); + let index = Index::get_instance(); let path = PathBuf::from("../mit_test_storage/.mit/HEAD"); //测试../相对路径的处理 index.add(path.clone(), FileMetaData::new(&Blob::new(&path), &path)); diff --git a/src/models/tree.rs b/src/models/tree.rs index 914d763..adefd84 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -165,7 +165,7 @@ mod test { #[test] fn test_new() { test_util::setup_test_with_clean_mit(); - let mut index = Index::new(); + let index = Index::get_instance(); for test_file in vec!["b.txt", "mit_src/a.txt", "test/test.txt"] { let test_file = PathBuf::from(test_file); test_util::ensure_test_file(&test_file, None); @@ -181,7 +181,7 @@ mod test { #[test] fn test_load() { test_util::setup_test_with_clean_mit(); - let mut index = Index::new(); + let index = Index::get_instance(); let test_files = vec!["b.txt", "mit_src/a.txt"]; for test_file in test_files.clone() { let test_file = PathBuf::from(test_file); @@ -201,7 +201,7 @@ mod test { #[test] fn test_get_recursive_file_entries() { test_util::setup_test_with_clean_mit(); - let mut index = Index::new(); + let index = Index::get_instance(); let mut test_files = vec![PathBuf::from("b.txt"), PathBuf::from("mit_src/a.txt")]; for test_file in test_files.clone() { test_util::ensure_test_file(&test_file, None); @@ -229,7 +229,7 @@ mod test { #[test] fn test_get_recursive_blobs() { test_util::setup_test_with_clean_mit(); - let mut index = Index::new(); + let index = Index::get_instance(); let test_files = vec!["b.txt", "mit_src/a.txt"]; let mut test_blobs = vec![]; for test_file in test_files.clone() { diff --git a/src/utils/util.rs b/src/utils/util.rs index f6e3950..6bcc9d4 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -9,12 +9,10 @@ use std::{ use crate::models::{commit::Commit, object::Hash, tree::Tree, Index}; pub const ROOT_DIR: &str = ".mit"; - #[cfg(test)] pub mod test_util { - use super::*; - pub const TEST_DIR: &str = "mit_test_storage"; // 执行测试的储存库 + use super::*; /* tools for test */ fn find_cargo_dir() -> PathBuf { let cargo_path = std::env::var("CARGO_MANIFEST_DIR"); @@ -37,26 +35,33 @@ pub mod test_util { } } - fn setup_test_dir() { + /// 准备测试环境,切换到测试目录 + fn setup_test_env() { color_backtrace::install(); // colorize backtrace + let mut path = find_cargo_dir(); path.push(TEST_DIR); if !path.exists() { fs::create_dir(&path).unwrap(); } - std::env::set_current_dir(&path).unwrap(); + std::env::set_current_dir(&path).unwrap(); // 将执行目录切换到测试目录 + } + + pub fn init_mit() { + let _ = crate::commands::init(); + Index::reload(); // 重置index, 以防止其他测试修改了index单例 } /// with 初始化的干净的mit pub fn setup_test_with_clean_mit() { setup_test_without_mit(); - let _ = crate::commands::init::init(); + init_mit(); } pub fn setup_test_without_mit() { // 将执行目录切换到测试目录,并清除测试目录下的.mit目录 - setup_test_dir(); - let mut path = std::env::current_dir().unwrap(); + setup_test_env(); + let mut path = cur_dir(); path.push(ROOT_DIR); if path.exists() { fs::remove_dir_all(&path).unwrap(); @@ -101,13 +106,13 @@ pub mod test_util { } } - pub fn ensure_no_file(path: &Path) { - // 以测试目录为根目录,删除文件 - if path.exists() { - fs::remove_file(get_working_dir().unwrap().join(path)).unwrap(); - } +pub fn ensure_no_file(path: &Path) { + // 以测试目录为根目录,删除文件 + if path.exists() { + fs::remove_file(get_working_dir().unwrap().join(path)).unwrap(); } } +} /* tools for mit */ pub fn calc_hash(data: &String) -> String { let mut hasher = Sha1::new(); @@ -529,9 +534,9 @@ pub fn is_typeof_commit(hash: Hash) -> bool { #[cfg(test)] mod tests { use crate::models::{blob::Blob, index::Index}; - use super::*; use super::test_util::*; + #[test] fn test_get_storage_path() { let path = get_storage_path();