| 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 std::fs::File; |
| 16 |
use std::io::Write as _; |
| 17 |
|
| 18 |
use bstr::ByteSlice as _; |
| 19 |
use jj_lib::config::ConfigLayer; |
| 20 |
use jj_lib::config::ConfigSource; |
| 21 |
use jj_lib::default_backend_factories::default_backend_factories; |
| 22 |
use jj_lib::default_backend_factories::default_working_copy_factories; |
| 23 |
use jj_lib::repo::Repo as _; |
| 24 |
use jj_lib::rewrite::merge_commit_trees; |
| 25 |
use jj_lib::settings::UserSettings; |
| 26 |
use jj_lib::workspace::Workspace; |
| 27 |
use pollster::FutureExt as _; |
| 28 |
use test_case::test_case; |
| 29 |
use testutils::CommitBuilderExt as _; |
| 30 |
use testutils::TestRepoBackend; |
| 31 |
use testutils::TestResult; |
| 32 |
use testutils::TestWorkspace; |
| 33 |
use testutils::assert_tree_eq; |
| 34 |
use testutils::base_user_config; |
| 35 |
use testutils::commit_with_tree; |
| 36 |
use testutils::repo_path; |
| 37 |
|
| 38 |
static LF_FILE_CONTENT: &[u8] = b"aaa\nbbbb\nccccc\n"; |
| 39 |
static CRLF_FILE_CONTENT: &[u8] = b"aaa\r\nbbbb\r\nccccc\r\n"; |
| 40 |
static MIXED_EOL_FILE_CONTENT: &[u8] = b"aaa\nbbbb\r\nccccc\n"; |
| 41 |
static BINARY_FILE_CONTENT: &[u8] = b"\0"; |
| 42 |
|
| 43 |
struct Config { |
| 44 |
extra_setting: &'static str, |
| 45 |
file_content: &'static [u8], |
| 46 |
} |
| 47 |
|
| 48 |
fn base_user_settings_with_extra_configs(extra_settings: &str) -> UserSettings { |
| 49 |
let mut config = base_user_config(); |
| 50 |
config.add_layer( |
| 51 |
ConfigLayer::parse(ConfigSource::User, extra_settings) |
| 52 |
.expect("Failed to parse the settings"), |
| 53 |
); |
| 54 |
UserSettings::from_config(config).expect("Failed to create the UserSettings from the config") |
| 55 |
} |
| 56 |
|
| 57 |
#[test_case(Config { |
| 58 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 59 |
file_content: LF_FILE_CONTENT, |
| 60 |
} => LF_FILE_CONTENT; "eol-conversion input-output LF only file")] |
| 61 |
#[test_case(Config { |
| 62 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 63 |
file_content: CRLF_FILE_CONTENT, |
| 64 |
} => LF_FILE_CONTENT; "eol-conversion input-output CRLF only file")] |
| 65 |
#[test_case(Config { |
| 66 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 67 |
file_content: MIXED_EOL_FILE_CONTENT, |
| 68 |
} => LF_FILE_CONTENT; "eol-conversion input-output mixed EOL file")] |
| 69 |
#[test_case(Config { |
| 70 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 71 |
file_content: BINARY_FILE_CONTENT, |
| 72 |
} => BINARY_FILE_CONTENT; "eol-conversion input-output binary file")] |
| 73 |
#[test_case(Config { |
| 74 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 75 |
file_content: LF_FILE_CONTENT, |
| 76 |
} => LF_FILE_CONTENT; "eol-conversion input LF only file")] |
| 77 |
#[test_case(Config { |
| 78 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 79 |
file_content: CRLF_FILE_CONTENT, |
| 80 |
} => LF_FILE_CONTENT; "eol-conversion input CRLF only file")] |
| 81 |
#[test_case(Config { |
| 82 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 83 |
file_content: MIXED_EOL_FILE_CONTENT, |
| 84 |
} => LF_FILE_CONTENT; "eol-conversion input mixed EOL file")] |
| 85 |
#[test_case(Config { |
| 86 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 87 |
file_content: BINARY_FILE_CONTENT, |
| 88 |
} => BINARY_FILE_CONTENT; "eol-conversion input binary file")] |
| 89 |
#[test_case(Config { |
| 90 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 91 |
file_content: LF_FILE_CONTENT, |
| 92 |
} => LF_FILE_CONTENT; "eol-conversion none LF only file")] |
| 93 |
#[test_case(Config { |
| 94 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 95 |
file_content: CRLF_FILE_CONTENT, |
| 96 |
} => CRLF_FILE_CONTENT; "eol-conversion none CRLF only file")] |
| 97 |
#[test_case(Config { |
| 98 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 99 |
file_content: MIXED_EOL_FILE_CONTENT, |
| 100 |
} => MIXED_EOL_FILE_CONTENT; "eol-conversion none mixed EOL file")] |
| 101 |
#[test_case(Config { |
| 102 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 103 |
file_content: BINARY_FILE_CONTENT, |
| 104 |
} => BINARY_FILE_CONTENT; "eol-conversion none binary file")] |
| 105 |
fn test_eol_conversion_snapshot( |
| 106 |
Config { |
| 107 |
extra_setting, |
| 108 |
file_content, |
| 109 |
}: Config, |
| 110 |
) -> Vec<u8> { |
| 111 |
// This test creates snapshots with different working-copy.eol-conversion |
| 112 |
// configurations, where proper EOL conversion should apply before writing files |
| 113 |
// back to the store. Then files are checked out with |
| 114 |
// working-copy.eol-conversion = "none", which won't touch the EOLs, so that we |
| 115 |
// can tell whether the exact EOLs written to the store are expected. |
| 116 |
|
| 117 |
let extra_setting = format!("{extra_setting}\n"); |
| 118 |
let user_settings = base_user_settings_with_extra_configs(&extra_setting); |
| 119 |
let mut test_workspace = |
| 120 |
TestWorkspace::init_with_backend_and_settings(TestRepoBackend::Git, &user_settings); |
| 121 |
let file_repo_path = repo_path("test-eol-file"); |
| 122 |
let file_disk_path = file_repo_path |
| 123 |
.to_fs_path(test_workspace.workspace.workspace_root()) |
| 124 |
.unwrap(); |
| 125 |
|
| 126 |
testutils::write_working_copy_file( |
| 127 |
test_workspace.workspace.workspace_root(), |
| 128 |
file_repo_path, |
| 129 |
file_content, |
| 130 |
); |
| 131 |
let tree = test_workspace.snapshot().unwrap(); |
| 132 |
let new_tree = test_workspace.snapshot().unwrap(); |
| 133 |
assert_tree_eq!(new_tree, tree, "The working copy should be clean."); |
| 134 |
let file_added_commit = commit_with_tree(test_workspace.repo.store(), tree); |
| 135 |
|
| 136 |
// Create a commit with the file removed, so that later when we checkout the |
| 137 |
// file_added_commit, the test file is recreated. |
| 138 |
std::fs::remove_file(&file_disk_path).unwrap(); |
| 139 |
let tree = test_workspace.snapshot().unwrap(); |
| 140 |
let file_removed_commit = commit_with_tree(test_workspace.repo.store(), tree); |
| 141 |
let workspace = &mut test_workspace.workspace; |
| 142 |
workspace |
| 143 |
.check_out( |
| 144 |
test_workspace.repo.op_id().clone(), |
| 145 |
None, |
| 146 |
&file_removed_commit, |
| 147 |
) |
| 148 |
.block_on() |
| 149 |
.unwrap(); |
| 150 |
assert!(!file_disk_path.exists()); |
| 151 |
|
| 152 |
let user_settings = |
| 153 |
base_user_settings_with_extra_configs("working-copy.eol-conversion = \"none\"\n"); |
| 154 |
// Reload the workspace with the new working-copy.eol-conversion = "none" |
| 155 |
// setting to verify the EOL of files previously written to the store. |
| 156 |
let mut workspace = Workspace::load( |
| 157 |
&user_settings, |
| 158 |
test_workspace.workspace.workspace_root(), |
| 159 |
&default_backend_factories(), |
| 160 |
&default_working_copy_factories(), |
| 161 |
) |
| 162 |
.expect("Failed to reload the workspace"); |
| 163 |
// We have to query the Commit again. The Workspace is backed by a different |
| 164 |
// Store from the original Commit. |
| 165 |
let file_added_commit = workspace |
| 166 |
.repo_loader() |
| 167 |
.store() |
| 168 |
.get_commit(file_added_commit.id()) |
| 169 |
.expect("Failed to find the commit with the test file"); |
| 170 |
workspace |
| 171 |
.check_out( |
| 172 |
test_workspace.repo.op_id().clone(), |
| 173 |
None, |
| 174 |
&file_added_commit, |
| 175 |
) |
| 176 |
.block_on() |
| 177 |
.unwrap(); |
| 178 |
assert!(file_disk_path.exists()); |
| 179 |
let new_tree = test_workspace.snapshot().unwrap(); |
| 180 |
assert_tree_eq!( |
| 181 |
new_tree, |
| 182 |
file_added_commit.tree(), |
| 183 |
"The working copy should be clean." |
| 184 |
); |
| 185 |
|
| 186 |
std::fs::read(&file_disk_path).expect("Failed to read the checked out test file") |
| 187 |
} |
| 188 |
|
| 189 |
// Create a conflict commit in a CRLF EOL file, and append another line with the |
| 190 |
// CRLF EOL to the file, create a snapshot on the modified merge conflict, |
| 191 |
// checkout the snapshot with the given setting, and return the content of the |
| 192 |
// file. |
| 193 |
fn create_conflict_snapshot_and_read(extra_setting: &str) -> Vec<u8> { |
| 194 |
// Use the working-copy.eol-conversion = "none" setting to write files to the |
| 195 |
// store as is. |
| 196 |
let no_eol_conversion_settings = |
| 197 |
base_user_settings_with_extra_configs("working-copy.eol-conversion = \"none\"\n"); |
| 198 |
let mut test_workspace = TestWorkspace::init_with_backend_and_settings( |
| 199 |
TestRepoBackend::Git, |
| 200 |
&no_eol_conversion_settings, |
| 201 |
); |
| 202 |
let file_repo_path = repo_path("test-eol-file"); |
| 203 |
let file_disk_path = file_repo_path |
| 204 |
.to_fs_path(test_workspace.workspace.workspace_root()) |
| 205 |
.unwrap(); |
| 206 |
|
| 207 |
// The commit graph: |
| 208 |
// C (conflict) |
| 209 |
// |\ |
| 210 |
// A B |
| 211 |
// |/ |
| 212 |
// (empty) |
| 213 |
let root_commit = test_workspace.repo.store().root_commit(); |
| 214 |
testutils::write_working_copy_file( |
| 215 |
test_workspace.workspace.workspace_root(), |
| 216 |
file_repo_path, |
| 217 |
"a\r\n", |
| 218 |
); |
| 219 |
let tree = test_workspace.snapshot().unwrap(); |
| 220 |
let mut tx = test_workspace.repo.start_transaction(); |
| 221 |
let parent1_commit = tx |
| 222 |
.repo_mut() |
| 223 |
.new_commit(vec![root_commit.id().clone()], tree) |
| 224 |
.write_unwrap(); |
| 225 |
tx.commit("commit parent1").block_on().unwrap(); |
| 226 |
|
| 227 |
test_workspace |
| 228 |
.workspace |
| 229 |
.check_out(test_workspace.repo.op_id().clone(), None, &root_commit) |
| 230 |
.block_on() |
| 231 |
.unwrap(); |
| 232 |
testutils::write_working_copy_file( |
| 233 |
test_workspace.workspace.workspace_root(), |
| 234 |
file_repo_path, |
| 235 |
"b\r\n", |
| 236 |
); |
| 237 |
let tree = test_workspace.snapshot().unwrap(); |
| 238 |
let mut tx = test_workspace.repo.start_transaction(); |
| 239 |
let parent2_commit = tx |
| 240 |
.repo_mut() |
| 241 |
.new_commit(vec![root_commit.id().clone()], tree) |
| 242 |
.write_unwrap(); |
| 243 |
tx.commit("commit parent2").block_on().unwrap(); |
| 244 |
|
| 245 |
// Reload the repo to pick up the new commits. |
| 246 |
test_workspace.repo = test_workspace.repo.reload_at_head().block_on().unwrap(); |
| 247 |
// Create the merge commit. |
| 248 |
let tree = merge_commit_trees(&*test_workspace.repo, &[parent1_commit, parent2_commit]) |
| 249 |
.block_on() |
| 250 |
.unwrap(); |
| 251 |
let merge_commit = commit_with_tree(test_workspace.repo.store(), tree); |
| 252 |
// Append new texts to the file with conflicts to make sure the last line is not |
| 253 |
// conflict markers. |
| 254 |
test_workspace |
| 255 |
.workspace |
| 256 |
.check_out(test_workspace.repo.op_id().clone(), None, &merge_commit) |
| 257 |
.block_on() |
| 258 |
.unwrap(); |
| 259 |
let mut file = File::options().append(true).open(&file_disk_path).unwrap(); |
| 260 |
file.write_all(b"c\r\n").unwrap(); |
| 261 |
drop(file); |
| 262 |
|
| 263 |
let extra_setting = format!("{extra_setting}\n"); |
| 264 |
let user_settings = base_user_settings_with_extra_configs(&extra_setting); |
| 265 |
// Reload the Workspace to apply the settings under testing. |
| 266 |
test_workspace.workspace = Workspace::load( |
| 267 |
&user_settings, |
| 268 |
test_workspace.workspace.workspace_root(), |
| 269 |
&default_backend_factories(), |
| 270 |
&default_working_copy_factories(), |
| 271 |
) |
| 272 |
.expect("Failed to reload the workspace"); |
| 273 |
let tree = test_workspace.snapshot().unwrap(); |
| 274 |
let new_tree = test_workspace.snapshot().unwrap(); |
| 275 |
assert_tree_eq!(new_tree, tree, "The working copy should be clean."); |
| 276 |
// Create the new merge commit with the conflict file appended. |
| 277 |
let merge_commit = commit_with_tree(test_workspace.repo.store(), tree); |
| 278 |
|
| 279 |
// Reload the Workspace with the working-copy.eol-conversion = "none" setting to |
| 280 |
// check the EOL of the file written to the store previously. |
| 281 |
test_workspace.workspace = Workspace::load( |
| 282 |
&no_eol_conversion_settings, |
| 283 |
test_workspace.workspace.workspace_root(), |
| 284 |
&default_backend_factories(), |
| 285 |
&default_working_copy_factories(), |
| 286 |
) |
| 287 |
.expect("Failed to reload the workspace"); |
| 288 |
// Checkout the empty commit to clear the directory, so that the test file will |
| 289 |
// be recreated. |
| 290 |
test_workspace |
| 291 |
.workspace |
| 292 |
.check_out( |
| 293 |
test_workspace.repo.op_id().clone(), |
| 294 |
None, |
| 295 |
&test_workspace.workspace.repo_loader().store().root_commit(), |
| 296 |
) |
| 297 |
.block_on() |
| 298 |
.unwrap(); |
| 299 |
// We have to query the Commit again. The Workspace is backed by a different |
| 300 |
// Store from the original Commit. |
| 301 |
let merge_commit = test_workspace |
| 302 |
.workspace |
| 303 |
.repo_loader() |
| 304 |
.store() |
| 305 |
.get_commit(merge_commit.id()) |
| 306 |
.expect("Failed to find the commit with the test file"); |
| 307 |
test_workspace |
| 308 |
.workspace |
| 309 |
.check_out(test_workspace.repo.op_id().clone(), None, &merge_commit) |
| 310 |
.block_on() |
| 311 |
.unwrap(); |
| 312 |
|
| 313 |
assert!(std::fs::exists(&file_disk_path).unwrap()); |
| 314 |
std::fs::read(&file_disk_path).unwrap() |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn test_eol_conversion_input_output_snapshot_conflicts() -> TestResult { |
| 319 |
let contents = |
| 320 |
create_conflict_snapshot_and_read(r#"working-copy.eol-conversion = "input-output""#); |
| 321 |
for line in contents.lines_with_terminator() { |
| 322 |
assert!( |
| 323 |
!line.ends_with(b"\r\n"), |
| 324 |
"{:?} should not end with CRLF", |
| 325 |
line.to_str_lossy().as_ref() |
| 326 |
); |
| 327 |
} |
| 328 |
Ok(()) |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn test_eol_conversion_input_snapshot_conflicts() -> TestResult { |
| 333 |
let contents = create_conflict_snapshot_and_read(r#"working-copy.eol-conversion = "input""#); |
| 334 |
for line in contents.lines_with_terminator() { |
| 335 |
assert!( |
| 336 |
!line.ends_with(b"\r\n"), |
| 337 |
"{:?} should not end with CRLF", |
| 338 |
line.to_str_lossy().as_ref() |
| 339 |
); |
| 340 |
} |
| 341 |
Ok(()) |
| 342 |
} |
| 343 |
|
| 344 |
#[test] |
| 345 |
fn test_eol_conversion_none_snapshot_conflicts() -> TestResult { |
| 346 |
let contents = create_conflict_snapshot_and_read(r#"working-copy.eol-conversion = "none""#); |
| 347 |
// We only check the last line, because it is only guaranteed that the last line |
| 348 |
// is not the conflict markers. The conflict markers in the store are supposed |
| 349 |
// to use the LF EOL. |
| 350 |
let line = contents.lines_with_terminator().next_back().unwrap(); |
| 351 |
assert!( |
| 352 |
line.ends_with(b"\r\n"), |
| 353 |
"{:?} should end with CRLF", |
| 354 |
line.to_str_lossy().as_ref() |
| 355 |
); |
| 356 |
Ok(()) |
| 357 |
} |
| 358 |
|
| 359 |
struct UpdateConflictsTestConfig { |
| 360 |
parent1_contents: &'static str, |
| 361 |
parent2_contents: &'static str, |
| 362 |
extra_setting: &'static str, |
| 363 |
|
| 364 |
expected_eol: &'static str, |
| 365 |
expected_conflict_side1: &'static str, |
| 366 |
expected_conflict_side2: &'static str, |
| 367 |
} |
| 368 |
|
| 369 |
#[test_case(UpdateConflictsTestConfig { |
| 370 |
parent1_contents: "a\n", |
| 371 |
parent2_contents: "b\n", |
| 372 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 373 |
expected_eol: "\n", |
| 374 |
expected_conflict_side1: "a\n", |
| 375 |
expected_conflict_side2: "b\n", |
| 376 |
}; "LF parents with none settings")] |
| 377 |
#[test_case(UpdateConflictsTestConfig { |
| 378 |
parent1_contents: "a\n", |
| 379 |
parent2_contents: "b\n", |
| 380 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 381 |
expected_eol: "\r\n", |
| 382 |
expected_conflict_side1: "a\r\n", |
| 383 |
expected_conflict_side2: "b\r\n", |
| 384 |
}; "LF parents with input-output settings")] |
| 385 |
#[test_case(UpdateConflictsTestConfig { |
| 386 |
parent1_contents: "a\r\n", |
| 387 |
parent2_contents: "b\r\n", |
| 388 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 389 |
expected_eol: "\r\n", |
| 390 |
expected_conflict_side1: "a\r\n", |
| 391 |
expected_conflict_side2: "b\r\n", |
| 392 |
}; "CRLF parents with input-output settings")] |
| 393 |
fn test_eol_conversion_update_conflicts( |
| 394 |
UpdateConflictsTestConfig { |
| 395 |
parent1_contents, |
| 396 |
parent2_contents, |
| 397 |
extra_setting, |
| 398 |
expected_eol, |
| 399 |
expected_conflict_side1, |
| 400 |
expected_conflict_side2, |
| 401 |
}: UpdateConflictsTestConfig, |
| 402 |
) -> TestResult { |
| 403 |
// Create a conflict commit with 2 given contents on one file, checkout that |
| 404 |
// conflict with the given EOL conversion settings, and test if the EOL matches. |
| 405 |
|
| 406 |
let extra_setting = format!("{extra_setting}\n"); |
| 407 |
let user_settings = base_user_settings_with_extra_configs(&extra_setting); |
| 408 |
let mut test_workspace = |
| 409 |
TestWorkspace::init_with_backend_and_settings(TestRepoBackend::Git, &user_settings); |
| 410 |
let file_repo_path = repo_path("test-eol-file"); |
| 411 |
let file_disk_path = file_repo_path.to_fs_path(test_workspace.workspace.workspace_root())?; |
| 412 |
|
| 413 |
// The commit graph: |
| 414 |
// C (conflict) |
| 415 |
// |\ |
| 416 |
// A B |
| 417 |
// |/ |
| 418 |
// (empty) |
| 419 |
let root_commit = test_workspace.repo.store().root_commit(); |
| 420 |
let mut tx = test_workspace.repo.start_transaction(); |
| 421 |
let tree = testutils::create_tree(&test_workspace.repo, &[(file_repo_path, parent1_contents)]); |
| 422 |
let parent1_commit = tx |
| 423 |
.repo_mut() |
| 424 |
.new_commit(vec![root_commit.id().clone()], tree) |
| 425 |
.write_unwrap(); |
| 426 |
let tree = testutils::create_tree(&test_workspace.repo, &[(file_repo_path, parent2_contents)]); |
| 427 |
let parent2_commit = tx |
| 428 |
.repo_mut() |
| 429 |
.new_commit(vec![root_commit.id().clone()], tree) |
| 430 |
.write_unwrap(); |
| 431 |
tx.commit("commit parent 2").block_on()?; |
| 432 |
|
| 433 |
// Reload the repo to pick up the new commits. |
| 434 |
test_workspace.repo = test_workspace.repo.reload_at_head().block_on()?; |
| 435 |
// Create the merge commit. |
| 436 |
let tree = |
| 437 |
merge_commit_trees(&*test_workspace.repo, &[parent1_commit, parent2_commit]).block_on()?; |
| 438 |
let merge_commit = commit_with_tree(test_workspace.repo.store(), tree); |
| 439 |
|
| 440 |
// Checkout the merge commit. |
| 441 |
test_workspace |
| 442 |
.workspace |
| 443 |
.check_out(test_workspace.repo.op_id().clone(), None, &merge_commit) |
| 444 |
.block_on()?; |
| 445 |
let contents = std::fs::read(&file_disk_path)?; |
| 446 |
for line in contents.lines_with_terminator() { |
| 447 |
assert!( |
| 448 |
line.ends_with_str(expected_eol), |
| 449 |
"{:?} should end with {:?}", |
| 450 |
&*line.to_str_lossy(), |
| 451 |
expected_eol |
| 452 |
); |
| 453 |
} |
| 454 |
let hunks = |
| 455 |
jj_lib::conflicts::parse_conflict(&contents, 2, jj_lib::conflicts::MIN_CONFLICT_MARKER_LEN) |
| 456 |
.unwrap(); |
| 457 |
let hunk = &hunks[0]; |
| 458 |
assert!(!hunk.is_resolved()); |
| 459 |
let sides = hunk.iter().collect::<Vec<_>>(); |
| 460 |
assert_eq!(sides[0], expected_conflict_side1); |
| 461 |
assert_eq!(sides[2], expected_conflict_side2); |
| 462 |
Ok(()) |
| 463 |
} |
| 464 |
|
| 465 |
#[test_case(Config { |
| 466 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 467 |
file_content: LF_FILE_CONTENT, |
| 468 |
} => CRLF_FILE_CONTENT; "eol-conversion input-output LF only file")] |
| 469 |
#[test_case(Config { |
| 470 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 471 |
file_content: CRLF_FILE_CONTENT, |
| 472 |
} => CRLF_FILE_CONTENT; "eol-conversion input-output CRLF only file")] |
| 473 |
#[test_case(Config { |
| 474 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 475 |
file_content: MIXED_EOL_FILE_CONTENT, |
| 476 |
} => CRLF_FILE_CONTENT; "eol-conversion input-output mixed EOL file")] |
| 477 |
#[test_case(Config { |
| 478 |
extra_setting: r#"working-copy.eol-conversion = "input-output""#, |
| 479 |
file_content: BINARY_FILE_CONTENT, |
| 480 |
} => BINARY_FILE_CONTENT; "eol-conversion input-output binary file")] |
| 481 |
#[test_case(Config { |
| 482 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 483 |
file_content: LF_FILE_CONTENT, |
| 484 |
} => LF_FILE_CONTENT; "eol-conversion input LF only file")] |
| 485 |
#[test_case(Config { |
| 486 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 487 |
file_content: CRLF_FILE_CONTENT, |
| 488 |
} => CRLF_FILE_CONTENT; "eol-conversion input CRLF only file")] |
| 489 |
#[test_case(Config { |
| 490 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 491 |
file_content: MIXED_EOL_FILE_CONTENT, |
| 492 |
} => MIXED_EOL_FILE_CONTENT; "eol-conversion input mixed EOL file")] |
| 493 |
#[test_case(Config { |
| 494 |
extra_setting: r#"working-copy.eol-conversion = "input""#, |
| 495 |
file_content: BINARY_FILE_CONTENT, |
| 496 |
} => BINARY_FILE_CONTENT; "eol-conversion input binary file")] |
| 497 |
#[test_case(Config { |
| 498 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 499 |
file_content: LF_FILE_CONTENT, |
| 500 |
} => LF_FILE_CONTENT; "eol-conversion none LF only file")] |
| 501 |
#[test_case(Config { |
| 502 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 503 |
file_content: CRLF_FILE_CONTENT, |
| 504 |
} => CRLF_FILE_CONTENT; "eol-conversion none CRLF only file")] |
| 505 |
#[test_case(Config { |
| 506 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 507 |
file_content: MIXED_EOL_FILE_CONTENT, |
| 508 |
} => MIXED_EOL_FILE_CONTENT; "eol-conversion none mixed EOL file")] |
| 509 |
#[test_case(Config { |
| 510 |
extra_setting: r#"working-copy.eol-conversion = "none""#, |
| 511 |
file_content: BINARY_FILE_CONTENT, |
| 512 |
} => BINARY_FILE_CONTENT; "eol-conversion none binary file")] |
| 513 |
fn test_eol_conversion_checkout( |
| 514 |
Config { |
| 515 |
extra_setting, |
| 516 |
file_content, |
| 517 |
}: Config, |
| 518 |
) -> Vec<u8> { |
| 519 |
// This test checks in files with working-copy.eol-conversion = "none", so that |
| 520 |
// the store stores files as is. Then we use jj to check out those files with |
| 521 |
// different working-copy.eol-conversion configurations to verify if the EOLs |
| 522 |
// are converted as expected. |
| 523 |
|
| 524 |
let no_eol_conversion_settings = |
| 525 |
base_user_settings_with_extra_configs("working-copy.eol-conversion = \"none\"\n"); |
| 526 |
// Use the working-copy.eol-conversion = "none" setting, so that the input files |
| 527 |
// are stored as is. |
| 528 |
let mut test_workspace = TestWorkspace::init_with_backend_and_settings( |
| 529 |
TestRepoBackend::Git, |
| 530 |
&no_eol_conversion_settings, |
| 531 |
); |
| 532 |
let file_repo_path = repo_path("test-eol-file"); |
| 533 |
let file_disk_path = file_repo_path |
| 534 |
.to_fs_path(test_workspace.workspace.workspace_root()) |
| 535 |
.unwrap(); |
| 536 |
testutils::write_working_copy_file( |
| 537 |
test_workspace.workspace.workspace_root(), |
| 538 |
file_repo_path, |
| 539 |
file_content, |
| 540 |
); |
| 541 |
let tree = test_workspace.snapshot().unwrap(); |
| 542 |
let commit = commit_with_tree(test_workspace.repo.store(), tree); |
| 543 |
|
| 544 |
// Checkout the empty commit to clear the directory, so that later when we |
| 545 |
// checkout, files are recreated. |
| 546 |
test_workspace |
| 547 |
.workspace |
| 548 |
.check_out( |
| 549 |
test_workspace.repo.op_id().clone(), |
| 550 |
None, |
| 551 |
&test_workspace.workspace.repo_loader().store().root_commit(), |
| 552 |
) |
| 553 |
.block_on() |
| 554 |
.unwrap(); |
| 555 |
assert!(!std::fs::exists(&file_disk_path).unwrap()); |
| 556 |
|
| 557 |
let extra_setting = format!("{extra_setting}\n"); |
| 558 |
let user_settings = base_user_settings_with_extra_configs(&extra_setting); |
| 559 |
// Change the working-copy.eol-conversion setting to the configuration under |
| 560 |
// testing. |
| 561 |
test_workspace.workspace = Workspace::load( |
| 562 |
&user_settings, |
| 563 |
test_workspace.workspace.workspace_root(), |
| 564 |
&default_backend_factories(), |
| 565 |
&default_working_copy_factories(), |
| 566 |
) |
| 567 |
.expect("Failed to reload the workspace"); |
| 568 |
// We have to query the Commit again. The Workspace is backed by a different |
| 569 |
// Store from the original Commit. |
| 570 |
let commit = test_workspace |
| 571 |
.workspace |
| 572 |
.repo_loader() |
| 573 |
.store() |
| 574 |
.get_commit(commit.id()) |
| 575 |
.expect("Failed to find the commit with the test file"); |
| 576 |
// Check out the commit with the test file. TreeState::update should update the |
| 577 |
// EOL accordingly. |
| 578 |
test_workspace |
| 579 |
.workspace |
| 580 |
.check_out(test_workspace.repo.op_id().clone(), None, &commit) |
| 581 |
.block_on() |
| 582 |
.unwrap(); |
| 583 |
|
| 584 |
// When we take a snapshot now, the tree may not be clean, because the EOL our |
| 585 |
// snapshot creates may not align with what is currently used in store. e.g. |
| 586 |
// with working-copy.eol-conversion = "input-output", the test-eol-file may have |
| 587 |
// CRLF line endings in the store, but the snapshot will change the EOL to LF, |
| 588 |
// hence the diff. |
| 589 |
|
| 590 |
assert!(std::fs::exists(&file_disk_path).unwrap()); |
| 591 |
std::fs::read(&file_disk_path).unwrap() |
| 592 |
} |