From ce2251fff605ffbced258108dc5586e7cefaaf82 Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Fri, 22 Dec 2023 23:50:23 +0800 Subject: [PATCH] =?UTF-8?q?test-fix:=20=E4=BF=AE=E6=AD=A3=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=B8=AD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/switch.rs | 2 -- src/models/blob.rs | 5 ++--- src/models/index.rs | 7 +++---- src/models/tree.rs | 21 +++++++++++++++------ src/utils/util.rs | 1 + 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/commands/switch.rs b/src/commands/switch.rs index 1aa1118..4fff518 100644 --- a/src/commands/switch.rs +++ b/src/commands/switch.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use colored::Colorize; use crate::{ diff --git a/src/models/blob.rs b/src/models/blob.rs index 68c2511..2e943b6 100644 --- a/src/models/blob.rs +++ b/src/models/blob.rs @@ -1,9 +1,8 @@ use crate::{models::object::Hash, store::Store, utils::util::calc_hash}; use std::{fs, path::Path}; -/** - Blob -Blob是git中最基本的对象,他储存一份文件的内容,并使用hash作为标识符。 +/**Blob
+git中最基本的对象,他储存一份文件的内容,并使用hash作为标识符。 */ #[derive(Debug, Clone)] pub struct Blob { diff --git a/src/models/index.rs b/src/models/index.rs index b320f52..0d6d540 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -33,8 +33,7 @@ impl FileMetaData { } } -/** - Index +/** Index 注意:逻辑处理均为绝对路径,但是存储时为相对路径 */ #[derive(Serialize, Deserialize, Debug, Default)] @@ -191,8 +190,8 @@ mod tests { #[test] fn test_meta_get() { - // 示例:获取文件的元数据 - let metadata = fs::metadata(".gitignore").unwrap(); + util::setup_test_with_mit(); + let metadata = fs::metadata(".mit/HEAD").unwrap(); println!("{:?}", util::format_time(&metadata.created().unwrap())); println!("{:?}", util::format_time(&metadata.modified().unwrap())); println!("{:?}", metadata.len()); diff --git a/src/models/tree.rs b/src/models/tree.rs index 1879c6a..1749a30 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -152,10 +152,12 @@ impl Tree { mod test { use std::path::PathBuf; + use crate::utils::util::{get_absolute_path, to_workdir_absolute_path}; use crate::{ models::{blob::Blob, index::FileMetaData}, utils::util, }; + #[test] fn test_new() { util::setup_test_with_clean_mit(); @@ -196,9 +198,8 @@ mod test { fn test_get_recursive_file_entries() { util::setup_test_with_clean_mit(); let mut index = super::Index::new(); - let test_files = vec!["b.txt", "mit_src/a.txt"]; + let mut test_files = vec![PathBuf::from("b.txt"), PathBuf::from("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); index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } @@ -207,10 +208,18 @@ mod test { let tree_hash = tree.get_hash(); let loaded_tree = super::Tree::load(&tree_hash); - let files = loaded_tree.get_recursive_file_entries(); - assert!(files.len() == test_files.len()); - assert!(files[0].to_str().unwrap() == test_files[0]); - assert!(files[1].to_str().unwrap() == test_files[1]); + let mut files = loaded_tree.get_recursive_file_entries(); + files.sort(); + test_files.sort(); + assert_eq!(files.len(), test_files.len()); + assert_eq!( + to_workdir_absolute_path(&files[0]).to_str().unwrap(), //TODO 罪大恶极的路径问题 + get_absolute_path(&test_files[0]).to_str().unwrap() + ); + assert_eq!( + to_workdir_absolute_path(&files[1]).to_str().unwrap(), + get_absolute_path(&test_files[1]).to_str().unwrap() + ); } #[test] diff --git a/src/utils/util.rs b/src/utils/util.rs index 2f3ec06..997ab10 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -66,6 +66,7 @@ pub fn setup_test_without_mit() { pub fn ensure_test_file(path: &Path, content: option::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 {