Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate ui verbosity when using scarbcommand in extensions #1994

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extensions/scarb-cairo-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ fn main_inner(ui: &Ui, args: Args) -> Result<()> {
ScarbCommand::new()
.arg("build")
.env("SCARB_PACKAGES_FILTER", filter.to_env())
.env_if(
"SCARB_UI_VERBOSITY",
ui.verbosity().to_string(),
!ui.verbosity().is_default(),
)
.run()?;
}

Expand Down
43 changes: 28 additions & 15 deletions extensions/scarb-cairo-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use indoc::formatdoc;
use scarb_metadata::{
Metadata, MetadataCommand, PackageId, PackageMetadata, ScarbCommand, TargetMetadata,
};
use scarb_ui::args::PackagesFilter;
use scarb_ui::args::{PackagesFilter, VerbositySpec};
use scarb_ui::components::{NewLine, Status};
use scarb_ui::{OutputFormat, Ui};

/// Execute all unit tests of a local package.
#[derive(Parser, Clone, Debug)]
Expand Down Expand Up @@ -40,6 +42,10 @@ struct Args {
/// Whether to print resource usage after each test.
#[arg(long, default_value_t = false)]
print_resource_usage: bool,

/// Logging verbosity.
#[command(flatten)]
pub verbose: VerbositySpec,
}

