| 1 |
// Copyright 2021 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::thread; |
| 16 |
|
| 17 |
use assert_matches::assert_matches; |
| 18 |
use jj_lib::default_backend_factories::default_working_copy_factories; |
| 19 |
use jj_lib::default_backend_factories::default_working_copy_factory; |
| 20 |
use jj_lib::ref_name::WorkspaceNameBuf; |
| 21 |
use jj_lib::repo::Repo as _; |
| 22 |
use jj_lib::workspace::Workspace; |
| 23 |
use jj_lib::workspace::WorkspaceLoadError; |
| 24 |
use pollster::FutureExt as _; |
| 25 |
use testutils::TestEnvironment; |
| 26 |
use testutils::TestResult; |
| 27 |
use testutils::TestWorkspace; |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn test_load_bad_path() { |
| 31 |
let settings = testutils::user_settings(); |
| 32 |
let test_env = TestEnvironment::init(); |
| 33 |
let workspace_root = test_env.root().to_owned(); |
| 34 |
// We haven't created a repo in the workspace_root, so it should fail to load. |
| 35 |
let result = Workspace::load( |
| 36 |
&settings, |
| 37 |
&workspace_root, |
| 38 |
&test_env.default_backend_factories(), |
| 39 |
&default_working_copy_factories(), |
| 40 |
); |
| 41 |
assert_matches!( |
| 42 |
result.err(), |
| 43 |
Some(WorkspaceLoadError::NoWorkspaceHere(root)) if root == workspace_root |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn test_init_additional_workspace() -> TestResult { |
| 49 |
let settings = testutils::user_settings(); |
| 50 |
let test_workspace = TestWorkspace::init_with_settings(&settings); |
| 51 |
let workspace = &test_workspace.workspace; |
| 52 |
|
| 53 |
let ws2_name = WorkspaceNameBuf::from("ws2"); |
| 54 |
let ws2_root = test_workspace.root_dir().join("ws2_root"); |
| 55 |
std::fs::create_dir(&ws2_root)?; |
| 56 |
let (ws2, repo) = Workspace::init_workspace_with_existing_repo( |
| 57 |
&ws2_root, |
| 58 |
test_workspace.repo_path(), |
| 59 |
&test_workspace.repo, |
| 60 |
&*default_working_copy_factory(), |
| 61 |
ws2_name.clone(), |
| 62 |
) |
| 63 |
.block_on()?; |
| 64 |
let wc_commit_id = repo.view().get_wc_commit_id(&ws2_name); |
| 65 |
assert_ne!(wc_commit_id, None); |
| 66 |
let wc_commit_id = wc_commit_id.unwrap(); |
| 67 |
let wc_commit = repo.store().get_commit(wc_commit_id)?; |
| 68 |
assert_eq!( |
| 69 |
wc_commit.parent_ids(), |
| 70 |
vec![repo.store().root_commit_id().clone()] |
| 71 |
); |
| 72 |
assert_eq!(ws2.workspace_name(), &ws2_name); |
| 73 |
assert_eq!( |
| 74 |
*ws2.repo_path(), |
| 75 |
dunce::canonicalize(workspace.repo_path())? |
| 76 |
); |
| 77 |
assert_eq!(*ws2.workspace_root(), dunce::canonicalize(&ws2_root)?); |
| 78 |
|
| 79 |
let repo_file_path = ws2_root.join(".jj").join("repo"); |
| 80 |
let repo_file_contents = std::fs::read(&repo_file_path)?; |
| 81 |
let stored_path = String::from_utf8(repo_file_contents)?; |
| 82 |
assert_eq!(stored_path, "../../repo/.jj/repo"); |
| 83 |
|
| 84 |
let same_workspace = Workspace::load( |
| 85 |
&settings, |
| 86 |
&ws2_root, |
| 87 |
&test_workspace.env.default_backend_factories(), |
| 88 |
&default_working_copy_factories(), |
| 89 |
); |
| 90 |
assert!(same_workspace.is_ok()); |
| 91 |
let same_workspace = same_workspace?; |
| 92 |
assert_eq!(same_workspace.workspace_name(), &ws2_name); |
| 93 |
assert_eq!( |
| 94 |
*same_workspace.repo_path(), |
| 95 |
dunce::canonicalize(workspace.repo_path())? |
| 96 |
); |
| 97 |
assert_eq!(same_workspace.workspace_root(), ws2.workspace_root()); |
| 98 |
Ok(()) |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn test_init_additional_workspace_absolute_path_compat() -> TestResult { |
| 103 |
let settings = testutils::user_settings(); |
| 104 |
let test_workspace = TestWorkspace::init_with_settings(&settings); |
| 105 |
let workspace = &test_workspace.workspace; |
| 106 |
|
| 107 |
let ws2_name = WorkspaceNameBuf::from("ws2"); |
| 108 |
let ws2_root = test_workspace.root_dir().join("ws2_root"); |
| 109 |
std::fs::create_dir(&ws2_root)?; |
| 110 |
let (ws2, _repo) = Workspace::init_workspace_with_existing_repo( |
| 111 |
&ws2_root, |
| 112 |
test_workspace.repo_path(), |
| 113 |
&test_workspace.repo, |
| 114 |
&*default_working_copy_factory(), |
| 115 |
ws2_name.clone(), |
| 116 |
) |
| 117 |
.block_on()?; |
| 118 |
|
| 119 |
let repo_file_path = ws2_root.join(".jj").join("repo"); |
| 120 |
let abs_repo_path = dunce::canonicalize(workspace.repo_path())?; |
| 121 |
std::fs::write( |
| 122 |
&repo_file_path, |
| 123 |
abs_repo_path.as_os_str().as_encoded_bytes(), |
| 124 |
)?; |
| 125 |
|
| 126 |
let same_workspace = Workspace::load( |
| 127 |
&settings, |
| 128 |
&ws2_root, |
| 129 |
&test_workspace.env.default_backend_factories(), |
| 130 |
&default_working_copy_factories(), |
| 131 |
); |
| 132 |
assert!(same_workspace.is_ok()); |
| 133 |
let same_workspace = same_workspace?; |
| 134 |
assert_eq!(same_workspace.workspace_name(), &ws2_name); |
| 135 |
assert_eq!(*same_workspace.repo_path(), abs_repo_path); |
| 136 |
assert_eq!(same_workspace.workspace_root(), ws2.workspace_root()); |
| 137 |
Ok(()) |
| 138 |
} |
| 139 |
|
| 140 |
#[cfg(unix)] |
| 141 |
#[test] |
| 142 |
fn test_init_additional_workspace_non_utf8_path() -> TestResult { |
| 143 |
use std::ffi::OsStr; |
| 144 |
use std::os::unix::ffi::OsStrExt as _; |
| 145 |
|
| 146 |
let settings = testutils::user_settings(); |
| 147 |
let test_env = TestEnvironment::init(); |
| 148 |
|
| 149 |
if testutils::check_strict_utf8_fs(test_env.root()) { |
| 150 |
eprintln!( |
| 151 |
"Skipping test \"test_init_additional_workspace_non_utf8_path\" due to strict UTF-8 \ |
| 152 |
filesystem for path {:?}", |
| 153 |
test_env.root() |
| 154 |
); |
| 155 |
return Ok(()); |
| 156 |
} |
| 157 |
|
| 158 |
let ws1_root = test_env.root().join(OsStr::from_bytes(b"ws1_root\xe0")); |
| 159 |
std::fs::create_dir(&ws1_root)?; |
| 160 |
let (ws1, repo) = Workspace::init_simple(&settings, &ws1_root).block_on()?; |
| 161 |
|
| 162 |
let ws2_name = WorkspaceNameBuf::from("ws2"); |
| 163 |
let ws2_root = test_env.root().join(OsStr::from_bytes(b"ws2_root\xe0")); |
| 164 |
std::fs::create_dir(&ws2_root)?; |
| 165 |
let (ws2, _repo) = Workspace::init_workspace_with_existing_repo( |
| 166 |
&ws2_root, |
| 167 |
ws1.repo_path(), |
| 168 |
&repo, |
| 169 |
&*default_working_copy_factory(), |
| 170 |
ws2_name.clone(), |
| 171 |
) |
| 172 |
.block_on()?; |
| 173 |
assert_eq!(ws2.workspace_name(), &ws2_name); |
| 174 |
assert_eq!(*ws2.repo_path(), dunce::canonicalize(ws1.repo_path())?); |
| 175 |
assert_eq!(*ws2.workspace_root(), dunce::canonicalize(&ws2_root)?); |
| 176 |
let same_workspace = Workspace::load( |
| 177 |
&settings, |
| 178 |
&ws2_root, |
| 179 |
&test_env.default_backend_factories(), |
| 180 |
&default_working_copy_factories(), |
| 181 |
); |
| 182 |
let same_workspace = same_workspace?; |
| 183 |
assert_eq!(same_workspace.workspace_name(), &ws2_name); |
| 184 |
assert_eq!( |
| 185 |
*same_workspace.repo_path(), |
| 186 |
dunce::canonicalize(ws1.repo_path())? |
| 187 |
); |
| 188 |
assert_eq!(same_workspace.workspace_root(), ws2.workspace_root()); |
| 189 |
Ok(()) |
| 190 |
} |
| 191 |
|
| 192 |
/// Test cross-thread access to a workspace, which requires it to be Send |
| 193 |
#[test] |
| 194 |
fn test_sendable() { |
| 195 |
let test_workspace = TestWorkspace::init(); |
| 196 |
let root = test_workspace.workspace.workspace_root().to_owned(); |
| 197 |
|
| 198 |
thread::spawn(move || { |
| 199 |
let shared_workspace = test_workspace.workspace; |
| 200 |
assert_eq!(shared_workspace.workspace_root(), &root); |
| 201 |
}) |
| 202 |
.join() |
| 203 |
.unwrap(); |
| 204 |
} |