summaryrefslogtreecommitdiff
path: root/helix-term/tests/test/commands/write.rs
diff options
context:
space:
mode:
authorEm Zhan2023-09-12 00:06:25 +0000
committerGitHub2023-09-12 00:06:25 +0000
commit7090555daba6bce37dc6cc900387015a10a1e791 (patch)
tree3b7a16a0621526ccddac5d0cdb39c64ae87c62d9 /helix-term/tests/test/commands/write.rs
parentef23847957463b95a68599efb12a32837d73727b (diff)
Add `insert-final-newline` config option (#8157)
Co-authored-by: Xalfer <64538944+Xalfer@users.noreply.github.com>
Diffstat (limited to 'helix-term/tests/test/commands/write.rs')
-rw-r--r--helix-term/tests/test/commands/write.rs125
1 files changed, 122 insertions, 3 deletions
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<ret>"), 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<ret>"), 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<ret>"), 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 {}<ret>ia time traveler walks into a bar<esc>:wa<ret>",
+ 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<ret>"), 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 {}<ret>:x<ret>", file.path().to_string_lossy())),
None,
true,