From 7090555daba6bce37dc6cc900387015a10a1e791 Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Mon, 11 Sep 2023 19:06:25 -0500 Subject: Add `insert-final-newline` config option (#8157) Co-authored-by: Xalfer <64538944+Xalfer@users.noreply.github.com>--- helix-term/tests/test/commands/write.rs | 125 +++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 3 deletions(-) (limited to 'helix-term/tests/test/commands') diff --git a/helix-term/tests/test/commands/write.rs b/helix-term/tests/test/commands/write.rs index f33c8aaf..376ba5e7 100644 --- a/helix-term/tests/test/commands/write.rs +++ b/helix-term/tests/test/commands/write.rs @@ -93,7 +93,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> { ) .await?; - helpers::assert_file_has_content(file.as_file_mut(), &RANGE.end().to_string())?; + helpers::assert_file_has_content(file.as_file_mut(), &platform_line(&RANGE.end().to_string()))?; Ok(()) } @@ -209,7 +209,7 @@ async fn test_write_concurrent() -> anyhow::Result<()> { let mut file_content = String::new(); file.as_file_mut().read_to_string(&mut file_content)?; - assert_eq!(RANGE.end().to_string(), file_content); + assert_eq!(platform_line(&RANGE.end().to_string()), file_content); Ok(()) } @@ -424,13 +424,132 @@ async fn test_write_utf_bom_file() -> anyhow::Result<()> { 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() + .with_file(file.path(), None) + .with_input_text("#[h|]#ave you tried chamomile tea?") + .build()?; + + test_key_sequence(&mut app, Some(":w"), None, false).await?; + + helpers::assert_file_has_content( + 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_not_missing() -> anyhow::Result<()> { + let mut file = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_file(file.path(), None) + .with_input_text(&helpers::platform_line("#[t|]#en minutes, please\n")) + .build()?; + + test_key_sequence(&mut app, Some(":w"), None, false).await?; + + helpers::assert_file_has_content( + file.as_file_mut(), + &helpers::platform_line("ten minutes, please\n"), + )?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_write_insert_final_newline_unchanged_if_missing_and_false() -> anyhow::Result<()> { + let mut file = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_config(Config { + editor: helix_view::editor::Config { + insert_final_newline: false, + ..Default::default() + }, + ..Default::default() + }) + .with_file(file.path(), None) + .with_input_text("#[t|]#he quiet rain continued through the night") + .build()?; + + test_key_sequence(&mut app, Some(":w"), None, false).await?; + + helpers::assert_file_has_content( + file.as_file_mut(), + "the quiet rain continued through the night", + )?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_write_all_insert_final_newline_add_if_missing_and_modified() -> anyhow::Result<()> { + let mut file1 = tempfile::NamedTempFile::new()?; + let mut file2 = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_file(file1.path(), None) + .with_input_text("#[w|]#e don't serve time travelers here") + .build()?; + + test_key_sequence( + &mut app, + Some(&format!( + ":o {}ia time traveler walks into a bar:wa", + file2.path().to_string_lossy() + )), + None, + false, + ) + .await?; + + helpers::assert_file_has_content( + file1.as_file_mut(), + &helpers::platform_line("we don't serve time travelers here\n"), + )?; + + helpers::assert_file_has_content( + file2.as_file_mut(), + &helpers::platform_line("a time traveler walks into a bar\n"), + )?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_write_all_insert_final_newline_do_not_add_if_unmodified() -> anyhow::Result<()> { + let mut file = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_file(file.path(), None) + .build()?; + + file.write_all(b"i lost on Jeopardy!")?; + file.rewind()?; + + test_key_sequence(&mut app, Some(":wa"), None, false).await?; + + helpers::assert_file_has_content(file.as_file_mut(), "i lost on Jeopardy!")?; + + Ok(()) +} + async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> { let mut file = tempfile::NamedTempFile::new()?; file.as_file_mut().write_all(&file_content)?; helpers::test_key_sequence( - &mut helpers::AppBuilder::new().build()?, + &mut helpers::AppBuilder::new() + .with_config(Config { + editor: helix_view::editor::Config { + insert_final_newline: false, + ..Default::default() + }, + ..Default::default() + }) + .build()?, Some(&format!(":o {}:x", file.path().to_string_lossy())), None, true, -- cgit v1.2.3-70-g09d2