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 +++++++++++++++++++++++++++++++- helix-term/tests/test/helpers.rs | 2 +- helix-term/tests/test/splits.rs | 6 +- 3 files changed, 126 insertions(+), 7 deletions(-) (limited to 'helix-term/tests') 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, diff --git a/helix-term/tests/test/helpers.rs b/helix-term/tests/test/helpers.rs index 6466bc76..e6762baf 100644 --- a/helix-term/tests/test/helpers.rs +++ b/helix-term/tests/test/helpers.rs @@ -350,7 +350,7 @@ pub fn assert_file_has_content(file: &mut File, content: &str) -> anyhow::Result let mut file_content = String::new(); file.read_to_string(&mut file_content)?; - assert_eq!(content, file_content); + assert_eq!(file_content, content); Ok(()) } diff --git a/helix-term/tests/test/splits.rs b/helix-term/tests/test/splits.rs index 1d70f24a..f010c86b 100644 --- a/helix-term/tests/test/splits.rs +++ b/helix-term/tests/test/splits.rs @@ -62,9 +62,9 @@ async fn test_split_write_quit_all() -> anyhow::Result<()> { ) .await?; - helpers::assert_file_has_content(file1.as_file_mut(), "hello1")?; - helpers::assert_file_has_content(file2.as_file_mut(), "hello2")?; - helpers::assert_file_has_content(file3.as_file_mut(), "hello3")?; + helpers::assert_file_has_content(file1.as_file_mut(), &platform_line("hello1"))?; + helpers::assert_file_has_content(file2.as_file_mut(), &platform_line("hello2"))?; + helpers::assert_file_has_content(file3.as_file_mut(), &platform_line("hello3"))?; Ok(()) } -- cgit v1.2.3-70-g09d2