From e994df19cfed170cf41a222edd14e1ede5dec845 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 18 Feb 2024 11:39:02 +0100 Subject: [PATCH] feat: add tests Signed-off-by: kjuulh --- Cargo.lock | 1 + crates/flux-releaser/Cargo.toml | 1 + crates/flux-releaser/src/cli.rs | 53 ++++++++++++------- .../flux-releaser/tests/publish_artifacts.rs | 48 +++++++++++++++++ 4 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 crates/flux-releaser/tests/publish_artifacts.rs diff --git a/Cargo.lock b/Cargo.lock index 7c2ccec..74c3632 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1178,6 +1178,7 @@ dependencies = [ "axum 0.7.4", "clap", "dotenv", + "lazy_static", "mockall", "mockall_double", "nats", diff --git a/crates/flux-releaser/Cargo.toml b/crates/flux-releaser/Cargo.toml index 2950429..1672848 100644 --- a/crates/flux-releaser/Cargo.toml +++ b/crates/flux-releaser/Cargo.toml @@ -28,4 +28,5 @@ tar = "0.4.40" tonic-build = "0.11.0" [dev-dependencies] +lazy_static = "1.4.0" mockall = "0.12.1" diff --git a/crates/flux-releaser/src/cli.rs b/crates/flux-releaser/src/cli.rs index 5983db1..ec880cf 100644 --- a/crates/flux-releaser/src/cli.rs +++ b/crates/flux-releaser/src/cli.rs @@ -1,12 +1,6 @@ use clap::{Parser, Subcommand}; use std::net::SocketAddr; -use crate::{ - api::axum_serve, - app::{App, SharedApp}, - grpc::tonic_serve, -}; - #[derive(Parser)] #[command(author, version, about, long_about = None, subcommand_required = true)] pub struct Command { @@ -29,22 +23,41 @@ impl Command { let cli = Command::parse(); if let Some(Commands::Serve { host, grpc_host }) = cli.command { - tracing_subscriber::fmt::init(); - - tracing::info!("Starting service"); - - let app = SharedApp::new(App::new().await?); - - tokio::select! { - res = axum_serve(host, app.clone()) => { - res?; - }, - res = tonic_serve(grpc_host, app.clone()) => { - res?; - }, - }; + server::run_server(host, grpc_host).await?; } Ok(()) } } + +pub mod server { + use std::net::SocketAddr; + + use crate::{ + api::axum_serve, + app::{App, SharedApp}, + grpc::tonic_serve, + }; + + pub async fn run_server( + host: impl Into, + grpc_host: impl Into, + ) -> anyhow::Result<()> { + tracing_subscriber::fmt::init(); + + tracing::info!("Starting service"); + + let app = SharedApp::new(App::new().await?); + + tokio::select! { + res = axum_serve(host.into(), app.clone()) => { + res?; + }, + res = tonic_serve(grpc_host.into(), app.clone()) => { + res?; + }, + }; + + Ok(()) + } +} diff --git a/crates/flux-releaser/tests/publish_artifacts.rs b/crates/flux-releaser/tests/publish_artifacts.rs new file mode 100644 index 0000000..51fdf81 --- /dev/null +++ b/crates/flux-releaser/tests/publish_artifacts.rs @@ -0,0 +1,48 @@ +struct Server {} + +impl Server { + pub async fn new() -> Self { + Self {} + } + + pub async fn start(&self) -> anyhow::Result<()> { + Ok(()) + } +} + +static INIT: std::sync::Once = std::sync::Once::new(); + +// Makes sure the setup is ready for execution +async fn is_ready() -> anyhow::Result<()> { + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + + Ok(()) +} + +async fn setup() -> anyhow::Result<()> { + INIT.call_once(|| { + tokio::spawn(async move { + println!("once was created once"); + Server::new().await.start().await.unwrap(); + }); + }); + + is_ready().await?; + + Ok(()) +} + +#[tokio::test] +async fn can_create_artifact() -> anyhow::Result<()> { + setup().await?; + anyhow::bail!("failed one"); + Ok(()) +} + +#[tokio::test] +async fn can_more_create_artifact() -> anyhow::Result<()> { + setup().await?; + + anyhow::bail!("failed two"); + Ok(()) +}