diff --git a/src/commands/branch.rs b/src/commands/branch.rs index 81d284a..50f02f1 100644 --- a/src/commands/branch.rs +++ b/src/commands/branch.rs @@ -126,7 +126,7 @@ pub fn branch( #[cfg(test)] mod test { use super::*; - use crate::{commands, utils::util::test_util}; + use crate::{commands, utils::test_util}; #[test] fn test_create_branch() { test_util::setup_test_with_clean_mit(); diff --git a/src/commands/commit.rs b/src/commands/commit.rs index 101972d..24b46b8 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -40,7 +40,7 @@ mod test { use crate::{ commands as cmd, models, - utils::{head, util::test_util}, + utils::{head, test_util}, }; #[test] diff --git a/src/commands/log.rs b/src/commands/log.rs index d09c79b..e2ea6b6 100644 --- a/src/commands/log.rs +++ b/src/commands/log.rs @@ -70,7 +70,7 @@ fn __log(all: bool, number: Option) -> usize { #[cfg(test)] mod test { use super::super::super::commands; - use crate::utils::util::test_util; + use crate::utils::test_util; #[test] fn test_log() { test_util::setup_test_with_clean_mit(); diff --git a/src/commands/merge.rs b/src/commands/merge.rs index f39c5dc..3c7cf01 100644 --- a/src/commands/merge.rs +++ b/src/commands/merge.rs @@ -80,12 +80,10 @@ pub fn merge(branch: String) { #[cfg(test)] mod test { - use std::fs; - use super::*; use crate::{ commands::{commit, switch::switch}, - utils::util::test_util, + utils::test_util, }; #[test] diff --git a/src/commands/restore.rs b/src/commands/restore.rs index 178ba94..14cb5dd 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -216,7 +216,7 @@ 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::test_util}; + use crate::{commands as cmd, commands::status, models::Index, utils::test_util}; use std::path::PathBuf; #[test] diff --git a/src/commands/status.rs b/src/commands/status.rs index 39c1aea..21a73ee 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -196,7 +196,7 @@ pub fn status() { #[cfg(test)] mod tests { use super::*; - use crate::{commands as cmd, utils::util::test_util}; + use crate::{commands as cmd, utils::test_util}; use std::path::Path; #[test] diff --git a/src/commands/switch.rs b/src/commands/switch.rs index ec8ad41..90053a1 100644 --- a/src/commands/switch.rs +++ b/src/commands/switch.rs @@ -88,7 +88,7 @@ mod test { use super::*; use crate::{ commands::{self as cmd}, - utils::util::test_util, + utils::test_util, }; use std::path::PathBuf; #[test] diff --git a/src/models/commit.rs b/src/models/commit.rs index 5d57ffb..23442f2 100644 --- a/src/models/commit.rs +++ b/src/models/commit.rs @@ -82,7 +82,7 @@ impl Commit { #[cfg(test)] mod test { - use crate::utils::util::test_util; + use crate::utils::test_util; #[test] fn test_commit() { diff --git a/src/models/index.rs b/src/models/index.rs index 2a691b6..b6afb98 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -209,7 +209,7 @@ impl Index { #[cfg(test)] mod tests { use super::*; - use crate::utils::util::test_util; + use crate::utils::test_util; use std::fs; #[test] diff --git a/src/models/tree.rs b/src/models/tree.rs index adefd84..c8d7721 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -159,7 +159,7 @@ mod test { use crate::{ models::*, - utils::{util, util::test_util}, + utils::{util, test_util}, }; #[test] diff --git a/src/utils/head.rs b/src/utils/head.rs index b109ccc..984a65d 100644 --- a/src/utils/head.rs +++ b/src/utils/head.rs @@ -113,7 +113,7 @@ pub fn change_head_to_commit(commit_hash: &String) { #[cfg(test)] mod test { - use crate::utils::util::test_util; + use crate::utils::test_util; use crate::utils::head; #[test] diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e2a3235..36e49f6 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,4 @@ pub mod util; pub mod head; -pub mod store; \ No newline at end of file +pub mod store; +pub mod test_util; \ No newline at end of file diff --git a/src/utils/store.rs b/src/utils/store.rs index 1693075..f0441da 100644 --- a/src/utils/store.rs +++ b/src/utils/store.rs @@ -87,7 +87,7 @@ mod tests { use std::fs; use super::*; - use util::test_util; + use crate::utils::test_util; #[test] fn test_new_success() { diff --git a/src/utils/test_util.rs b/src/utils/test_util.rs new file mode 100644 index 0000000..27d849b --- /dev/null +++ b/src/utils/test_util.rs @@ -0,0 +1,112 @@ +#![cfg(test)] + +pub const TEST_DIR: &str = "mit_test_storage"; +use std::{ + fs, + io::{self, Write}, + path::{Path, PathBuf}, +}; + +use crate::models::Index; + +// 执行测试的储存库 +use super::util; +/* 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 = util::cur_dir(); + loop { + path.push("Cargo.toml"); + if path.exists() { + break; + } + 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 = util::cur_dir(); + path.push(util::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(util::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(util::get_working_dir().unwrap().join(path)).unwrap(); + } +} diff --git a/src/utils/util.rs b/src/utils/util.rs index 6bcc9d4..1520b7d 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -2,117 +2,13 @@ use sha1::{Digest, Sha1}; use std::{ collections::HashSet, fs, io, - io::Write, path::{Path, PathBuf}, }; -use crate::models::{commit::Commit, object::Hash, tree::Tree, Index}; +use crate::models::{commit::Commit, object::Hash, tree::Tree}; pub const ROOT_DIR: &str = ".mit"; -#[cfg(test)] -pub mod test_util { - 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"); - 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"); - } - } - 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)?; // 如果是目录,则递归删除 - } 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(); - } -} -} /* tools for mit */ pub fn calc_hash(data: &String) -> String { let mut hasher = Sha1::new(); @@ -533,9 +429,10 @@ 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::*; + use crate::{ + models::{blob::Blob, index::Index}, + utils::{test_util, util::*}, + }; #[test] fn test_get_storage_path() { @@ -577,7 +474,7 @@ mod tests { #[test] fn test_get_relative_path() { - setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let path = Path::new("../../src\\main.rs"); let rel_path = get_relative_path(&path, &cur_dir()); println!("{:?}", rel_path); @@ -587,7 +484,7 @@ mod tests { #[test] fn test_to_workdir_absolute_path() { - setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let path = Path::new("./src/../main.rs"); let abs_path = to_workdir_absolute_path(path); println!("{:?}", abs_path); @@ -599,7 +496,7 @@ mod tests { #[test] fn test_is_inside_repo() { - setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); let path = Path::new("../Cargo.toml"); assert_eq!(is_inside_workdir(path), false); @@ -617,10 +514,10 @@ mod tests { #[test] fn test_list_files() { - setup_test_with_clean_mit(); - ensure_test_file(Path::new("test/test.txt"), None); - ensure_test_file(Path::new("a.txt"), None); - ensure_test_file(Path::new("b.txt"), None); + test_util::setup_test_with_clean_mit(); + test_util::ensure_test_file(Path::new("test/test.txt"), None); + test_util::ensure_test_file(Path::new("a.txt"), None); + test_util::ensure_test_file(Path::new("b.txt"), None); let files = list_files(Path::new("./")); match files { Ok(files) => { @@ -636,9 +533,9 @@ mod tests { #[test] fn test_check_object_type() { - setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); assert_eq!(check_object_type("123".into()), ObjectType::Invalid); - ensure_test_file(Path::new("test.txt"), Some("test")); + test_util::ensure_test_file(Path::new("test.txt"), Some("test")); let hash = Blob::new(get_working_dir().unwrap().join("test.txt").as_path()).get_hash(); assert_eq!(check_object_type(hash), ObjectType::Blob); let mut commit = Commit::new(&Index::get_instance(), vec![], "test".to_string()); @@ -649,7 +546,7 @@ mod tests { #[test] fn test_check_root_dir() { - setup_test_with_clean_mit(); + test_util::setup_test_with_clean_mit(); list_workdir_files().iter().for_each(|f| { fs::remove_file(f).unwrap(); });