Skip to content
Open
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 .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
- name: Setup Rust
run: |
rustup update stable
rustup toolchain add stable --component llvm-tools
rustup toolchain add stable --component llvm-tools,rustfmt,clippy
rustup override set stable
- name: Install pre-commit
run: pip install --no-cache-dir pre-commit
Expand Down
44 changes: 44 additions & 0 deletions src/cobertura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ fn write_lines(writer: &mut Writer<Cursor<Vec<u8>>>, lines: &[Line]) {
l.push_attribute(("number", number.to_string().as_ref()));
l.push_attribute(("hits", hits.to_string().as_ref()));
l.push_attribute(("branch", "true"));
l.push_attribute((
"condition-coverage",
format_condition_coverage(conditions).as_ref(),
));

writer.write_event(Event::Start(l)).unwrap();

let conditions_tag = "conditions";
Expand Down Expand Up @@ -562,6 +567,23 @@ fn write_lines(writer: &mut Writer<Cursor<Vec<u8>>>, lines: &[Line]) {
.unwrap();
}

fn format_condition_coverage(conditions: &[Condition]) -> String {
let conditions_hit: f64 = conditions.iter().map(|c| c.coverage).sum();
let num_conditions = conditions.len();
if num_conditions > 0 {
format!(
"{:.0}% ({:.0}/{})",
100.0 * conditions_hit / num_conditions as f64,
conditions_hit,
num_conditions
)
} else {
// What is the sensible string to put here,
// when a branch has no conditions?
"0% (0/0)".to_owned()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -860,4 +882,26 @@ mod tests {
assert!(results.contains(r#"<source>src</source>"#));
assert!(results.contains(r#"package name="main.rs""#));
}

#[test]
fn test_condition_coverage() {
const HIT: Condition = Condition {
coverage: 1.0,
number: 0,
cond_type: ConditionType::Jump,
};
const MISS: Condition = Condition {
coverage: 0.0,
number: 0,
cond_type: ConditionType::Jump,
};

assert_eq!("100% (1/1)", format_condition_coverage(&[HIT]));
assert_eq!("0% (0/1)", format_condition_coverage(&[MISS]));
assert_eq!("50% (1/2)", format_condition_coverage(&[HIT, MISS]));
assert_eq!("33% (1/3)", format_condition_coverage(&[HIT, MISS, MISS]));
assert_eq!("67% (2/3)", format_condition_coverage(&[HIT, HIT, MISS]));

assert_eq!("0% (0/0)", format_condition_coverage(&[]));
}
}
Loading