From acf931709a56e5af0ac101276fcfb3ba45f159f2 Mon Sep 17 00:00:00 2001 From: Skyler Hawthorne Date: Sat, 30 Apr 2022 00:35:00 -0400 Subject: [PATCH] use a read only file to ensure write failure --- helix-term/tests/integration/commands.rs | 4 +++- helix-term/tests/integration/helpers.rs | 12 ++++++++++++ helix-term/tests/integration/write.rs | 18 ++++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/helix-term/tests/integration/commands.rs b/helix-term/tests/integration/commands.rs index 0d2e14fda..1ff7cc90c 100644 --- a/helix-term/tests/integration/commands.rs +++ b/helix-term/tests/integration/commands.rs @@ -10,10 +10,12 @@ use super::*; #[tokio::test] async fn test_write_quit_fail() -> anyhow::Result<()> { + let file = helpers::new_readonly_tempfile()?; + test_key_sequence( &mut Application::new( Args { - files: vec![(PathBuf::from("/foo"), Position::default())], + files: vec![(file.path().to_path_buf(), Position::default())], ..Default::default() }, Config::default(), diff --git a/helix-term/tests/integration/helpers.rs b/helix-term/tests/integration/helpers.rs index 706e1afb0..3fe1934f1 100644 --- a/helix-term/tests/integration/helpers.rs +++ b/helix-term/tests/integration/helpers.rs @@ -5,6 +5,7 @@ use crossterm::event::{Event, KeyEvent}; use helix_core::{test, Selection, Transaction}; use helix_term::{application::Application, args::Args, config::Config}; use helix_view::{doc, input::parse_macro}; +use tempfile::NamedTempFile; use tokio_stream::wrappers::UnboundedReceiverStream; #[derive(Clone, Debug)] @@ -155,3 +156,14 @@ pub fn platform_line(input: &str) -> String { output } + +/// Creates a new temporary file that is set to read only. Useful for +/// testing write failures. +pub fn new_readonly_tempfile() -> anyhow::Result { + let mut file = tempfile::NamedTempFile::new()?; + let metadata = file.as_file().metadata()?; + let mut perms = metadata.permissions(); + perms.set_readonly(true); + file.as_file_mut().set_permissions(perms)?; + Ok(file) +} diff --git a/helix-term/tests/integration/write.rs b/helix-term/tests/integration/write.rs index 06af9dd84..f3abbd91b 100644 --- a/helix-term/tests/integration/write.rs +++ b/helix-term/tests/integration/write.rs @@ -75,10 +75,12 @@ async fn test_write_concurrent() -> anyhow::Result<()> { #[tokio::test] async fn test_write_fail_mod_flag() -> anyhow::Result<()> { + let file = helpers::new_readonly_tempfile()?; + test_key_sequences( &mut Application::new( Args { - files: vec![(PathBuf::from("/foo"), Position::default())], + files: vec![(file.path().to_path_buf(), Position::default())], ..Default::default() }, Config::default(), @@ -116,6 +118,8 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> { #[tokio::test] async fn test_write_fail_new_path() -> anyhow::Result<()> { + let file = helpers::new_readonly_tempfile()?; + test_key_sequences( &mut Application::new(Args::default(), Config::default())?, vec![ @@ -123,15 +127,21 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> { None, Some(&|app| { let doc = doc!(app.editor); - assert_eq!(None, app.editor.get_status()); + assert_ne!( + Some(&Severity::Error), + app.editor.get_status().map(|status| status.1) + ); assert_eq!(None, doc.path()); }), ), ( - Some(":w /foo"), + Some(&format!(":w {}", file.path().to_string_lossy())), Some(&|app| { let doc = doc!(app.editor); - assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); + assert_eq!( + Some(&Severity::Error), + app.editor.get_status().map(|status| status.1) + ); assert_eq!(None, doc.path()); }), ),