From 511a3d6c6e221805681b2ef09bdcaa91896b9bea Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 28 Dec 2023 14:55:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86test=E7=9A=84util=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E5=88=B0test=E5=AE=8F=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E9=99=A4unused=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/branch.rs | 6 +- src/commands/commit.rs | 11 ++- src/commands/log.rs | 4 +- src/commands/merge.rs | 8 +- src/commands/restore.rs | 10 +-- src/commands/status.rs | 8 +- src/commands/switch.rs | 17 ++-- src/models/commit.rs | 4 +- src/models/index.rs | 16 ++-- src/models/tree.rs | 29 ++++--- src/utils/head.rs | 12 +-- src/utils/store.rs | 9 +- src/utils/util.rs | 185 ++++++++++++++++++++-------------------- 13 files changed, 163 insertions(+), 156 deletions(-) diff --git a/src/commands/branch.rs b/src/commands/branch.rs index f49d7a5..81d284a 100644 --- a/src/commands/branch.rs +++ b/src/commands/branch.rs @@ -126,10 +126,10 @@ pub fn branch( #[cfg(test)] mod test { use super::*; - use crate::{commands, utils::util}; + use crate::{commands, utils::util::test_util}; #[test] fn test_create_branch() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); // no commit: invalid object let result = create_branch("test_branch".to_string(), head::current_head_commit()); @@ -170,7 +170,7 @@ mod test { #[test] fn test_delete_branch() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); // no commit: invalid object let result = delete_branch("test_branch".to_string()); diff --git a/src/commands/commit.rs b/src/commands/commit.rs index 8981526..44b8bdb 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -38,24 +38,27 @@ pub fn commit(message: String, allow_empty: bool) { mod test { use std::path::Path; - use crate::{commands as cmd, utils::head, models, utils::util}; + use crate::{ + commands as cmd, models, + utils::{head, util::test_util}, + }; #[test] #[should_panic] fn test_commit_empty() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); super::commit("".to_string(), false); } #[test] fn test_commit() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let test_file = "a.txt"; let head_one = head::current_head_commit(); assert!(head_one.is_empty()); - util::ensure_test_file(&Path::new(test_file), "test content".into()); + test_util::ensure_test_file(&Path::new(test_file), "test content".into()); cmd::add(vec![], true, false); cmd::commit("test commit 1".to_string(), true); let head_two = head::current_head_commit(); diff --git a/src/commands/log.rs b/src/commands/log.rs index 2ca869b..d09c79b 100644 --- a/src/commands/log.rs +++ b/src/commands/log.rs @@ -70,10 +70,10 @@ fn __log(all: bool, number: Option) -> usize { #[cfg(test)] mod test { use super::super::super::commands; - use crate::utils::util; + use crate::utils::util::test_util; #[test] fn test_log() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); assert_eq!(super::__log(false, None), 0); commands::commit::commit("test commit 2".into(), true); assert_eq!(super::__log(false, Some(1)), 1); diff --git a/src/commands/merge.rs b/src/commands/merge.rs index 95cab54..8128fb7 100644 --- a/src/commands/merge.rs +++ b/src/commands/merge.rs @@ -82,12 +82,14 @@ pub fn merge(branch: String) { mod test { use std::fs; - use crate::commands::{commit, switch::switch}; - use super::*; + use crate::{ + commands::{commit, switch::switch}, + utils::util::test_util, + }; #[test] fn test_check_ff() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); util::list_workdir_files().iter().for_each(|x| fs::remove_file(x).unwrap()); commit::commit("init".to_string(), true); diff --git a/src/commands/restore.rs b/src/commands/restore.rs index b680819..86811d8 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -217,14 +217,14 @@ pub fn restore(paths: Vec, source: Option, worktree: bool, stage mod test { use std::fs; //TODO 写测试! - use crate::{commands as cmd, commands::status, models::Index, utils::util}; + use crate::{commands as cmd, commands::status, models::Index, utils::util::test_util}; use std::path::PathBuf; #[test] fn test_restore_stage() { - util::setup_test_with_empty_workdir(); + test_util::setup_test_with_empty_workdir(); let path = PathBuf::from("a.txt"); - util::ensure_no_file(&path); + 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()); @@ -232,9 +232,9 @@ mod test { #[test] fn test_restore_worktree() { - util::setup_test_with_empty_workdir(); + test_util::setup_test_with_empty_workdir(); let files = vec!["a.txt", "b.txt", "c.txt", "test/in.txt"]; - util::ensure_test_files(&files); + test_util::ensure_test_files(&files); cmd::add(vec![], true, false); assert_eq!(status::changes_to_be_committed().new.iter().count(), 4); diff --git a/src/commands/status.rs b/src/commands/status.rs index 6f15ed2..362555b 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -196,14 +196,14 @@ pub fn status() { #[cfg(test)] mod tests { use super::*; - use crate::{commands as cmd, utils::util}; + use crate::{commands as cmd, utils::util::test_util}; use std::path::Path; #[test] fn test_changes_to_be_committed() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let test_file = "a.txt"; - util::ensure_test_file(Path::new(test_file), None); + test_util::ensure_test_file(Path::new(test_file), None); cmd::commit("test commit".to_string(), true); cmd::add(vec![test_file.to_string()], false, false); @@ -215,7 +215,7 @@ mod tests { println!("{:?}", change.to_absolute()); cmd::commit("test commit".to_string(), true); - util::ensure_test_file(Path::new(test_file), Some("new content")); + test_util::ensure_test_file(Path::new(test_file), Some("new content")); cmd::add(vec![test_file.to_string()], false, false); let change = changes_to_be_committed(); assert_eq!(change.new.len(), 0); diff --git a/src/commands/switch.rs b/src/commands/switch.rs index b1c70c6..812b9e1 100644 --- a/src/commands/switch.rs +++ b/src/commands/switch.rs @@ -1,10 +1,8 @@ use colored::Colorize; use crate::{ - utils::head, models::{Commit, Hash}, - utils::store, - utils::util, + utils::{head, store, util}, }; use super::{ @@ -86,11 +84,14 @@ pub fn switch(target_branch: Option, create: Option, detach: boo #[cfg(test)] mod test { use super::*; - use crate::commands::{self as cmd}; + use crate::{ + commands::{self as cmd}, + utils::util::test_util, + }; use std::path::PathBuf; #[test] fn test_switch() { - util::setup_test_with_empty_workdir(); + test_util::setup_test_with_empty_workdir(); cmd::commit("init".to_string(), true); let test_branch_1 = "test_branch_1".to_string(); @@ -98,7 +99,7 @@ mod test { /* test 1: NoClean */ let test_file_1 = PathBuf::from("test_file_1"); - util::ensure_test_file(&test_file_1, None); + test_util::ensure_test_file(&test_file_1, None); let result = switch_to(test_branch_1.clone(), false); assert!(result.is_err()); assert!(matches!(result.unwrap_err(), SwitchErr::NoClean)); @@ -119,12 +120,12 @@ mod test { assert!(matches!(result.unwrap_err(), SwitchErr::InvalidObject)); let tees_file_2 = PathBuf::from("test_file_2"); - util::ensure_test_file(&tees_file_2, None); + test_util::ensure_test_file(&tees_file_2, None); cmd::add(vec![], true, false); // add all cmd::commit("add file 2".to_string(), false); let history_commit = head::current_head_commit(); // commit: test_file_1 exists, test_file_2 exists - util::ensure_no_file(&test_file_1); + test_util::ensure_no_file(&test_file_1); cmd::add(vec![], true, false); // add all assert!(!test_file_1.exists()); cmd::commit("delete file 1".to_string(), false); diff --git a/src/models/commit.rs b/src/models/commit.rs index 1cb571c..5d57ffb 100644 --- a/src/models/commit.rs +++ b/src/models/commit.rs @@ -82,11 +82,11 @@ impl Commit { #[cfg(test)] mod test { - use crate::utils::util; + use crate::utils::util::test_util; #[test] fn test_commit() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let index = super::Index::get_instance(); let mut commit = super::Commit::new(&index, vec!["123".to_string(), "456".to_string()], "test".to_string()); diff --git a/src/models/index.rs b/src/models/index.rs index 2fcdb68..368d3fa 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -209,12 +209,12 @@ impl Index { #[cfg(test)] mod tests { use super::*; - use crate::utils::util; + use crate::utils::util::test_util; use std::fs; #[test] fn test_meta_get() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let metadata = fs::metadata(".mit/HEAD").unwrap(); println!("{:?}", util::format_time(&metadata.created().unwrap())); println!("{:?}", util::format_time(&metadata.modified().unwrap())); @@ -223,20 +223,20 @@ mod tests { #[test] fn test_load() { - util::setup_test_with_clean_mit(); - let index = Index::get_instance(); + test_util::setup_test_with_clean_mit(); + let index = Index::new(); println!("{:?}", index); } #[test] fn test_save() { - util::setup_test_with_clean_mit(); - let index = Index::get_instance(); + test_util::setup_test_with_clean_mit(); + let mut index = Index::new(); let path = PathBuf::from("../mit_test_storage/.mit/HEAD"); //测试../相对路径的处理 index.add(path.clone(), FileMetaData::new(&Blob::new(&path), &path)); let 中文路径 = "中文路径.txt"; - util::ensure_test_file(Path::new(中文路径), None); + test_util::ensure_test_file(Path::new(中文路径), None); let path = PathBuf::from(中文路径); index.add(path.clone(), FileMetaData::new(&Blob::new(&path), &path)); index.save(); @@ -245,7 +245,7 @@ mod tests { #[test] fn test_save_load() { - util::setup_test_with_empty_workdir(); + test_util::setup_test_with_empty_workdir(); let index = Index::get_instance(); let path = PathBuf::from(".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 90e03d7..914d763 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -157,15 +157,18 @@ impl Tree { mod test { use std::path::PathBuf; - use crate::{models::*, utils::util}; + use crate::{ + models::*, + utils::{util, util::test_util}, + }; #[test] fn test_new() { - util::setup_test_with_clean_mit(); - let index = Index::get_instance(); + test_util::setup_test_with_clean_mit(); + let mut index = Index::new(); for test_file in vec!["b.txt", "mit_src/a.txt", "test/test.txt"] { let test_file = PathBuf::from(test_file); - util::ensure_test_file(&test_file, None); + test_util::ensure_test_file(&test_file, None); index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } @@ -177,12 +180,12 @@ mod test { #[test] fn test_load() { - util::setup_test_with_clean_mit(); - let index = Index::get_instance(); + test_util::setup_test_with_clean_mit(); + let mut index = Index::new(); let test_files = vec!["b.txt", "mit_src/a.txt"]; for test_file in test_files.clone() { let test_file = PathBuf::from(test_file); - util::ensure_test_file(&test_file, None); + test_util::ensure_test_file(&test_file, None); index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } @@ -197,11 +200,11 @@ mod test { #[test] fn test_get_recursive_file_entries() { - util::setup_test_with_clean_mit(); - let index = Index::get_instance(); + test_util::setup_test_with_clean_mit(); + let mut index = Index::new(); let mut test_files = vec![PathBuf::from("b.txt"), PathBuf::from("mit_src/a.txt")]; for test_file in test_files.clone() { - util::ensure_test_file(&test_file, None); + test_util::ensure_test_file(&test_file, None); index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } @@ -225,13 +228,13 @@ mod test { #[test] fn test_get_recursive_blobs() { - util::setup_test_with_clean_mit(); - let index = Index::get_instance(); + test_util::setup_test_with_clean_mit(); + let mut index = Index::new(); let test_files = vec!["b.txt", "mit_src/a.txt"]; let mut test_blobs = vec![]; for test_file in test_files.clone() { let test_file = PathBuf::from(test_file); - util::ensure_test_file(&test_file, None); + test_util::ensure_test_file(&test_file, None); let blob = Blob::new(&test_file); test_blobs.push(blob.clone()); index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); diff --git a/src/utils/head.rs b/src/utils/head.rs index 342c658..b109ccc 100644 --- a/src/utils/head.rs +++ b/src/utils/head.rs @@ -113,12 +113,12 @@ pub fn change_head_to_commit(commit_hash: &String) { #[cfg(test)] mod test { - use crate::utils::util; + use crate::utils::util::test_util; use crate::utils::head; #[test] fn test_edit_branch() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let branch_name = "test_branch".to_string() + &rand::random::().to_string(); let branch_head = super::get_branch_head(&branch_name); assert!(branch_head.is_empty()); @@ -132,7 +132,7 @@ mod test { #[test] fn test_list_local_branches() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let branch_one = "test_branch".to_string() + &rand::random::().to_string(); let branch_two = "test_branch".to_string() + &rand::random::().to_string(); head::update_branch(&branch_one, &"1234567890".to_string()); @@ -145,7 +145,7 @@ mod test { #[test] fn test_change_head_to_branch() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let branch_name = "test_branch".to_string() + &rand::random::().to_string(); head::update_branch(&branch_name, &"1234567890".to_string()); super::change_head_to_branch(&branch_name); @@ -160,7 +160,7 @@ mod test { #[test] fn test_change_head_to_commit() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let commit_hash = "1234567890".to_string(); super::change_head_to_commit(&commit_hash); assert!( @@ -174,7 +174,7 @@ mod test { #[test] fn test_update_branch_head() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let branch_name = "test_branch".to_string() + &rand::random::().to_string(); let commit_hash = "1234567890".to_string(); super::update_branch(&branch_name, &commit_hash); diff --git a/src/utils/store.rs b/src/utils/store.rs index 96d3d6b..1693075 100644 --- a/src/utils/store.rs +++ b/src/utils/store.rs @@ -87,23 +87,24 @@ mod tests { use std::fs; use super::*; + use util::test_util; #[test] fn test_new_success() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let _ = Store::new(); } #[test] #[should_panic] fn test_new_fail() { - util::setup_test_without_mit(); + test_util::setup_test_without_mit(); let _ = Store::new(); } #[test] fn test_save_and_load() { - let _ = util::setup_test_with_clean_mit(); + let _ = test_util::setup_test_with_clean_mit(); let store = Store::new(); let content = "hello world".to_string(); let hash = store.save(&content); @@ -113,7 +114,7 @@ mod tests { #[test] fn test_search() { - util::setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let hashs = vec!["1234567890".to_string(), "1235467891".to_string(), "4567892".to_string()]; for hash in hashs.iter() { let mut path = util::get_storage_path().unwrap(); diff --git a/src/utils/util.rs b/src/utils/util.rs index e00e244..f6e3950 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -9,108 +9,105 @@ use std::{ use crate::models::{commit::Commit, object::Hash, tree::Tree, Index}; pub const ROOT_DIR: &str = ".mit"; -pub const TEST_DIR: &str = "mit_test_storage"; // 执行测试的储存库 -/* tools for test */ -fn find_cargo_dir() -> PathBuf { - let cargo_path = std::env::var("CARGO_MANIFEST_DIR"); - if cargo_path.is_err() { - // vscode DEBUG test没有CARGO_MANIFEST_DIR宏,手动尝试查找cargo.toml - let mut path = cur_dir(); - loop { - path.push("Cargo.toml"); - if path.exists() { - break; +#[cfg(test)] +pub mod test_util { + use super::*; + + pub const TEST_DIR: &str = "mit_test_storage"; // 执行测试的储存库 + /* tools for test */ + fn find_cargo_dir() -> PathBuf { + let cargo_path = std::env::var("CARGO_MANIFEST_DIR"); + if cargo_path.is_err() { + // vscode DEBUG test没有CARGO_MANIFEST_DIR宏,手动尝试查找cargo.toml + let mut path = cur_dir(); + loop { + path.push("Cargo.toml"); + if path.exists() { + break; + } + if !path.pop() { + panic!("找不到CARGO_MANIFEST_DIR"); + } } - if !path.pop() { - panic!("找不到CARGO_MANIFEST_DIR"); - } - } - path.pop(); - path - } else { - PathBuf::from(cargo_path.unwrap()) - } -} - -/// 准备测试环境,切换到测试目录 -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(); // 将执行目录切换到测试目录 -} - -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(); - init_mit(); -} - -pub fn setup_test_without_mit() { - // 将执行目录切换到测试目录,并清除测试目录下的.mit目录 - setup_test_env(); - let mut path = cur_dir(); - path.push(ROOT_DIR); - if path.exists() { - fs::remove_dir_all(&path).unwrap(); - } -} - -pub fn ensure_test_files>(paths: &Vec) { - for path in paths { - ensure_test_file(path.as_ref().as_ref(), None); - } -} - -pub fn ensure_empty_dir>(path: P) -> io::Result<()> { - let entries = fs::read_dir(path.as_ref())?; - for entry in entries { - let path = entry?.path(); - if path.is_dir() { - fs::remove_dir_all(&path)?; // 如果是目录,则递归删除 + path.pop(); + path } else { - fs::remove_file(&path)?; // 如果是文件,则直接删除 + PathBuf::from(cargo_path.unwrap()) } } - Ok(()) -} -pub fn setup_test_with_empty_workdir() { - let test_dir = find_cargo_dir().join(TEST_DIR); - ensure_empty_dir(&test_dir).unwrap(); - setup_test_with_clean_mit(); -} + fn setup_test_dir() { + 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(); + } -pub fn ensure_test_file(path: &Path, content: Option<&str>) { - // 以测试目录为根目录,创建文件 - fs::create_dir_all(path.parent().unwrap()).unwrap(); // ensure父目录 - let mut file = - fs::File::create(get_working_dir().unwrap().join(path)).expect(format!("无法创建文件:{:?}", path).as_str()); - if let Some(content) = content { - file.write(content.as_bytes()).unwrap(); - } else { - // 写入文件名 - file.write(path.file_name().unwrap().to_str().unwrap().as_bytes()).unwrap(); + /// with 初始化的干净的mit + pub fn setup_test_with_clean_mit() { + setup_test_without_mit(); + let _ = crate::commands::init::init(); + } + + pub fn setup_test_without_mit() { + // 将执行目录切换到测试目录,并清除测试目录下的.mit目录 + setup_test_dir(); + let mut path = std::env::current_dir().unwrap(); + path.push(ROOT_DIR); + if path.exists() { + fs::remove_dir_all(&path).unwrap(); + } + } + + pub fn ensure_test_files>(paths: &Vec) { + for path in paths { + ensure_test_file(path.as_ref().as_ref(), None); + } + } + + pub fn ensure_empty_dir>(path: P) -> io::Result<()> { + let entries = fs::read_dir(path.as_ref())?; + for entry in entries { + let path = entry?.path(); + if path.is_dir() { + fs::remove_dir_all(&path)?; // 如果是目录,则递归删除 + } else { + fs::remove_file(&path)?; // 如果是文件,则直接删除 + } + } + Ok(()) + } + + pub fn setup_test_with_empty_workdir() { + let test_dir = find_cargo_dir().join(TEST_DIR); + ensure_empty_dir(&test_dir).unwrap(); + setup_test_with_clean_mit(); + } + + pub fn ensure_test_file(path: &Path, content: Option<&str>) { + // 以测试目录为根目录,创建文件 + fs::create_dir_all(path.parent().unwrap()).unwrap(); // ensure父目录 + let mut file = fs::File::create(get_working_dir().unwrap().join(path)) + .expect(format!("无法创建文件:{:?}", path).as_str()); + if let Some(content) = content { + file.write(content.as_bytes()).unwrap(); + } else { + // 写入文件名 + file.write(path.file_name().unwrap().to_str().unwrap().as_bytes()).unwrap(); + } + } + + 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(); @@ -534,7 +531,7 @@ 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();