mirror of https://github.com/helix-editor/helix
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
410 lines
11 KiB
Rust
410 lines
11 KiB
Rust
3 years ago
|
use std::{
|
||
2 years ago
|
io::{Read, Seek, Write},
|
||
3 years ago
|
ops::RangeInclusive,
|
||
3 years ago
|
};
|
||
|
|
||
3 years ago
|
use helix_core::diagnostic::Severity;
|
||
|
use helix_view::doc;
|
||
3 years ago
|
|
||
|
use super::*;
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
|
async fn test_write_quit_fail() -> anyhow::Result<()> {
|
||
|
let file = helpers::new_readonly_tempfile()?;
|
||
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
|
|
||
|
test_key_sequence(
|
||
|
&mut app,
|
||
|
Some("ihello<esc>:wq<ret>"),
|
||
|
Some(&|app| {
|
||
|
let mut docs: Vec<_> = app.editor.documents().collect();
|
||
|
assert_eq!(1, docs.len());
|
||
|
|
||
|
let doc = docs.pop().unwrap();
|
||
|
assert_eq!(Some(file.path()), doc.path().map(PathBuf::as_path));
|
||
|
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
|
||
|
}),
|
||
|
false,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[tokio::test(flavor = "multi_thread")]
|
||
|
async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
|
||
|
test_key_sequences(
|
||
|
&mut helpers::AppBuilder::new().build()?,
|
||
|
vec![
|
||
|
(
|
||
|
None,
|
||
|
Some(&|app| {
|
||
|
assert_eq!(1, app.editor.documents().count());
|
||
|
assert!(!app.editor.is_err());
|
||
|
}),
|
||
|
),
|
||
|
(
|
||
|
Some("ihello<esc>:new<ret>"),
|
||
|
Some(&|app| {
|
||
|
assert_eq!(2, app.editor.documents().count());
|
||
|
assert!(!app.editor.is_err());
|
||
|
}),
|
||
|
),
|
||
|
(
|
||
|
Some(":buffer<minus>close<ret>"),
|
||
|
Some(&|app| {
|
||
|
assert_eq!(1, app.editor.documents().count());
|
||
|
assert!(!app.editor.is_err());
|
||
|
}),
|
||
|
),
|
||
|
],
|
||
|
false,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
|
// verify if writes are queued up, it finishes them before closing the buffer
|
||
|
let mut file = tempfile::NamedTempFile::new()?;
|
||
|
let mut command = String::new();
|
||
|
const RANGE: RangeInclusive<i32> = 1..=1000;
|
||
|
|
||
|
for i in RANGE {
|
||
|
let cmd = format!("%c{}<esc>:w!<ret>", i);
|
||
|
command.push_str(&cmd);
|
||
|
}
|
||
|
|
||
|
command.push_str(":buffer<minus>close<ret>");
|
||
|
|
||
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
|
|
||
|
test_key_sequence(
|
||
|
&mut app,
|
||
|
Some(&command),
|
||
|
Some(&|app| {
|
||
|
assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status());
|
||
|
|
||
|
let doc = app.editor.document_by_path(file.path());
|
||
|
assert!(doc.is_none(), "found doc: {:?}", doc);
|
||
|
}),
|
||
|
false,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
|
helpers::assert_file_has_content(file.as_file_mut(), &RANGE.end().to_string())?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
3 years ago
|
async fn test_write() -> anyhow::Result<()> {
|
||
3 years ago
|
let mut file = tempfile::NamedTempFile::new()?;
|
||
2 years ago
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
3 years ago
|
|
||
|
test_key_sequence(
|
||
2 years ago
|
&mut app,
|
||
3 years ago
|
Some("ithe gostak distims the doshes<ret><esc>:w<ret>"),
|
||
3 years ago
|
None,
|
||
3 years ago
|
false,
|
||
3 years ago
|
)
|
||
|
.await?;
|
||
|
|
||
|
file.as_file_mut().flush()?;
|
||
|
file.as_file_mut().sync_all()?;
|
||
|
|
||
|
let mut file_content = String::new();
|
||
|
file.as_file_mut().read_to_string(&mut file_content)?;
|
||
3 years ago
|
|
||
|
assert_eq!(
|
||
3 years ago
|
helpers::platform_line("the gostak distims the doshes"),
|
||
3 years ago
|
file_content
|
||
|
);
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
|
async fn test_overwrite_protection() -> anyhow::Result<()> {
|
||
|
let mut file = tempfile::NamedTempFile::new()?;
|
||
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
|
|
||
|
helpers::run_event_loop_until_idle(&mut app).await;
|
||
|
|
||
|
file.as_file_mut()
|
||
|
.write_all(helpers::platform_line("extremely important content").as_bytes())?;
|
||
|
|
||
|
file.as_file_mut().flush()?;
|
||
|
file.as_file_mut().sync_all()?;
|
||
|
|
||
|
test_key_sequence(&mut app, Some(":x<ret>"), None, false).await?;
|
||
|
|
||
|
file.as_file_mut().flush()?;
|
||
|
file.as_file_mut().sync_all()?;
|
||
|
|
||
2 years ago
|
file.rewind()?;
|
||
2 years ago
|
let mut file_content = String::new();
|
||
|
file.as_file_mut().read_to_string(&mut file_content)?;
|
||
|
|
||
|
assert_eq!(
|
||
|
helpers::platform_line("extremely important content"),
|
||
|
file_content
|
||
|
);
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
3 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
3 years ago
|
async fn test_write_quit() -> anyhow::Result<()> {
|
||
|
let mut file = tempfile::NamedTempFile::new()?;
|
||
2 years ago
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
3 years ago
|
|
||
|
test_key_sequence(
|
||
2 years ago
|
&mut app,
|
||
3 years ago
|
Some("ithe gostak distims the doshes<ret><esc>:wq<ret>"),
|
||
3 years ago
|
None,
|
||
|
true,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
|
file.as_file_mut().flush()?;
|
||
|
file.as_file_mut().sync_all()?;
|
||
|
|
||
|
let mut file_content = String::new();
|
||
|
file.as_file_mut().read_to_string(&mut file_content)?;
|
||
|
|
||
3 years ago
|
assert_eq!(
|
||
3 years ago
|
helpers::platform_line("the gostak distims the doshes"),
|
||
3 years ago
|
file_content
|
||
|
);
|
||
3 years ago
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
3 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
3 years ago
|
async fn test_write_concurrent() -> anyhow::Result<()> {
|
||
3 years ago
|
let mut file = tempfile::NamedTempFile::new()?;
|
||
3 years ago
|
let mut command = String::new();
|
||
2 years ago
|
const RANGE: RangeInclusive<i32> = 1..=1000;
|
||
2 years ago
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
3 years ago
|
|
||
|
for i in RANGE {
|
||
2 years ago
|
let cmd = format!("%c{}<esc>:w!<ret>", i);
|
||
3 years ago
|
command.push_str(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
test_key_sequence(&mut app, Some(&command), None, false).await?;
|
||
3 years ago
|
|
||
|
file.as_file_mut().flush()?;
|
||
|
file.as_file_mut().sync_all()?;
|
||
|
|
||
|
let mut file_content = String::new();
|
||
|
file.as_file_mut().read_to_string(&mut file_content)?;
|
||
|
assert_eq!(RANGE.end().to_string(), file_content);
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
3 years ago
|
async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
|
||
3 years ago
|
let file = helpers::new_readonly_tempfile()?;
|
||
2 years ago
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.build()?;
|
||
3 years ago
|
|
||
3 years ago
|
test_key_sequences(
|
||
2 years ago
|
&mut app,
|
||
3 years ago
|
vec![
|
||
|
(
|
||
3 years ago
|
None,
|
||
3 years ago
|
Some(&|app| {
|
||
|
let doc = doc!(app.editor);
|
||
|
assert!(!doc.is_modified());
|
||
|
}),
|
||
|
),
|
||
|
(
|
||
3 years ago
|
Some("ihello<esc>"),
|
||
3 years ago
|
Some(&|app| {
|
||
|
let doc = doc!(app.editor);
|
||
|
assert!(doc.is_modified());
|
||
|
}),
|
||
|
),
|
||
|
(
|
||
3 years ago
|
Some(":w<ret>"),
|
||
3 years ago
|
Some(&|app| {
|
||
|
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
|
||
|
|
||
|
let doc = doc!(app.editor);
|
||
|
assert!(doc.is_modified());
|
||
|
}),
|
||
|
),
|
||
|
],
|
||
3 years ago
|
false,
|
||
3 years ago
|
)
|
||
|
.await?;
|
||
|
|
||
3 years ago
|
Ok(())
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
2 years ago
|
async fn test_write_scratch_to_new_path() -> anyhow::Result<()> {
|
||
|
let mut file = tempfile::NamedTempFile::new()?;
|
||
|
|
||
|
test_key_sequence(
|
||
2 years ago
|
&mut AppBuilder::new().build()?,
|
||
2 years ago
|
Some(format!("ihello<esc>:w {}<ret>", file.path().to_string_lossy()).as_ref()),
|
||
|
Some(&|app| {
|
||
|
assert!(!app.editor.is_err());
|
||
|
|
||
|
let mut docs: Vec<_> = app.editor.documents().collect();
|
||
|
assert_eq!(1, docs.len());
|
||
|
|
||
|
let doc = docs.pop().unwrap();
|
||
|
assert_eq!(Some(&file.path().to_path_buf()), doc.path());
|
||
|
}),
|
||
|
false,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
|
helpers::assert_file_has_content(file.as_file_mut(), &helpers::platform_line("hello"))?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
2 years ago
|
async fn test_write_scratch_no_path_fails() -> anyhow::Result<()> {
|
||
|
helpers::test_key_sequence_with_input_text(
|
||
|
None,
|
||
|
("#[\n|]#", "ihello<esc>:w<ret>", "hello#[\n|]#"),
|
||
|
&|app| {
|
||
|
assert!(app.editor.is_err());
|
||
|
|
||
|
let mut docs: Vec<_> = app.editor.documents().collect();
|
||
|
assert_eq!(1, docs.len());
|
||
|
|
||
|
let doc = docs.pop().unwrap();
|
||
|
assert_eq!(None, doc.path());
|
||
|
},
|
||
|
false,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
2 years ago
|
async fn test_write_auto_format_fails_still_writes() -> anyhow::Result<()> {
|
||
|
let mut file = tempfile::Builder::new().suffix(".rs").tempfile()?;
|
||
|
|
||
|
let lang_conf = indoc! {r#"
|
||
|
[[language]]
|
||
|
name = "rust"
|
||
|
formatter = { command = "bash", args = [ "-c", "exit 1" ] }
|
||
|
"#};
|
||
|
|
||
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file.path(), None)
|
||
|
.with_input_text("#[l|]#et foo = 0;\n")
|
||
|
.with_lang_config(helpers::test_syntax_conf(Some(lang_conf.into())))
|
||
|
.build()?;
|
||
|
|
||
|
test_key_sequences(&mut app, vec![(Some(":w<ret>"), None)], false).await?;
|
||
|
|
||
|
// file still saves
|
||
|
helpers::assert_file_has_content(file.as_file_mut(), "let foo = 0;\n")?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
3 years ago
|
async fn test_write_new_path() -> anyhow::Result<()> {
|
||
|
let mut file1 = tempfile::NamedTempFile::new().unwrap();
|
||
|
let mut file2 = tempfile::NamedTempFile::new().unwrap();
|
||
2 years ago
|
let mut app = helpers::AppBuilder::new()
|
||
|
.with_file(file1.path(), None)
|
||
|
.build()?;
|
||
3 years ago
|
|
||
|
test_key_sequences(
|
||
2 years ago
|
&mut app,
|
||
3 years ago
|
vec![
|
||
|
(
|
||
|
Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
|
||
|
Some(&|app| {
|
||
|
let doc = doc!(app.editor);
|
||
|
assert!(!app.editor.is_err());
|
||
|
assert_eq!(file1.path(), doc.path().unwrap());
|
||
|
}),
|
||
|
),
|
||
|
(
|
||
|
Some(&format!(":w {}<ret>", file2.path().to_string_lossy())),
|
||
|
Some(&|app| {
|
||
|
let doc = doc!(app.editor);
|
||
|
assert!(!app.editor.is_err());
|
||
|
assert_eq!(file2.path(), doc.path().unwrap());
|
||
|
assert!(app.editor.document_by_path(file1.path()).is_none());
|
||
|
}),
|
||
|
),
|
||
|
],
|
||
|
false,
|
||
|
)
|
||
|
.await?;
|
||
|
|
||
2 years ago
|
helpers::assert_file_has_content(
|
||
|
file1.as_file_mut(),
|
||
|
&helpers::platform_line("i can eat glass, it will not hurt me\n"),
|
||
|
)?;
|
||
3 years ago
|
|
||
2 years ago
|
helpers::assert_file_has_content(
|
||
|
file2.as_file_mut(),
|
||
|
&helpers::platform_line("i can eat glass, it will not hurt me\n"),
|
||
|
)?;
|
||
3 years ago
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
2 years ago
|
#[tokio::test(flavor = "multi_thread")]
|
||
3 years ago
|
async fn test_write_fail_new_path() -> anyhow::Result<()> {
|
||
3 years ago
|
let file = helpers::new_readonly_tempfile()?;
|
||
|
|
||
3 years ago
|
test_key_sequences(
|
||
2 years ago
|
&mut AppBuilder::new().build()?,
|
||
3 years ago
|
vec![
|
||
|
(
|
||
3 years ago
|
None,
|
||
3 years ago
|
Some(&|app| {
|
||
|
let doc = doc!(app.editor);
|
||
3 years ago
|
assert_ne!(
|
||
|
Some(&Severity::Error),
|
||
|
app.editor.get_status().map(|status| status.1)
|
||
|
);
|
||
3 years ago
|
assert_eq!(None, doc.path());
|
||
|
}),
|
||
|
),
|
||
|
(
|
||
3 years ago
|
Some(&format!(":w {}<ret>", file.path().to_string_lossy())),
|
||
3 years ago
|
Some(&|app| {
|
||
|
let doc = doc!(app.editor);
|
||
3 years ago
|
assert_eq!(
|
||
|
Some(&Severity::Error),
|
||
|
app.editor.get_status().map(|status| status.1)
|
||
|
);
|
||
3 years ago
|
assert_eq!(None, doc.path());
|
||
|
}),
|
||
|
),
|
||
|
],
|
||
3 years ago
|
false,
|
||
3 years ago
|
)
|
||
|
.await?;
|
||
3 years ago
|
|
||
|
Ok(())
|
||
|
}
|