Debian Codegraph @

lib/tests/test_init.rs

raw
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
15 use std::path::Path;
16 use std::path::PathBuf;
17
18 use assert_matches::assert_matches;
19 use jj_lib::config::StackedConfig;
20 use jj_lib::git_backend::GitBackend;
21 use jj_lib::ref_name::WorkspaceName;
22 use jj_lib::repo::Repo as _;
23 use jj_lib::settings::UserSettings;
24 use jj_lib::workspace::Workspace;
25 use pollster::FutureExt as _;
26 use test_case::test_case;
27 use testutils::TestRepoBackend;
28 use testutils::TestResult;
29 use testutils::TestWorkspace;
30 use testutils::assert_tree_eq;
31 use testutils::git;
32 use testutils::write_random_commit;
33
34 fn canonicalize(input: &Path) -> (PathBuf, PathBuf) {
35 let uncanonical = input.join("..").join(input.file_name().unwrap());
36 let canonical = dunce::canonicalize(&uncanonical).unwrap();
37 (canonical, uncanonical)
38 }
39
40 #[test]
41 fn test_init_local() -> TestResult {
42 let settings = testutils::user_settings();
43 let temp_dir = testutils::new_temp_dir();
44 let (canonical, uncanonical) = canonicalize(temp_dir.path());
45 let (workspace, repo) = Workspace::init_simple(&settings, &uncanonical).block_on()?;
46 assert!(repo.store().backend_impl::<GitBackend>().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 Ok(())
53 }
54
55 #[test_case(gix::hash::Kind::Sha1 ; "sha1")]
56 #[test_case(gix::hash::Kind::Sha256; "sha256")]
57 fn test_init_internal_git(object_hash: gix::hash::Kind) -> TestResult {
58 let settings = testutils::user_settings();
59 let temp_dir = testutils::new_temp_dir();
60 let (canonical, uncanonical) = canonicalize(temp_dir.path());
61 let (workspace, repo) =
62 Workspace::init_internal_git(&settings, &uncanonical, object_hash).block_on()?;
63 let git_backend: &GitBackend = repo.store().backend_impl().unwrap();
64 let repo_path = canonical.join(".jj").join("repo");
65 assert_eq!(workspace.workspace_root(), &canonical);
66 assert_eq!(
67 git_backend.git_repo_path(),
68 canonical.join(PathBuf::from_iter([".jj", "repo", "store", "git"])),
69 );
70 assert!(git_backend.git_repo().workdir().is_none());
71 assert_eq!(
72 std::fs::read_to_string(repo_path.join("store").join("git_target"))?,
73 "git"
74 );
75
76 // Just test that we can write a commit to the store
77 let mut tx = repo.start_transaction();
78 write_random_commit(tx.repo_mut());
79 Ok(())
80 }
81
82 #[test_case(gix::hash::Kind::Sha1 ; "sha1")]
83 #[test_case(gix::hash::Kind::Sha256; "sha256")]
84 fn test_init_colocated_git(object_hash: gix::hash::Kind) -> TestResult {
85 let settings = testutils::user_settings();
86 let temp_dir = testutils::new_temp_dir();
87 let (canonical, uncanonical) = canonicalize(temp_dir.path());
88 let (workspace, repo) =
89 Workspace::init_colocated_git(&settings, &uncanonical, object_hash).block_on()?;
90 let git_backend: &GitBackend = repo.store().backend_impl().unwrap();
91 let repo_path = canonical.join(".jj").join("repo");
92 assert_eq!(workspace.workspace_root(), &canonical);
93 assert_eq!(git_backend.git_repo_path(), canonical.join(".git"));
94 assert_eq!(git_backend.git_repo().workdir(), Some(canonical.as_ref()));
95 assert_eq!(
96 std::fs::read_to_string(repo_path.join("store").join("git_target"))?,
97 "../../../.git"
98 );
99
100 // Just test that we can write a commit to the store
101 let mut tx = repo.start_transaction();
102 write_random_commit(tx.repo_mut());
103 Ok(())
104 }
105
106 #[test]
107 fn test_init_external_git() -> TestResult {
108 let settings = testutils::user_settings();
109 let temp_dir = testutils::new_temp_dir();
110 let (canonical, uncanonical) = canonicalize(temp_dir.path());
111 let git_repo_path = uncanonical.join("git");
112 git::init(&git_repo_path);
113 std::fs::create_dir(uncanonical.join("jj"))?;
114 let (workspace, repo) = Workspace::init_external_git(
115 &settings,
116 &uncanonical.join("jj"),
117 &git_repo_path.join(".git"),
118 )
119 .block_on()?;
120 let git_backend: &GitBackend = repo.store().backend_impl().unwrap();
121 assert_eq!(workspace.workspace_root(), &canonical.join("jj"));
122 assert_eq!(
123 git_backend.git_repo_path(),
124 canonical.join("git").join(".git")
125 );
126 assert_eq!(
127 git_backend.git_repo().workdir(),
128 Some(canonical.join("git").as_ref())
129 );
130
131 // Just test that we can write a commit to the store
132 let mut tx = repo.start_transaction();
133 write_random_commit(tx.repo_mut());
134 Ok(())
135 }
136
137 #[test_case(TestRepoBackend::Simple ; "simple backend")]
138 #[test_case(TestRepoBackend::Git ; "git backend")]
139 fn test_init_with_default_config(backend: TestRepoBackend) -> TestResult {
140 // Test that we can create a repo without setting any non-default config
141 let settings = UserSettings::from_config(StackedConfig::with_defaults())?;
142 let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings);
143 let repo = &test_workspace.repo;
144 let wc_commit_id = repo
145 .view()
146 .get_wc_commit_id(WorkspaceName::DEFAULT)
147 .unwrap();
148 let wc_commit = repo.store().get_commit(wc_commit_id)?;
149 assert_eq!(wc_commit.author().name, "".to_string());
150 assert_eq!(wc_commit.author().email, "".to_string());
151 assert_eq!(wc_commit.committer().name, "".to_string());
152 assert_eq!(wc_commit.committer().email, "".to_string());
153 Ok(())
154 }
155
156 #[test_case(TestRepoBackend::Simple ; "simple backend")]
157 #[test_case(TestRepoBackend::Git ; "git backend")]
158 fn test_init_checkout(backend: TestRepoBackend) -> TestResult {
159 // Test the contents of the working-copy commit after init
160 let settings = testutils::user_settings();
161 let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings);
162 let repo = &test_workspace.repo;
163 let wc_commit_id = repo
164 .view()
165 .get_wc_commit_id(WorkspaceName::DEFAULT)
166 .unwrap();
167 let wc_commit = repo.store().get_commit(wc_commit_id)?;
168 assert_tree_eq!(wc_commit.tree(), repo.store().empty_merged_tree());
169 assert_eq!(
170 wc_commit.store_commit().parents,
171 vec![repo.store().root_commit_id().clone()]
172 );
173 assert!(wc_commit.store_commit().predecessors.is_empty());
174 assert_eq!(wc_commit.description(), "");
175 assert_eq!(wc_commit.author().name, settings.user_name());
176 assert_eq!(wc_commit.author().email, settings.user_email());
177 assert_eq!(wc_commit.committer().name, settings.user_name());
178 assert_eq!(wc_commit.committer().email, settings.user_email());
179 assert_matches!(
180 repo.operation().predecessors_for_commit(wc_commit.id()),
181 Some([])
182 );
183 Ok(())
184 }
185
186 #[cfg(unix)]
187 #[test]
188 fn test_init_load_non_utf8_path() -> TestResult {
189 use std::ffi::OsStr;
190 use std::os::unix::ffi::OsStrExt as _;
191
192 use jj_lib::default_backend_factories::default_working_copy_factories;
193 use pollster::FutureExt as _;
194 use testutils::TestEnvironment;
195
196 let settings = testutils::user_settings();
197 let test_env = TestEnvironment::init();
198
199 if testutils::check_strict_utf8_fs(test_env.root()) {
200 eprintln!(
201 "Skipping test \"test_init_load_non_utf8_path\" due to strict UTF-8 filesystem for \
202 path {:?}",
203 test_env.root()
204 );
205 return Ok(());
206 }
207
208 let git_repo_path = test_env.root().join(OsStr::from_bytes(b"git\xe0"));
209 assert!(git_repo_path.to_str().is_none());
210 git::init(&git_repo_path);
211
212 // Workspace can be created
213 let workspace_root = test_env.root().join(OsStr::from_bytes(b"jj\xe0"));
214 std::fs::create_dir(&workspace_root)?;
215 Workspace::init_external_git(&settings, &workspace_root, &git_repo_path.join(".git"))
216 .block_on()?;
217
218 // Workspace can be loaded
219 let workspace = Workspace::load(
220 &settings,
221 &workspace_root,
222 &test_env.default_backend_factories(),
223 &default_working_copy_factories(),
224 )?;
225
226 // Just test that we can write a commit to the store
227 let repo = workspace.repo_loader().load_at_head().block_on()?;
228 let mut tx = repo.start_transaction();
229 write_random_commit(tx.repo_mut());
230 Ok(())
231 }