diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-02-03 23:58:47 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-02-03 23:58:47 +0100 |
| commit | f1663e24c148421692346f7470d77b258d78b585 (patch) | |
| tree | 2a44cdb4da386fcca6b6be9037dcd9ce0ac88471 /server/common | |
| parent | 67331b851295b0067f74cd410aef7986aa6a848e (diff) | |
code coverage: Add tests for common
Add coverage for common as well as server.
Fix fs_utils::create_dir_allow_existing to fail if entry exists but
isn't a dir.
Diffstat (limited to 'server/common')
| -rw-r--r-- | server/common/Cargo.toml | 5 | ||||
| -rw-r--r-- | server/common/src/fs_utils.rs | 11 | ||||
| -rw-r--r-- | server/common/src/lib.rs | 3 | ||||
| -rw-r--r-- | server/common/src/tests.rs | 45 |
4 files changed, 61 insertions, 3 deletions
diff --git a/server/common/Cargo.toml b/server/common/Cargo.toml index a17fb95..aa358b2 100644 --- a/server/common/Cargo.toml +++ b/server/common/Cargo.toml @@ -7,4 +7,7 @@ edition = "2021" futures.workspace = true pathdiff = "0.2.3" serde.workspace = true -tokio = { workspace = true, features = ["fs", "process", "sync"] } +tokio = { workspace = true, features = ["fs", "macros", "process", "rt", "sync"] } + +[dev-dependencies] +testdir.workspace = true diff --git a/server/common/src/fs_utils.rs b/server/common/src/fs_utils.rs index 7905d01..b8c8554 100644 --- a/server/common/src/fs_utils.rs +++ b/server/common/src/fs_utils.rs @@ -5,11 +5,18 @@ use std::path::Path; use tokio::fs; pub async fn create_dir_allow_existing(path: impl AsRef<Path>) -> io::Result<()> { - match fs::create_dir(path).await { + match fs::create_dir(path.as_ref()).await { Ok(_) => Ok(()), Err(e) => { if e.kind() == io::ErrorKind::AlreadyExists { - Ok(()) + match fs::metadata(path).await { + Ok(metadata) => if metadata.is_dir() { + Ok(()) + } else { + Err(e) + } + Err(e) => Err(e), + } } else { Err(e) } diff --git a/server/common/src/lib.rs b/server/common/src/lib.rs index a63e05b..abea52e 100644 --- a/server/common/src/lib.rs +++ b/server/common/src/lib.rs @@ -1,3 +1,6 @@ pub mod fs_utils; pub mod git; pub mod git_socket; + +#[cfg(test)] +mod tests; diff --git a/server/common/src/tests.rs b/server/common/src/tests.rs new file mode 100644 index 0000000..540ee2d --- /dev/null +++ b/server/common/src/tests.rs @@ -0,0 +1,45 @@ +use tokio::fs; +use testdir::testdir; + +use crate::fs_utils; + +#[tokio::test] +async fn test_fs_utils_create_dir_allow_existing() { + let dir = testdir!(); + let foo = dir.join("foo"); + assert!(fs_utils::create_dir_allow_existing(&foo).await.is_ok()); + assert!(fs::try_exists(&foo).await.unwrap()); + assert!(fs_utils::create_dir_allow_existing(&foo).await.is_ok()); +} + +#[tokio::test] +async fn test_fs_utils_create_dir_allow_existing_file() { + let dir = testdir!(); + let foo = dir.join("foo"); + assert!(fs::write(&foo, "hello").await.is_ok()); + assert!(fs_utils::create_dir_allow_existing(&foo).await.is_err()); +} + +#[tokio::test] +async fn test_fs_utils_remove_file_allow_not_found() { + let dir = testdir!(); + let foo = dir.join("foo"); + assert!(fs_utils::remove_file_allow_not_found(&foo).await.is_ok()); + assert!(fs::write(&foo, "hello").await.is_ok()); + assert!(fs_utils::remove_file_allow_not_found(&foo).await.is_ok()); + assert!(!fs::try_exists(&foo).await.unwrap()); +} + +#[tokio::test] +async fn test_fs_utils_symlink_update_existing() { + let dir = testdir!(); + let foo = dir.join("foo"); + let bar = dir.join("bar"); + let fum = dir.join("fum"); + assert!(fs::write(&foo, "hello").await.is_ok()); + assert!(fs_utils::symlink_update_existing(&foo, &bar).await.is_ok()); + assert!(fs_utils::symlink_update_existing(&foo, &bar).await.is_ok()); + assert_eq!(fs::read_link(&bar).await.unwrap(), foo); + assert!(fs_utils::symlink_update_existing(&fum, &bar).await.is_ok()); + assert_eq!(fs::read_link(&bar).await.unwrap(), fum); +} |
