Skip to content

Commit 4b9a9ce

Browse files
test(local): add compose override flags
Add environment variables which override the following compose flags: clean, allow_clean_on_panic, logs_on_panic. The used format for the variables is COMPOSE_$FLAG_NAME_CAPS, eg: COMPOSE_LOGS_ON_PANIC This should facilitate debugging the tests locally, without having to modify the test code...
1 parent 1aeb34a commit 4b9a9ce

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

composer/src/lib.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,41 @@ impl Builder {
512512
Ok(compose)
513513
}
514514

515+
fn override_flags(flag: &mut bool, flag_name: &str) {
516+
let key = format!("COMPOSE_{}", flag_name.to_ascii_uppercase());
517+
if let Some(val) = std::env::var_os(&key) {
518+
let clean = match val.to_str().unwrap_or_default() {
519+
"true" => true,
520+
"false" => false,
521+
_ => return,
522+
};
523+
if clean != *flag {
524+
tracing::warn!(
525+
"env::{} => Overriding the {} flag to {}",
526+
key,
527+
flag_name,
528+
clean
529+
);
530+
*flag = clean;
531+
}
532+
}
533+
}
534+
/// override clean flags with environment variable
535+
/// useful for testing without having to change the code
536+
fn override_clean(&mut self) {
537+
Self::override_flags(&mut self.clean, "clean");
538+
Self::override_flags(
539+
&mut self.allow_clean_on_panic,
540+
"allow_clean_on_panic",
541+
);
542+
Self::override_flags(&mut self.logs_on_panic, "logs_on_panic");
543+
}
544+
515545
/// build the config but don't start the containers
516546
async fn build_only(
517-
self,
547+
mut self,
518548
) -> Result<ComposeTest, Box<dyn std::error::Error>> {
549+
self.override_clean();
519550
let net: Ipv4Network = self.network.parse()?;
520551

521552
let path = std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR"));

0 commit comments

Comments
 (0)