Skip to content

Commit

Permalink
恢复index更改
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBeanCpp authored and Hou-Xiaoxuan committed Dec 28, 2023
1 parent 7b1e344 commit 2106951
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
let mut file_paths = util::integrate_paths(&input_paths); //根据用户输入整合存在的文件(绝对路径)
file_paths.extend(deleted_files); //已删除的文件

let index = Index::new();
let index = Index::get_instance();
let store = store::Store::new();

for path in &file_paths {
Expand Down Expand Up @@ -139,7 +139,6 @@ pub fn restore_index(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathBuf,
}
}
}
index.save();
}
/**
对于工作区中的新文件,若已跟踪,则删除;若未跟踪,则保留<br>
Expand Down Expand Up @@ -227,7 +226,8 @@ mod test {
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());
let index = Index::get_instance();
assert!(index.get_tracked_files().is_empty());
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/models/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Index {

impl Index {
/// 从index文件加载
pub fn new() -> Index {
fn new() -> Index {
let mut index = Index::default();
index.load();
return index;
Expand All @@ -67,7 +67,7 @@ impl Index {
unsafe { &mut INSTANCE }
}

/// 重置index 从文件重新加载,主要用于测试,防止单例模式的影响
/// 重置index,主要用于测试,防止单例模式的影响
pub fn reload() {
let index = Index::get_instance();
index.load();
Expand Down Expand Up @@ -224,14 +224,14 @@ mod tests {
#[test]
fn test_load() {
test_util::setup_test_with_clean_mit();
let index = Index::new();
let index = Index::get_instance();
println!("{:?}", index);
}

#[test]
fn test_save() {
test_util::setup_test_with_clean_mit();
let mut index = Index::new();
let index = Index::get_instance();
let path = PathBuf::from("../mit_test_storage/.mit/HEAD"); //测试../相对路径的处理
index.add(path.clone(), FileMetaData::new(&Blob::new(&path), &path));

Expand Down
8 changes: 4 additions & 4 deletions src/models/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod test {
#[test]
fn test_new() {
test_util::setup_test_with_clean_mit();
let mut index = Index::new();
let index = Index::get_instance();
for test_file in vec!["b.txt", "mit_src/a.txt", "test/test.txt"] {
let test_file = PathBuf::from(test_file);
test_util::ensure_test_file(&test_file, None);
Expand All @@ -181,7 +181,7 @@ mod test {
#[test]
fn test_load() {
test_util::setup_test_with_clean_mit();
let mut index = Index::new();
let index = Index::get_instance();
let test_files = vec!["b.txt", "mit_src/a.txt"];
for test_file in test_files.clone() {
let test_file = PathBuf::from(test_file);
Expand All @@ -201,7 +201,7 @@ mod test {
#[test]
fn test_get_recursive_file_entries() {
test_util::setup_test_with_clean_mit();
let mut index = Index::new();
let index = Index::get_instance();
let mut test_files = vec![PathBuf::from("b.txt"), PathBuf::from("mit_src/a.txt")];
for test_file in test_files.clone() {
test_util::ensure_test_file(&test_file, None);
Expand Down Expand Up @@ -229,7 +229,7 @@ mod test {
#[test]
fn test_get_recursive_blobs() {
test_util::setup_test_with_clean_mit();
let mut index = Index::new();
let index = Index::get_instance();
let test_files = vec!["b.txt", "mit_src/a.txt"];
let mut test_blobs = vec![];
for test_file in test_files.clone() {
Expand Down
33 changes: 19 additions & 14 deletions src/utils/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use std::{
use crate::models::{commit::Commit, object::Hash, tree::Tree, Index};

pub const ROOT_DIR: &str = ".mit";

#[cfg(test)]
pub mod test_util {
use super::*;

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");
Expand All @@ -37,26 +35,33 @@ pub mod test_util {
}
}

fn setup_test_dir() {
/// 准备测试环境,切换到测试目录
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();
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();
let _ = crate::commands::init::init();
init_mit();
}

pub fn setup_test_without_mit() {
// 将执行目录切换到测试目录,并清除测试目录下的.mit目录
setup_test_dir();
let mut path = std::env::current_dir().unwrap();
setup_test_env();
let mut path = cur_dir();
path.push(ROOT_DIR);
if path.exists() {
fs::remove_dir_all(&path).unwrap();
Expand Down Expand Up @@ -101,13 +106,13 @@ pub mod test_util {
}
}

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();
Expand Down Expand Up @@ -529,9 +534,9 @@ 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::*;

#[test]
fn test_get_storage_path() {
let path = get_storage_path();
Expand Down

0 comments on commit 2106951

Please sign in to comment.