#[derive(ValueEnum, Clone, Debug, Default)]
Expand All @@ -62,11 +68,12 @@ impl TestKind {

fn main() -> Result<()> {
let args: Args = Args::parse();
let ui = Ui::new(args.verbose.clone().into(), OutputFormat::Text);

let metadata = MetadataCommand::new().inherit_stderr().exec()?;

check_scarb_version(&metadata);
check_cairo_test_plugin(&metadata);
check_scarb_version(&metadata, &ui);
check_cairo_test_plugin(&metadata, &ui);

let matched = args.packages_filter.match_many(&metadata)?;
let filter = PackagesFilter::generate_for::<Metadata>(matched.iter());
Expand Down Expand Up @@ -95,6 +102,11 @@ fn main() -> Result<()> {
.arg("--test")
.env("SCARB_TARGET_NAMES", target_names.clone().join(","))
.env("SCARB_PACKAGES_FILTER", filter.to_env())
.env_if(
"SCARB_UI_VERBOSITY",
ui.verbosity().to_string(),
!ui.verbosity().is_default(),
)
.run()?;

let profile = env::var("SCARB_PROFILE").unwrap_or("dev".into());
Expand All @@ -107,7 +119,8 @@ fn main() -> Result<()> {

let mut deduplicator = TargetGroupDeduplicator::default();
for package in matched {
println!("testing {} ...", package.name);
ui.print(Status::new("Testing", &package.name.to_string()));
let mut first = true;
for target in find_testable_targets(&package) {
if !target_names.contains(&target.name) {
continue;
Expand All @@ -122,6 +135,10 @@ fn main() -> Result<()> {
if already_seen {
continue;
}
if !first {
ui.print(NewLine::new());
}
first = false;
let test_compilation = deserialize_test_compilation(&target_dir, name.clone())?;
let config = TestRunConfig {
filter: args.filter.clone(),
Expand All @@ -133,7 +150,6 @@ fn main() -> Result<()> {
};
let runner = CompiledTestRunner::new(test_compilation, config);
runner.run(None)?;
println!();
}
}

Expand Down Expand Up @@ -197,7 +213,7 @@ fn find_testable_targets(package: &PackageMetadata) -> Vec<&TargetMetadata> {
.collect()
}

fn check_scarb_version(metadata: &Metadata) {
fn check_scarb_version(metadata: &Metadata, ui: &Ui) {
let app_version = env!("CARGO_PKG_VERSION").to_string();
let scarb_version = metadata
.app_version_info
Expand All @@ -206,28 +222,25 @@ fn check_scarb_version(metadata: &Metadata) {
.clone()
.to_string();
if app_version != scarb_version {
println!(
ui.print(format!(
"warn: the version of cairo-test does not match the version of scarb.\
cairo-test: `{}`, scarb: `{}`",
app_version, scarb_version
);
app_version, scarb_version,
));
}
}

fn check_cairo_test_plugin(metadata: &Metadata) {
fn check_cairo_test_plugin(metadata: &Metadata, ui: &Ui) {
let app_version = env!("CARGO_PKG_VERSION").to_string();
let warn = || {
println!(
"{}",
formatdoc! {r#"
ui.print(formatdoc! {r#"
warn: `cairo_test` plugin not found
please add the following snippet to your Scarb.toml manifest:
```
[dev-dependencies]
cairo_test = "{}"
```
"#, app_version}
);
"#, app_version});
};

let Some(plugin_pkg) = metadata.packages.iter().find(|pkg| {
Expand Down
23 changes: 8 additions & 15 deletions extensions/scarb-cairo-test/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ fn can_test_without_gas() {
.stdout_matches(indoc! {r#"
[..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 1 test
test hello::tests::test_foo ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}

Expand Down Expand Up @@ -98,14 +97,13 @@ fn can_print_test_resources() {
.stdout_matches(indoc! {r#"
[..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 1 test
test hello::tests::it_works ... ok (gas usage est.: [..])
steps: [..]
memory holes: [..]
builtins: ("range_check_builtin": [..])
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}

Expand Down Expand Up @@ -150,11 +148,10 @@ fn features_test_build_success() {
[..]Running cairo-test hello
[..]Compiling test(hello_unittest) hello v1.0.0 ([..])
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 1 test
test hello::tests::it_works ... ok[..]
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}

Expand Down Expand Up @@ -235,16 +232,15 @@ fn integration_tests() {
[..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Compiling test(hello_integrationtest) hello_integrationtest v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 2 tests
test hello_integrationtest::[..]::tests::it_works ... ok (gas usage est.: 43130)
test hello_integrationtest::[..]::tests::it_works ... ok (gas usage est.: 43130)
test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out;

running 1 test
test hello::tests::it_works ... ok (gas usage est.: 43130)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}

Expand Down Expand Up @@ -282,10 +278,9 @@ fn warn_if_cairo_test_plugin_missing() {

[..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}

Expand Down Expand Up @@ -334,11 +329,10 @@ fn can_choose_test_kind_to_run() {
.stdout_matches(indoc! {r#"
[..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 1 test
test hello::tests::it_works ... ok (gas usage est.: 43130)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
Scarb::quick_snapbox()
.arg("cairo-test")
Expand All @@ -350,11 +344,10 @@ fn can_choose_test_kind_to_run() {
.stdout_matches(indoc! {r#"
[..]Compiling test(hello_integrationtest) hello_integrationtest v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 2 tests
test hello_integrationtest::[..]::tests::it_works ... ok (gas usage est.: 43130)
test hello_integrationtest::[..]::tests::it_works ... ok (gas usage est.: 43130)
test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}
5 changes: 5 additions & 0 deletions extensions/scarb-execute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub fn execute(
ScarbCommand::new()
.arg("build")
.env("SCARB_PACKAGES_FILTER", filter.to_env())
.env_if(
"SCARB_UI_VERBOSITY",
ui.verbosity().to_string(),
!ui.verbosity().is_default(),
)
.run()?;
}

Expand Down
13 changes: 13 additions & 0 deletions scarb-metadata/src/command/scarb_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ impl ScarbCommand {
self
}

/// Inserts or updates an environment variable mapping, if `cond` is `true`.
pub fn env_if(
&mut self,
key: impl AsRef<OsStr>,
val: impl AsRef<OsStr>,
cond: bool,
) -> &mut Self {
if cond {
self.inner.env(key, val);
}
self
}

/// Adds or updates multiple environment variable mappings.
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
where
Expand Down
3 changes: 1 addition & 2 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,10 +1537,9 @@ fn can_expand_impl_inner_func_attrr() {
[..] Compiling some v1.0.0 ([..]Scarb.toml)
[..] Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
testing hello ...
[..]Testing hello
running 1 test
test hello::tests::test_flow ... ok (gas usage est.: [..])
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;

"#});
}
2 changes: 2 additions & 0 deletions utils/scarb-ui/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
//! a [`Ui`][crate::Ui].

pub use machine::*;
pub use new_line::*;
pub use spinner::*;
pub use status::*;
pub use typed::*;
pub use value::*;

mod machine;
mod new_line;
mod spinner;
mod status;
mod typed;
Expand Down
32 changes: 32 additions & 0 deletions utils/scarb-ui/src/components/new_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::Message;

/// A message that prints a new line.
pub struct NewLine {}

impl Default for NewLine {
fn default() -> Self {
Self::new()
}
}

impl NewLine {
/// Create a new instance of `NewLine`.
pub fn new() -> Self {
Self {}
}
}

impl Message for NewLine {
fn print_text(self)
where
Self: Sized,
{
println!();
}

fn print_json(self)
where
Self: Sized,
{
}
}
5 changes: 5 additions & 0 deletions utils/scarb-ui/src/verbosity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ impl Verbosity {
let env_var = env::var(env_var_name)?;
Self::from_str(env_var.as_str())
}

/// Check if the verbosity level is the default one.
pub fn is_default(&self) -> bool {
*self == Verbosity::default()
}
}

#[cfg(test)]
Expand Down