Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Oct 12, 2024
1 parent 6abe4cb commit efd6272
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ impl<'a> WorktreePlan<'a> {
match &args.inner.name_or_path {
Some(name_or_path) => {
if name_or_path.contains('/') {
// Test case: `add_by_path`.
NormalPath::from_cwd(name_or_path)
} else {
// Test case: `add_by_name_new_local`.
NormalPath::from_cwd(
git.worktree()
.container()?
.tap_mut(|p| p.push(name_or_path)),
)
}
}
// Test case: `add_branch_new_local`.
None => NormalPath::from_cwd(git.worktree().path_for(new_branch.branch_name())?),
}
}
Expand Down Expand Up @@ -245,13 +248,13 @@ impl BranchStartPointPlan {
/// --branch | NAME_OR_PATH | START_POINT | behavior | start | test case
/// -------- | ------------ | ----------- | ------------------ | ------------- | ---------
/// BRANCH | [ignored] | | new BRANCH | DEFAULT | add_branch_new
/// BRANCH | [ignored] | LOCAL_BRANCH | new BRANCH | LOCAL_BRANCH |
/// BRANCH | [ignored] | REMOTE_BRANCH | new BRANCH | REMOTE_BRANCH |
/// BRANCH | [ignored] | COMMITISH | new BRANCH | COMMITISH |
/// BRANCH | [ignored] | LOCAL_BRANCH | new BRANCH | LOCAL_BRANCH | add_branch_start_point_exiting_local
/// BRANCH | [ignored] | REMOTE_BRANCH | new BRANCH | REMOTE_BRANCH | add_branch_start_point_existing_remote
/// BRANCH | [ignored] | COMMITISH | new BRANCH | COMMITISH | add_branch_start_point_new_local
/// -------- | ------------ | ----------- | ------------------ | ------------- | ---------
/// | NAME | LOCAL_BRANCH | existing LOCAL_BRANCH | | add_start_point_existing_local
/// | NAME | REMOTE_BRANCH | new REMOTE_BRANCH | REMOTE_BRANCH | add_start_point_existing_remote
/// | NAME | OTHER | new NAME | OTHER | add_start_point_new_local
/// | NAME | COMMITISH | new NAME | COMMITISH | add_start_point_new_local
/// -------- | ------------ | ----------- | ------------------ | ------------- | ---------
/// | LOCAL_BRANCH | | existing LOCAL_BRANCH | | add_by_name_existing_local
/// | REMOTE_BRANCH | | new REMOTE_BRANCH | REMOTE_BRANCH | add_by_name_existing_remote
Expand Down
3 changes: 2 additions & 1 deletion src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ pub fn clone(git: AppGit<'_>, args: CloneArgs) -> miette::Result<()> {
&& looks_like_gh_url(&args.repository)
&& which_global("gh").is_ok()
{
// TODO: Test this?
// TODO: Test this!!!
Command::new("gh")
.args(["repo", "clone", &args.repository, destination.as_str()])
.args(args.clone_args)
.status_checked()
.into_diagnostic()?;
} else {
// Test case: `clone_simple`.
git.clone_repository(&args.repository, Some(&destination), &args.clone_args)?;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/add_branch_new.rs → tests/add_branch_new_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pretty_assertions::assert_eq;
use test_harness::GitProle;

#[test]
fn add_new_branch() {
fn add_branch_new_local() {
let prole = GitProle::new().unwrap();
prole.setup_worktree_repo("my-repo").unwrap();
prole
Expand Down
43 changes: 43 additions & 0 deletions tests/add_branch_start_point_existing_local.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use command_error::CommandExt;
use expect_test::expect;
use pretty_assertions::assert_eq;
use test_harness::GitProle;

#[test]
fn add_branch_start_point_existing_local() {
let prole = GitProle::new().unwrap();
prole.setup_worktree_repo("my-repo").unwrap();

// Create a new branch and commit to base our new worktree off of.
prole
.sh("
cd my-repo/main || exit
git switch -c doggy
echo 'cutie puppy' > README.md
git commit -am 'Cooler README'
git switch main
")
.unwrap();

prole
.cd_cmd("my-repo/main")
.args(["add", "-b", "softy", "puppy", "doggy"])
.status_checked()
.unwrap();

prole.assert_contents(&[(
"my-repo/puppy/README.md",
expect![[r#"
cutie puppy
"#]],
)]);

assert_eq!(prole.current_branch_in("my-repo/puppy").unwrap(), "softy");

assert_eq!(
prole
.upstream_for_branch_in("my-repo/puppy", "softy")
.unwrap(),
"doggy"
);
}
50 changes: 50 additions & 0 deletions tests/add_branch_start_point_existing_remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use command_error::CommandExt;
use expect_test::expect;
use pretty_assertions::assert_eq;
use test_harness::GitProle;

#[test]
fn add_branch_start_point_existing_remote() {
let prole = GitProle::new().unwrap();
prole.setup_repo("my-remote/my-repo").unwrap();
// Set up a `puppy` branch in the remote.
prole
.sh("
cd my-remote/my-repo || exit
git switch -c puppy
echo 'softy pup' > README.md
git commit -am 'cooler readme'
git switch main
")
.unwrap();

prole
.cmd()
.args(["clone", "my-remote/my-repo"])
.status_checked()
.unwrap();

prole
.cd_cmd("my-repo/main")
.args(["add", "-b", "softie", "doggy", "puppy"])
.status_checked()
.unwrap();

// We get a checkout for the remote-tracking branch!
prole.assert_contents(&[(
"my-repo/doggy/README.md",
expect![[r#"
softy pup
"#]],
)]);

assert_eq!(prole.current_branch_in("my-repo/doggy").unwrap(), "softie");

// We're tracking the remote branch we expect.
assert_eq!(
prole
.upstream_for_branch_in("my-repo/doggy", "softie")
.unwrap(),
"origin/puppy"
);
}
34 changes: 34 additions & 0 deletions tests/add_branch_start_point_new_local.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use command_error::CommandExt;
use expect_test::expect;
use pretty_assertions::assert_eq;
use test_harness::GitProle;

#[test]
fn add_branch_start_point_new_local() {
let prole = GitProle::new().unwrap();
prole.setup_worktree_repo("my-repo").unwrap();

prole
.sh("
cd my-repo/main || exit
git switch -c puppy
echo 'soft cutie' > README.md
git commit -am 'Cooler readme'
")
.unwrap();

prole
.cd_cmd("my-repo/main")
.args(["add", "-c", "softy", "doggy", "@"])
.status_checked()
.unwrap();

prole.assert_contents(&[(
"my-repo/doggy/README.md",
expect![[r#"
soft cutie
"#]],
)]);

assert_eq!(prole.current_branch_in("my-repo/doggy").unwrap(), "softy");
}

0 comments on commit efd6272

Please sign in to comment.