简化test util用法与名称,将head归为models

This commit is contained in:
mrbeanc
2023-12-29 13:04:53 +08:00
parent cdc289b3bb
commit 4d69273293
18 changed files with 99 additions and 104 deletions

View File

@@ -8,6 +8,7 @@ use crate::models::*;
use crate::utils::util;
/// add是对index的操作不会对工作区产生影响
/// @see <a href="https://juejin.cn/post/7053831273277554696">git add .git add -Agit add -ugit add * 的区别与联系</a>
pub fn add(raw_paths: Vec<String>, all: bool, mut update: bool) {
util::check_repo_exist();

View File

@@ -2,7 +2,7 @@ use colored::Colorize;
use crate::{
models::*,
utils::{head, store, util},
utils::{store, util},
};
// branch error
@@ -126,10 +126,10 @@ pub fn branch(
#[cfg(test)]
mod test {
use super::*;
use crate::{commands, utils::test_util};
use crate::{commands, utils::test};
#[test]
fn test_create_branch() {
test_util::setup_test_with_clean_mit();
test::setup_with_clean_mit();
// no commit: invalid object
let result = create_branch("test_branch".to_string(), head::current_head_commit());
@@ -170,7 +170,7 @@ mod test {
#[test]
fn test_delete_branch() {
test_util::setup_test_with_clean_mit();
test::setup_with_clean_mit();
// no commit: invalid object
let result = delete_branch("test_branch".to_string());

View File

@@ -1,4 +1,4 @@
use crate::{models::*, utils::head};
use crate::models::*;
use super::status;
@@ -38,33 +38,30 @@ pub fn commit(message: String, allow_empty: bool) {
mod test {
use std::path::Path;
use crate::{
commands as cmd, models,
utils::{head, test_util},
};
use crate::{commands as cmd, models::*, utils::test};
#[test]
#[should_panic]
fn test_commit_empty() {
test_util::setup_test_with_empty_workdir();
test::setup_with_empty_workdir();
super::commit("".to_string(), false);
}
#[test]
fn test_commit() {
test_util::setup_test_with_clean_mit();
test::setup_with_clean_mit();
let test_file = "a.txt";
let head_one = head::current_head_commit();
assert!(head_one.is_empty());
test_util::ensure_test_file(&Path::new(test_file), "test content".into());
test::ensure_file(&Path::new(test_file), "test content".into());
cmd::add(vec![], true, false);
cmd::commit("test commit 1".to_string(), true);
let head_two = head::current_head_commit();
assert!(head_two.len() > 0);
let commit = models::commit::Commit::load(&head_two);
assert!(commit.get_parent_hash().len() == 0);
assert!(commit.get_message() == "test commit 1");
let commit = Commit::load(&head_two);
assert_eq!(commit.get_parent_hash().len(), 0);
assert_eq!(commit.get_message(), "test commit 1");
}
}

View File

@@ -1,4 +1,4 @@
use crate::{models::Commit, utils::head};
use crate::models::{head, Commit};
use colored::Colorize;
const DEFAULT_LOG_NUMBER: usize = 10;
@@ -70,10 +70,10 @@ fn __log(all: bool, number: Option<usize>) -> usize {
#[cfg(test)]
mod test {
use super::super::super::commands;
use crate::utils::test_util;
use crate::utils::test;
#[test]
fn test_log() {
test_util::setup_test_with_clean_mit();
test::setup_with_clean_mit();
assert_eq!(super::__log(false, None), 0);
commands::commit::commit("test commit 2".into(), true);
assert_eq!(super::__log(false, Some(1)), 1);

View File

@@ -1,7 +1,7 @@
use crate::{
commands::{self, status::*},
models::{Commit, Hash},
utils::{head, store, util},
models::{head, Commit, Hash},
utils::{store, util},
};
enum MergeErr {
@@ -83,12 +83,12 @@ mod test {
use super::*;
use crate::{
commands::{commit, switch::switch},
utils::test_util,
utils::test,
};
#[test]
fn test_check_ff() {
test_util::setup_test_with_empty_workdir();
test::setup_with_empty_workdir();
commit::commit("init".to_string(), true);
let commit1 = head::current_head_commit();
let origin_branch = match head::current_head() {

View File

@@ -6,7 +6,7 @@ use std::{
use crate::{
models::*,
utils::{head, store, util},
utils::{store, util},
};
/// 统计[工作区]中相对于target_blobs已删除的文件根据filters进行过滤
@@ -216,14 +216,14 @@ 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::test_util};
use crate::{commands as cmd, commands::status, models::Index, utils::test};
use std::path::PathBuf;
#[test]
fn test_restore_stage() {
test_util::setup_test_with_empty_workdir();
test::setup_with_empty_workdir();
let path = PathBuf::from("a.txt");
test_util::ensure_no_file(&path);
test::ensure_no_file(&path);
cmd::add(vec![], true, false); //add -A
cmd::restore(vec![".".to_string()], Some("HEAD".to_string()), false, true);
let index = Index::get_instance();
@@ -232,9 +232,9 @@ mod test {
#[test]
fn test_restore_worktree() {
test_util::setup_test_with_empty_workdir();
test::setup_with_empty_workdir();
let files = vec!["a.txt", "b.txt", "c.txt", "test/in.txt"];
test_util::ensure_test_files(&files);
test::ensure_files(&files);
cmd::add(vec![], true, false);
assert_eq!(status::changes_to_be_committed().new.iter().count(), 4);

View File

@@ -1,6 +1,5 @@
use crate::{
utils::head,
models::{Commit, Index},
models::{head, Commit, Index},
utils::util,
};
use colored::Colorize;
@@ -196,14 +195,14 @@ pub fn status() {
#[cfg(test)]
mod tests {
use super::*;
use crate::{commands as cmd, utils::test_util};
use crate::{commands as cmd, utils::test};
use std::path::Path;
#[test]
fn test_changes_to_be_committed() {
test_util::setup_test_with_empty_workdir();
test::setup_with_empty_workdir();
let test_file = "a.txt";
test_util::ensure_test_file(Path::new(test_file), None);
test::ensure_file(Path::new(test_file), None);
cmd::commit("test commit".to_string(), true);
cmd::add(vec![test_file.to_string()], false, false);
@@ -215,7 +214,7 @@ mod tests {
println!("{:?}", change.to_absolute());
cmd::commit("test commit".to_string(), true);
test_util::ensure_test_file(Path::new(test_file), Some("new content"));
test::ensure_file(Path::new(test_file), Some("new content"));
cmd::add(vec![test_file.to_string()], false, false);
let change = changes_to_be_committed();
assert_eq!(change.new.len(), 0);

View File

@@ -1,8 +1,8 @@
use colored::Colorize;
use crate::{
models::{Commit, Hash},
utils::{head, store, util},
models::{head, Commit, Hash},
utils::{store, util},
};
use super::{
@@ -88,12 +88,12 @@ mod test {
use super::*;
use crate::{
commands::{self as cmd},
utils::test_util,
utils::test,
};
use std::path::PathBuf;
#[test]
fn test_switch() {
test_util::setup_test_with_empty_workdir();
test::setup_with_empty_workdir();
cmd::commit("init".to_string(), true);
let test_branch_1 = "test_branch_1".to_string();
@@ -101,7 +101,7 @@ mod test {
/* test 1: NoClean */
let test_file_1 = PathBuf::from("test_file_1");
test_util::ensure_test_file(&test_file_1, None);
test::ensure_file(&test_file_1, None);
let result = switch_to(test_branch_1.clone(), false);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), SwitchErr::NoClean));
@@ -122,12 +122,12 @@ mod test {
assert!(matches!(result.unwrap_err(), SwitchErr::InvalidObject));
let tees_file_2 = PathBuf::from("test_file_2");
test_util::ensure_test_file(&tees_file_2, None);
test::ensure_file(&tees_file_2, None);
cmd::add(vec![], true, false); // add all
cmd::commit("add file 2".to_string(), false);
let history_commit = head::current_head_commit(); // commit: test_file_1 exists, test_file_2 exists
test_util::ensure_no_file(&test_file_1);
test::ensure_no_file(&test_file_1);
cmd::add(vec![], true, false); // add all
assert!(!test_file_1.exists());
cmd::commit("delete file 1".to_string(), false);