| 1 |
use std::{ |
| 2 |
io::{Read, Seek, Write}, |
| 3 |
ops::RangeInclusive, |
| 4 |
}; |
| 5 |
|
| 6 |
use helix_core::diagnostic::Severity; |
| 7 |
use helix_stdx::path; |
| 8 |
use helix_view::doc; |
| 9 |
|
| 10 |
use super::*; |
| 11 |
|
| 12 |
#[tokio::test(flavor = "multi_thread")] |
| 13 |
async fn test_exit_w_buffer_w_path() -> anyhow::Result<()> { |
| 14 |
let mut file = tempfile::NamedTempFile::new()?; |
| 15 |
let mut app = helpers::AppBuilder::new() |
| 16 |
.with_file(file.path(), None) |
| 17 |
.build()?; |
| 18 |
|
| 19 |
test_key_sequence( |
| 20 |
&mut app, |
| 21 |
Some("iBecause of the obvious threat to untold numbers of citizens due to the crisis that is even now developing, this radio station will remain on the air day and night.<ret><esc>:x<ret>"), |
| 22 |
None, |
| 23 |
true, |
| 24 |
) |
| 25 |
.await?; |
| 26 |
|
| 27 |
reload_file(&mut file).unwrap(); |
| 28 |
let mut file_content = String::new(); |
| 29 |
file.as_file_mut().read_to_string(&mut file_content)?; |
| 30 |
|
| 31 |
assert_eq!( |
| 32 |
LineFeedHandling::Native.apply("Because of the obvious threat to untold numbers of citizens due to the crisis that is even now developing, this radio station will remain on the air day and night.\n"), |
| 33 |
file_content |
| 34 |
); |
| 35 |
|
| 36 |
Ok(()) |
| 37 |
} |
| 38 |
|
| 39 |
#[tokio::test(flavor = "multi_thread")] |
| 40 |
async fn test_exit_wo_buffer_w_path() -> anyhow::Result<()> { |
| 41 |
let mut file = tempfile::NamedTempFile::new()?; |
| 42 |
let mut app = helpers::AppBuilder::new() |
| 43 |
.with_file(file.path(), None) |
| 44 |
.build()?; |
| 45 |
|
| 46 |
helpers::run_event_loop_until_idle(&mut app).await; |
| 47 |
|
| 48 |
file.as_file_mut() |
| 49 |
.write_all("extremely important content".as_bytes())?; |
| 50 |
file.as_file_mut().flush()?; |
| 51 |
file.as_file_mut().sync_all()?; |
| 52 |
|
| 53 |
test_key_sequence(&mut app, Some(":x<ret>"), None, true).await?; |
| 54 |
|
| 55 |
reload_file(&mut file).unwrap(); |
| 56 |
let mut file_content = String::new(); |
| 57 |
file.read_to_string(&mut file_content)?; |
| 58 |
|
| 59 |
assert_eq!("extremely important content", file_content); |
| 60 |
|
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
|
| 64 |
#[tokio::test(flavor = "multi_thread")] |
| 65 |
async fn test_exit_wo_buffer_wo_path() -> anyhow::Result<()> { |
| 66 |
test_key_sequence( |
| 67 |
&mut AppBuilder::new().build()?, |
| 68 |
Some(":x<ret>"), |
| 69 |
Some(&|app| { |
| 70 |
assert!(!app.editor.is_err()); |
| 71 |
}), |
| 72 |
true, |
| 73 |
) |
| 74 |
.await?; |
| 75 |
|
| 76 |
Ok(()) |
| 77 |
} |
| 78 |
|
| 79 |
#[tokio::test(flavor = "multi_thread")] |
| 80 |
async fn test_exit_w_buffer_wo_file() -> anyhow::Result<()> { |
| 81 |
let mut file = tempfile::NamedTempFile::new()?; |
| 82 |
test_key_sequence( |
| 83 |
|
| 84 |
&mut AppBuilder::new().build()?, |
| 85 |
Some("itest<esc>:x<ret>"), |
| 86 |
None, |
| 87 |
false, |
| 88 |
) |
| 89 |
.await?; |
| 90 |
test_key_sequence( |
| 91 |
|
| 92 |
&mut AppBuilder::new().build()?, |
| 93 |
Some(format!("iMicCheck<esc>:x {}<ret>", file.path().to_string_lossy()).as_ref()), |
| 94 |
Some(&|app| { |
| 95 |
assert!(!app.editor.is_err()); |
| 96 |
}), |
| 97 |
true, |
| 98 |
) |
| 99 |
.await?; |
| 100 |
|
| 101 |
helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("MicCheck"))?; |
| 102 |
|
| 103 |
Ok(()) |
| 104 |
} |
| 105 |
|
| 106 |
#[tokio::test(flavor = "multi_thread")] |
| 107 |
async fn test_write_quit_fail() -> anyhow::Result<()> { |
| 108 |
let file = helpers::new_readonly_tempfile()?; |
| 109 |
let mut app = helpers::AppBuilder::new() |
| 110 |
.with_file(file.path(), None) |
| 111 |
.build()?; |
| 112 |
|
| 113 |
test_key_sequence( |
| 114 |
&mut app, |
| 115 |
Some("ihello<esc>:wq<ret>"), |
| 116 |
Some(&|app| { |
| 117 |
let mut docs: Vec<_> = app.editor.documents().collect(); |
| 118 |
assert_eq!(1, docs.len()); |
| 119 |
|
| 120 |
let doc = docs.pop().unwrap(); |
| 121 |
assert_eq!( |
| 122 |
Some(path::normalize(file.path())), |
| 123 |
doc.path().map(ToOwned::to_owned) |
| 124 |
); |
| 125 |
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); |
| 126 |
}), |
| 127 |
false, |
| 128 |
) |
| 129 |
.await?; |
| 130 |
|
| 131 |
Ok(()) |
| 132 |
} |
| 133 |
|
| 134 |
#[tokio::test(flavor = "multi_thread")] |
| 135 |
async fn test_buffer_close_concurrent() -> anyhow::Result<()> { |
| 136 |
test_key_sequences( |
| 137 |
&mut helpers::AppBuilder::new().build()?, |
| 138 |
vec![ |
| 139 |
( |
| 140 |
None, |
| 141 |
Some(&|app| { |
| 142 |
assert_eq!(1, app.editor.documents().count()); |
| 143 |
assert!(!app.editor.is_err()); |
| 144 |
}), |
| 145 |
), |
| 146 |
( |
| 147 |
Some("ihello<esc>:new<ret>"), |
| 148 |
Some(&|app| { |
| 149 |
assert_eq!(2, app.editor.documents().count()); |
| 150 |
assert!(!app.editor.is_err()); |
| 151 |
}), |
| 152 |
), |
| 153 |
( |
| 154 |
Some(":buffer<minus>close<ret>"), |
| 155 |
Some(&|app| { |
| 156 |
assert_eq!(1, app.editor.documents().count()); |
| 157 |
assert!(!app.editor.is_err()); |
| 158 |
}), |
| 159 |
), |
| 160 |
], |
| 161 |
false, |
| 162 |
) |
| 163 |
.await?; |
| 164 |
|
| 165 |
|
| 166 |
let mut file = tempfile::NamedTempFile::new()?; |
| 167 |
let mut command = String::new(); |
| 168 |
const RANGE: RangeInclusive<i32> = 1..=1000; |
| 169 |
|
| 170 |
for i in RANGE { |
| 171 |
let cmd = format!("%c{}<esc>:w!<ret>", i); |
| 172 |
command.push_str(&cmd); |
| 173 |
} |
| 174 |
|
| 175 |
command.push_str(":buffer<minus>close<ret>"); |
| 176 |
|
| 177 |
let mut app = helpers::AppBuilder::new() |
| 178 |
.with_file(file.path(), None) |
| 179 |
.build()?; |
| 180 |
|
| 181 |
test_key_sequence( |
| 182 |
&mut app, |
| 183 |
Some(&command), |
| 184 |
Some(&|app| { |
| 185 |
assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status()); |
| 186 |
|
| 187 |
let doc = app.editor.document_by_path(file.path()); |
| 188 |
assert!(doc.is_none(), "found doc: {:?}", doc); |
| 189 |
}), |
| 190 |
false, |
| 191 |
) |
| 192 |
.await?; |
| 193 |
|
| 194 |
helpers::assert_file_has_content( |
| 195 |
&mut file, |
| 196 |
&LineFeedHandling::Native.apply(&RANGE.end().to_string()), |
| 197 |
)?; |
| 198 |
|
| 199 |
Ok(()) |
| 200 |
} |
| 201 |
|
| 202 |
#[tokio::test(flavor = "multi_thread")] |
| 203 |
async fn test_write() -> anyhow::Result<()> { |
| 204 |
let mut file = tempfile::NamedTempFile::new()?; |
| 205 |
let mut app = helpers::AppBuilder::new() |
| 206 |
.with_file(file.path(), None) |
| 207 |
.build()?; |
| 208 |
|
| 209 |
test_key_sequence( |
| 210 |
&mut app, |
| 211 |
Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), |
| 212 |
None, |
| 213 |
false, |
| 214 |
) |
| 215 |
.await?; |
| 216 |
|
| 217 |
reload_file(&mut file).unwrap(); |
| 218 |
let mut file_content = String::new(); |
| 219 |
file.as_file_mut().read_to_string(&mut file_content)?; |
| 220 |
|
| 221 |
assert_eq!( |
| 222 |
LineFeedHandling::Native.apply("the gostak distims the doshes"), |
| 223 |
file_content |
| 224 |
); |
| 225 |
|
| 226 |
Ok(()) |
| 227 |
} |
| 228 |
|
| 229 |
#[tokio::test(flavor = "multi_thread")] |
| 230 |
async fn test_overwrite_protection() -> anyhow::Result<()> { |
| 231 |
let mut file = tempfile::NamedTempFile::new()?; |
| 232 |
let mut app = helpers::AppBuilder::new() |
| 233 |
.with_file(file.path(), None) |
| 234 |
.build()?; |
| 235 |
|
| 236 |
helpers::run_event_loop_until_idle(&mut app).await; |
| 237 |
|
| 238 |
file.as_file_mut() |
| 239 |
.write_all("extremely important content".as_bytes())?; |
| 240 |
|
| 241 |
file.as_file_mut().flush()?; |
| 242 |
file.as_file_mut().sync_all()?; |
| 243 |
|
| 244 |
test_key_sequence(&mut app, Some("iOverwriteData<esc>:x<ret>"), None, false).await?; |
| 245 |
|
| 246 |
reload_file(&mut file).unwrap(); |
| 247 |
let mut file_content = String::new(); |
| 248 |
file.read_to_string(&mut file_content)?; |
| 249 |
|
| 250 |
assert_eq!("extremely important content", file_content); |
| 251 |
|
| 252 |
Ok(()) |
| 253 |
} |
| 254 |
|
| 255 |
#[tokio::test(flavor = "multi_thread")] |
| 256 |
async fn test_write_quit() -> anyhow::Result<()> { |
| 257 |
let mut file = tempfile::NamedTempFile::new()?; |
| 258 |
let mut app = helpers::AppBuilder::new() |
| 259 |
.with_file(file.path(), None) |
| 260 |
.build()?; |
| 261 |
|
| 262 |
test_key_sequence( |
| 263 |
&mut app, |
| 264 |
Some("ithe gostak distims the doshes<ret><esc>:wq<ret>"), |
| 265 |
None, |
| 266 |
true, |
| 267 |
) |
| 268 |
.await?; |
| 269 |
|
| 270 |
reload_file(&mut file).unwrap(); |
| 271 |
|
| 272 |
let mut file_content = String::new(); |
| 273 |
file.read_to_string(&mut file_content)?; |
| 274 |
|
| 275 |
assert_eq!( |
| 276 |
LineFeedHandling::Native.apply("the gostak distims the doshes"), |
| 277 |
file_content |
| 278 |
); |
| 279 |
|
| 280 |
Ok(()) |
| 281 |
} |
| 282 |
|
| 283 |
#[tokio::test(flavor = "multi_thread")] |
| 284 |
async fn test_write_concurrent() -> anyhow::Result<()> { |
| 285 |
let mut file = tempfile::NamedTempFile::new()?; |
| 286 |
let mut command = String::new(); |
| 287 |
const RANGE: RangeInclusive<i32> = 1..=1000; |
| 288 |
let mut app = helpers::AppBuilder::new() |
| 289 |
.with_file(file.path(), None) |
| 290 |
.build()?; |
| 291 |
|
| 292 |
for i in RANGE { |
| 293 |
let cmd = format!("%c{}<esc>:w!<ret>", i); |
| 294 |
command.push_str(&cmd); |
| 295 |
} |
| 296 |
|
| 297 |
test_key_sequence(&mut app, Some(&command), None, false).await?; |
| 298 |
|
| 299 |
reload_file(&mut file).unwrap(); |
| 300 |
let mut file_content = String::new(); |
| 301 |
file.read_to_string(&mut file_content)?; |
| 302 |
assert_eq!( |
| 303 |
LineFeedHandling::Native.apply(&RANGE.end().to_string()), |
| 304 |
file_content |
| 305 |
); |
| 306 |
|
| 307 |
Ok(()) |
| 308 |
} |
| 309 |
|
| 310 |
#[tokio::test(flavor = "multi_thread")] |
| 311 |
async fn test_write_fail_mod_flag() -> anyhow::Result<()> { |
| 312 |
let file = helpers::new_readonly_tempfile()?; |
| 313 |
let mut app = helpers::AppBuilder::new() |
| 314 |
.with_file(file.path(), None) |
| 315 |
.build()?; |
| 316 |
|
| 317 |
test_key_sequences( |
| 318 |
&mut app, |
| 319 |
vec![ |
| 320 |
( |
| 321 |
None, |
| 322 |
Some(&|app| { |
| 323 |
let doc = doc!(app.editor); |
| 324 |
assert!(!doc.is_modified()); |
| 325 |
}), |
| 326 |
), |
| 327 |
( |
| 328 |
Some("ihello<esc>"), |
| 329 |
Some(&|app| { |
| 330 |
let doc = doc!(app.editor); |
| 331 |
assert!(doc.is_modified()); |
| 332 |
}), |
| 333 |
), |
| 334 |
( |
| 335 |
Some(":w<ret>"), |
| 336 |
Some(&|app| { |
| 337 |
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); |
| 338 |
|
| 339 |
let doc = doc!(app.editor); |
| 340 |
assert!(doc.is_modified()); |
| 341 |
}), |
| 342 |
), |
| 343 |
], |
| 344 |
false, |
| 345 |
) |
| 346 |
.await?; |
| 347 |
|
| 348 |
Ok(()) |
| 349 |
} |
| 350 |
|
| 351 |
#[tokio::test(flavor = "multi_thread")] |
| 352 |
async fn test_write_scratch_to_new_path() -> anyhow::Result<()> { |
| 353 |
let mut file = tempfile::NamedTempFile::new()?; |
| 354 |
|
| 355 |
test_key_sequence( |
| 356 |
&mut AppBuilder::new().build()?, |
| 357 |
Some(format!("ihello<esc>:w {}<ret>", file.path().to_string_lossy()).as_ref()), |
| 358 |
Some(&|app| { |
| 359 |
assert!(!app.editor.is_err()); |
| 360 |
|
| 361 |
let mut docs: Vec<_> = app.editor.documents().collect(); |
| 362 |
assert_eq!(1, docs.len()); |
| 363 |
|
| 364 |
let doc = docs.pop().unwrap(); |
| 365 |
assert_eq!( |
| 366 |
Some(path::normalize(file.path())), |
| 367 |
doc.path().map(ToOwned::to_owned) |
| 368 |
); |
| 369 |
}), |
| 370 |
false, |
| 371 |
) |
| 372 |
.await?; |
| 373 |
|
| 374 |
helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("hello"))?; |
| 375 |
|
| 376 |
Ok(()) |
| 377 |
} |
| 378 |
|
| 379 |
#[tokio::test(flavor = "multi_thread")] |
| 380 |
async fn test_write_scratch_to_new_path_force_creates_file() -> anyhow::Result<()> { |
| 381 |
let dir = tempfile::tempdir()?; |
| 382 |
let new_path = dir.path().join("new-file.txt"); |
| 383 |
|
| 384 |
test_key_sequence( |
| 385 |
&mut AppBuilder::new().build()?, |
| 386 |
Some(format!("ihello<esc>:w! {}<ret>", new_path.to_string_lossy()).as_ref()), |
| 387 |
Some(&|app| { |
| 388 |
assert!(!app.editor.is_err()); |
| 389 |
|
| 390 |
let mut docs: Vec<_> = app.editor.documents().collect(); |
| 391 |
assert_eq!(1, docs.len()); |
| 392 |
|
| 393 |
let doc = docs.pop().unwrap(); |
| 394 |
assert_eq!( |
| 395 |
Some(path::normalize(&new_path)), |
| 396 |
doc.path().map(ToOwned::to_owned) |
| 397 |
); |
| 398 |
}), |
| 399 |
false, |
| 400 |
) |
| 401 |
.await?; |
| 402 |
|
| 403 |
let file_content = std::fs::read_to_string(&new_path)?; |
| 404 |
assert_eq!(file_content, LineFeedHandling::Native.apply("hello")); |
| 405 |
|
| 406 |
Ok(()) |
| 407 |
} |
| 408 |
|
| 409 |
#[tokio::test(flavor = "multi_thread")] |
| 410 |
async fn test_write_scratch_no_path_fails() -> anyhow::Result<()> { |
| 411 |
helpers::test_key_sequence_with_input_text( |
| 412 |
None, |
| 413 |
("#[\n|]#", "ihello<esc>:w<ret>", "hello#[\n|]#"), |
| 414 |
&|app| { |
| 415 |
assert!(app.editor.is_err()); |
| 416 |
|
| 417 |
let mut docs: Vec<_> = app.editor.documents().collect(); |
| 418 |
assert_eq!(1, docs.len()); |
| 419 |
|
| 420 |
let doc = docs.pop().unwrap(); |
| 421 |
assert_eq!(None, doc.path()); |
| 422 |
}, |
| 423 |
false, |
| 424 |
) |
| 425 |
.await?; |
| 426 |
|
| 427 |
Ok(()) |
| 428 |
} |
| 429 |
|
| 430 |
#[tokio::test(flavor = "multi_thread")] |
| 431 |
async fn test_write_auto_format_fails_still_writes() -> anyhow::Result<()> { |
| 432 |
let mut file = tempfile::Builder::new().suffix(".rs").tempfile()?; |
| 433 |
|
| 434 |
let lang_conf = indoc! {r#" |
| 435 |
[[language]] |
| 436 |
name = "rust" |
| 437 |
formatter = { command = "bash", args = [ "-c", "exit 1" ] } |
| 438 |
"#}; |
| 439 |
|
| 440 |
let mut app = helpers::AppBuilder::new() |
| 441 |
.with_file(file.path(), None) |
| 442 |
.with_input_text("#[l|]#et foo = 0;\n") |
| 443 |
.with_lang_loader(helpers::test_syntax_loader(Some(lang_conf.into()))) |
| 444 |
.build()?; |
| 445 |
|
| 446 |
test_key_sequences(&mut app, vec![(Some(":w<ret>"), None)], false).await?; |
| 447 |
|
| 448 |
|
| 449 |
helpers::assert_file_has_content(&mut file, "let foo = 0;\n")?; |
| 450 |
|
| 451 |
Ok(()) |
| 452 |
} |
| 453 |
|
| 454 |
#[tokio::test(flavor = "multi_thread")] |
| 455 |
async fn test_write_quit_auto_format_exits_after_format() -> anyhow::Result<()> { |
| 456 |
let mut file = tempfile::Builder::new().suffix(".rs").tempfile()?; |
| 457 |
|
| 458 |
let lang_conf = indoc! {r#" |
| 459 |
[[language]] |
| 460 |
name = "rust" |
| 461 |
formatter = { command = "bash", args = [ "-c", "echo new content" ] } |
| 462 |
"#}; |
| 463 |
|
| 464 |
let mut app = helpers::AppBuilder::new() |
| 465 |
.with_file(file.path(), None) |
| 466 |
.with_input_text("#[l|]#et foo = 0;\n") |
| 467 |
.with_lang_loader(helpers::test_syntax_loader(Some(lang_conf.into()))) |
| 468 |
.build()?; |
| 469 |
|
| 470 |
test_key_sequences(&mut app, vec![(Some(":x<ret>"), None)], true).await?; |
| 471 |
|
| 472 |
|
| 473 |
helpers::assert_file_has_content(&mut file, "new content\n")?; |
| 474 |
|
| 475 |
Ok(()) |
| 476 |
} |
| 477 |
|
| 478 |
#[tokio::test(flavor = "multi_thread")] |
| 479 |
async fn test_write_code_actions_on_save_without_server_still_saves() -> anyhow::Result<()> { |
| 480 |
let mut file = tempfile::Builder::new().suffix(".rs").tempfile()?; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
let lang_conf = indoc! {r#" |
| 486 |
[[language]] |
| 487 |
name = "rust" |
| 488 |
code-actions-on-save = ["source.organizeImports"] |
| 489 |
"#}; |
| 490 |
|
| 491 |
let mut app = helpers::AppBuilder::new() |
| 492 |
.with_file(file.path(), None) |
| 493 |
.with_input_text("#[l|]#et foo = 0;\n") |
| 494 |
.with_lang_loader(helpers::test_syntax_loader(Some(lang_conf.into()))) |
| 495 |
.build()?; |
| 496 |
|
| 497 |
test_key_sequences(&mut app, vec![(Some(":w --no-format<ret>"), None)], false).await?; |
| 498 |
|
| 499 |
helpers::assert_file_has_content(&mut file, "let foo = 0;\n")?; |
| 500 |
|
| 501 |
Ok(()) |
| 502 |
} |
| 503 |
|
| 504 |
#[tokio::test(flavor = "multi_thread")] |
| 505 |
async fn test_write_new_path() -> anyhow::Result<()> { |
| 506 |
let mut file1 = tempfile::NamedTempFile::new().unwrap(); |
| 507 |
let mut file2 = tempfile::NamedTempFile::new().unwrap(); |
| 508 |
let mut app = helpers::AppBuilder::new() |
| 509 |
.with_file(file1.path(), None) |
| 510 |
.build()?; |
| 511 |
|
| 512 |
test_key_sequences( |
| 513 |
&mut app, |
| 514 |
vec![ |
| 515 |
( |
| 516 |
Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"), |
| 517 |
Some(&|app| { |
| 518 |
let doc = doc!(app.editor); |
| 519 |
assert!(!app.editor.is_err()); |
| 520 |
assert_eq!(&path::normalize(file1.path()), doc.path().unwrap()); |
| 521 |
}), |
| 522 |
), |
| 523 |
( |
| 524 |
Some(&format!(":w {}<ret>", file2.path().to_string_lossy())), |
| 525 |
Some(&|app| { |
| 526 |
let doc = doc!(app.editor); |
| 527 |
assert!(!app.editor.is_err()); |
| 528 |
assert_eq!(&path::normalize(file2.path()), doc.path().unwrap()); |
| 529 |
assert!(app.editor.document_by_path(file1.path()).is_none()); |
| 530 |
}), |
| 531 |
), |
| 532 |
], |
| 533 |
false, |
| 534 |
) |
| 535 |
.await?; |
| 536 |
|
| 537 |
helpers::assert_file_has_content( |
| 538 |
&mut file1, |
| 539 |
&LineFeedHandling::Native.apply("i can eat glass, it will not hurt me\n"), |
| 540 |
)?; |
| 541 |
|
| 542 |
helpers::assert_file_has_content( |
| 543 |
&mut file2, |
| 544 |
&LineFeedHandling::Native.apply("i can eat glass, it will not hurt me\n"), |
| 545 |
)?; |
| 546 |
|
| 547 |
Ok(()) |
| 548 |
} |
| 549 |
|
| 550 |
#[tokio::test(flavor = "multi_thread")] |
| 551 |
async fn test_write_fail_new_path() -> anyhow::Result<()> { |
| 552 |
let file = helpers::new_readonly_tempfile()?; |
| 553 |
|
| 554 |
test_key_sequences( |
| 555 |
&mut AppBuilder::new().build()?, |
| 556 |
vec![ |
| 557 |
( |
| 558 |
None, |
| 559 |
Some(&|app| { |
| 560 |
let doc = doc!(app.editor); |
| 561 |
assert_ne!( |
| 562 |
Some(&Severity::Error), |
| 563 |
app.editor.get_status().map(|status| status.1) |
| 564 |
); |
| 565 |
assert_eq!(None, doc.path()); |
| 566 |
}), |
| 567 |
), |
| 568 |
( |
| 569 |
Some(&format!(":w {}<ret>", file.path().to_string_lossy())), |
| 570 |
Some(&|app| { |
| 571 |
let doc = doc!(app.editor); |
| 572 |
assert_eq!( |
| 573 |
Some(&Severity::Error), |
| 574 |
app.editor.get_status().map(|status| status.1) |
| 575 |
); |
| 576 |
assert_eq!(None, doc.path()); |
| 577 |
}), |
| 578 |
), |
| 579 |
], |
| 580 |
false, |
| 581 |
) |
| 582 |
.await?; |
| 583 |
|
| 584 |
Ok(()) |
| 585 |
} |
| 586 |
|
| 587 |
#[tokio::test(flavor = "multi_thread")] |
| 588 |
async fn test_write_utf_bom_file() -> anyhow::Result<()> { |
| 589 |
|
| 590 |
const UTF8_FILE: [u8; 6] = [0xef, 0xbb, 0xbf, b'A', b'B', b'C']; |
| 591 |
|
| 592 |
|
| 593 |
const UTF16LE_FILE: [u8; 8] = [0xff, 0xfe, b'A', 0x00, b'B', 0x00, b'C', 0x00]; |
| 594 |
const UTF16BE_FILE: [u8; 8] = [0xfe, 0xff, 0x00, b'A', 0x00, b'B', 0x00, b'C']; |
| 595 |
|
| 596 |
edit_file_with_content(&UTF8_FILE).await?; |
| 597 |
edit_file_with_content(&UTF16LE_FILE).await?; |
| 598 |
edit_file_with_content(&UTF16BE_FILE).await?; |
| 599 |
|
| 600 |
Ok(()) |
| 601 |
} |
| 602 |
|
| 603 |
#[tokio::test(flavor = "multi_thread")] |
| 604 |
async fn test_write_trim_trailing_whitespace() -> anyhow::Result<()> { |
| 605 |
let mut file = tempfile::NamedTempFile::new()?; |
| 606 |
let mut app = helpers::AppBuilder::new() |
| 607 |
.with_config(Config { |
| 608 |
editor: helix_view::editor::Config { |
| 609 |
trim_trailing_whitespace: true, |
| 610 |
..Default::default() |
| 611 |
}, |
| 612 |
..Default::default() |
| 613 |
}) |
| 614 |
.with_file(file.path(), None) |
| 615 |
.with_input_text(LineFeedHandling::Native.apply("#[f|]#oo \n\n \nbar ")) |
| 616 |
.build()?; |
| 617 |
|
| 618 |
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; |
| 619 |
|
| 620 |
helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("foo\n\n\nbar"))?; |
| 621 |
|
| 622 |
Ok(()) |
| 623 |
} |
| 624 |
|
| 625 |
#[tokio::test(flavor = "multi_thread")] |
| 626 |
async fn test_write_trim_final_newlines() -> anyhow::Result<()> { |
| 627 |
let mut file = tempfile::NamedTempFile::new()?; |
| 628 |
let mut app = helpers::AppBuilder::new() |
| 629 |
.with_config(Config { |
| 630 |
editor: helix_view::editor::Config { |
| 631 |
trim_final_newlines: true, |
| 632 |
..Default::default() |
| 633 |
}, |
| 634 |
..Default::default() |
| 635 |
}) |
| 636 |
.with_file(file.path(), None) |
| 637 |
.with_input_text(LineFeedHandling::Native.apply("#[f|]#oo\n \n\n\n")) |
| 638 |
.build()?; |
| 639 |
|
| 640 |
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; |
| 641 |
|
| 642 |
helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("foo\n \n"))?; |
| 643 |
|
| 644 |
Ok(()) |
| 645 |
} |
| 646 |
|
| 647 |
#[tokio::test(flavor = "multi_thread")] |
| 648 |
async fn test_write_insert_final_newline_added_if_missing() -> anyhow::Result<()> { |
| 649 |
let mut file = tempfile::NamedTempFile::new()?; |
| 650 |
let mut app = helpers::AppBuilder::new() |
| 651 |
.with_file(file.path(), None) |
| 652 |
.with_input_text("#[h|]#ave you tried chamomile tea?") |
| 653 |
.build()?; |
| 654 |
|
| 655 |
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; |
| 656 |
|
| 657 |
helpers::assert_file_has_content( |
| 658 |
&mut file, |
| 659 |
&LineFeedHandling::Native.apply("have you tried chamomile tea?\n"), |
| 660 |
)?; |
| 661 |
|
| 662 |
Ok(()) |
| 663 |
} |
| 664 |
|
| 665 |
#[tokio::test(flavor = "multi_thread")] |
| 666 |
async fn test_write_insert_final_newline_unchanged_if_empty() -> anyhow::Result<()> { |
| 667 |
let mut file = tempfile::NamedTempFile::new()?; |
| 668 |
let mut app = helpers::AppBuilder::new() |
| 669 |
.with_file(file.path(), None) |
| 670 |
.with_input_text("#[|]#") |
| 671 |
.build()?; |
| 672 |
|
| 673 |
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; |
| 674 |
|
| 675 |
helpers::assert_file_has_content(&mut file, "")?; |
| 676 |
|
| 677 |
Ok(()) |
| 678 |
} |
| 679 |
|
| 680 |
#[tokio::test(flavor = "multi_thread")] |
| 681 |
async fn test_write_insert_final_newline_unchanged_if_not_missing() -> anyhow::Result<()> { |
| 682 |
let mut file = tempfile::NamedTempFile::new()?; |
| 683 |
let mut app = helpers::AppBuilder::new() |
| 684 |
.with_file(file.path(), None) |
| 685 |
.with_input_text(LineFeedHandling::Native.apply("#[t|]#en minutes, please\n")) |
| 686 |
.build()?; |
| 687 |
|
| 688 |
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; |
| 689 |
|
| 690 |
helpers::assert_file_has_content( |
| 691 |
&mut file, |
| 692 |
&LineFeedHandling::Native.apply("ten minutes, please\n"), |
| 693 |
)?; |
| 694 |
|
| 695 |
Ok(()) |
| 696 |
} |
| 697 |
|
| 698 |
#[tokio::test(flavor = "multi_thread")] |
| 699 |
async fn test_write_insert_final_newline_unchanged_if_missing_and_false() -> anyhow::Result<()> { |
| 700 |
let mut file = tempfile::NamedTempFile::new()?; |
| 701 |
let mut app = helpers::AppBuilder::new() |
| 702 |
.with_config(Config { |
| 703 |
editor: helix_view::editor::Config { |
| 704 |
insert_final_newline: false, |
| 705 |
..Default::default() |
| 706 |
}, |
| 707 |
..Default::default() |
| 708 |
}) |
| 709 |
.with_file(file.path(), None) |
| 710 |
.with_input_text("#[t|]#he quiet rain continued through the night") |
| 711 |
.build()?; |
| 712 |
|
| 713 |
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; |
| 714 |
|
| 715 |
reload_file(&mut file).unwrap(); |
| 716 |
helpers::assert_file_has_content(&mut file, "the quiet rain continued through the night")?; |
| 717 |
|
| 718 |
Ok(()) |
| 719 |
} |
| 720 |
|
| 721 |
#[tokio::test(flavor = "multi_thread")] |
| 722 |
async fn test_write_all_insert_final_newline_add_if_missing_and_modified() -> anyhow::Result<()> { |
| 723 |
let mut file1 = tempfile::NamedTempFile::new()?; |
| 724 |
let mut file2 = tempfile::NamedTempFile::new()?; |
| 725 |
let mut app = helpers::AppBuilder::new() |
| 726 |
.with_file(file1.path(), None) |
| 727 |
.with_input_text("#[w|]#e don't serve time travelers here") |
| 728 |
.build()?; |
| 729 |
|
| 730 |
test_key_sequence( |
| 731 |
&mut app, |
| 732 |
Some(&format!( |
| 733 |
":o {}<ret>ia time traveler walks into a bar<esc>:wa<ret>", |
| 734 |
file2.path().to_string_lossy() |
| 735 |
)), |
| 736 |
None, |
| 737 |
false, |
| 738 |
) |
| 739 |
.await?; |
| 740 |
|
| 741 |
helpers::assert_file_has_content( |
| 742 |
&mut file1, |
| 743 |
&LineFeedHandling::Native.apply("we don't serve time travelers here\n"), |
| 744 |
)?; |
| 745 |
|
| 746 |
helpers::assert_file_has_content( |
| 747 |
&mut file2, |
| 748 |
&LineFeedHandling::Native.apply("a time traveler walks into a bar\n"), |
| 749 |
)?; |
| 750 |
|
| 751 |
Ok(()) |
| 752 |
} |
| 753 |
|
| 754 |
#[tokio::test(flavor = "multi_thread")] |
| 755 |
async fn test_write_all_insert_final_newline_do_not_add_if_unmodified() -> anyhow::Result<()> { |
| 756 |
let mut file = tempfile::NamedTempFile::new()?; |
| 757 |
let mut app = helpers::AppBuilder::new() |
| 758 |
.with_file(file.path(), None) |
| 759 |
.build()?; |
| 760 |
|
| 761 |
file.write_all(b"i lost on Jeopardy!")?; |
| 762 |
file.rewind()?; |
| 763 |
|
| 764 |
test_key_sequence(&mut app, Some(":wa<ret>"), None, false).await?; |
| 765 |
|
| 766 |
helpers::assert_file_has_content(&mut file, "i lost on Jeopardy!")?; |
| 767 |
|
| 768 |
Ok(()) |
| 769 |
} |
| 770 |
|
| 771 |
#[tokio::test(flavor = "multi_thread")] |
| 772 |
async fn test_symlink_write() -> anyhow::Result<()> { |
| 773 |
#[cfg(unix)] |
| 774 |
use std::os::unix::fs::symlink; |
| 775 |
#[cfg(not(unix))] |
| 776 |
use std::os::windows::fs::symlink_file as symlink; |
| 777 |
|
| 778 |
let dir = tempfile::tempdir()?; |
| 779 |
|
| 780 |
let mut file = tempfile::NamedTempFile::new_in(&dir)?; |
| 781 |
let symlink_path = dir.path().join("linked"); |
| 782 |
symlink(file.path(), &symlink_path)?; |
| 783 |
|
| 784 |
let mut app = helpers::AppBuilder::new() |
| 785 |
.with_file(&symlink_path, None) |
| 786 |
.build()?; |
| 787 |
|
| 788 |
test_key_sequence( |
| 789 |
&mut app, |
| 790 |
Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), |
| 791 |
None, |
| 792 |
false, |
| 793 |
) |
| 794 |
.await?; |
| 795 |
|
| 796 |
reload_file(&mut file).unwrap(); |
| 797 |
let mut file_content = String::new(); |
| 798 |
file.as_file_mut().read_to_string(&mut file_content)?; |
| 799 |
|
| 800 |
assert_eq!( |
| 801 |
LineFeedHandling::Native.apply("the gostak distims the doshes"), |
| 802 |
file_content |
| 803 |
); |
| 804 |
assert!(symlink_path.is_symlink()); |
| 805 |
|
| 806 |
Ok(()) |
| 807 |
} |
| 808 |
|
| 809 |
#[tokio::test(flavor = "multi_thread")] |
| 810 |
async fn test_symlink_write_fail() -> anyhow::Result<()> { |
| 811 |
#[cfg(unix)] |
| 812 |
use std::os::unix::fs::symlink; |
| 813 |
#[cfg(not(unix))] |
| 814 |
use std::os::windows::fs::symlink_file as symlink; |
| 815 |
|
| 816 |
let dir = tempfile::tempdir()?; |
| 817 |
|
| 818 |
let file = helpers::new_readonly_tempfile_in_dir(&dir)?; |
| 819 |
let symlink_path = dir.path().join("linked"); |
| 820 |
symlink(file.path(), &symlink_path)?; |
| 821 |
|
| 822 |
let mut app = helpers::AppBuilder::new() |
| 823 |
.with_file(&symlink_path, None) |
| 824 |
.build()?; |
| 825 |
|
| 826 |
test_key_sequence( |
| 827 |
&mut app, |
| 828 |
Some("ihello<esc>:wq<ret>"), |
| 829 |
Some(&|app| { |
| 830 |
let mut docs: Vec<_> = app.editor.documents().collect(); |
| 831 |
assert_eq!(1, docs.len()); |
| 832 |
|
| 833 |
let doc = docs.pop().unwrap(); |
| 834 |
assert_eq!( |
| 835 |
Some(path::normalize(&symlink_path)), |
| 836 |
doc.path().map(ToOwned::to_owned) |
| 837 |
); |
| 838 |
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); |
| 839 |
}), |
| 840 |
false, |
| 841 |
) |
| 842 |
.await?; |
| 843 |
|
| 844 |
assert!(symlink_path.is_symlink()); |
| 845 |
|
| 846 |
Ok(()) |
| 847 |
} |
| 848 |
|
| 849 |
#[tokio::test(flavor = "multi_thread")] |
| 850 |
async fn test_symlink_write_relative() -> anyhow::Result<()> { |
| 851 |
#[cfg(unix)] |
| 852 |
use std::os::unix::fs::symlink; |
| 853 |
#[cfg(not(unix))] |
| 854 |
use std::os::windows::fs::symlink_file as symlink; |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
let dir = tempfile::tempdir()?; |
| 861 |
let inner_dir = dir.path().join("b"); |
| 862 |
std::fs::create_dir(&inner_dir)?; |
| 863 |
|
| 864 |
let mut file = tempfile::NamedTempFile::new_in(&inner_dir)?; |
| 865 |
let symlink_path = dir.path().join("linked"); |
| 866 |
let relative_path = std::path::PathBuf::from("b").join(file.path().file_name().unwrap()); |
| 867 |
symlink(relative_path, &symlink_path)?; |
| 868 |
|
| 869 |
let mut app = helpers::AppBuilder::new() |
| 870 |
.with_file(&symlink_path, None) |
| 871 |
.build()?; |
| 872 |
|
| 873 |
test_key_sequence( |
| 874 |
&mut app, |
| 875 |
Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), |
| 876 |
None, |
| 877 |
false, |
| 878 |
) |
| 879 |
.await?; |
| 880 |
|
| 881 |
reload_file(&mut file).unwrap(); |
| 882 |
let mut file_content = String::new(); |
| 883 |
file.as_file_mut().read_to_string(&mut file_content)?; |
| 884 |
|
| 885 |
assert_eq!( |
| 886 |
LineFeedHandling::Native.apply("the gostak distims the doshes"), |
| 887 |
file_content |
| 888 |
); |
| 889 |
assert!(symlink_path.is_symlink()); |
| 890 |
|
| 891 |
Ok(()) |
| 892 |
} |
| 893 |
|
| 894 |
#[tokio::test(flavor = "multi_thread")] |
| 895 |
#[cfg(not(target_os = "android"))] |
| 896 |
async fn test_hardlink_write() -> anyhow::Result<()> { |
| 897 |
let dir = tempfile::tempdir()?; |
| 898 |
|
| 899 |
let mut file = tempfile::NamedTempFile::new_in(&dir)?; |
| 900 |
let hardlink_path = dir.path().join("linked"); |
| 901 |
std::fs::hard_link(file.path(), &hardlink_path)?; |
| 902 |
|
| 903 |
let mut app = helpers::AppBuilder::new() |
| 904 |
.with_file(&hardlink_path, None) |
| 905 |
.build()?; |
| 906 |
|
| 907 |
test_key_sequence( |
| 908 |
&mut app, |
| 909 |
Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), |
| 910 |
None, |
| 911 |
false, |
| 912 |
) |
| 913 |
.await?; |
| 914 |
|
| 915 |
reload_file(&mut file).unwrap(); |
| 916 |
let mut file_content = String::new(); |
| 917 |
file.as_file_mut().read_to_string(&mut file_content)?; |
| 918 |
|
| 919 |
assert_eq!( |
| 920 |
LineFeedHandling::Native.apply("the gostak distims the doshes"), |
| 921 |
file_content |
| 922 |
); |
| 923 |
assert!(helix_stdx::faccess::hardlink_count(&hardlink_path)? > 1); |
| 924 |
assert!(same_file::is_same_file(file.path(), &hardlink_path)?); |
| 925 |
|
| 926 |
Ok(()) |
| 927 |
} |
| 928 |
|
| 929 |
async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> { |
| 930 |
let mut file = tempfile::NamedTempFile::new()?; |
| 931 |
|
| 932 |
file.as_file_mut().write_all(&file_content)?; |
| 933 |
|
| 934 |
helpers::test_key_sequence( |
| 935 |
&mut helpers::AppBuilder::new() |
| 936 |
.with_config(Config { |
| 937 |
editor: helix_view::editor::Config { |
| 938 |
insert_final_newline: false, |
| 939 |
..Default::default() |
| 940 |
}, |
| 941 |
..Default::default() |
| 942 |
}) |
| 943 |
.build()?, |
| 944 |
Some(&format!(":o {}<ret>:x<ret>", file.path().to_string_lossy())), |
| 945 |
None, |
| 946 |
true, |
| 947 |
) |
| 948 |
.await?; |
| 949 |
|
| 950 |
reload_file(&mut file).unwrap(); |
| 951 |
let mut new_file_content: Vec<u8> = Vec::new(); |
| 952 |
file.read_to_end(&mut new_file_content)?; |
| 953 |
|
| 954 |
assert_eq!(file_content, new_file_content); |
| 955 |
|
| 956 |
Ok(()) |
| 957 |
} |
| 958 |
|
| 959 |
#[tokio::test(flavor = "multi_thread")] |
| 960 |
async fn test_move_file_when_given_dir_and_filename() -> anyhow::Result<()> { |
| 961 |
let dir = tempfile::tempdir()?; |
| 962 |
let source_file = tempfile::NamedTempFile::new_in(&dir)?; |
| 963 |
let target_file = dir.path().join("new_name.ext"); |
| 964 |
|
| 965 |
let mut app = helpers::AppBuilder::new() |
| 966 |
.with_file(source_file.path(), None) |
| 967 |
.build()?; |
| 968 |
|
| 969 |
test_key_sequence( |
| 970 |
&mut app, |
| 971 |
Some(format!(":move {}<ret>", target_file.to_string_lossy()).as_ref()), |
| 972 |
None, |
| 973 |
false, |
| 974 |
) |
| 975 |
.await?; |
| 976 |
|
| 977 |
assert!( |
| 978 |
target_file.is_file(), |
| 979 |
"target file '{}' should have been created", |
| 980 |
target_file.display() |
| 981 |
); |
| 982 |
assert!( |
| 983 |
!source_file.path().exists(), |
| 984 |
"Source file '{}' should have been removed", |
| 985 |
source_file.path().display() |
| 986 |
); |
| 987 |
|
| 988 |
Ok(()) |
| 989 |
} |
| 990 |
|
| 991 |
#[tokio::test(flavor = "multi_thread")] |
| 992 |
async fn test_move_file_when_given_dir_only() -> anyhow::Result<()> { |
| 993 |
let source_dir = tempfile::tempdir()?; |
| 994 |
let target_dir = tempfile::tempdir()?; |
| 995 |
let source_file = source_dir.path().join("file.ext"); |
| 996 |
std::fs::File::create(&source_file)?; |
| 997 |
|
| 998 |
let mut app = helpers::AppBuilder::new() |
| 999 |
.with_file(&source_file, None) |
| 1000 |
.build()?; |
| 1001 |
|
| 1002 |
test_key_sequence( |
| 1003 |
&mut app, |
| 1004 |
Some(format!(":move {}<ret>", target_dir.path().to_string_lossy()).as_ref()), |
| 1005 |
None, |
| 1006 |
false, |
| 1007 |
) |
| 1008 |
.await?; |
| 1009 |
|
| 1010 |
let target_file = target_dir.path().join("file.ext"); |
| 1011 |
|
| 1012 |
assert!( |
| 1013 |
target_file.is_file(), |
| 1014 |
"target file '{}' should have been created", |
| 1015 |
target_file.display() |
| 1016 |
); |
| 1017 |
assert!( |
| 1018 |
!source_file.exists(), |
| 1019 |
"Source file '{}' should have been removed", |
| 1020 |
source_file.display() |
| 1021 |
); |
| 1022 |
|
| 1023 |
Ok(()) |
| 1024 |
} |
| 1025 |
|
| 1026 |
#[tokio::test(flavor = "multi_thread")] |
| 1027 |
async fn test_write_then_open_does_not_panic_on_closed_scratch() -> anyhow::Result<()> { |
| 1028 |
let other = tempfile::NamedTempFile::new()?; |
| 1029 |
|
| 1030 |
test_key_sequence( |
| 1031 |
&mut AppBuilder::new().build()?, |
| 1032 |
Some(format!(":write<ret>:open {}<ret>", other.path().to_string_lossy()).as_ref()), |
| 1033 |
Some(&|app| { |
| 1034 |
assert_eq!(1, app.editor.documents().count()); |
| 1035 |
}), |
| 1036 |
false, |
| 1037 |
) |
| 1038 |
.await?; |
| 1039 |
|
| 1040 |
Ok(()) |
| 1041 |
} |