Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,6 +23,7 @@ resolver = "2"
members = [
"crates/fsl_test_api",
]
exclude = ["test_crates/wasm_example"]

[features]
alpha = []
Expand Down
88 changes: 85 additions & 3 deletions src/commands/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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"));
});
}
}
28 changes: 17 additions & 11 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ pub fn get_registry_env(registry_name: String) -> HashMap<String, String> {
]);
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());
Expand All @@ -219,14 +223,16 @@ pub fn get_registry_env(registry_name: String) -> HashMap<String, String> {
"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
}