mod common;
use common::assertions::assert_on_branch;
use common::fixtures::WorkspaceBuilder;
#[test]
fn test_checkout_existing_branch() {
let ws = WorkspaceBuilder::new()
.add_repo("frontend")
.add_repo("backend")
.build();
let manifest = ws.load_manifest();
gitgrip::cli::commands::branch::run_branch(gitgrip::cli::commands::branch::BranchOptions {
workspace_root: &ws.workspace_root,
manifest: &manifest,
name: Some("feat/checkout-test"),
delete: false,
move_commits: false,
repos_filter: None,
group_filter: None,
json: false,
})
.unwrap();
gitgrip::cli::commands::checkout::run_checkout(
&ws.workspace_root,
&manifest,
"main",
false,
None,
None,
)
.unwrap();
assert_on_branch(&ws.repo_path("frontend"), "main");
assert_on_branch(&ws.repo_path("backend"), "main");
let result = gitgrip::cli::commands::checkout::run_checkout(
&ws.workspace_root,
&manifest,
"feat/checkout-test",
false,
None,
None,
);
assert!(
result.is_ok(),
"checkout should succeed: {:?}",
result.err()
);
assert_on_branch(&ws.repo_path("frontend"), "feat/checkout-test");
assert_on_branch(&ws.repo_path("backend"), "feat/checkout-test");
}
#[test]
fn test_checkout_nonexistent_branch() {
let ws = WorkspaceBuilder::new().add_repo("app").build();
let manifest = ws.load_manifest();
let result = gitgrip::cli::commands::checkout::run_checkout(
&ws.workspace_root,
&manifest,
"feat/does-not-exist",
false,
None,
None,
);
assert!(
result.is_ok(),
"checkout of nonexistent branch should not error: {:?}",
result.err()
);
assert_on_branch(&ws.repo_path("app"), "main");
}
#[test]
fn test_checkout_main() {
let ws = WorkspaceBuilder::new()
.add_repo("app")
.add_repo("lib")
.build();
let manifest = ws.load_manifest();
gitgrip::cli::commands::branch::run_branch(gitgrip::cli::commands::branch::BranchOptions {
workspace_root: &ws.workspace_root,
manifest: &manifest,
name: Some("feat/temp"),
delete: false,
move_commits: false,
repos_filter: None,
group_filter: None,
json: false,
})
.unwrap();
assert_on_branch(&ws.repo_path("app"), "feat/temp");
let result = gitgrip::cli::commands::checkout::run_checkout(
&ws.workspace_root,
&manifest,
"main",
false,
None,
None,
);
assert!(
result.is_ok(),
"checkout main should succeed: {:?}",
result.err()
);
assert_on_branch(&ws.repo_path("app"), "main");
assert_on_branch(&ws.repo_path("lib"), "main");
}
#[test]
fn test_checkout_create_flag() {
let ws = WorkspaceBuilder::new()
.add_repo("frontend")
.add_repo("backend")
.build();
let manifest = ws.load_manifest();
let result = gitgrip::cli::commands::checkout::run_checkout(
&ws.workspace_root,
&manifest,
"feat/new-feature",
true, None,
None,
);
assert!(
result.is_ok(),
"checkout -b should succeed: {:?}",
result.err()
);
assert_on_branch(&ws.repo_path("frontend"), "feat/new-feature");
assert_on_branch(&ws.repo_path("backend"), "feat/new-feature");
}
#[test]
fn test_checkout_skips_non_git_repo() {
let ws = WorkspaceBuilder::new()
.add_repo("frontend")
.add_repo("backend")
.build();
let manifest = ws.load_manifest();
gitgrip::cli::commands::branch::run_branch(gitgrip::cli::commands::branch::BranchOptions {
workspace_root: &ws.workspace_root,
manifest: &manifest,
name: Some("feat/checkout-safe"),
delete: false,
move_commits: false,
repos_filter: None,
group_filter: None,
json: false,
})
.unwrap();
std::fs::remove_dir_all(ws.repo_path("backend").join(".git")).unwrap();
let result = gitgrip::cli::commands::checkout::run_checkout(
&ws.workspace_root,
&manifest,
"feat/checkout-safe",
false,
None,
None,
);
assert!(
result.is_ok(),
"checkout should not crash on non-git repo: {:?}",
result.err()
);
assert_on_branch(&ws.repo_path("frontend"), "feat/checkout-safe");
assert!(!ws.repo_path("backend").join(".git").exists());
}