on master help sign in
add test for write-quit happy path
commitfac36bc5eab804e823ddef01e50d1e36495c7967 authorSkyler Hawthorne <skyler@dead10ck.com> dateMon 23 May 2022, 00:24 (4 years ago) parent7c0bca1
3 files changed+85−18
helix-term/tests/test/commands.rs+3 helix-term/tests/test/helpers.rs+45−18 helix-term/tests/test/write.rs+37
helix-term/tests/test/commands.rs+3
@@ -25,6 +25,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
2525 Some(&|app| {
2626 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
2727 }),
28+ false,
2829 )
2930 .await?;
3031
@@ -59,6 +60,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
5960 }),
6061 ),
6162 ],
63+ false,
6264 )
6365 .await?;
6466
@@ -89,6 +91,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
8991 let doc = app.editor.document_by_path(file.path());
9092 assert!(doc.is_none(), "found doc: {:?}", doc);
9193 }),
94+ false,
9295 )
9396 .await?;
9497
helix-term/tests/test/helpers.rs+45−18
@@ -37,41 +37,56 @@ pub async fn test_key_sequence(
3737 app: &mut Application,
3838 in_keys: Option<&str>,
3939 test_fn: Option<&dyn Fn(&Application)>,
40+ should_exit: bool,
4041) -> anyhow::Result<()> {
41− test_key_sequences(app, vec![(in_keys, test_fn)]).await
42+ test_key_sequences(app, vec![(in_keys, test_fn)], should_exit).await
4243}
4344
4445#[allow(clippy::type_complexity)]
4546pub async fn test_key_sequences(
4647 app: &mut Application,
4748 inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
49+ should_exit: bool,
4850) -> anyhow::Result<()> {
4951 const TIMEOUT: Duration = Duration::from_millis(500);
5052 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
5153 let mut rx_stream = UnboundedReceiverStream::new(rx);
54+ let num_inputs = inputs.len();
5255
53− for (in_keys, test_fn) in inputs {
56+ for (i, (in_keys, test_fn)) in inputs.into_iter().enumerate() {
5457 if let Some(in_keys) = in_keys {
5558 for key_event in parse_macro(in_keys)?.into_iter() {
5659 tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
5760 }
5861 }
5962
60− if !app.event_loop_until_idle(&mut rx_stream).await {
63+ let app_exited = !app.event_loop_until_idle(&mut rx_stream).await;
64+
65+ // the app should not exit from any test until the last one
66+ if i < num_inputs - 1 && app_exited {
6167 bail!("application exited before test function could run");
6268 }
6369
70+ // verify if it exited on the last iteration if it should have and
71+ // the inverse
72+ if i == num_inputs - 1 && app_exited != should_exit {
73+ bail!("expected app to exit: {} != {}", app_exited, should_exit);
74+ }
75+
6476 if let Some(test) = test_fn {
6577 test(app);
6678 };
6779 }
6880
69− for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
70− tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
81+ if !should_exit {
82+ for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
83+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
84+ }
85+
86+ let event_loop = app.event_loop(&mut rx_stream);
87+ tokio::time::timeout(TIMEOUT, event_loop).await?;
7188 }
7289
73− let event_loop = app.event_loop(&mut rx_stream);
74− tokio::time::timeout(TIMEOUT, event_loop).await?;
7590 app.close().await?;
7691
7792 Ok(())
@@ -81,6 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
8196 app: Option<Application>,
8297 test_case: T,
8398 test_fn: &dyn Fn(&Application),
99+ should_exit: bool,
84100) -> anyhow::Result<()> {
85101 let test_case = test_case.into();
86102 let mut app = match app {
@@ -100,7 +116,13 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
100116 view.id,
101117 );
102118
103− test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn)).await
119+ test_key_sequence(
120+ &mut app,
121+ Some(&test_case.in_keys),
122+ Some(test_fn),
123+ should_exit,
124+ )
125+ .await
104126}
105127
106128/// Use this for very simple test cases where there is one input
@@ -114,16 +136,21 @@ pub async fn test_with_config<T: Into<TestCase>>(
114136 let test_case = test_case.into();
115137 let app = Application::new(args, config)?;
116138
117− test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
118− let doc = doc!(app.editor);
119− assert_eq!(&test_case.out_text, doc.text());
120−
121− let mut selections: Vec<_> = doc.selections().values().cloned().collect();
122− assert_eq!(1, selections.len());
123−
124− let sel = selections.pop().unwrap();
125− assert_eq!(test_case.out_selection, sel);
126− })
139+ test_key_sequence_with_input_text(
140+ Some(app),
141+ test_case.clone(),
142+ &|app| {
143+ let doc = doc!(app.editor);
144+ assert_eq!(&test_case.out_text, doc.text());
145+
146+ let mut selections: Vec<_> = doc.selections().values().cloned().collect();
147+ assert_eq!(1, selections.len());
148+
149+ let sel = selections.pop().unwrap();
150+ assert_eq!(test_case.out_selection, sel);
151+ },
152+ false,
153+ )
127154 .await
128155}
129156
helix-term/tests/test/write.rs+37
@@ -23,6 +23,7 @@ async fn test_write() -> anyhow::Result<()> {
2323 )?,
2424 Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
2525 None,
26+ false,
2627 )
2728 .await?;
2829
@@ -31,6 +32,39 @@ async fn test_write() -> anyhow::Result<()> {
3132
3233 let mut file_content = String::new();
3334 file.as_file_mut().read_to_string(&mut file_content)?;
35+
36+ assert_eq!(
37+ helpers::platform_line("i can eat glass, it will not hurt me"),
38+ file_content
39+ );
40+
41+ Ok(())
42+}
43+
44+#[tokio::test]
45+async fn test_write_quit() -> anyhow::Result<()> {
46+ let mut file = tempfile::NamedTempFile::new()?;
47+
48+ test_key_sequence(
49+ &mut Application::new(
50+ Args {
51+ files: vec![(file.path().to_path_buf(), Position::default())],
52+ ..Default::default()
53+ },
54+ Config::default(),
55+ )?,
56+ Some("ii can eat glass, it will not hurt me<ret><esc>:wq<ret>"),
57+ None,
58+ true,
59+ )
60+ .await?;
61+
62+ file.as_file_mut().flush()?;
63+ file.as_file_mut().sync_all()?;
64+
65+ let mut file_content = String::new();
66+ file.as_file_mut().read_to_string(&mut file_content)?;
67+
3468 assert_eq!(
3569 helpers::platform_line("i can eat glass, it will not hurt me"),
3670 file_content
@@ -61,6 +95,7 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
6195 )?,
6296 Some(&command),
6397 None,
98+ false,
6499 )
65100 .await?;
66101
@@ -112,6 +147,7 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
112147 }),
113148 ),
114149 ],
150+ false,
115151 )
116152 .await?;
117153
@@ -149,6 +185,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
149185 }),
150186 ),
151187 ],
188+ false,
152189 )
153190 .await?;
154191