Debian Codegraph @

cli/tests/test_git_root.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use testutils::TestResult;
16 use testutils::git;
17
18 use crate::common::TestEnvironment;
19
20 #[test]
21 fn test_git_root_git_backend_noncolocated() {
22 let test_env = TestEnvironment::default();
23 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
24 let work_dir = test_env.work_dir("repo");
25
26 let output = work_dir.run_jj(["git", "root"]);
27 insta::assert_snapshot!(output, @"
28 $TEST_ENV/repo/.jj/repo/store/git
29 [EOF]
30 ");
31 }
32
33 #[test]
34 fn test_git_root_git_backend_colocated() {
35 let test_env = TestEnvironment::default();
36 test_env
37 .run_jj_in(".", ["git", "init", "--colocate", "repo"])
38 .success();
39 let work_dir = test_env.work_dir("repo");
40
41 let output = work_dir.run_jj(["git", "root"]);
42 insta::assert_snapshot!(output, @"
43 $TEST_ENV/repo/.git
44 [EOF]
45 ");
46 }
47
48 #[test]
49 fn test_git_root_git_backend_external_git_dir() -> TestResult {
50 let test_env = TestEnvironment::default();
51 let work_dir = test_env.work_dir("").create_dir("repo");
52 let git_repo_work_dir = test_env.work_dir("git-repo");
53 let git_repo = git::init(git_repo_work_dir.root());
54
55 // Create an initial commit in Git
56 let tree_id = git::add_commit(
57 &git_repo,
58 "refs/heads/master",
59 "file",
60 b"contents",
61 "initial",
62 &[],
63 )
64 .tree_id;
65 git::checkout_tree_index(&git_repo, tree_id);
66 assert_eq!(git_repo_work_dir.read_file("file"), b"contents");
67 insta::assert_snapshot!(
68 git_repo.head_id()?.to_string(),
69 @"97358f54806c7cd005ed5ade68a779595efbae7e"
70 );
71
72 work_dir
73 .run_jj([
74 "git",
75 "init",
76 "--git-repo",
77 git_repo_work_dir.root().to_str().unwrap(),
78 ])
79 .success();
80
81 let output = work_dir.run_jj(["git", "root"]);
82 insta::assert_snapshot!(output, @"
83 $TEST_ENV/git-repo/.git
84 [EOF]
85 ");
86 Ok(())
87 }
88
89 #[test]
90 fn test_git_root_simple_backend() {
91 let test_env = TestEnvironment::default();
92 test_env
93 .run_jj_in(".", ["debug", "init-simple", "repo"])
94 .success();
95 let work_dir = test_env.work_dir("repo");
96
97 let output = work_dir.run_jj(["git", "root"]);
98 insta::assert_snapshot!(output, @"
99 ------- stderr -------
100 Error: The repo is not backed by a Git repo
101 [EOF]
102 [exit status: 1]
103 ");
104 }