清理警告,删除不需要的util,注释暂时不用的成员函数,为仅测试函数加宏

This commit is contained in:
HouXiaoxuan
2023-12-28 22:45:19 +08:00
parent f88ab0e33a
commit cc428bcae2
4 changed files with 38 additions and 69 deletions

View File

@@ -29,6 +29,7 @@ impl Commit {
pub fn get_date(&self) -> String {
util::format_time(&self.date)
}
#[cfg(test)]
pub fn get_tree_hash(&self) -> String {
self.tree.clone()
}
@@ -44,9 +45,9 @@ impl Commit {
pub fn get_author(&self) -> String {
self.author.clone()
}
pub fn get_committer(&self) -> String {
self.committer.clone()
}
// pub fn get_committer(&self) -> String {
// self.committer.clone()
// }
pub fn new(index: &Index, parent: Vec<Hash>, message: String) -> Commit {
let mut tree = Tree::new(index);

View File

@@ -68,6 +68,7 @@ impl Index {
}
/// 重置index主要用于测试防止单例模式的影响
#[cfg(test)]
pub fn reload() {
let index = Index::get_instance();
index.load();
@@ -115,16 +116,16 @@ impl Index {
self.contains(path)
}
/// 与暂存区比较,获取工作区中被删除的文件
pub fn get_deleted_files(&self, dir: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
self.entries.keys().for_each(|file| {
if !file.exists() && util::is_sub_path(file, dir) {
files.push(file.clone());
}
});
files
}
// /// 与暂存区比较,获取工作区中被删除的文件
// pub fn get_deleted_files(&self, dir: &Path) -> Vec<PathBuf> {
// let mut files = Vec::new();
// self.entries.keys().for_each(|file| {
// if !file.exists() && util::is_sub_path(file, dir) {
// files.push(file.clone());
// }
// });
// files
// }
/// 与暂存区比较确定文件自上次add以来是否被编辑内容不一定修改还需要算hash
pub fn is_modified(&self, file: &Path) -> bool {
@@ -201,7 +202,8 @@ impl Index {
self.entries.clone()
}
pub fn is_empty(&self) -> bool {
#[cfg(test)]
fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}

View File

@@ -110,3 +110,19 @@ pub fn ensure_no_file(path: &Path) {
fs::remove_file(util::get_working_dir().unwrap().join(path)).unwrap();
}
}
/** 列出子文件夹 */
pub fn list_subpath(path: &Path) -> io::Result<Vec<PathBuf>> {
let mut files = Vec::new();
let path = util::get_absolute_path(path);
if path.is_dir() {
for entry in fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() && path.file_name().unwrap_or_default() != util::ROOT_DIR {
files.push(path)
}
}
}
Ok(files)
}

View File

@@ -1,4 +1,3 @@
use std::{
collections::HashSet,
fs, io,
@@ -126,11 +125,6 @@ where
.collect::<O>()
}
/// 检查文件是否在工作区内, 若不存在则false
pub fn is_inside_workdir(file: &Path) -> bool {
is_inside_dir(file, &get_working_dir().unwrap())
}
/// 检查文件是否在.mit内 若不存在则false
pub fn is_inside_repo(file: &Path) -> bool {
is_inside_dir(file, &get_storage_path().unwrap())
@@ -164,21 +158,6 @@ pub fn list_files(path: &Path) -> io::Result<Vec<PathBuf>> {
Ok(files)
}
/** 列出子文件夹 */
pub fn list_subpath(path: &Path) -> io::Result<Vec<PathBuf>> {
let mut files = Vec::new();
let path = get_absolute_path(path);
if path.is_dir() {
for entry in fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() && path.file_name().unwrap_or_default() != ROOT_DIR {
files.push(path)
}
}
}
Ok(files)
}
/** 检查一个dir是否包含.mit考虑.mit嵌套 */
pub fn include_root_dir(dir: &Path) -> bool {
// 检查子文件夹是否有ROOT_DIR
@@ -320,25 +299,6 @@ pub fn get_file_mode(path: &Path) -> String {
}
}
/// 清除Windows下的绝对路径前缀"\\\\?\\" (由[PathBuf::canonicalize]函数产生)
/// <br><a href="https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation">Windows 系统中的文件路径格式</a>
pub fn clean_win_abs_path_pre(path: PathBuf) -> PathBuf {
#[cfg(windows)]
{
const DOS_PREFIX: &str = "\\\\?\\";
let path_str = path.to_string_lossy();
if path_str.starts_with(DOS_PREFIX) {
PathBuf::from(&path_str[DOS_PREFIX.len()..])
} else {
path
}
}
#[cfg(not(target_os = "windows"))]
{
path
}
}
/// 获取绝对路径相对于目录current_dir 不论是否存在
pub fn get_absolute_path(path: &Path) -> PathBuf {
get_absolute_path_to_dir(path, &std::env::current_dir().unwrap())
@@ -350,8 +310,6 @@ pub fn get_absolute_path_to_dir(path: &Path, dir: &Path) -> PathBuf {
path.to_path_buf()
} else {
//相对路径
/*let abs_path = path.canonicalize().unwrap(); //这一步会统一路径分隔符 //canonicalize()不能处理不存在的文件
clean_win_abs_path_pre(abs_path)*/
// 所以决定手动解析相对路径中的../ ./
let mut abs_path = dir.to_path_buf();
// 这里会拆分所有组件,所以会自动统一路径分隔符
@@ -415,7 +373,6 @@ pub fn is_typeof_commit(hash: Hash) -> bool {
check_object_type(hash) == ObjectType::Commit
}
/// 将内容对应的文件内容(主要是blob)还原到file
pub fn write_workfile(content: String, file: &PathBuf) {
let mut parent = file.clone();
@@ -433,7 +390,10 @@ pub fn read_workfile(file: &Path) -> String {
mod tests {
use crate::{
models::{blob::Blob, index::Index},
utils::{test_util, util::{*, self}},
utils::{
test_util,
util::{self, *},
},
};
#[test]
@@ -496,16 +456,6 @@ mod tests {
assert_eq!(abs_path, cur_dir);
}
#[test]
fn test_is_inside_repo() {
test_util::setup_test_with_clean_mit();
let path = Path::new("../Cargo.toml");
assert_eq!(is_inside_workdir(path), false);
let path = Path::new(".mit/HEAD");
assert_eq!(is_inside_workdir(path), true);
}
#[test]
fn test_format_time() {
let time = std::time::SystemTime::now();
@@ -553,7 +503,7 @@ mod tests {
list_workdir_files().iter().for_each(|f| {
fs::remove_file(f).unwrap();
});
list_subpath(Path::new("./")).unwrap().iter().for_each(|f| {
test_util::list_subpath(Path::new("./")).unwrap().iter().for_each(|f| {
if include_root_dir(f) {
fs::remove_dir_all(f).unwrap();
}