mirror of https://github.com/helix-editor/helix
Merge b0c2c567c6
into f6878f62f7
commit
8bca24c90f
|
@ -130,7 +130,7 @@ async fn test_write() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread")]
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
async fn test_overwrite_protection() -> anyhow::Result<()> {
|
async fn test_file_overwrite_protection() -> anyhow::Result<()> {
|
||||||
let mut file = tempfile::NamedTempFile::new()?;
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
let mut app = helpers::AppBuilder::new()
|
let mut app = helpers::AppBuilder::new()
|
||||||
.with_file(file.path(), None)
|
.with_file(file.path(), None)
|
||||||
|
@ -155,6 +155,42 @@ async fn test_overwrite_protection() -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
|
async fn test_directory_overwrite_protection() -> anyhow::Result<()> {
|
||||||
|
let directory = tempfile::TempDir::new()?;
|
||||||
|
assert!(!directory
|
||||||
|
.path()
|
||||||
|
.to_string_lossy()
|
||||||
|
.ends_with(std::path::MAIN_SEPARATOR_STR));
|
||||||
|
|
||||||
|
let mut file = tempfile::NamedTempFile::new_in(directory.path())?;
|
||||||
|
file.as_file_mut()
|
||||||
|
.write_all("extremely important content".as_bytes())?;
|
||||||
|
file.as_file_mut().flush()?;
|
||||||
|
file.as_file_mut().sync_all()?;
|
||||||
|
|
||||||
|
let mut app = helpers::AppBuilder::new()
|
||||||
|
.with_input_text("some text#[|]#")
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
helpers::run_event_loop_until_idle(&mut app).await;
|
||||||
|
|
||||||
|
test_key_sequence(
|
||||||
|
&mut app,
|
||||||
|
Some(&format!(":w {}", directory.path().to_string_lossy())),
|
||||||
|
None,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// reload_file(&mut file).unwrap();
|
||||||
|
// let mut file_content = String::new();
|
||||||
|
let file_content = std::fs::read_to_string(file.path())?;
|
||||||
|
assert_eq!("extremely important content", file_content);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread")]
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
async fn test_write_quit() -> anyhow::Result<()> {
|
async fn test_write_quit() -> anyhow::Result<()> {
|
||||||
let mut file = tempfile::NamedTempFile::new()?;
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
|
|
|
@ -998,9 +998,14 @@ impl Document {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Protect against overwriting changes made externally
|
|
||||||
if !force {
|
if !force {
|
||||||
if let Ok(metadata) = fs::metadata(&path).await {
|
if let Ok(metadata) = fs::metadata(&path).await {
|
||||||
|
// Protect against overwriting entire directories
|
||||||
|
// if `path` doesn't end with a directory separator.
|
||||||
|
if !metadata.is_file() {
|
||||||
|
bail!("path does not point to a regular file, use :w! to overwrite that directory and its contents");
|
||||||
|
}
|
||||||
|
// Protect against overwriting changes made externally
|
||||||
if let Ok(mtime) = metadata.modified() {
|
if let Ok(mtime) = metadata.modified() {
|
||||||
if last_saved_time < mtime {
|
if last_saved_time < mtime {
|
||||||
bail!("file modified by an external process, use :w! to overwrite");
|
bail!("file modified by an external process, use :w! to overwrite");
|
||||||
|
|
Loading…
Reference in New Issue