just playing with tangled
1// Copyright 2020 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
15use std::path::Path;
16use std::path::PathBuf;
17
18use jj_lib::config::StackedConfig;
19use jj_lib::git_backend::GitBackend;
20use jj_lib::ref_name::WorkspaceName;
21use jj_lib::repo::Repo as _;
22use jj_lib::settings::UserSettings;
23use jj_lib::workspace::Workspace;
24use test_case::test_case;
25use testutils::git;
26use testutils::write_random_commit;
27use testutils::TestRepoBackend;
28use testutils::TestWorkspace;
29
30fn canonicalize(input: &Path) -> (PathBuf, PathBuf) {
31 let uncanonical = input.join("..").join(input.file_name().unwrap());
32 let canonical = dunce::canonicalize(&uncanonical).unwrap();
33 (canonical, uncanonical)
34}
35
36#[test]
37fn test_init_local() {
38 let settings = testutils::user_settings();
39 let temp_dir = testutils::new_temp_dir();
40 let (canonical, uncanonical) = canonicalize(temp_dir.path());
41 let (workspace, repo) = Workspace::init_simple(&settings, &uncanonical).unwrap();
42 assert!(repo
43 .store()
44 .backend_impl()
45 .downcast_ref::<GitBackend>()
46 .is_none());
47 assert_eq!(workspace.workspace_root(), &canonical);
48
49 // Just test that we can write a commit to the store
50 let mut tx = repo.start_transaction();
51 write_random_commit(tx.repo_mut());
52}
53
54#[test]
55fn test_init_internal_git() {
56 let settings = testutils::user_settings();
57 let temp_dir = testutils::new_temp_dir();
58 let (canonical, uncanonical) = canonicalize(temp_dir.path());
59 let (workspace, repo) = Workspace::init_internal_git(&settings, &uncanonical).unwrap();
60 let git_backend = repo
61 .store()
62 .backend_impl()
63 .downcast_ref::<GitBackend>()
64 .unwrap();
65 let repo_path = canonical.join(".jj").join("repo");
66 assert_eq!(workspace.workspace_root(), &canonical);
67 assert_eq!(
68 git_backend.git_repo_path(),
69 canonical.join(PathBuf::from_iter([".jj", "repo", "store", "git"])),
70 );
71 assert!(git_backend.git_workdir().is_none());
72 assert_eq!(
73 std::fs::read_to_string(repo_path.join("store").join("git_target")).unwrap(),
74 "git"
75 );
76
77 // Just test that we can write a commit to the store
78 let mut tx = repo.start_transaction();
79 write_random_commit(tx.repo_mut());
80}
81
82#[test]
83fn test_init_colocated_git() {
84 let settings = testutils::user_settings();
85 let temp_dir = testutils::new_temp_dir();
86 let (canonical, uncanonical) = canonicalize(temp_dir.path());
87 let (workspace, repo) = Workspace::init_colocated_git(&settings, &uncanonical).unwrap();
88 let git_backend = repo
89 .store()
90 .backend_impl()
91 .downcast_ref::<GitBackend>()
92 .unwrap();
93 let repo_path = canonical.join(".jj").join("repo");
94 assert_eq!(workspace.workspace_root(), &canonical);
95 assert_eq!(git_backend.git_repo_path(), canonical.join(".git"));
96 assert_eq!(git_backend.git_workdir(), Some(canonical.as_ref()));
97 assert_eq!(
98 std::fs::read_to_string(repo_path.join("store").join("git_target")).unwrap(),
99 "../../../.git"
100 );
101
102 // Just test that we can write a commit to the store
103 let mut tx = repo.start_transaction();
104 write_random_commit(tx.repo_mut());
105}
106
107#[test]
108fn test_init_external_git() {
109 let settings = testutils::user_settings();
110 let temp_dir = testutils::new_temp_dir();
111 let (canonical, uncanonical) = canonicalize(temp_dir.path());
112 let git_repo_path = uncanonical.join("git");
113 git::init(&git_repo_path);
114 std::fs::create_dir(uncanonical.join("jj")).unwrap();
115 let (workspace, repo) = Workspace::init_external_git(
116 &settings,
117 &uncanonical.join("jj"),
118 &git_repo_path.join(".git"),
119 )
120 .unwrap();
121 let git_backend = repo
122 .store()
123 .backend_impl()
124 .downcast_ref::<GitBackend>()
125 .unwrap();
126 assert_eq!(workspace.workspace_root(), &canonical.join("jj"));
127 assert_eq!(
128 git_backend.git_repo_path(),
129 canonical.join("git").join(".git")
130 );
131 assert_eq!(
132 git_backend.git_workdir(),
133 Some(canonical.join("git").as_ref())
134 );
135
136 // Just test that we can write a commit to the store
137 let mut tx = repo.start_transaction();
138 write_random_commit(tx.repo_mut());
139}
140
141#[test_case(TestRepoBackend::Simple ; "simple backend")]
142#[test_case(TestRepoBackend::Git ; "git backend")]
143fn test_init_with_default_config(backend: TestRepoBackend) {
144 // Test that we can create a repo without setting any non-default config
145 let settings = UserSettings::from_config(StackedConfig::with_defaults()).unwrap();
146 let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings);
147 let repo = &test_workspace.repo;
148 let wc_commit_id = repo
149 .view()
150 .get_wc_commit_id(WorkspaceName::DEFAULT)
151 .unwrap();
152 let wc_commit = repo.store().get_commit(wc_commit_id).unwrap();
153 assert_eq!(wc_commit.author().name, "".to_string());
154 assert_eq!(wc_commit.author().email, "".to_string());
155 assert_eq!(wc_commit.committer().name, "".to_string());
156 assert_eq!(wc_commit.committer().email, "".to_string());
157}
158
159#[test_case(TestRepoBackend::Simple ; "simple backend")]
160#[test_case(TestRepoBackend::Git ; "git backend")]
161fn test_init_checkout(backend: TestRepoBackend) {
162 // Test the contents of the working-copy commit after init
163 let settings = testutils::user_settings();
164 let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings);
165 let repo = &test_workspace.repo;
166 let wc_commit_id = repo
167 .view()
168 .get_wc_commit_id(WorkspaceName::DEFAULT)
169 .unwrap();
170 let wc_commit = repo.store().get_commit(wc_commit_id).unwrap();
171 assert_eq!(*wc_commit.tree_id(), repo.store().empty_merged_tree_id());
172 assert_eq!(
173 wc_commit.store_commit().parents,
174 vec![repo.store().root_commit_id().clone()]
175 );
176 assert!(wc_commit.predecessors().next().is_none());
177 assert_eq!(wc_commit.description(), "");
178 assert_eq!(wc_commit.author().name, settings.user_name());
179 assert_eq!(wc_commit.author().email, settings.user_email());
180 assert_eq!(wc_commit.committer().name, settings.user_name());
181 assert_eq!(wc_commit.committer().email, settings.user_email());
182}