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

feat(zk_toolbox): Add zk_supervisor run unit tests command #2610

Merged
merged 18 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod integration;
pub mod recovery;
pub mod revert;
pub mod rust;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use clap::Parser;

use crate::messages::MSG_TEST_RUST_OPTIONS_HELP;

#[derive(Debug, Parser)]
pub struct RustArgs {
#[clap(long, help = MSG_TEST_RUST_OPTIONS_HELP)]
pub options: Option<String>,
}
10 changes: 8 additions & 2 deletions zk_toolbox/crates/zk_supervisor/src/commands/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use args::{integration::IntegrationArgs, recovery::RecoveryArgs, revert::RevertArgs};
use args::{
integration::IntegrationArgs, recovery::RecoveryArgs, revert::RevertArgs, rust::RustArgs,
};
use clap::Subcommand;
use xshell::Shell;

use crate::messages::{
MSG_INTEGRATION_TESTS_ABOUT, MSG_RECOVERY_TEST_ABOUT, MSG_REVERT_TEST_ABOUT,
MSG_UPGRADE_TEST_ABOUT,
MSG_RUST_TEST_ABOUT, MSG_UPGRADE_TEST_ABOUT,
};

mod args;
mod integration;
mod recovery;
mod revert;
mod rust;
mod upgrade;

#[derive(Subcommand, Debug)]
Expand All @@ -23,6 +26,8 @@ pub enum TestCommands {
Recovery(RecoveryArgs),
#[clap(about = MSG_UPGRADE_TEST_ABOUT, alias = "u")]
Upgrade,
#[clap(about = MSG_RUST_TEST_ABOUT, alias = "unit")]
Rust(RustArgs),
}

pub fn run(shell: &Shell, args: TestCommands) -> anyhow::Result<()> {
Expand All @@ -31,5 +36,6 @@ pub fn run(shell: &Shell, args: TestCommands) -> anyhow::Result<()> {
TestCommands::Revert(args) => revert::run(shell, args),
TestCommands::Recovery(args) => recovery::run(shell, args),
TestCommands::Upgrade => upgrade::run(shell),
TestCommands::Rust(args) => rust::run(shell, args),
}
}
41 changes: 41 additions & 0 deletions zk_toolbox/crates/zk_supervisor/src/commands/test/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use common::{cmd::Cmd, logger};
use config::EcosystemConfig;
use xshell::{cmd, Shell};

use super::args::rust::RustArgs;
use crate::messages::{
MSG_CARGO_NEXTEST_MISSING_ERR, MSG_UNIT_TESTS_RUN_SUCCESS, MSG_USING_CARGO_NEXTEST,
};

pub fn run(shell: &Shell, args: RustArgs) -> anyhow::Result<()> {
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
let ecosystem = EcosystemConfig::from_file(shell)?;
let _dir_guard = shell.push_dir(&ecosystem.link_to_code);

let cmd = if nextest_is_installed(shell)? {
logger::info(MSG_USING_CARGO_NEXTEST);
cmd!(shell, "cargo nextest run --release")
} else {
logger::error(MSG_CARGO_NEXTEST_MISSING_ERR);
cmd!(shell, "cargo test --release")
};

if let Some(options) = args.options {
Cmd::new(cmd.args(options.split_whitespace()))
.with_force_run()
.run()?;
} else {
Cmd::new(cmd).with_force_run().run()?;
}

logger::outro(MSG_UNIT_TESTS_RUN_SUCCESS);
Ok(())
}

fn nextest_is_installed(shell: &Shell) -> anyhow::Result<bool> {
let out = String::from_utf8(
Cmd::new(cmd!(shell, "cargo install --list"))
.run_with_output()?
.stdout,
)?;
Ok(out.contains("cargo-nextest"))
}
5 changes: 5 additions & 0 deletions zk_toolbox/crates/zk_supervisor/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ pub(super) const MSG_INTEGRATION_TESTS_ABOUT: &str = "Run integration tests";
pub(super) const MSG_REVERT_TEST_ABOUT: &str = "Run revert tests";
pub(super) const MSG_RECOVERY_TEST_ABOUT: &str = "Run recovery tests";
pub(super) const MSG_UPGRADE_TEST_ABOUT: &str = "Run upgrade tests";
pub(super) const MSG_RUST_TEST_ABOUT: &str = "Run unit-tests, accepts optional cargo test flags";
pub(super) const MSG_TEST_RUST_OPTIONS_HELP: &str = "Cargo test flags";
pub(super) const MSG_TESTS_EXTERNAL_NODE_HELP: &str = "Run tests for external node";
pub(super) const MSG_TESTS_RECOVERY_SNAPSHOT_HELP: &str =
"Run recovery from a snapshot instead of genesis";
pub(super) const MSG_UNIT_TESTS_RUN_SUCCESS: &str = "Unit tests ran successfully";
pub(super) const MSG_USING_CARGO_NEXTEST: &str = "Using cargo-nextest for running tests";
pub(super) const MSG_CARGO_NEXTEST_MISSING_ERR: &str = "cargo-nextest is missing, please run 'cargo install cargo-nextest'. Falling back to 'cargo test'";

// Integration tests related messages
pub(super) fn msg_integration_tests_run(external_node: bool) -> String {
Expand Down
Loading