Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566//! Reading the Erlang cookie from a file.
use std::io;use std::path::Path;
/// The cookie file autoconnect reads when none is named on the command line.pub const DEFAULT_COOKIE_FILE: &str = ".erlang.cookie";
/// Reads a cookie from `path`, trimming the surrounding whitespace Erlang's own/// cookie files carry.////// # Errors/// Returns an error if the file cannot be read, or holds nothing but whitespace.pub fn read_cookie(path: &Path) -> io::Result<String> { let contents = std::fs::read_to_string(path)?; let cookie = contents.trim();
if cookie.is_empty() { return Err(io::Error::new( io::ErrorKind::InvalidData, format!("cookie file '{}' is empty", path.display()), )); }
Ok(cookie.to_string())}
#[cfg(test)]#[allow(clippy::unwrap_used, clippy::expect_used)]mod tests { use super::*; use std::io::Write;
fn cookie_file(contents: &str) -> tempfile::NamedTempFile { let mut file = tempfile::NamedTempFile::new().unwrap(); file.write_all(contents.as_bytes()).unwrap(); file.flush().unwrap(); file }
#[test] fn reads_a_cookie() { let file = cookie_file("SECRETCOOKIE"); assert_eq!(read_cookie(file.path()).unwrap(), "SECRETCOOKIE"); }
#[test] fn trims_surrounding_whitespace() { let file = cookie_file(" SECRETCOOKIE\n"); assert_eq!(read_cookie(file.path()).unwrap(), "SECRETCOOKIE"); }
#[test] fn rejects_a_whitespace_only_cookie() { let file = cookie_file("\n \n"); let err = read_cookie(file.path()).unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::InvalidData); }
#[test] fn reports_a_missing_file() { let err = read_cookie(Path::new("/nonexistent/.erlang.cookie")).unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::NotFound); }}