mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-02-13 15:15:35 +08:00
拆分util文件
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -40,7 +40,7 @@ mod test {
|
||||
|
||||
use crate::{
|
||||
commands as cmd, models,
|
||||
utils::{head, util::test_util},
|
||||
utils::{head, test_util},
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -70,7 +70,7 @@ fn __log(all: bool, number: Option<usize>) -> 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();
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -216,7 +216,7 @@ pub fn restore(paths: Vec<String>, source: Option<String>, 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]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -159,7 +159,7 @@ mod test {
|
||||
|
||||
use crate::{
|
||||
models::*,
|
||||
utils::{util, util::test_util},
|
||||
utils::{util, test_util},
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod util;
|
||||
pub mod head;
|
||||
pub mod store;
|
||||
pub mod store;
|
||||
pub mod test_util;
|
||||
@@ -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() {
|
||||
|
||||
112
src/utils/test_util.rs
Normal file
112
src/utils/test_util.rs
Normal file
@@ -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<T: AsRef<str>>(paths: &Vec<T>) {
|
||||
for path in paths {
|
||||
ensure_test_file(path.as_ref().as_ref(), None);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ensure_empty_dir<P: AsRef<Path>>(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();
|
||||
}
|
||||
}
|
||||
@@ -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<T: AsRef<str>>(paths: &Vec<T>) {
|
||||
for path in paths {
|
||||
ensure_test_file(path.as_ref().as_ref(), None);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ensure_empty_dir<P: AsRef<Path>>(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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user