#![allow(clippy::panic)]
const CARGO_TOML: &str = include_str!("../Cargo.toml");
fn profile_value<'a>(toml: &'a str, header: &str, key: &str) -> Option<&'a str> {
let mut in_section = false;
for line in toml.lines() {
let trimmed = line.trim();
if !in_section {
if trimmed == header {
in_section = true;
}
continue;
}
if trimmed.starts_with('[') {
break; }
if trimmed.starts_with('#') {
continue; }
if let Some((k, v)) = trimmed.split_once('=') {
if k.trim() == key {
return Some(v.trim().trim_matches('"'));
}
}
}
None
}
#[test]
fn release_profile_aborts_on_panic() {
assert_eq!(
profile_value(CARGO_TOML, "[profile.release]", "panic"),
Some("abort"),
"[profile.release] must set panic = \"abort\" (the fail-closed DoS posture)"
);
}
#[test]
fn python_profile_unwinds_on_panic() {
assert_eq!(
profile_value(CARGO_TOML, "[profile.python]", "panic"),
Some("unwind"),
"[profile.python] must set panic = \"unwind\" so PyO3 can surface a panic \
as a catchable PanicException instead of aborting the worker"
);
assert_eq!(
profile_value(CARGO_TOML, "[profile.python]", "inherits"),
Some("release"),
"[profile.python] must inherit `release` (same codegen as the shipped wheel)"
);
}
#[test]
fn profile_value_ignores_comment_prose() {
let sample = "\
# panic = \"unwind\" is mentioned here in a comment about [profile.python]
[profile.release]
# another comment: panic = \"unwind\"
panic = \"abort\"
[profile.python]
inherits = \"release\"
panic = \"unwind\"
";
assert_eq!(
profile_value(sample, "[profile.release]", "panic"),
Some("abort")
);
assert_eq!(
profile_value(sample, "[profile.python]", "panic"),
Some("unwind")
);
}