From fd5b78c337d044e4705a916f7863bde67602a4ac Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 28 May 2024 00:15:19 +1000 Subject: [PATCH] Add logging to unit testing Signed-off-by: Jiahao XU --- Cargo.lock | 3 +++ crates/binstalk-git-repo-api/Cargo.toml | 1 + .../src/gh_api_client.rs | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 29e029cf..828d35b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -361,6 +361,7 @@ dependencies = [ "thiserror", "tokio", "tracing", + "tracing-subscriber", "url", ] @@ -4124,8 +4125,10 @@ dependencies = [ "serde", "serde_json", "sharded-slab", + "smallvec", "thread_local", "tracing-core", + "tracing-log", "tracing-serde", ] diff --git a/crates/binstalk-git-repo-api/Cargo.toml b/crates/binstalk-git-repo-api/Cargo.toml index 28f243c6..71b0844c 100644 --- a/crates/binstalk-git-repo-api/Cargo.toml +++ b/crates/binstalk-git-repo-api/Cargo.toml @@ -25,3 +25,4 @@ url = "2.3.1" [dev-dependencies] binstalk-downloader = { version = "0.10.3", path = "../binstalk-downloader" } +tracing-subscriber = "0.3" diff --git a/crates/binstalk-git-repo-api/src/gh_api_client.rs b/crates/binstalk-git-repo-api/src/gh_api_client.rs index 69e2ea38..30d7d1d3 100644 --- a/crates/binstalk-git-repo-api/src/gh_api_client.rs +++ b/crates/binstalk-git-repo-api/src/gh_api_client.rs @@ -210,6 +210,8 @@ mod test { use super::*; use compact_str::{CompactString, ToCompactString}; use std::{env, num::NonZeroU16}; + use tracing::subscriber::set_global_default; + use tracing_subscriber::{filter::LevelFilter, fmt::fmt}; mod cargo_binstall_v0_20_1 { use super::{CompactString, GhRelease, GhRepo}; @@ -305,6 +307,24 @@ mod test { } } + fn init_logger() { + // Disable time, target, file, line_num, thread name/ids to make the + // output more readable + let subscriber = fmt() + .without_time() + .with_target(false) + .with_file(false) + .with_line_number(false) + .with_thread_names(false) + .with_thread_ids(false) + .with_test_writer() + .with_max_level(LevelFilter::DEBUG) + .finish(); + + // Setup global subscriber + set_global_default(subscriber).unwrap(); + } + /// Mark this as an async fn so that you won't accidentally use it in /// sync context. async fn create_client() -> Vec { @@ -327,6 +347,8 @@ mod test { } async fn test_specific_release(release: &GhRelease, artifacts: &[&str]) { + init_logger(); + for client in create_client().await { eprintln!("In client {client:?}"); @@ -371,6 +393,8 @@ mod test { #[tokio::test] async fn test_gh_api_client_cargo_binstall_no_such_release() { + init_logger(); + for client in create_client().await { let release = GhRelease { repo: GhRepo {