From edf5af2e6380be78834b9ff4d98be4f195626330 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 8 Nov 2024 17:03:25 +0000 Subject: [PATCH] tests(fs): add more test --- src/fs/reader.rs | 17 ----- tests/fs.rs | 167 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 141 insertions(+), 43 deletions(-) diff --git a/src/fs/reader.rs b/src/fs/reader.rs index ae45ee5..e17c960 100644 --- a/src/fs/reader.rs +++ b/src/fs/reader.rs @@ -15,23 +15,6 @@ impl Reader { Ok(Self { contents }) } - /// Returns the contents of the file as a string. - /// - /// ``` - /// # use kxio::fs::Result; - /// # fn main() -> Result<()> { - /// let fs = kxio::fs::temp()?; - /// let path = fs.base().join("foo"); - /// let file = fs.file(&path); - /// # file.write("new file contents")?; - /// let contents = file.reader()?.contents(); - /// # Ok(()) - /// # } - /// ``` - pub fn contents(&self) -> &str { - &self.contents - } - /// Returns the contents of the file as a string. /// /// ``` diff --git a/tests/fs.rs b/tests/fs.rs index dfa7bbc..f22fdf5 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -7,6 +7,55 @@ type TestResult = Result<(), fs::Error>; mod path { use super::*; + mod is_link { + use super::*; + + #[test] + fn create_soft_link() -> TestResult { + let fs = fs::temp().expect("temp fs"); + + let file_path = fs.base().join("foo"); + let file = fs.file(&file_path); + file.write("content").expect("write"); + + let link_path = fs.base().join("bar"); + let link = fs.path(&link_path); + file.soft_link(&link).expect("soft_link"); + + let exists = link.exists().expect("exists"); + assert!(exists); + + let is_link = link.is_link().expect("is_link"); + assert!(is_link); + + Ok(()) + } + + #[test] + fn dir_is_not_a_link() { + let fs = fs::temp().expect("temp fs"); + + let dir_path = fs.base().join("foo"); + let dir = fs.dir(&dir_path); + dir.create().expect("create"); + + let is_link = dir.is_link().expect("is link"); + assert!(!is_link); + } + + #[test] + fn file_is_not_a_link() { + let fs = fs::temp().expect("temp fs"); + + let file_path = fs.base().join("foo"); + let file = fs.file(&file_path); + file.write("content").expect("write"); + + let is_link = file.is_link().expect("is link"); + assert!(!is_link); + } + } + mod set_permissions { use super::*; @@ -355,6 +404,22 @@ mod path { Ok(()) } } + + mod from_pathbuf { + use std::path::PathBuf; + + use super::*; + + #[test] + fn should_convert_from_pathbuf() { + let fs = fs::temp().expect("temp fs"); + + let src_pathbuf = fs.base().join("foo"); + let dst_pathbuf: PathBuf = fs.path(&src_pathbuf).into(); + + assert_eq!(src_pathbuf, dst_pathbuf); + } + } } mod file { @@ -375,27 +440,6 @@ mod file { // Ok(()) // } - #[test] - fn create_soft_link() -> TestResult { - let fs = fs::temp().expect("temp fs"); - - let file_path = fs.base().join("foo"); - let file = fs.file(&file_path); - file.write("content").expect("write"); - - let link_path = fs.base().join("bar"); - let link = fs.path(&link_path); - file.soft_link(&link).expect("soft_link"); - - let exists = link.exists().expect("exists"); - assert!(exists); - - let is_link = link.is_link().expect("is_link"); - assert!(is_link); - - Ok(()) - } - #[test] fn create_hard_link() -> TestResult { let fs = fs::temp().expect("temp fs"); @@ -432,6 +476,50 @@ mod file { Ok(()) } + #[test] + fn use_file_as_file() { + let fs = fs::temp().expect("temp fs"); + + let file_path = fs.base().join("foo"); + let file = fs.file(&file_path); + file.write("contents").expect("write"); + + file.remove().expect("remove"); + } + + #[test] + fn use_dir_as_file() { + let fs = fs::temp().expect("temp fs"); + + let dir_path = fs.base().join("foo"); + let dir = fs.dir(&dir_path); + dir.create().expect("create"); + + let file = fs.file(&dir_path); + let_assert!(Err(fs::Error::NotAFile { path }) = file.remove()); + assert_eq!(path, dir_path); + } + + #[test] + fn use_link_as_file() { + let fs = fs::temp().expect("temp fs"); + + let file_path = fs.base().join("foo"); + let file = fs.file(&file_path); + file.write("contents").expect("write"); + + let link_path = fs.base().join("bar"); + let link = fs.path(&link_path); + + file.soft_link(&link).expect("soft_link"); + + let path = fs.file(&link_path); + + let contents = path.reader().expect("reader").to_string(); + assert_eq!(contents, "contents"); + path.remove().expect("remove"); + } + mod remove { use super::*; #[test] @@ -538,20 +626,47 @@ mod file { use super::*; mod to_string { use super::*; + + use std::time::SystemTime; + #[test] - fn read_file_to_string() -> TestResult { + fn read_file_to_string() { let fs = fs::temp().expect("temp fs"); let path = fs.base().join("foo"); let file = fs.file(&path); - file.write("line 1\nline 2").expect("write"); + let line3 = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("duration") + .as_millis() + .to_string(); + let contents = format!("line 1\nline 2\n{line3}"); + file.write(&contents).expect("write"); let reader = file.reader().expect("reader"); - let string = reader.to_string(); + let string = reader.as_str(); - assert_eq!(string, "line 1\nline 2"); + assert_eq!(string, contents); + } - Ok(()) + #[test] + fn read_file_lines() { + let fs = fs::temp().expect("temp fs"); + + let path = fs.base().join("foo"); + let file = fs.file(&path); + let line3 = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("duration") + .as_millis() + .to_string(); + let contents = format!("line 1\nline 2\n{line3}"); + file.write(&contents).expect("write"); + + let reader = file.reader().expect("reader"); + let lines = reader.lines().collect::>(); + + assert_eq!(lines, vec!["line 1", "line 2", &line3]); } } mod lines {