From 35abd5d98cf188be9a416d86833a91461c351b07 Mon Sep 17 00:00:00 2001 From: maciektr Date: Thu, 11 Apr 2024 10:54:23 +0200 Subject: [PATCH] Fix log verbosity calculation (#1255) --- Cargo.lock | 15 +------ Cargo.toml | 1 - plugins/cairo-lang-macro/Cargo.toml | 1 - scarb/Cargo.toml | 3 -- utils/scarb-ui/Cargo.toml | 3 ++ utils/scarb-ui/src/args/verbosity.rs | 58 ++++++++++++++++++++++++---- xtask/Cargo.toml | 1 - 7 files changed, 55 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a6dd12f4..0938ebd76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -724,7 +724,6 @@ dependencies = [ "cairo-lang-macro-attributes", "cairo-lang-macro-stable", "linkme", - "scarb-stable-hash", "serde", "serde_json", ] @@ -1196,16 +1195,6 @@ dependencies = [ "clap_derive", ] -[[package]] -name = "clap-verbosity-flag" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb9b20c0dd58e4c2e991c8d203bbeb76c11304d1011659686b5b644bc29aa478" -dependencies = [ - "clap", - "log", -] - [[package]] name = "clap_builder" version = "4.4.15" @@ -4493,7 +4482,6 @@ dependencies = [ "cairo-lang-utils", "camino", "clap", - "clap-verbosity-flag", "create-output-dir", "data-encoding", "deno_task_shell", @@ -4545,7 +4533,6 @@ dependencies = [ "toml", "toml_edit 0.22.9", "tracing", - "tracing-log", "tracing-subscriber", "typed-builder", "url", @@ -4730,6 +4717,7 @@ dependencies = [ "scarb-metadata 1.12.0", "serde", "serde_json", + "test-case", "tracing-core", ] @@ -6227,7 +6215,6 @@ dependencies = [ "anyhow", "clap", "semver", - "serde", "serde_json", "time", "toml_edit 0.22.9", diff --git a/Cargo.toml b/Cargo.toml index 6a79476c1..7df379c04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,6 @@ cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", rev = "c06 camino = { version = "1", features = ["serde1"] } cargo_metadata = ">=0.18" clap = { version = "4", features = ["derive", "env", "string"] } -clap-verbosity-flag = "2" console = "0.15" darling = "0.20" data-encoding = "2" diff --git a/plugins/cairo-lang-macro/Cargo.toml b/plugins/cairo-lang-macro/Cargo.toml index 76b3a6099..b4a25dc3b 100644 --- a/plugins/cairo-lang-macro/Cargo.toml +++ b/plugins/cairo-lang-macro/Cargo.toml @@ -16,7 +16,6 @@ repository.workspace = true cairo-lang-macro-attributes = { path = "../cairo-lang-macro-attributes" } cairo-lang-macro-stable = { path = "../cairo-lang-macro-stable" } linkme.workspace = true -scarb-stable-hash = { path = "../../utils/scarb-stable-hash" } [dev-dependencies] serde.workspace = true diff --git a/scarb/Cargo.toml b/scarb/Cargo.toml index c5673c212..1b8aae7e0 100644 --- a/scarb/Cargo.toml +++ b/scarb/Cargo.toml @@ -32,7 +32,6 @@ cairo-lang-syntax.workspace = true cairo-lang-test-plugin.workspace = true cairo-lang-utils.workspace = true camino.workspace = true -clap-verbosity-flag.workspace = true clap.workspace = true create-output-dir = { path = "../utils/create-output-dir" } data-encoding.workspace = true @@ -72,7 +71,6 @@ thiserror.workspace = true tokio.workspace = true toml.workspace = true toml_edit.workspace = true -tracing-log.workspace = true tracing-subscriber.workspace = true tracing.workspace = true typed-builder.workspace = true @@ -80,7 +78,6 @@ url.workspace = true walkdir.workspace = true which.workspace = true windows-sys.workspace = true -zip.workspace = true zstd.workspace = true [target.'cfg(not(target_os = "linux"))'.dependencies] diff --git a/utils/scarb-ui/Cargo.toml b/utils/scarb-ui/Cargo.toml index b16f72fce..a7683b332 100644 --- a/utils/scarb-ui/Cargo.toml +++ b/utils/scarb-ui/Cargo.toml @@ -22,3 +22,6 @@ scarb-metadata = { version = "1", path = "../../scarb-metadata" } serde.workspace = true serde_json.workspace = true tracing-core.workspace = true + +[dev-dependencies] +test-case.workspace = true diff --git a/utils/scarb-ui/src/args/verbosity.rs b/utils/scarb-ui/src/args/verbosity.rs index c04be04fd..48fcd5399 100644 --- a/utils/scarb-ui/src/args/verbosity.rs +++ b/utils/scarb-ui/src/args/verbosity.rs @@ -35,9 +35,9 @@ pub struct VerbositySpec { impl Verbosity { fn level_value(level: Self) -> i8 { match level { - Self::Quiet => 0, - Self::Normal => 2, - Self::Verbose => 4, + Self::Quiet => -1, + Self::Normal => 0, + Self::Verbose => 1, } } } @@ -62,8 +62,7 @@ impl VerbositySpec { } fn integer_verbosity(&self) -> i8 { - let int_level = Verbosity::level_value(Verbosity::default()) - (self.quiet as i8) - + (self.verbose as i8); + let int_level = (self.verbose as i8) - (self.quiet as i8); if self.is_present() { int_level } else { @@ -77,9 +76,54 @@ impl VerbositySpec { impl From for Verbosity { fn from(spec: VerbositySpec) -> Self { match spec.integer_verbosity() { - v if v < 2 => Verbosity::Quiet, - 2 => Verbosity::Normal, + v if v < 0 => Verbosity::Quiet, + 0 => Verbosity::Normal, _ => Verbosity::Verbose, } } } + +#[cfg(test)] +mod tests { + use test_case::test_case; + + use crate::args::VerbositySpec; + use crate::Verbosity; + + #[test_case(Verbosity::Quiet)] + #[test_case(Verbosity::Normal)] + #[test_case(Verbosity::Verbose)] + fn verbosity_serialization_identity(level: Verbosity) { + assert_eq!( + Verbosity::from(VerbositySpec { + verbose: 0, + quiet: 0, + verbosity: Some(level), + }), + level + ); + } + + #[test_case(2, 0, Verbosity::Quiet, tracing_core::LevelFilter::OFF)] + #[test_case(1, 0, Verbosity::Quiet, tracing_core::LevelFilter::OFF)] + #[test_case(0, 0, Verbosity::Normal, tracing_core::LevelFilter::ERROR)] + #[test_case(0, 1, Verbosity::Verbose, tracing_core::LevelFilter::WARN)] + #[test_case(0, 2, Verbosity::Verbose, tracing_core::LevelFilter::INFO)] + #[test_case(0, 3, Verbosity::Verbose, tracing_core::LevelFilter::DEBUG)] + #[test_case(0, 4, Verbosity::Verbose, tracing_core::LevelFilter::TRACE)] + #[test_case(0, 5, Verbosity::Verbose, tracing_core::LevelFilter::TRACE)] + fn verbosity_levels( + quiet: u8, + verbose: u8, + level: Verbosity, + trace: tracing_core::LevelFilter, + ) { + let spec = VerbositySpec { + verbose, + quiet, + verbosity: None, + }; + assert_eq!(spec.as_trace(), trace); + assert_eq!(Verbosity::from(spec), level); + } +} diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 64829682c..b1eba56fb 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -9,7 +9,6 @@ publish = false anyhow.workspace = true clap.workspace = true semver.workspace = true -serde.workspace = true serde_json.workspace = true time.workspace = true toml_edit.workspace = true