Commit 7bdd1a6b authored by Nugine's avatar Nugine
Browse files

refactor(s3s-test): add macro main

parent c41ac1d3
Loading
Loading
Loading
Loading
+18 −20
Original line number Diff line number Diff line
@@ -15,21 +15,19 @@
    clippy::multiple_crate_versions, // TODO: check later
)]

use aws_sdk_s3::primitives::ByteStream;
use s3s_test::tcx::TestContext;
use s3s_test::Result;
use s3s_test::TestFixture;
use s3s_test::TestSuite;
use tracing::debug;
use tracing::warn;

use std::fmt;
use std::ops::Not;
use std::process::Termination;
use std::sync::Arc;

use aws_sdk_s3::error::ProvideErrorMetadata;
use aws_sdk_s3::error::SdkError;
use tracing::error;
use aws_sdk_s3::primitives::ByteStream;
use tracing::{debug, error, warn};

fn check<T, E>(result: Result<T, SdkError<E>>, allowed_codes: &[&str]) -> Result<Option<T>, SdkError<E>>
where
@@ -333,8 +331,7 @@ impl STS {
    }
}

fn main() -> impl Termination {
    s3s_test::cli::main(|tcx| {
fn register(tcx: &mut TestContext) {
    macro_rules! case {
        ($s:ident, $x:ident, $c:ident) => {{
            let mut suite = tcx.suite::<$s>(stringify!($s));
@@ -348,5 +345,6 @@ fn main() -> impl Termination {
    case!(E2E, Basic, test_get_object);
    case!(E2E, Put, test_put_object_tiny);
    case!(E2E, STS, test_assume_role);
    })
}

s3s_test::main!(register);
+50 −21
Original line number Diff line number Diff line
@@ -8,17 +8,29 @@ use crate::report::FnResult;
use crate::report::Report;
use crate::tcx::TestContext;

use clap::Parser;
use colored::ColoredString;
use colored::Colorize;
use regex::RegexSet;

type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;

fn setup_tracing() {
#[doc(hidden)]
pub use clap;

#[doc(hidden)]
pub struct Options {
    pub json: Option<PathBuf>,
    pub filter: Vec<String>,
    pub list: bool,
}

#[doc(hidden)]
pub fn setup() {
    use std::io::IsTerminal;
    use tracing_subscriber::EnvFilter;

    dotenvy::dotenv().ok();

    let env_filter = EnvFilter::from_default_env();
    let enable_color = std::io::stdout().is_terminal();

@@ -29,18 +41,6 @@ fn setup_tracing() {
        .init();
}

#[derive(Debug, Parser)]
struct Opt {
    #[clap(long)]
    json: Option<PathBuf>,

    #[clap(long)]
    filter: Vec<String>,

    #[clap(long)]
    list: bool,
}

fn status(passed: bool) -> ColoredString {
    if passed {
        "PASSED".green()
@@ -97,9 +97,9 @@ fn print_summary(report: &Report) {
}

#[tokio::main]
async fn async_main(opt: &Opt, register: impl FnOnce(&mut TestContext)) -> ExitCode {
async fn async_main(reg: impl FnOnce(&mut TestContext), opt: &Options) -> ExitCode {
    let mut tcx = TestContext::new();
    register(&mut tcx);
    reg(&mut tcx);

    if opt.filter.is_empty().not() {
        let filter_set = match RegexSet::new(&opt.filter) {
@@ -146,10 +146,39 @@ async fn async_main(opt: &Opt, register: impl FnOnce(&mut TestContext)) -> ExitC
    }
}

#[doc(hidden)]
#[must_use]
pub fn main(register: impl FnOnce(&mut TestContext)) -> impl Termination {
    dotenvy::dotenv().ok();
    setup_tracing();
pub fn main(reg: impl FnOnce(&mut TestContext), opt: &Options) -> impl Termination {
    setup();
    async_main(reg, opt)
}

#[macro_export]
macro_rules! main {
    ($register:expr) => {
        #[derive(Debug, s3s_test::cli::clap::Parser)]
        struct Opt {
            #[clap(long)]
            json: Option<::std::path::PathBuf>,

            #[clap(long)]
            filter: Vec<::std::string::String>,

            #[clap(long)]
            list: bool,
        }

        fn main() -> impl ::std::process::Termination {
            use s3s_test::cli::clap::Parser as _;
            let opt = Opt::parse();
    async_main(&opt, register)
            s3s_test::cli::main(
                $register,
                &s3s_test::cli::Options {
                    json: opt.json,
                    filter: opt.filter,
                    list: opt.list,
                },
            )
        }
    };
}