Skip to content

Commit

Permalink
test_git_push: demo limitations of pushing without --force-with-lease
Browse files Browse the repository at this point in the history
Adds two tests where pushing should fail, but currently succeeds.
  • Loading branch information
ilyagr committed Apr 17, 2024
1 parent 1975acb commit a0e98d2
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions cli/tests/test_git_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,117 @@ fn test_git_push_not_fast_forward() {
"###);
}

// Short-term TODO: implement this.
// This tests whether the push checks that the remote branches are in expected
// positions. Once this is implemented, `jj git push` will be similar to `git
// push --force-with-lease`
#[test]
fn test_git_push_sideways_unexpectedly_moved() {
let (test_env, workspace_root) = set_up();

// Move branch1 forward on the remote
let origin_path = test_env.env_root().join("origin");
test_env.jj_cmd_ok(&origin_path, &["new", "branch1", "-m=remote"]);
std::fs::write(origin_path.join("remote"), "remote").unwrap();
test_env.jj_cmd_ok(&origin_path, &["branch", "set", "branch1"]);
let assert = test_env
.jj_cmd(&origin_path, &["branch", "list", "--all"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
branch1: vruxwmqv fb645b4b remote
@git (behind by 1 commits): qpvuntsm 45a3aa29 (empty) description 1
branch2: zsuskuln 8476341e (empty) description 2
@git: zsuskuln 8476341e (empty) description 2
"###);
insta::assert_snapshot!(get_stderr_string(&assert), @"");
test_env.jj_cmd_ok(&origin_path, &["git", "export"]);

// Move branch1 forward to another commit locally
test_env.jj_cmd_ok(&workspace_root, &["new", "root()", "-m=local"]);
std::fs::write(workspace_root.join("local"), "local").unwrap();
test_env.jj_cmd_ok(
&workspace_root,
&["branch", "set", "branch1", "--allow-backwards"],
);
let assert = test_env
.jj_cmd(&workspace_root, &["branch", "list", "--all"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
branch1: kmkuslsw 0f8bf988 local
@origin (ahead by 1 commits, behind by 1 commits): lzmmnrxq 45a3aa29 (empty) description 1
branch2: rlzusymt 8476341e (empty) description 2
@origin: rlzusymt 8476341e (empty) description 2
"###);
insta::assert_snapshot!(get_stderr_string(&assert), @"");

// BUG: Pushing should fail. Currently, it succeeds because moving the branch
// sideways causes `jj` to use the analogue of `git push --force` when pushing.
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"");
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
Branch changes to push to origin:
Force branch branch1 from 45a3aa29e907 to 0f8bf988588e
"###);
}

// Short-term TODO: implement this.
// This tests whether the push checks that the remote branches are in expected
// positions. Once this is implemented, `jj git push` will be similar to `git
// push --force-with-lease`
#[test]
fn test_git_push_deletion_unexpectedly_moved() {
let (test_env, workspace_root) = set_up();

// Move branch1 forward on the remote
let origin_path = test_env.env_root().join("origin");
test_env.jj_cmd_ok(&origin_path, &["new", "branch1", "-m=remote"]);
std::fs::write(origin_path.join("remote"), "remote").unwrap();
test_env.jj_cmd_ok(&origin_path, &["branch", "set", "branch1"]);
let assert = test_env
.jj_cmd(&origin_path, &["branch", "list", "--all"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
branch1: vruxwmqv fb645b4b remote
@git (behind by 1 commits): qpvuntsm 45a3aa29 (empty) description 1
branch2: zsuskuln 8476341e (empty) description 2
@git: zsuskuln 8476341e (empty) description 2
"###);
insta::assert_snapshot!(get_stderr_string(&assert), @"");
test_env.jj_cmd_ok(&origin_path, &["git", "export"]);

// Delete branch1 locally
test_env.jj_cmd_ok(&workspace_root, &["branch", "delete", "branch1"]);
let assert = test_env
.jj_cmd(&workspace_root, &["branch", "list", "--all"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
branch1 (deleted)
@origin: lzmmnrxq 45a3aa29 (empty) description 1
(this branch will be *deleted permanently* on the remote on the next `jj git push`. Use `jj branch forget` to prevent this)
branch2: rlzusymt 8476341e (empty) description 2
@origin: rlzusymt 8476341e (empty) description 2
"###);
insta::assert_snapshot!(get_stderr_string(&assert), @"");

// BUG: Pushing should fail because the branch was moved on the remote
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push", "--branch", "branch1"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"");
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
Branch changes to push to origin:
Delete branch branch1 from 45a3aa29e907
"###);
}

#[test]
fn test_git_push_locally_created_and_rewritten() {
let (test_env, workspace_root) = set_up();
Expand Down

0 comments on commit a0e98d2

Please sign in to comment.