use std::path::Path;
use std::process::{Command, Output};
use regex::Regex;
use tempfile::TempDir;
fn strip_ansi(s: &str) -> String {
let re = Regex::new(r"\x1b\[[0-9;]*m").unwrap();
re.replace_all(s, "").to_string()
}
fn create_origin_repo() -> TempDir {
let dir = TempDir::new().expect("Failed to create temp dir");
run_git(dir.path(), &["init", "--bare", "--initial-branch=main"]);
dir
}
fn create_local_repo(origin_path: &Path) -> TempDir {
let dir = TempDir::new().expect("Failed to create temp dir");
run_git(dir.path(), &["init"]);
run_git(dir.path(), &["config", "user.email", "test@example.com"]);
run_git(dir.path(), &["config", "user.name", "Test User"]);
run_git(dir.path(), &["checkout", "-b", "main"]);
std::fs::write(dir.path().join("README.md"), "# Test").expect("Failed to write file");
run_git(dir.path(), &["add", "."]);
run_git(dir.path(), &["commit", "-m", "Initial commit"]);
let origin_url = format!("file://{}", origin_path.display());
run_git(dir.path(), &["remote", "add", "origin", &origin_url]);
run_git(dir.path(), &["push", "-u", "origin", "main"]);
dir
}
fn run_git(dir: &Path, args: &[&str]) -> String {
let output = Command::new("git")
.args(args)
.current_dir(dir)
.output()
.expect("Failed to run git command");
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("git {} failed: {}", args.join(" "), stderr);
}
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
fn run_gw(dir: &Path, args: &[&str]) -> Output {
let gw_path = env!("CARGO_BIN_EXE_gw");
Command::new(gw_path)
.args(args)
.current_dir(dir)
.env("NO_COLOR", "1") .output()
.expect("Failed to run gw command")
}
fn get_head(dir: &Path) -> String {
run_git(dir, &["rev-parse", "HEAD"])
}
#[test]
fn test_sync_on_home_branch_pulls_from_origin() {
let origin = create_origin_repo();
let local = create_local_repo(origin.path());
let initial_head = get_head(local.path());
let contributor = TempDir::new().expect("Failed to create temp dir");
let origin_url = format!("file://{}", origin.path().display());
run_git(contributor.path(), &["clone", &origin_url, "."]);
run_git(
contributor.path(),
&["config", "user.email", "contrib@example.com"],
);
run_git(contributor.path(), &["config", "user.name", "Contributor"]);
run_git(contributor.path(), &["checkout", "main"]);
std::fs::write(contributor.path().join("new_file.txt"), "new content")
.expect("Failed to write file");
run_git(contributor.path(), &["add", "."]);
run_git(contributor.path(), &["commit", "-m", "Add new file"]);
run_git(contributor.path(), &["push", "origin", "main"]);
assert_eq!(get_head(local.path()), initial_head);
let output = run_gw(local.path(), &["sync"]);
assert!(
output.status.success(),
"gw sync failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = strip_ansi(&String::from_utf8_lossy(&output.stdout));
assert!(
stdout.contains("Pulled 1 commit") || stdout.contains("Already up to date"),
"Expected sync output, got: {}",
stdout
);
let new_head = get_head(local.path());
assert_ne!(
new_head, initial_head,
"HEAD should have advanced after sync"
);
}
#[test]
fn test_sync_on_home_branch_when_already_up_to_date() {
let origin = create_origin_repo();
let local = create_local_repo(origin.path());
let output = run_gw(local.path(), &["sync"]);
assert!(
output.status.success(),
"gw sync failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = strip_ansi(&String::from_utf8_lossy(&output.stdout));
assert!(
stdout.contains("Already up to date"),
"Expected 'Already up to date', got: {}",
stdout
);
}