From 9a2caf0921728fb1d02f42223f72f0623ebbf1db Mon Sep 17 00:00:00 2001 From: Fergus Molloy Date: Fri, 18 Jul 2025 09:04:37 +0100 Subject: [PATCH] spawn app for each integration test also adds missing dep to flake --- .gitea/workflows/cargo.yaml | 41 +++++++++++++++++++++++++++++++++++++ flake.nix | 1 + tests/common/mod.rs | 16 +++++++++++++++ tests/healthcheck_test.rs | 15 ++++---------- 4 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 .gitea/workflows/cargo.yaml create mode 100644 tests/common/mod.rs diff --git a/.gitea/workflows/cargo.yaml b/.gitea/workflows/cargo.yaml new file mode 100644 index 0000000..f6858d6 --- /dev/null +++ b/.gitea/workflows/cargo.yaml @@ -0,0 +1,41 @@ +name: Cargo +run-name: ${{ gitea.actor }} is verifying cargo build and tests +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + + - name: Install latest stable + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + components: rustfmt, clippy + + - name: Build Project + uses: actions-rs/cargo@v1 + with: + command: build + args: --release + + - name: Run Clippy + uses: actions-rs/cargo@v1 + with: + command: clippy + args: -Dwarnings -Wclippy::correctness -Wclippy::complexity -Wclippy::perf -Aclippy::missing_errors_doc -Aclippy::missing_panics_doc + + - name: Run Unit Tests + uses: actions-rs/cargo@v1 + with: + command: test + args: --lib --bins + + - name: Run Integration Tests + uses: actions-rs/cargo@v1 + with: + command: test + args: --tests '*' diff --git a/flake.nix b/flake.nix index c40adad..118fc07 100644 --- a/flake.nix +++ b/flake.nix @@ -31,6 +31,7 @@ nodejs pnpm just + openssl ]; }; diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..12ddc04 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,16 @@ +use std::net::TcpListener; + +pub fn spawn_app() -> String { + let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); + let _ = listener.set_nonblocking(true); + // We retrieve the port assigned to us by the OS + let port = listener.local_addr().unwrap().port(); + let server = nuchat::run( + tokio::net::TcpListener::from_std(listener) + .expect("Failed to convert from_std to tokio listener"), + ) + .expect("Failed to bind address"); + tokio::spawn(server.into_future()); + // We return the application address to the caller! + format!("http://127.0.0.1:{port}") +} diff --git a/tests/healthcheck_test.rs b/tests/healthcheck_test.rs index 1f79e24..d56e4aa 100644 --- a/tests/healthcheck_test.rs +++ b/tests/healthcheck_test.rs @@ -1,18 +1,11 @@ +mod common; +use common::spawn_app; use reqwest::StatusCode; #[tokio::test] async fn test_healthcheck() -> reqwest::Result<()> { - let response = reqwest::get("http://localhost:7000/healthcheck").await?; - - assert_eq!(response.status(), StatusCode::OK); - assert_eq!(response.text().await?, r#"{"healthy":true}"#); - - Ok(()) -} - -#[tokio::test] -async fn test_healthcheck2() -> reqwest::Result<()> { - let response = reqwest::get("http://localhost:7000/healthcheck").await?; + let address = spawn_app(); + let response = reqwest::get(format!("{address}/api/healthcheck")).await?; assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.text().await?, r#"{"healthy":true}"#);