Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo clippy --manifest-path scorpio/Cargo.toml --all-targets --all-features -- -D warnings
- run: cargo clippy --manifest-path scorpio/Cargo.toml --all-targets --all-features -- -D warnings
3 changes: 3 additions & 0 deletions libra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default = []
p2p = ["gemini"]

[dependencies]
http = "1.3.1"
anyhow = { workspace = true }
byte-unit = "5.1.4"
byteorder = "1.5.0"
Expand Down Expand Up @@ -61,3 +62,5 @@ tempfile = { workspace = true }
serial_test = { workspace = true }
tokio = { workspace = true, features = ["macros", "process"] }
tracing-test = "0.2.4"
testcontainers = { version = "0.23.0", features = ["http_wait","reusable-containers"] }
reqwest = { version = "0.12.12", features = ["blocking"] }
1 change: 1 addition & 0 deletions libra/src/command/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ mod test {

#[test]
#[should_panic]
/// Test that `-A` and `-u` cannot be used together
fn test_args_parse_update_conflict_with_all() {
AddArgs::try_parse_from(["test", "-A", "-u"]).unwrap();
}
Expand Down
7 changes: 6 additions & 1 deletion libra/src/command/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ mod tests {

#[tokio::test]
#[serial]
/// Tests core branch management functionality including creation and listing.
/// Verifies branches can be created from specific commits.
async fn test_branch() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand Down Expand Up @@ -330,6 +332,8 @@ mod tests {

#[tokio::test]
#[serial]
/// Tests branch creation using remote branches as starting points.
/// Verifies that local branches can be created from remote branch references.
async fn test_create_branch_from_remote() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand Down Expand Up @@ -366,6 +370,7 @@ mod tests {

#[tokio::test]
#[serial]
/// Tests the behavior of creating a branch with an invalid name.
async fn test_invalid_branch_name() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand All @@ -391,7 +396,7 @@ mod tests {
};
execute(args).await;

let branch = Branch::find_branch("new", None).await;
let branch = Branch::find_branch("@{mega}", None).await;
assert!(branch.is_none(), "invalid branch should not be created");
}
}
2 changes: 2 additions & 0 deletions libra/src/command/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ mod tests {

#[tokio::test]
#[serial]
/// Tests branch creation, switching and validation functionality in the checkout module.
/// Verifies proper branch management and HEAD reference updates when switching between branches.
async fn test_checkout_module_functions() {
println!("\n\x1b[1mTest checkout module functions.\x1b[0m");

Expand Down
3 changes: 3 additions & 0 deletions libra/src/command/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ mod tests {

#[tokio::test]
#[serial]
/// Test the clone command with a specific branch
async fn test_clone_branch() {
let temp_path = tempdir().unwrap();
let _guard = test::ChangeDirGuard::new(temp_path.path());
Expand Down Expand Up @@ -191,6 +192,7 @@ mod tests {

#[tokio::test]
#[serial]
/// Test the clone command with the default branch
async fn test_clone_default_branch() {
let temp_path = tempdir().unwrap();
let _guard = test::ChangeDirGuard::new(temp_path.path());
Expand Down Expand Up @@ -219,6 +221,7 @@ mod tests {

#[tokio::test]
#[serial]
/// Test the clone command with an empty repository
async fn test_clone_empty_repo() {
let temp_path = tempdir().unwrap();
let _guard = test::ChangeDirGuard::new(temp_path.path());
Expand Down
11 changes: 11 additions & 0 deletions libra/src/command/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct CommitArgs {
#[arg(long, requires("message"))]
pub conventional: bool,

/// amend the last commit
#[arg(long)]
pub amend: bool,
}
Expand Down Expand Up @@ -211,6 +212,7 @@ mod test {

use super::*;
#[test]
///Testing basic parameter parsing functionality.
fn test_parse_args() {
let args = CommitArgs::try_parse_from(["commit", "-m", "init"]);
assert!(args.is_ok());
Expand All @@ -236,6 +238,8 @@ mod test {

#[tokio::test]
#[serial]
/// Tests the recursive tree creation from index entries.
/// Verifies that tree objects are correctly created, saved to storage, and properly organized in a hierarchical structure.
async fn test_create_tree() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand Down Expand Up @@ -264,6 +268,9 @@ mod test {
#[tokio::test]
#[serial]
#[should_panic]
/// A commit with no file changes should fail if `allow_empty` is false.
/// This test verifies that the commit command rejects empty changesets
/// when not explicitly permitted.
async fn test_execute_commit_with_empty_index_fail() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand All @@ -280,6 +287,10 @@ mod test {

#[tokio::test]
#[serial]
/// Tests normal commit functionality with both `--amend` and `--allow_empty` flags.
/// Verifies that:
/// 1. Amending works correctly when allowed
/// 2. Empty commits are permitted when explicitly enabled
async fn test_execute_commit() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand Down
6 changes: 6 additions & 0 deletions libra/src/command/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ mod test {

use super::*;
#[test]
/// Tests command line argument parsing for the diff command with various parameter combinations.
/// Verifies parameter requirements, conflicts and default values are handled correctly.
fn test_args() {
{
let args = DiffArgs::try_parse_from(["diff", "--old", "old", "--new", "new", "paths"]);
Expand Down Expand Up @@ -387,6 +389,8 @@ mod test {
}

#[test]
/// Tests the functionality of the `similar_diff_result` function.
/// Verifies that it correctly generates a diff between two text inputs.
fn test_similar_diff_result() {
let old = "Hello World\nThis is the second line.\nThis is the third.";
let new = "Hallo Welt\nThis is the second line.\nThis is life.\nMoar and more";
Expand All @@ -398,6 +402,8 @@ mod test {

#[tokio::test]
#[serial]
/// Tests that the get_files_blobs function properly respects .libraignore patterns.
/// Verifies ignored files are correctly excluded from the blob collection process.
async fn test_get_files_blob_gitignore() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand Down
33 changes: 23 additions & 10 deletions libra/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ fn set_dir_hidden(_dir: &str) -> io::Result<()> {
/// Unit tests for the init module
#[cfg(test)]
mod tests {
use super::*;
use crate::internal::head::Head;
use crate::utils::test;
use serial_test::serial;
use tempfile::tempdir;

use super::*;
use crate::{internal::head::Head, utils::test};

pub fn verify_init(base_dir: &Path) {
// List of subdirectories to verify
let dirs = ["objects/pack", "objects/info", "info"];
Expand All @@ -273,10 +273,13 @@ mod tests {
assert!(file_path.exists(), "File {} does not exist", file);
}
}
/// Test the init function with no parameters
#[tokio::test]
#[serial]
/// Test the init function with no parameters
async fn test_init() {
let target_dir = tempdir().unwrap().into_path();
// let _guard = ChangeDirGuard::new(target_dir.clone());

let args = InitArgs {
bare: false,
initial_branch: None,
Expand All @@ -294,10 +297,13 @@ mod tests {
verify_init(libra_dir.as_path());
}

/// Test the init function with the --bare flag
#[tokio::test]
#[serial]
/// Test the init function with the --bare flag
async fn test_init_bare() {
let target_dir = tempdir().unwrap().into_path();
// let _guard = ChangeDirGuard::new(target_dir.clone());

// Run the init function with --bare flag
let args = InitArgs {
bare: true,
Expand All @@ -311,8 +317,9 @@ mod tests {
// Verify the contents of the other directory
verify_init(target_dir.as_path());
}
/// Test the init function with the --bare flag and an existing repository
#[tokio::test]
#[serial]
/// Test the init function with the --bare flag and an existing repository
async fn test_init_bare_with_existing_repo() {
let target_dir = tempdir().unwrap().into_path();

Expand Down Expand Up @@ -342,9 +349,9 @@ mod tests {
assert!(err.to_string().contains("Initialization failed")); // Check error message contains "Already initialized"
}

/// Test the init function with an initial branch name
#[tokio::test]
#[serial]
/// Test the init function with an initial branch name
async fn test_init_with_initial_branch() {
// Set up the test environment without a Libra repository
let temp_path = tempdir().unwrap();
Expand Down Expand Up @@ -372,8 +379,9 @@ mod tests {
};
}

/// Test the init function with an invalid branch name
#[tokio::test]
#[serial]
/// Test the init function with an invalid branch name
async fn test_init_with_invalid_branch() {
// Cover all invalid branch name cases
test_invalid_branch_name("master ").await;
Expand Down Expand Up @@ -411,8 +419,9 @@ mod tests {
assert!(err.to_string().contains("invalid branch name")); // Check error message contains "invalid branch name"
}

/// Test the init function with [directory] parameter
#[tokio::test]
#[serial]
/// Test the init function with [directory] parameter
async fn test_init_with_directory() {
let target_dir = tempdir().unwrap().into_path();

Expand All @@ -436,8 +445,9 @@ mod tests {
verify_init(&libra_dir);
}

/// Test the init function with invalid [directory] parameter
#[tokio::test]
#[serial]
/// Test the init function with invalid [directory] parameter
async fn test_init_with_invalid_directory() {
let target_dir = tempdir().unwrap().into_path();

Expand Down Expand Up @@ -465,6 +475,8 @@ mod tests {
}

#[tokio::test]
#[serial]
/// Tests that repository initialization fails when lacking write permissions in the target directory
async fn test_init_with_unauthorized_directory() {
let target_dir = tempdir().unwrap().into_path();

Expand Down Expand Up @@ -503,6 +515,7 @@ mod tests {
}

#[tokio::test]
#[serial]
/// Test the init function with the --quiet flag by using --show-output
async fn test_init_quiet() {
let target_dir = tempdir().unwrap().into_path();
Expand Down
34 changes: 31 additions & 3 deletions libra/src/command/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub async fn execute(args: LogArgs) {
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::test::ChangeDirGuard;
use crate::{command::save_object, utils::test};
use common::utils::format_commit_msg;
use mercury::{hash::SHA1, internal::object::commit::Commit};
Expand All @@ -135,6 +136,7 @@ mod tests {

#[tokio::test]
#[serial]
/// Tests retrieval of commits reachable from a specific commit hash
async fn test_get_reachable_commits() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand All @@ -147,14 +149,40 @@ mod tests {
}

#[tokio::test]
#[ignore] // ignore this test because it will open less and block the test
#[serial]
/// Tests log command execution functionality
async fn test_execute_log() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
let _guard = ChangeDirGuard::new(temp_path.path());
let _ = create_test_commit_tree().await;

let args = LogArgs { number: Some(6) };
execute(args).await;
// let args = LogArgs { number: Some(1) };
// execute(args).await;
let head = Head::current().await;
// check if the current branch has any commits
if let Head::Branch(branch_name) = head.to_owned() {
let branch = Branch::find_branch(&branch_name, None).await;
if branch.is_none() {
panic!(
"fatal: your current branch '{}' does not have any commits yet ",
branch_name
);
}
}

let commit_hash = Head::current_commit().await.unwrap().to_string();

let mut reachable_commits = get_reachable_commits(commit_hash.clone()).await;
// default sort with signature time
reachable_commits.sort_by(|a, b| b.committer.timestamp.cmp(&a.committer.timestamp));
//the last seven commits
let max_output_number = min(6, reachable_commits.len());
let mut output_number = 6;
for commit in reachable_commits.iter().take(max_output_number) {
assert_eq!(commit.message, format!("\nCommit_{}", output_number));
output_number -= 1;
}
}

/// create a test commit tree structure as graph and create branch (master) head to commit 6
Expand Down
3 changes: 3 additions & 0 deletions libra/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mod tests {
use crate::utils::test;
#[tokio::test]
#[serial]
/// Test objects can be correctly saved to and loaded from storage.
async fn test_save_load_object() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
Expand All @@ -148,6 +149,8 @@ mod tests {
}

#[test]
/// Tests commit message formatting and parsing with signatures.
/// Verifies correct handling of GPG/SSH signatures and proper message extraction.
fn test_format_and_parse_commit_msg() {
{
let msg = "commit message";
Expand Down
4 changes: 4 additions & 0 deletions libra/src/command/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ fn diff_tree_objs(old_tree: Option<&SHA1>, new_tree: &SHA1) -> HashSet<Entry> {
mod test {
use super::*;
#[test]
/// Tests successful parsing of push command arguments with different parameter combinations.
/// Verifies repository, refspec and upstream flag settings are correctly interpreted.
fn test_parse_args_success() {
let args = vec!["push"];
let args = PushArgs::parse_from(args);
Expand All @@ -352,6 +354,8 @@ mod test {
}

#[test]
/// Tests failure cases for push command argument parsing with invalid parameter combinations.
/// Verifies that missing required parameters are properly detected as errors.
fn test_parse_args_fail() {
let args = vec!["push", "-u"];
let args = PushArgs::try_parse_from(args);
Expand Down
2 changes: 2 additions & 0 deletions libra/src/command/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ mod test {

#[tokio::test]
#[serial]
/// Tests the file status detection functionality with respect to ignore patterns.
/// Verifies that files matching patterns in .libraignore are properly excluded from status reports.
async fn test_changes_to_be_staged() {
let test_dir = tempdir().unwrap();
test::setup_with_new_libra_in(test_dir.path()).await;
Expand Down
Loading