Skip to content

Commit

Permalink
拆分util文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Hou-Xiaoxuan committed Dec 28, 2023
1 parent 2106951 commit cdc289b
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod test {

use crate::{
commands as cmd, models,
utils::{head, util::test_util},
utils::{head, test_util},
};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions src/commands/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/models/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/models/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/models/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod test {

use crate::{
models::*,
utils::{util, util::test_util},
utils::{util, test_util},
};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/utils/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod util;
pub mod head;
pub mod store;
pub mod store;
pub mod test_util;
2 changes: 1 addition & 1 deletion src/utils/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
112 changes: 112 additions & 0 deletions src/utils/test_util.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading

0 comments on commit cdc289b

Please sign in to comment.