Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-term/tests/test/commands/write.rs')
| -rw-r--r-- | helix-term/tests/test/commands/write.rs | 473 |
1 files changed, 52 insertions, 421 deletions
diff --git a/helix-term/tests/test/commands/write.rs b/helix-term/tests/test/commands/write.rs index 4f44cd36..376ba5e7 100644 --- a/helix-term/tests/test/commands/write.rs +++ b/helix-term/tests/test/commands/write.rs @@ -3,107 +3,12 @@ use std::{ ops::RangeInclusive, }; -use helix_core::diagnostic::Severity; -use helix_stdx::path; +use helix_core::{diagnostic::Severity, path::get_normalized_path}; use helix_view::doc; use super::*; #[tokio::test(flavor = "multi_thread")] -async fn test_exit_w_buffer_w_path() -> anyhow::Result<()> { - let mut file = tempfile::NamedTempFile::new()?; - let mut app = helpers::AppBuilder::new() - .with_file(file.path(), None) - .build()?; - // Check for write operation on given path and edited buffer - test_key_sequence( - &mut app, - 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>"), - None, - true, - ) - .await?; - - reload_file(&mut file).unwrap(); - let mut file_content = String::new(); - file.as_file_mut().read_to_string(&mut file_content)?; - - assert_eq!( - 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"), - file_content - ); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_exit_wo_buffer_w_path() -> anyhow::Result<()> { - let mut file = tempfile::NamedTempFile::new()?; - let mut app = helpers::AppBuilder::new() - .with_file(file.path(), None) - .build()?; - - helpers::run_event_loop_until_idle(&mut app).await; - - file.as_file_mut() - .write_all("extremely important content".as_bytes())?; - file.as_file_mut().flush()?; - file.as_file_mut().sync_all()?; - - test_key_sequence(&mut app, Some(":x<ret>"), None, true).await?; - - reload_file(&mut file).unwrap(); - let mut file_content = String::new(); - file.read_to_string(&mut file_content)?; - // check that nothing is written to file - assert_eq!("extremely important content", file_content); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_exit_wo_buffer_wo_path() -> anyhow::Result<()> { - test_key_sequence( - &mut AppBuilder::new().build()?, - Some(":x<ret>"), - Some(&|app| { - assert!(!app.editor.is_err()); - }), - true, - ) - .await?; - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_exit_w_buffer_wo_file() -> anyhow::Result<()> { - let mut file = tempfile::NamedTempFile::new()?; - test_key_sequence( - // try to write without destination - &mut AppBuilder::new().build()?, - Some("itest<esc>:x<ret>"), - None, - false, - ) - .await?; - test_key_sequence( - // try to write with path succeeds - &mut AppBuilder::new().build()?, - Some(format!("iMicCheck<esc>:x {}<ret>", file.path().to_string_lossy()).as_ref()), - Some(&|app| { - assert!(!app.editor.is_err()); - }), - true, - ) - .await?; - - helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("MicCheck"))?; - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] async fn test_write_quit_fail() -> anyhow::Result<()> { let file = helpers::new_readonly_tempfile()?; let mut app = helpers::AppBuilder::new() @@ -118,7 +23,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> { assert_eq!(1, docs.len()); let doc = docs.pop().unwrap(); - assert_eq!(Some(&path::normalize(file.path())), doc.path()); + assert_eq!(Some(&get_normalized_path(file.path())), doc.path()); assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); }), false, @@ -188,10 +93,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> { ) .await?; - helpers::assert_file_has_content( - &mut file, - &LineFeedHandling::Native.apply(&RANGE.end().to_string()), - )?; + helpers::assert_file_has_content(file.as_file_mut(), &platform_line(&RANGE.end().to_string()))?; Ok(()) } @@ -211,12 +113,14 @@ async fn test_write() -> anyhow::Result<()> { ) .await?; - reload_file(&mut file).unwrap(); + file.as_file_mut().flush()?; + file.as_file_mut().sync_all()?; + let mut file_content = String::new(); file.as_file_mut().read_to_string(&mut file_content)?; assert_eq!( - LineFeedHandling::Native.apply("the gostak distims the doshes"), + helpers::platform_line("the gostak distims the doshes"), file_content ); @@ -233,18 +137,24 @@ async fn test_overwrite_protection() -> anyhow::Result<()> { helpers::run_event_loop_until_idle(&mut app).await; file.as_file_mut() - .write_all("extremely important content".as_bytes())?; + .write_all(helpers::platform_line("extremely important content").as_bytes())?; file.as_file_mut().flush()?; file.as_file_mut().sync_all()?; - test_key_sequence(&mut app, Some("iOverwriteData<esc>:x<ret>"), None, false).await?; + test_key_sequence(&mut app, Some(":x<ret>"), None, false).await?; + + file.as_file_mut().flush()?; + file.as_file_mut().sync_all()?; - reload_file(&mut file).unwrap(); + file.rewind()?; let mut file_content = String::new(); - file.read_to_string(&mut file_content)?; + file.as_file_mut().read_to_string(&mut file_content)?; - assert_eq!("extremely important content", file_content); + assert_eq!( + helpers::platform_line("extremely important content"), + file_content + ); Ok(()) } @@ -264,13 +174,14 @@ async fn test_write_quit() -> anyhow::Result<()> { ) .await?; - reload_file(&mut file).unwrap(); + file.as_file_mut().flush()?; + file.as_file_mut().sync_all()?; let mut file_content = String::new(); - file.read_to_string(&mut file_content)?; + file.as_file_mut().read_to_string(&mut file_content)?; assert_eq!( - LineFeedHandling::Native.apply("the gostak distims the doshes"), + helpers::platform_line("the gostak distims the doshes"), file_content ); @@ -293,13 +204,12 @@ async fn test_write_concurrent() -> anyhow::Result<()> { test_key_sequence(&mut app, Some(&command), None, false).await?; - reload_file(&mut file).unwrap(); + file.as_file_mut().flush()?; + file.as_file_mut().sync_all()?; + let mut file_content = String::new(); - file.read_to_string(&mut file_content)?; - assert_eq!( - LineFeedHandling::Native.apply(&RANGE.end().to_string()), - file_content - ); + file.as_file_mut().read_to_string(&mut file_content)?; + assert_eq!(platform_line(&RANGE.end().to_string()), file_content); Ok(()) } @@ -359,13 +269,13 @@ async fn test_write_scratch_to_new_path() -> anyhow::Result<()> { assert_eq!(1, docs.len()); let doc = docs.pop().unwrap(); - assert_eq!(Some(&path::normalize(file.path())), doc.path()); + assert_eq!(Some(&get_normalized_path(file.path())), doc.path()); }), false, ) .await?; - helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("hello"))?; + helpers::assert_file_has_content(file.as_file_mut(), &helpers::platform_line("hello"))?; Ok(()) } @@ -404,13 +314,13 @@ async fn test_write_auto_format_fails_still_writes() -> anyhow::Result<()> { let mut app = helpers::AppBuilder::new() .with_file(file.path(), None) .with_input_text("#[l|]#et foo = 0;\n") - .with_lang_loader(helpers::test_syntax_loader(Some(lang_conf.into()))) + .with_lang_config(helpers::test_syntax_conf(Some(lang_conf.into()))) .build()?; test_key_sequences(&mut app, vec![(Some(":w<ret>"), None)], false).await?; // file still saves - helpers::assert_file_has_content(&mut file, "let foo = 0;\n")?; + helpers::assert_file_has_content(file.as_file_mut(), "let foo = 0;\n")?; Ok(()) } @@ -431,7 +341,7 @@ async fn test_write_new_path() -> anyhow::Result<()> { Some(&|app| { let doc = doc!(app.editor); assert!(!app.editor.is_err()); - assert_eq!(&path::normalize(file1.path()), doc.path().unwrap()); + assert_eq!(&get_normalized_path(file1.path()), doc.path().unwrap()); }), ), ( @@ -439,7 +349,7 @@ async fn test_write_new_path() -> anyhow::Result<()> { Some(&|app| { let doc = doc!(app.editor); assert!(!app.editor.is_err()); - assert_eq!(&path::normalize(file2.path()), doc.path().unwrap()); + assert_eq!(&get_normalized_path(file2.path()), doc.path().unwrap()); assert!(app.editor.document_by_path(file1.path()).is_none()); }), ), @@ -449,13 +359,13 @@ async fn test_write_new_path() -> anyhow::Result<()> { .await?; helpers::assert_file_has_content( - &mut file1, - &LineFeedHandling::Native.apply("i can eat glass, it will not hurt me\n"), + file1.as_file_mut(), + &helpers::platform_line("i can eat glass, it will not hurt me\n"), )?; helpers::assert_file_has_content( - &mut file2, - &LineFeedHandling::Native.apply("i can eat glass, it will not hurt me\n"), + file2.as_file_mut(), + &helpers::platform_line("i can eat glass, it will not hurt me\n"), )?; Ok(()) @@ -515,50 +425,6 @@ async fn test_write_utf_bom_file() -> anyhow::Result<()> { } #[tokio::test(flavor = "multi_thread")] -async fn test_write_trim_trailing_whitespace() -> anyhow::Result<()> { - let mut file = tempfile::NamedTempFile::new()?; - let mut app = helpers::AppBuilder::new() - .with_config(Config { - editor: helix_view::editor::Config { - trim_trailing_whitespace: true, - ..Default::default() - }, - ..Default::default() - }) - .with_file(file.path(), None) - .with_input_text(LineFeedHandling::Native.apply("#[f|]#oo \n\n \nbar ")) - .build()?; - - test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; - - helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("foo\n\n\nbar"))?; - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_write_trim_final_newlines() -> anyhow::Result<()> { - let mut file = tempfile::NamedTempFile::new()?; - let mut app = helpers::AppBuilder::new() - .with_config(Config { - editor: helix_view::editor::Config { - trim_final_newlines: true, - ..Default::default() - }, - ..Default::default() - }) - .with_file(file.path(), None) - .with_input_text(LineFeedHandling::Native.apply("#[f|]#oo\n \n\n\n")) - .build()?; - - test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; - - helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("foo\n \n"))?; - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] async fn test_write_insert_final_newline_added_if_missing() -> anyhow::Result<()> { let mut file = tempfile::NamedTempFile::new()?; let mut app = helpers::AppBuilder::new() @@ -569,41 +435,26 @@ async fn test_write_insert_final_newline_added_if_missing() -> anyhow::Result<() test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; helpers::assert_file_has_content( - &mut file, - &LineFeedHandling::Native.apply("have you tried chamomile tea?\n"), + file.as_file_mut(), + &helpers::platform_line("have you tried chamomile tea?\n"), )?; Ok(()) } #[tokio::test(flavor = "multi_thread")] -async fn test_write_insert_final_newline_unchanged_if_empty() -> anyhow::Result<()> { - let mut file = tempfile::NamedTempFile::new()?; - let mut app = helpers::AppBuilder::new() - .with_file(file.path(), None) - .with_input_text("#[|]#") - .build()?; - - test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; - - helpers::assert_file_has_content(&mut file, "")?; - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] async fn test_write_insert_final_newline_unchanged_if_not_missing() -> anyhow::Result<()> { let mut file = tempfile::NamedTempFile::new()?; let mut app = helpers::AppBuilder::new() .with_file(file.path(), None) - .with_input_text(LineFeedHandling::Native.apply("#[t|]#en minutes, please\n")) + .with_input_text(&helpers::platform_line("#[t|]#en minutes, please\n")) .build()?; test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; helpers::assert_file_has_content( - &mut file, - &LineFeedHandling::Native.apply("ten minutes, please\n"), + file.as_file_mut(), + &helpers::platform_line("ten minutes, please\n"), )?; Ok(()) @@ -626,8 +477,10 @@ async fn test_write_insert_final_newline_unchanged_if_missing_and_false() -> any test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; - reload_file(&mut file).unwrap(); - helpers::assert_file_has_content(&mut file, "the quiet rain continued through the night")?; + helpers::assert_file_has_content( + file.as_file_mut(), + "the quiet rain continued through the night", + )?; Ok(()) } @@ -653,13 +506,13 @@ async fn test_write_all_insert_final_newline_add_if_missing_and_modified() -> an .await?; helpers::assert_file_has_content( - &mut file1, - &LineFeedHandling::Native.apply("we don't serve time travelers here\n"), + file1.as_file_mut(), + &helpers::platform_line("we don't serve time travelers here\n"), )?; helpers::assert_file_has_content( - &mut file2, - &LineFeedHandling::Native.apply("a time traveler walks into a bar\n"), + file2.as_file_mut(), + &helpers::platform_line("a time traveler walks into a bar\n"), )?; Ok(()) @@ -677,162 +530,7 @@ async fn test_write_all_insert_final_newline_do_not_add_if_unmodified() -> anyho test_key_sequence(&mut app, Some(":wa<ret>"), None, false).await?; - helpers::assert_file_has_content(&mut file, "i lost on Jeopardy!")?; - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_symlink_write() -> anyhow::Result<()> { - #[cfg(unix)] - use std::os::unix::fs::symlink; - #[cfg(not(unix))] - use std::os::windows::fs::symlink_file as symlink; - - let dir = tempfile::tempdir()?; - - let mut file = tempfile::NamedTempFile::new_in(&dir)?; - let symlink_path = dir.path().join("linked"); - symlink(file.path(), &symlink_path)?; - - let mut app = helpers::AppBuilder::new() - .with_file(&symlink_path, None) - .build()?; - - test_key_sequence( - &mut app, - Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), - None, - false, - ) - .await?; - - reload_file(&mut file).unwrap(); - let mut file_content = String::new(); - file.as_file_mut().read_to_string(&mut file_content)?; - - assert_eq!( - LineFeedHandling::Native.apply("the gostak distims the doshes"), - file_content - ); - assert!(symlink_path.is_symlink()); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_symlink_write_fail() -> anyhow::Result<()> { - #[cfg(unix)] - use std::os::unix::fs::symlink; - #[cfg(not(unix))] - use std::os::windows::fs::symlink_file as symlink; - - let dir = tempfile::tempdir()?; - - let file = helpers::new_readonly_tempfile_in_dir(&dir)?; - let symlink_path = dir.path().join("linked"); - symlink(file.path(), &symlink_path)?; - - let mut app = helpers::AppBuilder::new() - .with_file(&symlink_path, None) - .build()?; - - test_key_sequence( - &mut app, - Some("ihello<esc>:wq<ret>"), - Some(&|app| { - let mut docs: Vec<_> = app.editor.documents().collect(); - assert_eq!(1, docs.len()); - - let doc = docs.pop().unwrap(); - assert_eq!(Some(&path::normalize(&symlink_path)), doc.path()); - assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); - }), - false, - ) - .await?; - - assert!(symlink_path.is_symlink()); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_symlink_write_relative() -> anyhow::Result<()> { - #[cfg(unix)] - use std::os::unix::fs::symlink; - #[cfg(not(unix))] - use std::os::windows::fs::symlink_file as symlink; - - // tempdir - // |- - b - // | |- file - // |- linked (symlink to file) - let dir = tempfile::tempdir()?; - let inner_dir = dir.path().join("b"); - std::fs::create_dir(&inner_dir)?; - - let mut file = tempfile::NamedTempFile::new_in(&inner_dir)?; - let symlink_path = dir.path().join("linked"); - let relative_path = std::path::PathBuf::from("b").join(file.path().file_name().unwrap()); - symlink(relative_path, &symlink_path)?; - - let mut app = helpers::AppBuilder::new() - .with_file(&symlink_path, None) - .build()?; - - test_key_sequence( - &mut app, - Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), - None, - false, - ) - .await?; - - reload_file(&mut file).unwrap(); - let mut file_content = String::new(); - file.as_file_mut().read_to_string(&mut file_content)?; - - assert_eq!( - LineFeedHandling::Native.apply("the gostak distims the doshes"), - file_content - ); - assert!(symlink_path.is_symlink()); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -#[cfg(not(target_os = "android"))] -async fn test_hardlink_write() -> anyhow::Result<()> { - let dir = tempfile::tempdir()?; - - let mut file = tempfile::NamedTempFile::new_in(&dir)?; - let hardlink_path = dir.path().join("linked"); - std::fs::hard_link(file.path(), &hardlink_path)?; - - let mut app = helpers::AppBuilder::new() - .with_file(&hardlink_path, None) - .build()?; - - test_key_sequence( - &mut app, - Some("ithe gostak distims the doshes<ret><esc>:w<ret>"), - None, - false, - ) - .await?; - - reload_file(&mut file).unwrap(); - let mut file_content = String::new(); - file.as_file_mut().read_to_string(&mut file_content)?; - - assert_eq!( - LineFeedHandling::Native.apply("the gostak distims the doshes"), - file_content - ); - assert!(helix_stdx::faccess::hardlink_count(&hardlink_path)? > 1); - assert!(same_file::is_same_file(file.path(), &hardlink_path)?); + helpers::assert_file_has_content(file.as_file_mut(), "i lost on Jeopardy!")?; Ok(()) } @@ -858,7 +556,7 @@ async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> { ) .await?; - reload_file(&mut file).unwrap(); + file.rewind()?; let mut new_file_content: Vec<u8> = Vec::new(); file.read_to_end(&mut new_file_content)?; @@ -866,70 +564,3 @@ async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> { Ok(()) } - -#[tokio::test(flavor = "multi_thread")] -async fn test_move_file_when_given_dir_and_filename() -> anyhow::Result<()> { - let dir = tempfile::tempdir()?; - let source_file = tempfile::NamedTempFile::new_in(&dir)?; - let target_file = dir.path().join("new_name.ext"); - - let mut app = helpers::AppBuilder::new() - .with_file(source_file.path(), None) - .build()?; - - test_key_sequence( - &mut app, - Some(format!(":move {}<ret>", target_file.to_string_lossy()).as_ref()), - None, - false, - ) - .await?; - - assert!( - target_file.is_file(), - "target file '{}' should have been created", - target_file.display() - ); - assert!( - !source_file.path().exists(), - "Source file '{}' should have been removed", - source_file.path().display() - ); - - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn test_move_file_when_given_dir_only() -> anyhow::Result<()> { - let source_dir = tempfile::tempdir()?; - let target_dir = tempfile::tempdir()?; - let source_file = source_dir.path().join("file.ext"); - std::fs::File::create(&source_file)?; - - let mut app = helpers::AppBuilder::new() - .with_file(&source_file, None) - .build()?; - - test_key_sequence( - &mut app, - Some(format!(":move {}<ret>", target_dir.path().to_string_lossy()).as_ref()), - None, - false, - ) - .await?; - - let target_file = target_dir.path().join("file.ext"); - - assert!( - target_file.is_file(), - "target file '{}' should have been created", - target_file.display() - ); - assert!( - !source_file.exists(), - "Source file '{}' should have been removed", - source_file.display() - ); - - Ok(()) -} |