on master help sign in
tests for buffer-close
commit40120967e9ba48d2e8b5fb4976a6ca1ce8993704 authorSkyler Hawthorne <skyler@dead10ck.com> dateSat 30 Apr 2022, 20:47 (4 years ago) parent07fc80a
4 files changed+137−12
helix-term/tests/integration/commands.rs+79−1 helix-term/tests/integration/helpers.rs+7−5 helix-term/tests/integration/write.rs+42−6 helix-view/src/editor.rs+9
helix-term/tests/integration/commands.rs+79−1
@@ -1,3 +1,9 @@
1+use std::{
2+ io::{Read, Write},
3+ ops::RangeInclusive,
4+ time::Duration,
5+};
6+
17use helix_core::diagnostic::Severity;
28use helix_term::application::Application;
39
@@ -13,7 +19,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
1319 },
1420 Config::default(),
1521 )?,
16− "ihello<esc>:wq<ret>",
22+ Some("ihello<esc>:wq<ret>"),
1723 Some(&|app| {
1824 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
1925 }),
@@ -23,3 +29,75 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
2329
2430 Ok(())
2531}
32+
33+#[tokio::test]
34+async fn test_buffer_close() -> anyhow::Result<()> {
35+ test_key_sequences(
36+ &mut Application::new(Args::default(), Config::default())?,
37+ vec![
38+ (
39+ None,
40+ Some(&|app| {
41+ assert_eq!(1, app.editor.documents().count());
42+ assert!(!app.editor.is_err());
43+ }),
44+ ),
45+ (
46+ Some("ihello<esc>:new<ret>"),
47+ Some(&|app| {
48+ assert_eq!(2, app.editor.documents().count());
49+ assert!(!app.editor.is_err());
50+ }),
51+ ),
52+ (
53+ Some(":buffer<minus>close<ret>"),
54+ Some(&|app| {
55+ assert_eq!(1, app.editor.documents().count());
56+ assert!(!app.editor.is_err());
57+ }),
58+ ),
59+ ],
60+ None,
61+ )
62+ .await?;
63+
64+ // verify if writes are queued up, it finishes them before closing the buffer
65+ let mut file = tempfile::NamedTempFile::new().unwrap();
66+ let mut command = String::new();
67+ const RANGE: RangeInclusive<i32> = 1..=10;
68+
69+ for i in RANGE {
70+ let cmd = format!("%c{}<esc>:w<ret>", i);
71+ command.push_str(&cmd);
72+ }
73+
74+ command.push_str(":buffer<minus>close<ret>");
75+
76+ test_key_sequence(
77+ &mut Application::new(
78+ Args {
79+ files: vec![(file.path().to_path_buf(), Position::default())],
80+ ..Default::default()
81+ },
82+ Config::default(),
83+ )?,
84+ Some(&command),
85+ Some(&|app| {
86+ assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status());
87+
88+ let doc = app.editor.document_by_path(file.path());
89+ assert!(doc.is_none(), "found doc: {:?}", doc);
90+ }),
91+ Some(Duration::from_millis(5000)),
92+ )
93+ .await?;
94+
95+ file.as_file_mut().flush()?;
96+ file.as_file_mut().sync_all()?;
97+
98+ let mut file_content = String::new();
99+ file.as_file_mut().read_to_string(&mut file_content)?;
100+ assert_eq!(RANGE.end().to_string(), file_content);
101+
102+ Ok(())
103+}
helix-term/tests/integration/helpers.rs+7−5
@@ -34,7 +34,7 @@ impl<S: Into<String>> From<(S, S, S)> for TestCase {
3434#[inline]
3535pub async fn test_key_sequence(
3636 app: &mut Application,
37− in_keys: &str,
37+ in_keys: Option<&str>,
3838 test_fn: Option<&dyn Fn(&Application)>,
3939 timeout: Option<Duration>,
4040) -> anyhow::Result<()> {
@@ -43,7 +43,7 @@ pub async fn test_key_sequence(
4343
4444pub async fn test_key_sequences(
4545 app: &mut Application,
46− inputs: Vec<(&str, Option<&dyn Fn(&Application)>)>,
46+ inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
4747 timeout: Option<Duration>,
4848) -> anyhow::Result<()> {
4949 let timeout = timeout.unwrap_or(Duration::from_millis(500));
@@ -51,8 +51,10 @@ pub async fn test_key_sequences(
5151 let mut rx_stream = UnboundedReceiverStream::new(rx);
5252
5353 for (in_keys, test_fn) in inputs {
54− for key_event in parse_macro(&in_keys)?.into_iter() {
55− tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
54+ if let Some(in_keys) = in_keys {
55+ for key_event in parse_macro(&in_keys)?.into_iter() {
56+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
57+ }
5658 }
5759
5860 let event_loop = app.event_loop(&mut rx_stream);
@@ -100,7 +102,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
100102 view.id,
101103 );
102104
103− test_key_sequence(&mut app, &test_case.in_keys, Some(test_fn), timeout).await
105+ test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn), timeout).await
104106}
105107
106108/// Use this for very simple test cases where there is one input
helix-term/tests/integration/write.rs+42−6
@@ -1,5 +1,6 @@
11use std::{
22 io::{Read, Write},
3+ ops::RangeInclusive,
34 time::Duration,
45};
56
@@ -21,7 +22,7 @@ async fn test_write() -> anyhow::Result<()> {
2122 },
2223 Config::default(),
2324 )?,
24− "ii can eat glass, it will not hurt me<ret><esc>:w<ret>",
25+ Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
2526 None,
2627 Some(Duration::from_millis(1000)),
2728 )
@@ -37,6 +38,41 @@ async fn test_write() -> anyhow::Result<()> {
3738 Ok(())
3839}
3940
41+#[tokio::test]
42+async fn test_write_concurrent() -> anyhow::Result<()> {
43+ let mut file = tempfile::NamedTempFile::new().unwrap();
44+ let mut command = String::new();
45+ const RANGE: RangeInclusive<i32> = 1..=5000;
46+
47+ for i in RANGE {
48+ let cmd = format!("%c{}<esc>:w<ret>", i);
49+ command.push_str(&cmd);
50+ }
51+
52+ test_key_sequence(
53+ &mut Application::new(
54+ Args {
55+ files: vec![(file.path().to_path_buf(), Position::default())],
56+ ..Default::default()
57+ },
58+ Config::default(),
59+ )?,
60+ Some(&command),
61+ None,
62+ Some(Duration::from_millis(10000)),
63+ )
64+ .await?;
65+
66+ file.as_file_mut().flush()?;
67+ file.as_file_mut().sync_all()?;
68+
69+ let mut file_content = String::new();
70+ file.as_file_mut().read_to_string(&mut file_content)?;
71+ assert_eq!(RANGE.end().to_string(), file_content);
72+
73+ Ok(())
74+}
75+
4076#[tokio::test]
4177async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
4278 test_key_sequences(
@@ -49,21 +85,21 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
4985 )?,
5086 vec![
5187 (
52− "",
88+ None,
5389 Some(&|app| {
5490 let doc = doc!(app.editor);
5591 assert!(!doc.is_modified());
5692 }),
5793 ),
5894 (
59− "ihello<esc>",
95+ Some("ihello<esc>"),
6096 Some(&|app| {
6197 let doc = doc!(app.editor);
6298 assert!(doc.is_modified());
6399 }),
64100 ),
65101 (
66− ":w<ret>",
102+ Some(":w<ret>"),
67103 Some(&|app| {
68104 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
69105
@@ -86,7 +122,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
86122 &mut Application::new(Args::default(), Config::default())?,
87123 vec![
88124 (
89− "",
125+ None,
90126 Some(&|app| {
91127 let doc = doc!(app.editor);
92128 assert_eq!(None, app.editor.get_status());
@@ -94,7 +130,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
94130 }),
95131 ),
96132 (
97− ":w /foo<ret>",
133+ Some(":w /foo<ret>"),
98134 Some(&|app| {
99135 let doc = doc!(app.editor);
100136 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
helix-view/src/editor.rs+9
@@ -576,6 +576,15 @@ impl Editor {
576576 self.status_msg.as_ref().map(|(status, sev)| (status, sev))
577577 }
578578
579+ /// Returns true if the current status is an error
580+ #[inline]
581+ pub fn is_err(&self) -> bool {
582+ self.status_msg
583+ .as_ref()
584+ .map(|(_, sev)| *sev == Severity::Error)
585+ .unwrap_or(false)
586+ }
587+
579588 pub fn set_theme(&mut self, theme: Theme) {
580589 // `ui.selection` is the only scope required to be able to render a theme.
581590 if theme.find_scope_index("ui.selection").is_none() {