on master help sign in
add test for write-quit happy path
fac36bcSkyler Hawthorne
helix-term/tests/test/commands.rs+3
@@ -25,6 +25,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
25 Some(&|app| {
25 Some(&|app| {
26 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
26 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
27 }),
27 }),
28+ false,
28 )
29 )
29 .await?;
30 .await?;
30
31
@@ -59,6 +60,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
59 }),
60 }),
60 ),
61 ),
61 ],
62 ],
63+ false,
62 )
64 )
63 .await?;
65 .await?;
64
66
@@ -89,6 +91,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
89 let doc = app.editor.document_by_path(file.path());
91 let doc = app.editor.document_by_path(file.path());
90 assert!(doc.is_none(), "found doc: {:?}", doc);
92 assert!(doc.is_none(), "found doc: {:?}", doc);
91 }),
93 }),
94+ false,
92 )
95 )
93 .await?;
96 .await?;
94
97
helix-term/tests/test/helpers.rs+45−18
@@ -37,41 +37,56 @@ pub async fn test_key_sequence(
37 app: &mut Application,
37 app: &mut Application,
38 in_keys: Option<&str>,
38 in_keys: Option<&str>,
39 test_fn: Option<&dyn Fn(&Application)>,
39 test_fn: Option<&dyn Fn(&Application)>,
40+ should_exit: bool,
40) -> anyhow::Result<()> {
41) -> 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
42}
43}
43
44
44#[allow(clippy::type_complexity)]
45#[allow(clippy::type_complexity)]
45pub async fn test_key_sequences(
46pub async fn test_key_sequences(
46 app: &mut Application,
47 app: &mut Application,
47 inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
48 inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
49+ should_exit: bool,
48) -> anyhow::Result<()> {
50) -> anyhow::Result<()> {
49 const TIMEOUT: Duration = Duration::from_millis(500);
51 const TIMEOUT: Duration = Duration::from_millis(500);
50 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
52 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
51 let mut rx_stream = UnboundedReceiverStream::new(rx);
53 let mut rx_stream = UnboundedReceiverStream::new(rx);
54+ let num_inputs = inputs.len();
52
55
53− for (in_keys, test_fn) in inputs {
56+ for (i, (in_keys, test_fn)) in inputs.into_iter().enumerate() {
54 if let Some(in_keys) = in_keys {
57 if let Some(in_keys) = in_keys {
55 for key_event in parse_macro(in_keys)?.into_iter() {
58 for key_event in parse_macro(in_keys)?.into_iter() {
56 tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
59 tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
57 }
60 }
58 }
61 }
59
62
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 {
61 bail!("application exited before test function could run");
67 bail!("application exited before test function could run");
62 }
68 }
63
69
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+
64 if let Some(test) = test_fn {
76 if let Some(test) = test_fn {
65 test(app);
77 test(app);
66 };
78 };
67 }
79 }
68
80
81+ if !should_exit {
69− for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
82+ for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
70− tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
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?;
71 }
88 }
72
89
73− let event_loop = app.event_loop(&mut rx_stream);
74− tokio::time::timeout(TIMEOUT, event_loop).await?;
75 app.close().await?;
90 app.close().await?;
76
91
77 Ok(())
92 Ok(())
@@ -81,6 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
81 app: Option<Application>,
96 app: Option<Application>,
82 test_case: T,
97 test_case: T,
83 test_fn: &dyn Fn(&Application),
98 test_fn: &dyn Fn(&Application),
99+ should_exit: bool,
84) -> anyhow::Result<()> {
100) -> anyhow::Result<()> {
85 let test_case = test_case.into();
101 let test_case = test_case.into();
86 let mut app = match app {
102 let mut app = match app {
@@ -100,7 +116,13 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
100 view.id,
116 view.id,
101 );
117 );
102
118
119+ test_key_sequence(
120+ &mut app,
103− test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn)).await
121+ Some(&test_case.in_keys),
122+ Some(test_fn),
123+ should_exit,
124+ )
125+ .await
104}
126}
105
127
106/// Use this for very simple test cases where there is one input
128/// 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>>(
114 let test_case = test_case.into();
136 let test_case = test_case.into();
115 let app = Application::new(args, config)?;
137 let app = Application::new(args, config)?;
116
138
117− test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
139+ test_key_sequence_with_input_text(
140+ Some(app),
141+ test_case.clone(),
142+ &|app| {
118− let doc = doc!(app.editor);
143+ let doc = doc!(app.editor);
119− assert_eq!(&test_case.out_text, doc.text());
144+ assert_eq!(&test_case.out_text, doc.text());
120−
145+
121− let mut selections: Vec<_> = doc.selections().values().cloned().collect();
146+ let mut selections: Vec<_> = doc.selections().values().cloned().collect();
122− assert_eq!(1, selections.len());
147+ assert_eq!(1, selections.len());
123−
148+
124− let sel = selections.pop().unwrap();
149+ let sel = selections.pop().unwrap();
125− assert_eq!(test_case.out_selection, sel);
150+ assert_eq!(test_case.out_selection, sel);
151+ },
152+ false,
126− })
153+ )
127 .await
154 .await
128}
155}
129
156
helix-term/tests/test/write.rs+37
@@ -23,6 +23,7 @@ async fn test_write() -> anyhow::Result<()> {
23 )?,
23 )?,
24 Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
24 Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
25 None,
25 None,
26+ false,
26 )
27 )
27 .await?;
28 .await?;
28
29
@@ -31,6 +32,39 @@ async fn test_write() -> anyhow::Result<()> {
31
32
32 let mut file_content = String::new();
33 let mut file_content = String::new();
33 file.as_file_mut().read_to_string(&mut file_content)?;
34 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+
34 assert_eq!(
68 assert_eq!(
35 helpers::platform_line("i can eat glass, it will not hurt me"),
69 helpers::platform_line("i can eat glass, it will not hurt me"),
36 file_content
70 file_content
@@ -61,6 +95,7 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
61 )?,
95 )?,
62 Some(&command),
96 Some(&command),
63 None,
97 None,
98+ false,
64 )
99 )
65 .await?;
100 .await?;
66
101
@@ -112,6 +147,7 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
112 }),
147 }),
113 ),
148 ),
114 ],
149 ],
150+ false,
115 )
151 )
116 .await?;
152 .await?;
117
153
@@ -149,6 +185,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
149 }),
185 }),
150 ),
186 ),
151 ],
187 ],
188+ false,
152 )
189 )
153 .await?;
190 .await?;
154
191