From cd88d7b86d6b8e6ce77d38ae4af1cd8aa1bdaa1a Mon Sep 17 00:00:00 2001 From: Alexis Lapierre <128792625+Alexis-Lapierre@users.noreply.github.com> Date: Sun, 23 Apr 2023 21:39:06 +0200 Subject: [PATCH] Integration test UTF8/16 file save --- helix-term/tests/test/commands/write.rs | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/helix-term/tests/test/commands/write.rs b/helix-term/tests/test/commands/write.rs index 26515b7aed642..fc069a0f5cbb0 100644 --- a/helix-term/tests/test/commands/write.rs +++ b/helix-term/tests/test/commands/write.rs @@ -407,3 +407,40 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> { Ok(()) } + +#[tokio::test(flavor = "multi_thread")] +async fn test_write_utf_bom_file() -> anyhow::Result<()> { + // "ABC" with utf8 bom byte + const UTF8_FILE: [u8; 6] = [0xef, 0xbb, 0xbf, b'A', b'B', b'C']; + + const UTF16LE_FILE: [u8; 8] = [0xff, 0xfe, b'A', 0x00, b'B', 0x00, b'C', 0x00]; + const UTF16BE_FILE: [u8; 8] = [0xfe, 0xff, 0x00, b'A', 0x00, b'B', 0x00, b'C']; + + open_and_close_file_content(&UTF8_FILE).await?; + open_and_close_file_content(&UTF16LE_FILE).await?; + open_and_close_file_content(&UTF16BE_FILE).await?; + + Ok(()) +} + +async fn open_and_close_file_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()?, + Some(&format!(":o {}:x", file.path().to_string_lossy())), + None, + true, + ) + .await?; + + file.rewind()?; + let mut new_file_content: Vec = Vec::new(); + file.read_to_end(&mut new_file_content)?; + + assert_eq!(file_content, new_file_content); + + Ok(()) +}