tests(fs): add more test
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 2m1s
Rust / build (map[name:stable]) (push) Successful in 4m5s
Release Please / Release-plz (push) Successful in 42s

This commit is contained in:
Paul Campbell 2024-11-08 17:03:25 +00:00
parent e264bea729
commit ed6d83cb7b
2 changed files with 141 additions and 45 deletions

View file

@ -15,23 +15,6 @@ impl Reader {
Ok(Self { contents }) 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. /// Returns the contents of the file as a string.
/// ///
/// ``` /// ```

View file

@ -7,6 +7,55 @@ type TestResult = Result<(), fs::Error>;
mod path { mod path {
use super::*; 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 { mod set_permissions {
use super::*; use super::*;
@ -216,7 +265,6 @@ mod path {
fn should_be_false_when_is_a_link() -> TestResult { fn should_be_false_when_is_a_link() -> TestResult {
let fs = fs::temp().expect("temp fs"); let fs = fs::temp().expect("temp fs");
let path = fs.base().join("foo"); let path = fs.base().join("foo");
// TODO: (#38) create a link
// let_assert!(Ok(_) = fs.file_write(&path, "bar")); // let_assert!(Ok(_) = fs.file_write(&path, "bar"));
let is_dir = fs.path(&path).is_dir().expect("is_dir"); let is_dir = fs.path(&path).is_dir().expect("is_dir");
assert!(!is_dir); assert!(!is_dir);
@ -300,7 +348,6 @@ mod path {
fn should_be_false_when_is_a_link() -> TestResult { fn should_be_false_when_is_a_link() -> TestResult {
let fs = fs::temp().expect("temp fs"); let fs = fs::temp().expect("temp fs");
let path = fs.base().join("foo"); let path = fs.base().join("foo");
// TODO: (#38) create a link
// let_assert!(Ok(_) = fs.file_write(&path, "bar")); // let_assert!(Ok(_) = fs.file_write(&path, "bar"));
let is_file = fs.path(&path).is_file().expect("is_file"); let is_file = fs.path(&path).is_file().expect("is_file");
assert!(!is_file); assert!(!is_file);
@ -355,6 +402,22 @@ mod path {
Ok(()) 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 { mod file {
@ -375,27 +438,6 @@ mod file {
// Ok(()) // 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] #[test]
fn create_hard_link() -> TestResult { fn create_hard_link() -> TestResult {
let fs = fs::temp().expect("temp fs"); let fs = fs::temp().expect("temp fs");
@ -432,6 +474,50 @@ mod file {
Ok(()) 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 { mod remove {
use super::*; use super::*;
#[test] #[test]
@ -538,20 +624,47 @@ mod file {
use super::*; use super::*;
mod to_string { mod to_string {
use super::*; use super::*;
use std::time::SystemTime;
#[test] #[test]
fn read_file_to_string() -> TestResult { fn read_file_to_string() {
let fs = fs::temp().expect("temp fs"); let fs = fs::temp().expect("temp fs");
let path = fs.base().join("foo"); let path = fs.base().join("foo");
let file = fs.file(&path); 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 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::<Vec<_>>();
assert_eq!(lines, vec!["line 1", "line 2", &line3]);
} }
} }
mod lines { mod lines {