use std::io::{Error, ErrorKind}; use std::path::Path; use super::{io_refusal, plain_reason}; /// Every path here is synthetic. fn path() -> &'static Path { Path::new("/tmp/share-inbox/00000000-0000-0000-0000-000000000000.opus") } /// **The finding.** `Permission denied (os error 13)` is libc talking, and it /// was the second half of a toast. #[test] fn a_refused_copy_says_so_in_words_and_never_in_os_error_numbers() { let message = io_refusal( "the recording could not be copied into this device's store", path(), &Error::from(ErrorKind::PermissionDenied), ); assert_eq!( message, "the recording could not be copied into this device's store: this device would not allow it" ); assert!(!message.contains("os error"), "{message}"); } #[test] fn a_file_that_has_gone_says_it_has_gone() { assert_eq!( plain_reason(&Error::from(ErrorKind::NotFound)), Some("the file is no longer there") ); } /// `ENOSPC` by number: there is no `ErrorKind` for it on every platform this /// crate builds for, and a full disk is the one io failure the person can /// actually do something about. #[test] fn a_full_disk_says_there_is_no_room() { assert_eq!( plain_reason(&Error::from_raw_os_error(28)), Some("there is no room left on this device") ); } /// Anything else adds nothing — the caller's sentence already said what /// failed, and a guess at the cause would be worse than silence. The detail is /// in the log. #[test] fn an_unclassified_failure_is_the_sentence_alone() { for error in [ Error::from(ErrorKind::InvalidData), Error::from(ErrorKind::UnexpectedEof), Error::from(ErrorKind::Other), Error::from_raw_os_error(5), ] { assert_eq!(plain_reason(&error), None, "{error}"); } assert_eq!( io_refusal("that export could not be read", path(), &Error::from(ErrorKind::InvalidData)), "that export could not be read" ); } /// The sentence is the CALLER's, verbatim — this module never rewrites it, /// because only the caller knows whether what failed was a recording, a /// picture or an export. #[test] fn the_caller_s_sentence_is_kept_word_for_word() { let message = io_refusal( "the screenshot could not be copied into this device's store", path(), &Error::from(ErrorKind::PermissionDenied), ); assert!( message.starts_with("the screenshot could not be copied into this device's store"), "{message}" ); }