From 4ad6f45379c8e6f261746716104462ce249bac69 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 28 Apr 2024 15:01:29 +0100 Subject: [PATCH] refactor(fs/tests): group into modules --- src/tests/fs.rs | 103 ++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/src/tests/fs.rs b/src/tests/fs.rs index aa358b8..699f914 100644 --- a/src/tests/fs.rs +++ b/src/tests/fs.rs @@ -4,65 +4,74 @@ use crate::fs; type TestResult = Result<(), fs::Error>; -#[test] -fn path_of_validate_fails_path_traversal() -> TestResult { - let fs = fs::temp()?; +mod path_of { + use super::*; + #[test] + fn path_of_validate_fails_path_traversal() -> TestResult { + let fs = fs::temp()?; - let_assert!(Err(fs::Error::PathTraversal { base, path: _path }) = fs.path_of("..".into())); - assert_eq!(base, fs.base()); + let_assert!(Err(fs::Error::PathTraversal { base, path: _path }) = fs.path_of("..".into())); + assert_eq!(base, fs.base()); - Ok(()) + Ok(()) + } } -#[test] -fn write_read_file_exists() -> TestResult { - let fs = fs::temp()?; - let pathbuf = fs.base().join("foo"); +mod file { + use super::*; + #[test] + fn write_read_file_exists() -> TestResult { + let fs = fs::temp()?; + let pathbuf = fs.base().join("foo"); - let_assert!(Ok(_) = fs.file_write(&pathbuf, "content")); - let_assert!( - Ok(c) = fs.file_read_to_string(&pathbuf), - "file_read_to_string" - ); - assert_eq!(c, "content"); + let_assert!(Ok(_) = fs.file_write(&pathbuf, "content")); + let_assert!( + Ok(c) = fs.file_read_to_string(&pathbuf), + "file_read_to_string" + ); + assert_eq!(c, "content"); - let_assert!(Ok(exists) = fs.path_exists(&pathbuf)); - assert!(exists); + let_assert!(Ok(exists) = fs.path_exists(&pathbuf)); + assert!(exists); - let_assert!(Ok(is_file) = fs.path_is_file(&pathbuf)); - assert!(is_file); + let_assert!(Ok(is_file) = fs.path_is_file(&pathbuf)); + assert!(is_file); - Ok(()) + Ok(()) + } } -#[test] -fn create_dir_should_create_a_dir() -> TestResult { - let fs = fs::temp()?; - let pathbuf = fs.base().join("subdir"); +mod dir_create { + use super::*; + #[test] + fn dir_create_should_create_a_dir() -> TestResult { + let fs = fs::temp()?; + let pathbuf = fs.base().join("subdir"); - let_assert!(Ok(_) = fs.dir_create(&pathbuf)); + let_assert!(Ok(_) = fs.dir_create(&pathbuf)); - let_assert!(Ok(exists) = fs.path_exists(&pathbuf)); - assert!(exists); + let_assert!(Ok(exists) = fs.path_exists(&pathbuf)); + assert!(exists); - let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf)); - assert!(is_dir); + let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf)); + assert!(is_dir); - Ok(()) -} - -#[test] -fn create_dir_all_should_create_a_dir() -> TestResult { - let fs = fs::temp()?; - let pathbuf = fs.base().join("subdir").join("child"); - - let_assert!(Ok(_) = fs.dir_create_all(&pathbuf)); - - let_assert!(Ok(exists) = fs.path_exists(&pathbuf)); - assert!(exists, "path exists"); - - let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf)); - assert!(is_dir, "path is a directory"); - - Ok(()) + Ok(()) + } + + #[test] + fn dir_create_all_should_create_a_dir() -> TestResult { + let fs = fs::temp()?; + let pathbuf = fs.base().join("subdir").join("child"); + + let_assert!(Ok(_) = fs.dir_create_all(&pathbuf)); + + let_assert!(Ok(exists) = fs.path_exists(&pathbuf)); + assert!(exists, "path exists"); + + let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf)); + assert!(is_dir, "path is a directory"); + + Ok(()) + } }