mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-06-30 00:06:04 +08:00
将test的util函数移动到test宏下,去除unused警告
This commit is contained in:
@@ -113,12 +113,12 @@ pub fn change_head_to_commit(commit_hash: &String) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::util;
|
||||
use crate::utils::util::test_util;
|
||||
use crate::utils::head;
|
||||
|
||||
#[test]
|
||||
fn test_edit_branch() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let branch_name = "test_branch".to_string() + &rand::random::<u32>().to_string();
|
||||
let branch_head = super::get_branch_head(&branch_name);
|
||||
assert!(branch_head.is_empty());
|
||||
@@ -132,7 +132,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_list_local_branches() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let branch_one = "test_branch".to_string() + &rand::random::<u32>().to_string();
|
||||
let branch_two = "test_branch".to_string() + &rand::random::<u32>().to_string();
|
||||
head::update_branch(&branch_one, &"1234567890".to_string());
|
||||
@@ -145,7 +145,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_change_head_to_branch() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let branch_name = "test_branch".to_string() + &rand::random::<u32>().to_string();
|
||||
head::update_branch(&branch_name, &"1234567890".to_string());
|
||||
super::change_head_to_branch(&branch_name);
|
||||
@@ -160,7 +160,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_change_head_to_commit() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let commit_hash = "1234567890".to_string();
|
||||
super::change_head_to_commit(&commit_hash);
|
||||
assert!(
|
||||
@@ -174,7 +174,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_update_branch_head() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let branch_name = "test_branch".to_string() + &rand::random::<u32>().to_string();
|
||||
let commit_hash = "1234567890".to_string();
|
||||
super::update_branch(&branch_name, &commit_hash);
|
||||
|
||||
@@ -87,23 +87,24 @@ mod tests {
|
||||
use std::fs;
|
||||
|
||||
use super::*;
|
||||
use util::test_util;
|
||||
|
||||
#[test]
|
||||
fn test_new_success() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let _ = Store::new();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_new_fail() {
|
||||
util::setup_test_without_mit();
|
||||
test_util::setup_test_without_mit();
|
||||
let _ = Store::new();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_and_load() {
|
||||
let _ = util::setup_test_with_clean_mit();
|
||||
let _ = test_util::setup_test_with_clean_mit();
|
||||
let store = Store::new();
|
||||
let content = "hello world".to_string();
|
||||
let hash = store.save(&content);
|
||||
@@ -113,7 +114,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_search() {
|
||||
util::setup_test_with_clean_mit();
|
||||
test_util::setup_test_with_clean_mit();
|
||||
let hashs = vec!["1234567890".to_string(), "1235467891".to_string(), "4567892".to_string()];
|
||||
for hash in hashs.iter() {
|
||||
let mut path = util::get_storage_path().unwrap();
|
||||
|
||||
@@ -9,108 +9,105 @@ use std::{
|
||||
use crate::models::{commit::Commit, object::Hash, tree::Tree, Index};
|
||||
|
||||
pub const ROOT_DIR: &str = ".mit";
|
||||
pub const TEST_DIR: &str = "mit_test_storage"; // 执行测试的储存库
|
||||
|
||||
/* 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;
|
||||
#[cfg(test)]
|
||||
pub mod test_util {
|
||||
use super::*;
|
||||
|
||||
pub const TEST_DIR: &str = "mit_test_storage"; // 执行测试的储存库
|
||||
/* 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");
|
||||
}
|
||||
}
|
||||
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)?; // 如果是目录,则递归删除
|
||||
path.pop();
|
||||
path
|
||||
} else {
|
||||
fs::remove_file(&path)?; // 如果是文件,则直接删除
|
||||
PathBuf::from(cargo_path.unwrap())
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
fn setup_test_dir() {
|
||||
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 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();
|
||||
/// with 初始化的干净的mit
|
||||
pub fn setup_test_with_clean_mit() {
|
||||
setup_test_without_mit();
|
||||
let _ = crate::commands::init::init();
|
||||
}
|
||||
|
||||
pub fn setup_test_without_mit() {
|
||||
// 将执行目录切换到测试目录,并清除测试目录下的.mit目录
|
||||
setup_test_dir();
|
||||
let mut path = std::env::current_dir().unwrap();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -534,7 +531,7 @@ 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();
|
||||
|
||||
Reference in New Issue
Block a user