From d5e8896d1bedb80e5f8587f33012ccb10ce24ecc Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sun, 11 Jun 2023 18:04:55 -0700 Subject: [PATCH] tests: demo `jj branch forget` behavior in colocated repos --- tests/test_branch_command.rs | 58 ++++++++++++++++++++++++++++++++++++ tests/test_git_colocated.rs | 29 ++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 12897a50c..ce8502b4a 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -70,6 +70,7 @@ fn test_branch_empty_name() { For more information, try '--help'. "###); } + #[test] fn test_branch_forget_glob() { let test_env = TestEnvironment::default(); @@ -113,6 +114,63 @@ fn test_branch_forget_glob() { "###); } +#[test] +fn test_branch_forget_export() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + test_env.jj_cmd_success(&repo_path, &["new"]); + test_env.jj_cmd_success(&repo_path, &["branch", "set", "foo"]); + let stdout = test_env.jj_cmd_success(&repo_path, &["branch", "list"]); + insta::assert_snapshot!(stdout, @r###" + foo: 65b6b74e0897 (no description set) + "###); + + // Exporting the branch to git creates a local-git tracking branch + let stdout = test_env.jj_cmd_success(&repo_path, &["git", "export"]); + insta::assert_snapshot!(stdout, @""); + let stdout = test_env.jj_cmd_success(&repo_path, &["branch", "forget", "foo"]); + insta::assert_snapshot!(stdout, @""); + // Forgetting a branch does not delete its local-git tracking branch. This is + // the opposite of what happens to remote-tracking branches. + // TODO: Consider allowing forgetting local-git tracking branches as an option + let stdout = test_env.jj_cmd_success(&repo_path, &["branch", "list"]); + insta::assert_snapshot!(stdout, @r###" + foo (deleted) + @git: 65b6b74e0897 (no description set) + "###); + + // Aside: the way we currently resolve git refs means that `foo` + // resolves to `foo@git` when actual `foo` doesn't exist. + // Short-term TODO: This behavior will be changed in a subsequent commit. + let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r=foo", "--no-graph"]); + insta::assert_snapshot!(stdout, @r###" + rlvkpnrzqnoo test.user@example.com 2001-02-03 04:05:08.000 +07:00 65b6b74e0897 + (empty) (no description set) + "###); + + // The presence of the @git branch means that a `jj git import` is a no-op... + let stdout = test_env.jj_cmd_success(&repo_path, &["git", "import"]); + insta::assert_snapshot!(stdout, @r###" + Nothing changed. + "###); + // ... and a `jj git export` will delete the branch from git and will delete the + // git-tracking branch. In a colocated repo, this will happen automatically + // immediately after a `jj branch forget`. This is demonstrated in + // `test_git_colocated_branch_forget` in test_git_colocated.rs + let stdout = test_env.jj_cmd_success(&repo_path, &["git", "export"]); + insta::assert_snapshot!(stdout, @""); + let stdout = test_env.jj_cmd_success(&repo_path, &["branch", "list"]); + insta::assert_snapshot!(stdout, @""); + + // Note that if `jj branch forget` *did* delete foo@git, a subsequent `jj + // git export` would be a no-op and a `jj git import` would resurrect + // the branch. In a normal repo, that might be OK. In a colocated repo, + // this would automatically happen before the next command, making `jj + // branch forget` useless. +} + // TODO: Test `jj branch list` with a remote named `git` fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String { diff --git a/tests/test_git_colocated.rs b/tests/test_git_colocated.rs index 5edd41cc3..c481dc6f8 100644 --- a/tests/test_git_colocated.rs +++ b/tests/test_git_colocated.rs @@ -216,6 +216,35 @@ fn test_git_colocated_branches() { "###); } +#[test] +fn test_git_colocated_branch_forget() { + let test_env = TestEnvironment::default(); + let workspace_root = test_env.env_root().join("repo"); + let _git_repo = git2::Repository::init(&workspace_root).unwrap(); + test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]); + test_env.jj_cmd_success(&workspace_root, &["new"]); + test_env.jj_cmd_success(&workspace_root, &["branch", "set", "foo"]); + insta::assert_snapshot!(get_log_output(&test_env, &workspace_root), @r###" + @ 65b6b74e08973b88d38404430f119c8c79465250 foo + ◉ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 master HEAD@git + ◉ 0000000000000000000000000000000000000000 + "###); + let stdout = test_env.jj_cmd_success(&workspace_root, &["branch", "list"]); + insta::assert_snapshot!(stdout, @r###" + foo: 65b6b74e0897 (no description set) + master: 230dd059e1b0 (no description set) + "###); + + let stdout = test_env.jj_cmd_success(&workspace_root, &["branch", "forget", "foo"]); + insta::assert_snapshot!(stdout, @""); + // A forgotten branch is deleted in the git repo. For a detailed demo explaining + // this, see `test_branch_forget_export` in `test_branch_command.rs`. + let stdout = test_env.jj_cmd_success(&workspace_root, &["branch", "list"]); + insta::assert_snapshot!(stdout, @r###" + master: 230dd059e1b0 (no description set) + "###); +} + #[test] fn test_git_colocated_conflicting_git_refs() { let test_env = TestEnvironment::default();