diff --git a/Cargo.lock b/Cargo.lock index ea84fc465..3a3c3cbcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -763,7 +763,7 @@ dependencies = [ [[package]] name = "cargo-fslabscli" -version = "2.39.0" +version = "2.40.0" dependencies = [ "anyhow", "assert_fs", diff --git a/Cargo.toml b/Cargo.toml index 28ee5f7ee..41dd97703 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" name = "cargo-fslabscli" publish = ["fsl"] repository = "https://github.com/fslabs/fslabsci" -version = "2.39.0" +version = "2.40.0" [package.metadata.fslabs.publish.binary] name = "FSLABS Cli tool" @@ -23,6 +23,7 @@ resolver = "2" members = [ "crates/fsl_test_api", ] +exclude = ["test_crates/wasm_example"] [features] alpha = [] diff --git a/src/commands/tests/mod.rs b/src/commands/tests/mod.rs index f75cab0fd..feb105a1d 100644 --- a/src/commands/tests/mod.rs +++ b/src/commands/tests/mod.rs @@ -23,6 +23,14 @@ use tokio::sync::OnceCell; use tokio::sync::Semaphore; use tracing::Instrument; +/// Checks whether a step should be skipped based on the env var `SKIP_{ID}_TEST` (uppercased). +/// Returns `true` only when the env var is present and equals `"true"`. +#[allow(dead_code)] // false positive: module is named `tests` +fn should_skip_step(id: &str) -> bool { + let skip_env = format!("SKIP_{}_TEST", id).to_uppercase(); + matches!(env::var(skip_env), Ok(v) if v == "true") +} + use crate::{ PackageRelatedOptions, PrettyPrintable, cli_args::DiffOptions, @@ -474,6 +482,11 @@ pub async fn tests( ), ]; + let steps: Vec<_> = steps + .into_iter() + .filter(|(id, _, _)| !should_skip_step(id)) + .collect(); + let mut ts = TestSuiteBuilder::new(&format!("Batch {workspace}")) .set_timestamp(OffsetDateTime::now_utc()) .build(); @@ -1085,9 +1098,8 @@ async fn run_package_tests( t.skip = true; } // Let's check if the test need to be skip - let skip_env = format!("SKIP_{}_TEST", t.id).to_uppercase(); - if let Ok(skip) = env::var(skip_env) { - t.skip = skip == "true"; + if should_skip_step(&t.id) { + t.skip = true; } t }) @@ -1384,3 +1396,73 @@ async fn run_package_tests( tracing::Span::current().record("otel.status_code", if failed { "ERROR" } else { "OK" }); (failed, junit_report) } + +#[cfg(test)] +mod unit_tests { + use super::*; + + /// Helper: sets an env var, runs the closure, then removes the var. + /// Keeps the unsafe surface small and centralised. + fn with_env_var(key: &str, value: &str, f: impl FnOnce()) { + unsafe { env::set_var(key, value) }; + f(); + unsafe { env::remove_var(key) }; + } + + /// Helper: ensures an env var is absent, runs the closure, then cleans up. + fn without_env_var(key: &str, f: impl FnOnce()) { + unsafe { env::remove_var(key) }; + f(); + } + + #[test] + fn should_not_skip_when_env_var_is_absent() { + without_env_var("SKIP_MYFAKESTEP_TEST", || { + assert!(!should_skip_step("myfakestep")); + }); + } + + #[test] + fn should_skip_when_env_var_is_true() { + with_env_var("SKIP_SKIPME_TEST", "true", || { + assert!(should_skip_step("skipme")); + }); + } + + #[test] + fn should_not_skip_when_env_var_is_false() { + with_env_var("SKIP_KEEPME_TEST", "false", || { + assert!(!should_skip_step("keepme")); + }); + } + + #[test] + fn should_not_skip_when_env_var_is_other_value() { + with_env_var("SKIP_OTHERSTEP_TEST", "yes", || { + assert!(!should_skip_step("otherstep")); + }); + } + + #[test] + fn should_uppercase_step_id_for_env_var_lookup() { + // step id "cargo_fmt" should check "SKIP_CARGO_FMT_TEST" + with_env_var("SKIP_CARGO_FMT_TEST", "true", || { + assert!(should_skip_step("cargo_fmt")); + }); + } + + #[test] + fn should_handle_mixed_case_step_id() { + // step id "MyStep" should check "SKIP_MYSTEP_TEST" + with_env_var("SKIP_MYSTEP_TEST", "true", || { + assert!(should_skip_step("MyStep")); + }); + } + + #[test] + fn should_not_skip_when_env_var_is_empty_string() { + with_env_var("SKIP_EMPTYSTEP_TEST", "", || { + assert!(!should_skip_step("emptystep")); + }); + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 829351a3b..90ef6f4e3 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -208,9 +208,13 @@ pub fn get_registry_env(registry_name: String) -> HashMap { ]); let registry_prefix = format!("CARGO_REGISTRIES_{}", registry_name.replace("-", "_")).to_uppercase(); - if let Ok(index) = get_env_or_log(format!("{registry_prefix}_INDEX")) { - envs.insert(format!("{registry_prefix}_INDEX"), index.clone()); - } + let skip_git_vars = if let Ok(index) = get_env_or_log(format!("{registry_prefix}_INDEX")) { + let sparse = index.starts_with("sparse+"); + envs.insert(format!("{registry_prefix}_INDEX"), index); + sparse + } else { + true + }; if let Ok(token) = get_env_or_log(format!("{registry_prefix}_TOKEN")) { envs.insert(format!("{registry_prefix}_TOKEN"), token.clone()); envs.insert("Authorization".to_string(), token.clone()); @@ -219,14 +223,16 @@ pub fn get_registry_env(registry_name: String) -> HashMap { "cargo:token".to_string(), ); } - if let Ok(user_agent) = get_env_or_log(format!("{registry_prefix}_USER_AGENT")) { - envs.insert("CARGO_HTTP_USER_AGENT".to_string(), user_agent.clone()); - } - if let Ok(private_key) = get_env_or_log(format!("{registry_prefix}_PRIVATE_KEY")) { - envs.insert( - "GIT_SSH_COMMAND".to_string(), - format!("ssh -i {}", private_key.clone()), - ); + if !skip_git_vars { + if let Ok(user_agent) = get_env_or_log(format!("{registry_prefix}_USER_AGENT")) { + envs.insert("CARGO_HTTP_USER_AGENT".to_string(), user_agent); + } + if let Ok(private_key) = get_env_or_log(format!("{registry_prefix}_PRIVATE_KEY")) { + envs.insert( + "GIT_SSH_COMMAND".to_string(), + format!("ssh -i {}", private_key), + ); + } } envs }