diff options
| -rw-r--r-- | server/.gitignore | 2 | ||||
| -rw-r--r-- | server/Cargo.lock | 1 | ||||
| -rw-r--r-- | server/Cargo.toml | 4 | ||||
| -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 | ||||
| -rwxr-xr-x | server/coverage.sh | 8 |
8 files changed, 70 insertions, 9 deletions
diff --git a/server/.gitignore b/server/.gitignore index fade025..22d2c3f 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1,3 +1,3 @@ /.env /git_server -/target +target/ diff --git a/server/Cargo.lock b/server/Cargo.lock index 7dfa00a..be83d6f 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -574,6 +574,7 @@ dependencies = [ "futures", "pathdiff", "serde", + "testdir", "tokio", ] diff --git a/server/Cargo.toml b/server/Cargo.toml index 9288bc2..906c589 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -5,12 +5,14 @@ edition = "2021" [workspace] members = ["hook"] +default-members = [".", "common", "hook"] resolver = "2" [workspace.dependencies] futures = "0.3.31" rmp-serde = "1.3" serde = { version = "1.0", features = ["derive"] } +testdir = "0.9.3" tokio = { version = "1" } [dependencies] @@ -30,4 +32,4 @@ utoipa-swagger-ui = { version = "9", features = ["rocket", "vendored"], default- [dev-dependencies] stdext = "0.3.3" -testdir = "0.9.3" +testdir.workspace = true 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); +} diff --git a/server/coverage.sh b/server/coverage.sh index fb2ad88..2298516 100755 --- a/server/coverage.sh +++ b/server/coverage.sh @@ -5,9 +5,9 @@ set +e # Remove old instrumented data rm -rf ./target/debug/coverage/ - - # Run tests, with instrument-coverage enabled -RUSTFLAGS="-Cinstrument-coverage" LLVM_PROFILE_FILE="target/debug/coverage/%p-%m.profraw" cargo test +RUSTFLAGS="-C instrument-coverage" LLVM_PROFILE_FILE="target/debug/coverage/%p-%m.profraw" cargo test + +~/.cargo/bin/grcov ./target/debug/coverage/ ./common/target/debug/coverage/ -s . --binary-path ./target/debug/ -t html --keep-only 'src/*' --keep-only 'common/src/*' -o ./target/debug/coverage/ -~/.cargo/bin/grcov ./target/debug/coverage/ -s . --binary-path ./target/debug/ -t html --keep-only 'src/*' -o ./target/debug/coverage/ +echo "Coverage report found in: ./target/debug/coverage/html/" |
