feat(test): log for integration tests

- log for test are configurable to either be spit into the void (cargo
test default) or into `stdout`.
This commit is contained in:
2024-05-07 21:36:16 -04:00
parent 70a4eb23c6
commit 9336235b64
5 changed files with 37 additions and 5 deletions

View File

@@ -1,8 +1,22 @@
use email_newsletter_api::configuration::{get_configuration, DatabaseSettings};
use email_newsletter_api::{
configuration::{get_configuration, DatabaseSettings},
telemetry::{get_subscriber, init_subscriber},
};
use once_cell::sync::Lazy;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener;
use uuid::Uuid;
static TRACING: Lazy<()> = Lazy::new(|| {
if std::env::var("TEST_LOG").is_ok() {
let subscriber = get_subscriber("test".into(), "info".into(), std::io::stdout);
init_subscriber(subscriber);
} else {
let subscriber = get_subscriber("test".into(), "debug".into(), std::io::sink);
init_subscriber(subscriber);
}
});
pub struct TestApp {
pub address: String,
pub db_pool: PgPool,
@@ -10,6 +24,8 @@ pub struct TestApp {
#[allow(clippy::let_underscore_future)]
pub async fn spawn_app() -> TestApp {
Lazy::force(&TRACING);
/* Spawn a app server with a TcpListener bound to localhost:<random port>
*
* Returns a valid IPv4 string (i.e localhost:8080)