mod common;
use common::*;
#[tokio::test]
async fn test_import_from_git() -> Result<()> {
let _serial = WORKDIR_LOCK.lock().await;
let temp_dir = tempfile::TempDir::new()?;
let repo_path = temp_dir.path().to_path_buf();
let _guard = DirGuard::new(&repo_path);
if std::process::Command::new("git").arg("--version").output().is_err() {
eprintln!("Skipping test_import_from_git: git not available");
return Ok(());
}
std::process::Command::new("git")
.args(["init"])
.current_dir(&repo_path)
.output()
.with_context(|| "git init failed")?;
std::process::Command::new("git")
.args(["config", "core.hooksPath", "/dev/null"])
.current_dir(&repo_path)
.output()?;
std::fs::write(repo_path.join("file1.txt"), "content 1\n")?;
std::process::Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()?;
std::process::Command::new("git")
.args(["commit", "-m", "first commit"])
.current_dir(&repo_path)
.output()?;
std::fs::write(repo_path.join("file2.txt"), "content 2\n")?;
std::process::Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()?;
std::process::Command::new("git")
.args(["commit", "-m", "second commit"])
.current_dir(&repo_path)
.output()?;
commands::import_command(false).await?;
let git_manager = VcsManager::new(repo_path.clone())?;
let head = git_manager.get_head_hash()?;
let head_commit = git_manager.repo().read_commit(&head)?;
assert_eq!(head_commit.message, "second commit");
assert_eq!(head_commit.parents.len(), 1);
let parent_commit = git_manager.repo().read_commit(&head_commit.parents[0])?;
assert_eq!(parent_commit.message, "first commit");
assert!(repo_path.join("file1.txt").exists());
assert!(repo_path.join("file2.txt").exists());
Ok(())
}
#[tokio::test]
async fn test_export_to_git() -> Result<()> {
let _serial = WORKDIR_LOCK.lock().await;
let temp_dir = tempfile::TempDir::new()?;
let repo_path = temp_dir.path().to_path_buf();
let _guard = DirGuard::new(&repo_path);
if std::process::Command::new("git").arg("--version").output().is_err() {
eprintln!("Skipping test_export_to_git: git not available");
return Ok(());
}
commands::init_command(false).await?;
let git_manager = VcsManager::new(repo_path.clone())?;
tokio::fs::write(repo_path.join("file1.txt"), "content 1\n").await?;
git_manager.add_all().await?;
git_manager.commit("first commit").await?;
tokio::fs::write(repo_path.join("file2.txt"), "content 2\n").await?;
git_manager.add_all().await?;
git_manager.commit("second commit").await?;
commands::export_command().await?;
let log = std::process::Command::new("git")
.args(["log", "--oneline"])
.current_dir(&repo_path)
.output()
.context("git log failed")?;
let log_str = String::from_utf8_lossy(&log.stdout);
assert!(log_str.contains("second commit"), "Git should have 'second commit'");
assert!(log_str.contains("first commit"), "Git should have 'first commit'");
Ok(())
}
#[tokio::test]
async fn test_bundle_and_unbundle() -> Result<()> {
let _serial = WORKDIR_LOCK.lock().await;
let temp_dir = tempfile::TempDir::new()?;
let repo_path = temp_dir.path().to_path_buf();
let _guard = DirGuard::new(&repo_path);
if std::process::Command::new("tar").arg("--version").output().is_err() {
eprintln!("Skipping test_bundle_and_unbundle: tar not available");
return Ok(());
}
commands::init_command(false).await?;
let git_manager = VcsManager::new(repo_path.clone())?;
tokio::fs::write(repo_path.join("file.txt"), "content\n").await?;
git_manager.add_all().await?;
git_manager.commit("test commit").await?;
let head_before = git_manager.get_head_hash()?;
let bundle_path = repo_path.join("test-bundle.tar.gz");
commands::bundle_command(Some(bundle_path.to_string_lossy().to_string())).await?;
assert!(bundle_path.exists(), "Bundle file should exist");
let restore_dir = tempfile::TempDir::new()?;
let restore_path = restore_dir.path().to_path_buf();
std::fs::copy(&bundle_path, restore_path.join("test-bundle.tar.gz"))?;
let original_dir = std::env::current_dir()?;
std::env::set_current_dir(&restore_path)?;
let result = commands::unbundle_command("test-bundle.tar.gz".to_string()).await;
let _ = std::env::set_current_dir(&original_dir);
result?;
let restored_manager = VcsManager::new(restore_path.clone())?;
let head_after = restored_manager.get_head_hash()?;
assert_eq!(head_before, head_after, "HEAD should match after unbundle");
assert!(restore_path.join("file.txt").exists(), "File should exist after unbundle");
Ok(())
}