on master help sign in
use a read only file to ensure write failure
commitacf931709a56e5af0ac101276fcfb3ba45f159f2 authorSkyler Hawthorne <skyler@dead10ck.com> dateSat 30 Apr 2022, 00:35 (4 years ago) parentef8fe5a
3 files changed+29−5
helix-term/tests/integration/commands.rs+3−1 helix-term/tests/integration/helpers.rs+12 helix-term/tests/integration/write.rs+14−4
helix-term/tests/integration/commands.rs+3−1
@@ -10,10 +10,12 @@ use super::*;
1010
1111#[tokio::test]
1212async fn test_write_quit_fail() -> anyhow::Result<()> {
13+ let file = helpers::new_readonly_tempfile()?;
14+
1315 test_key_sequence(
1416 &mut Application::new(
1517 Args {
16− files: vec![(PathBuf::from("/foo"), Position::default())],
18+ files: vec![(file.path().to_path_buf(), Position::default())],
1719 ..Default::default()
1820 },
1921 Config::default(),
helix-term/tests/integration/helpers.rs+12
@@ -5,6 +5,7 @@ use crossterm::event::{Event, KeyEvent};
55use helix_core::{test, Selection, Transaction};
66use helix_term::{application::Application, args::Args, config::Config};
77use helix_view::{doc, input::parse_macro};
8+use tempfile::NamedTempFile;
89use tokio_stream::wrappers::UnboundedReceiverStream;
910
1011#[derive(Clone, Debug)]
@@ -155,3 +156,14 @@ pub fn platform_line(input: &str) -> String {
155156
156157 output
157158}
159+
160+/// Creates a new temporary file that is set to read only. Useful for
161+/// testing write failures.
162+pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> {
163+ let mut file = tempfile::NamedTempFile::new()?;
164+ let metadata = file.as_file().metadata()?;
165+ let mut perms = metadata.permissions();
166+ perms.set_readonly(true);
167+ file.as_file_mut().set_permissions(perms)?;
168+ Ok(file)
169+}
helix-term/tests/integration/write.rs+14−4
@@ -75,10 +75,12 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
7575
7676#[tokio::test]
7777async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
78+ let file = helpers::new_readonly_tempfile()?;
79+
7880 test_key_sequences(
7981 &mut Application::new(
8082 Args {
81− files: vec![(PathBuf::from("/foo"), Position::default())],
83+ files: vec![(file.path().to_path_buf(), Position::default())],
8284 ..Default::default()
8385 },
8486 Config::default(),
@@ -116,6 +118,8 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
116118
117119#[tokio::test]
118120async fn test_write_fail_new_path() -> anyhow::Result<()> {
121+ let file = helpers::new_readonly_tempfile()?;
122+
119123 test_key_sequences(
120124 &mut Application::new(Args::default(), Config::default())?,
121125 vec![
@@ -123,15 +127,21 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
123127 None,
124128 Some(&|app| {
125129 let doc = doc!(app.editor);
126− assert_eq!(None, app.editor.get_status());
130+ assert_ne!(
131+ Some(&Severity::Error),
132+ app.editor.get_status().map(|status| status.1)
133+ );
127134 assert_eq!(None, doc.path());
128135 }),
129136 ),
130137 (
131− Some(":w /foo<ret>"),
138+ Some(&format!(":w {}<ret>", file.path().to_string_lossy())),
132139 Some(&|app| {
133140 let doc = doc!(app.editor);
134− assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
141+ assert_eq!(
142+ Some(&Severity::Error),
143+ app.editor.get_status().map(|status| status.1)
144+ );
135145 assert_eq!(None, doc.path());
136146 }),
137147 ),