From 32c25dff8cd4b33236aa0ddd00a6bfbd4a5dc5e8 Mon Sep 17 00:00:00 2001 From: Yevgeny Papazyan Date: Fri, 19 Jul 2024 04:32:13 +0300 Subject: [PATCH] Add integration tests for `:reload`, `:reload-all!`, etc. --- helix-term/tests/test/commands/write.rs | 110 ++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/helix-term/tests/test/commands/write.rs b/helix-term/tests/test/commands/write.rs index aba101e9f..a08c8e8e6 100644 --- a/helix-term/tests/test/commands/write.rs +++ b/helix-term/tests/test/commands/write.rs @@ -684,6 +684,116 @@ async fn test_hardlink_write() -> anyhow::Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread")] +async fn test_reload_no_force() -> anyhow::Result<()> { + let mut file = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_file(file.path(), None) + .with_input_text("hello#[ |]#") + .build()?; + + test_key_sequences( + &mut app, + vec![ + (Some("athere"), None), + ( + Some(":reload"), + Some(&|app| { + assert!(app.editor.is_err()); + + let doc = app.editor.documents().next().unwrap(); + assert!(doc.is_modified()); + assert_eq!(doc.text(), &LineFeedHandling::Native.apply("hello there")); + }), + ), + ], + false, + ) + .await?; + + helpers::assert_file_has_content(&mut file, "")?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_reload_all_no_force() -> anyhow::Result<()> { + let file1 = tempfile::NamedTempFile::new()?; + let mut file2 = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_file(file1.path(), None) + .with_file(file2.path(), None) + .with_input_text("#[c|]#hange1") + .build()?; + + file2.as_file_mut().write_all(b"change2")?; + + test_key_sequence( + &mut app, + Some(":reload-all"), + Some(&|app| { + assert!(app.editor.is_err()); + + let (mut doc1_visited, mut doc2_visited) = (false, false); + for doc in app.editor.documents() { + if doc.path().unwrap() == file1.path() { + assert!(doc.is_modified()); + assert_eq!(doc.text(), "change1"); + doc1_visited = true; + } else if doc.path().unwrap() == file2.path() { + assert!(!doc.is_modified()); + assert_eq!(doc.text(), "change2"); + doc2_visited = true; + } + } + assert!(app.editor.documents().count() == 2 && doc1_visited && doc2_visited); + }), + false, + ) + .await?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_reload_all_force() -> anyhow::Result<()> { + let file1 = tempfile::NamedTempFile::new()?; + let mut file2 = tempfile::NamedTempFile::new()?; + let mut app = helpers::AppBuilder::new() + .with_file(file1.path(), None) + .with_file(file2.path(), None) + .with_input_text("#[c|]#hange1") + .build()?; + + file2.as_file_mut().write_all(b"change2")?; + + test_key_sequence( + &mut app, + Some(":reload-all!"), + Some(&|app| { + assert!(!app.editor.is_err()); + + let (mut doc1_visited, mut doc2_visited) = (false, false); + for doc in app.editor.documents() { + if doc.path().unwrap() == file1.path() { + assert!(!doc.is_modified()); + assert_eq!(doc.text(), ""); + doc1_visited = true; + } else if doc.path().unwrap() == file2.path() { + assert!(!doc.is_modified()); + assert_eq!(doc.text(), "change2"); + doc2_visited = true; + } + } + assert!(app.editor.documents().count() == 2 && doc1_visited && doc2_visited); + }), + false, + ) + .await?; + + Ok(()) +} + async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> { let mut file = tempfile::NamedTempFile::new()?;