on master help sign in
tests for buffer-close
4012096Skyler Hawthorne
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+
1use helix_core::diagnostic::Severity;
7use helix_core::diagnostic::Severity;
2use helix_term::application::Application;
8use helix_term::application::Application;
3
9
@@ -13,7 +19,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
13 },
19 },
14 Config::default(),
20 Config::default(),
15 )?,
21 )?,
16− "ihello<esc>:wq<ret>",
22+ Some("ihello<esc>:wq<ret>"),
17 Some(&|app| {
23 Some(&|app| {
18 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
24 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
19 }),
25 }),
@@ -23,3 +29,75 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
23
29
24 Ok(())
30 Ok(())
25}
31}
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 {
34#[inline]
34#[inline]
35pub async fn test_key_sequence(
35pub async fn test_key_sequence(
36 app: &mut Application,
36 app: &mut Application,
37− in_keys: &str,
37+ in_keys: Option<&str>,
38 test_fn: Option<&dyn Fn(&Application)>,
38 test_fn: Option<&dyn Fn(&Application)>,
39 timeout: Option<Duration>,
39 timeout: Option<Duration>,
40) -> anyhow::Result<()> {
40) -> anyhow::Result<()> {
@@ -43,7 +43,7 @@ pub async fn test_key_sequence(
43
43
44pub async fn test_key_sequences(
44pub async fn test_key_sequences(
45 app: &mut Application,
45 app: &mut Application,
46− inputs: Vec<(&str, Option<&dyn Fn(&Application)>)>,
46+ inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
47 timeout: Option<Duration>,
47 timeout: Option<Duration>,
48) -> anyhow::Result<()> {
48) -> anyhow::Result<()> {
49 let timeout = timeout.unwrap_or(Duration::from_millis(500));
49 let timeout = timeout.unwrap_or(Duration::from_millis(500));
@@ -51,8 +51,10 @@ pub async fn test_key_sequences(
51 let mut rx_stream = UnboundedReceiverStream::new(rx);
51 let mut rx_stream = UnboundedReceiverStream::new(rx);
52
52
53 for (in_keys, test_fn) in inputs {
53 for (in_keys, test_fn) in inputs {
54+ if let Some(in_keys) = in_keys {
54− for key_event in parse_macro(&in_keys)?.into_iter() {
55+ for key_event in parse_macro(&in_keys)?.into_iter() {
55− tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
56+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
57+ }
56 }
58 }
57
59
58 let event_loop = app.event_loop(&mut rx_stream);
60 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>>(
100 view.id,
102 view.id,
101 );
103 );
102
104
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
104}
106}
105
107
106/// Use this for very simple test cases where there is one input
108/// Use this for very simple test cases where there is one input
helix-term/tests/integration/write.rs+42−6
@@ -1,5 +1,6 @@
1use std::{
1use std::{
2 io::{Read, Write},
2 io::{Read, Write},
3+ ops::RangeInclusive,
3 time::Duration,
4 time::Duration,
4};
5};
5
6
@@ -21,7 +22,7 @@ async fn test_write() -> anyhow::Result<()> {
21 },
22 },
22 Config::default(),
23 Config::default(),
23 )?,
24 )?,
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>"),
25 None,
26 None,
26 Some(Duration::from_millis(1000)),
27 Some(Duration::from_millis(1000)),
27 )
28 )
@@ -37,6 +38,41 @@ async fn test_write() -> anyhow::Result<()> {
37 Ok(())
38 Ok(())
38}
39}
39
40
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+
40#[tokio::test]
76#[tokio::test]
41async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
77async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
42 test_key_sequences(
78 test_key_sequences(
@@ -49,21 +85,21 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
49 )?,
85 )?,
50 vec![
86 vec![
51 (
87 (
52− "",
88+ None,
53 Some(&|app| {
89 Some(&|app| {
54 let doc = doc!(app.editor);
90 let doc = doc!(app.editor);
55 assert!(!doc.is_modified());
91 assert!(!doc.is_modified());
56 }),
92 }),
57 ),
93 ),
58 (
94 (
59− "ihello<esc>",
95+ Some("ihello<esc>"),
60 Some(&|app| {
96 Some(&|app| {
61 let doc = doc!(app.editor);
97 let doc = doc!(app.editor);
62 assert!(doc.is_modified());
98 assert!(doc.is_modified());
63 }),
99 }),
64 ),
100 ),
65 (
101 (
66− ":w<ret>",
102+ Some(":w<ret>"),
67 Some(&|app| {
103 Some(&|app| {
68 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
104 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
69
105
@@ -86,7 +122,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
86 &mut Application::new(Args::default(), Config::default())?,
122 &mut Application::new(Args::default(), Config::default())?,
87 vec![
123 vec![
88 (
124 (
89− "",
125+ None,
90 Some(&|app| {
126 Some(&|app| {
91 let doc = doc!(app.editor);
127 let doc = doc!(app.editor);
92 assert_eq!(None, app.editor.get_status());
128 assert_eq!(None, app.editor.get_status());
@@ -94,7 +130,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
94 }),
130 }),
95 ),
131 ),
96 (
132 (
97− ":w /foo<ret>",
133+ Some(":w /foo<ret>"),
98 Some(&|app| {
134 Some(&|app| {
99 let doc = doc!(app.editor);
135 let doc = doc!(app.editor);
100 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
136 assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
helix-view/src/editor.rs+9
@@ -576,6 +576,15 @@ impl Editor {
576 self.status_msg.as_ref().map(|(status, sev)| (status, sev))
576 self.status_msg.as_ref().map(|(status, sev)| (status, sev))
577 }
577 }
578
578
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+
579 pub fn set_theme(&mut self, theme: Theme) {
588 pub fn set_theme(&mut self, theme: Theme) {
580 // `ui.selection` is the only scope required to be able to render a theme.
589 // `ui.selection` is the only scope required to be able to render a theme.
581 if theme.find_scope_index("ui.selection").is_none() {
590 if theme.find_scope_index("ui.selection").is_none() {