test-fix: 修正测试中的问题

This commit is contained in:
mrbeanc
2023-12-22 23:50:23 +08:00
parent 1e16179e0a
commit ce2251fff6
5 changed files with 21 additions and 15 deletions

View File

@@ -1,5 +1,3 @@
use std::path::PathBuf;
use colored::Colorize; use colored::Colorize;
use crate::{ use crate::{

View File

@@ -1,9 +1,8 @@
use crate::{models::object::Hash, store::Store, utils::util::calc_hash}; use crate::{models::object::Hash, store::Store, utils::util::calc_hash};
use std::{fs, path::Path}; use std::{fs, path::Path};
/** /**Blob<br>
Blob git中最基本的对象他储存一份文件的内容并使用hash作为标识符。
Blob是git中最基本的对象他储存一份文件的内容并使用hash作为标识符。
*/ */
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Blob { pub struct Blob {

View File

@@ -33,8 +33,7 @@ impl FileMetaData {
} }
} }
/** /** Index
Index
注意:逻辑处理均为绝对路径,但是存储时为相对路径 注意:逻辑处理均为绝对路径,但是存储时为相对路径
*/ */
#[derive(Serialize, Deserialize, Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)]
@@ -191,8 +190,8 @@ mod tests {
#[test] #[test]
fn test_meta_get() { fn test_meta_get() {
// 示例:获取文件的元数据 util::setup_test_with_mit();
let metadata = fs::metadata(".gitignore").unwrap(); let metadata = fs::metadata(".mit/HEAD").unwrap();
println!("{:?}", util::format_time(&metadata.created().unwrap())); println!("{:?}", util::format_time(&metadata.created().unwrap()));
println!("{:?}", util::format_time(&metadata.modified().unwrap())); println!("{:?}", util::format_time(&metadata.modified().unwrap()));
println!("{:?}", metadata.len()); println!("{:?}", metadata.len());

View File

@@ -152,10 +152,12 @@ impl Tree {
mod test { mod test {
use std::path::PathBuf; use std::path::PathBuf;
use crate::utils::util::{get_absolute_path, to_workdir_absolute_path};
use crate::{ use crate::{
models::{blob::Blob, index::FileMetaData}, models::{blob::Blob, index::FileMetaData},
utils::util, utils::util,
}; };
#[test] #[test]
fn test_new() { fn test_new() {
util::setup_test_with_clean_mit(); util::setup_test_with_clean_mit();
@@ -196,9 +198,8 @@ mod test {
fn test_get_recursive_file_entries() { fn test_get_recursive_file_entries() {
util::setup_test_with_clean_mit(); util::setup_test_with_clean_mit();
let mut index = super::Index::new(); 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() { for test_file in test_files.clone() {
let test_file = PathBuf::from(test_file);
util::ensure_test_file(&test_file, None); 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));
} }
@@ -207,10 +208,18 @@ mod test {
let tree_hash = tree.get_hash(); let tree_hash = tree.get_hash();
let loaded_tree = super::Tree::load(&tree_hash); let loaded_tree = super::Tree::load(&tree_hash);
let files = loaded_tree.get_recursive_file_entries(); let mut files = loaded_tree.get_recursive_file_entries();
assert!(files.len() == test_files.len()); files.sort();
assert!(files[0].to_str().unwrap() == test_files[0]); test_files.sort();
assert!(files[1].to_str().unwrap() == test_files[1]); 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] #[test]

View File

@@ -66,6 +66,7 @@ pub fn setup_test_without_mit() {
pub fn ensure_test_file(path: &Path, content: option::Option<&str>) { pub fn ensure_test_file(path: &Path, content: option::Option<&str>) {
// 以测试目录为根目录,创建文件 // 以测试目录为根目录,创建文件
fs::create_dir_all(path.parent().unwrap()).unwrap(); // ensure父目录
let mut file = let mut file =
fs::File::create(get_working_dir().unwrap().join(path)).expect(format!("无法创建文件:{:?}", path).as_str()); fs::File::create(get_working_dir().unwrap().join(path)).expect(format!("无法创建文件:{:?}", path).as_str());
if let Some(content) = content { if let Some(content) = content {