From c8b45c0b83bd87b1e15aea4518b4174204597293 Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Sat, 23 Dec 2023 10:30:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4setup=5Fwith=5Fmit=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8clarm=5Fmit=E4=BB=A3=E6=9B=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/head.rs | 10 +++++----- src/models/index.rs | 4 ++-- src/store.rs | 4 ++-- src/utils/util.rs | 9 ++++++++- tests/test.rs | 4 ++-- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/head.rs b/src/head.rs index 8ca15b1..513a070 100644 --- a/src/head.rs +++ b/src/head.rs @@ -117,7 +117,7 @@ mod test { #[test] fn test_edit_branch() { - util::setup_test_with_mit(); + 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()); @@ -131,7 +131,7 @@ mod test { #[test] fn test_list_local_branches() { - util::setup_test_with_mit(); + 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(); update_branch(&branch_one, &"1234567890".to_string()); @@ -144,7 +144,7 @@ mod test { #[test] fn test_change_head_to_branch() { - util::setup_test_with_mit(); + util::setup_test_with_clean_mit(); let branch_name = "test_branch".to_string() + &rand::random::().to_string(); update_branch(&branch_name, &"1234567890".to_string()); super::change_head_to_branch(&branch_name); @@ -159,7 +159,7 @@ mod test { #[test] fn test_change_head_to_commit() { - util::setup_test_with_mit(); + util::setup_test_with_clean_mit(); let commit_hash = "1234567890".to_string(); super::change_head_to_commit(&commit_hash); assert!( @@ -173,7 +173,7 @@ mod test { #[test] fn test_update_branch_head() { - util::setup_test_with_mit(); + 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/models/index.rs b/src/models/index.rs index 0d6d540..aef003b 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -190,7 +190,7 @@ mod tests { #[test] fn test_meta_get() { - util::setup_test_with_mit(); + 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())); @@ -199,7 +199,7 @@ mod tests { #[test] fn test_load() { - util::setup_test_with_mit(); + util::setup_test_with_clean_mit(); let index = Index::new(); println!("{:?}", index); } diff --git a/src/store.rs b/src/store.rs index 9dbc452..6871ba0 100644 --- a/src/store.rs +++ b/src/store.rs @@ -86,7 +86,7 @@ mod tests { #[test] fn test_new_success() { - util::setup_test_with_mit(); + util::setup_test_with_clean_mit(); let _ = Store::new(); } @@ -99,7 +99,7 @@ mod tests { #[test] fn test_save_and_load() { - let _ = util::setup_test_with_mit(); + let _ = util::setup_test_with_clean_mit(); let store = Store::new(); let content = "hello world".to_string(); let hash = store.save(&content); diff --git a/src/utils/util.rs b/src/utils/util.rs index 997ab10..200722f 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -77,6 +77,13 @@ pub fn ensure_test_file(path: &Path, content: option::Option<&str>) { } } +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(); @@ -394,7 +401,7 @@ mod tests { #[test] fn test_is_inside_repo() { - setup_test_with_mit(); + setup_test_with_clean_mit(); let path = Path::new("../Cargo.toml"); assert_eq!(is_inside_workdir(path), false); diff --git a/tests/test.rs b/tests/test.rs index 777b311..71af2aa 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -14,7 +14,7 @@ fn test_hash() { #[test] fn test_write() -> Result<(), Error> { - util::setup_test_with_mit(); + util::setup_test_with_clean_mit(); let path = "lines.txt"; //create会截断文件 let mut output = File::create(path)?; // ? 用于传播错误 @@ -24,7 +24,7 @@ fn test_write() -> Result<(), Error> { #[test] fn test_read() -> Result<(), Error> { - util::setup_test_with_mit(); + util::setup_test_with_clean_mit(); let path = "lines.txt"; util::ensure_test_file(path.as_ref(), None); let input = File::open(path)?;