Skip to content
Merged
Changes from 4 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
103 changes: 103 additions & 0 deletions tests/command/clone_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,106 @@ async fn test_clone_empty_repo() {
_ => panic!("should be branch"),
};
}

#[tokio::test]
#[serial]
#[ignore]
/// Test the clone command with an existing empty directory
async fn test_clone_to_existing_empty_dir() {
let temp_path = tempdir().unwrap();
let _guard = test::ChangeDirGuard::new(temp_path.path());
let repo_path = temp_path.path().join("mega-libra-clone-branch-test");
std::fs::create_dir(&repo_path).unwrap();

let remote_url = "https://gitee.com/pikady/mega-libra-clone-branch-test.git".to_string();

command::clone::execute(CloneArgs {
remote_repo: remote_url,
local_path: Some(repo_path.to_str().unwrap().to_string()),
branch: Some("dev".to_string()),
})
.await;

// Verify that the `.libra` directory exists
let libra_dir = repo_path.join(".libra");
assert!(libra_dir.exists());

// Verify the Head reference
match Head::current().await {
Head::Branch(current_branch) => {
assert_eq!(current_branch, "dev");
}
_ => panic!("should be branch"),
};
}

#[tokio::test]
#[serial]
#[ignore]
/// Test that clone fails when the target directory exists and is not empty
async fn test_clone_to_existing_dir() {
let temp_path = tempdir().unwrap();
let _guard = test::ChangeDirGuard::new(temp_path.path());

let repo_path = temp_path.path().join("mega-libra-clone-branch-test");
std::fs::create_dir(&repo_path).unwrap();
let dummy_file = repo_path.join("exists.txt");
std::fs::write(&dummy_file, "test").unwrap();

let remote_url = "https://gitee.com/pikady/mega-libra-clone-branch-test.git".to_string();

command::clone::execute(CloneArgs {
remote_repo: remote_url,
local_path: Some(repo_path.to_str().unwrap().to_string()),
branch: Some("dev".to_string()),
})
.await;

// Verify that the `.libra` directory does not exist
let libra_dir = repo_path.join(".libra");
assert!(!libra_dir.exists());
// Make sure that the pre-existing file should still exist
assert!(dummy_file.exists(), "pre-existing file should still exist");
let content = std::fs::read_to_string(&dummy_file).unwrap();
// Make sure that the pre-existing file content should remain unchanged
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment "Make sure that the pre-existing file content should remain unchanged" is redundant with the assertion message. Consider removing the comment or making it more informative.

Copilot uses AI. Check for mistakes.
assert_eq!(
content, "test",
"pre-existing file content should remain unchanged"
);
}

#[tokio::test]
#[serial]
#[ignore]
/// Test the clone command in the case where a file with the same name as the target directory already exists.
async fn test_clone_to_dir_with_existing_file_name() {
let temp_path = tempdir().unwrap();
let _guard = test::ChangeDirGuard::new(temp_path.path());

let conflict_path = temp_path.path().join("mega-libra-clone-branch-test");
std::fs::write(&conflict_path, "test").unwrap();

let remote_url = "https://gitee.com/pikady/mega-libra-clone-branch-test.git".to_string();

command::clone::execute(CloneArgs {
remote_repo: remote_url,
local_path: Some(conflict_path.to_str().unwrap().to_string()),
branch: Some("dev".to_string()),
})
.await;

// Verify that the `.libra` directory does not exist
let libra_dir = conflict_path.join(".libra");
assert!(!libra_dir.exists());
// Make sure that the pre-existing file should still exist
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment "Make sure that the pre-existing file should still exist" is redundant with the assertion message. Consider removing the comment or making it more informative.

Suggested change
// Make sure that the pre-existing file should still exist

Copilot uses AI. Check for mistakes.
assert!(
conflict_path.exists(),
"pre-existing file should still exist"
);
let content = std::fs::read_to_string(&conflict_path).unwrap();
// Make sure that the pre-existing file content should remain unchanged
assert_eq!(
content, "test",
"pre-existing file content should remain unchanged"
);
}
Loading