-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cobertura conciseness and summary option #1180
Open
rshearman
wants to merge
5
commits into
mozilla:master
Choose a base branch
from
rshearman:cobertura-summary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74e48c5
Make output of plain lines in cobertura reports more concise
rshearman 9db5eeb
Make cobertura output more concise and add cobertura-pretty output op…
rshearman b4c0c21
Add --print-summary option
rshearman 3429f54
Merge branch 'master' into cobertura-summary
marco-c 1bcca5f
Merge branch 'master' into cobertura-summary
marco-c File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use std::path::PathBuf; | ||
|
||
use crate::CovResult; | ||
|
||
#[derive(Debug, Default)] | ||
struct CoverageStats { | ||
lines_covered: i64, | ||
lines_valid: i64, | ||
branches_covered: i64, | ||
branches_valid: i64, | ||
} | ||
|
||
fn get_coverage_stats(results: &[(PathBuf, PathBuf, CovResult)]) -> CoverageStats { | ||
results | ||
.iter() | ||
.fold(CoverageStats::default(), |stats, (_, _, result)| { | ||
let (lines_covered, lines_valid) = result.lines.values().fold( | ||
(stats.lines_covered, stats.lines_valid), | ||
|(covered, valid), l| { | ||
if *l == 0 { | ||
(covered, valid + 1) | ||
} else { | ||
(covered + 1, valid + 1) | ||
} | ||
}, | ||
); | ||
let (branches_covered, branches_valid) = result.branches.values().fold( | ||
(stats.branches_covered, stats.branches_valid), | ||
|(covered, valid), branches| { | ||
branches | ||
.iter() | ||
.fold((covered, valid), |(covered, valid), b| { | ||
if *b { | ||
(covered + 1, valid + 1) | ||
} else { | ||
(covered, valid + 1) | ||
} | ||
}) | ||
}, | ||
); | ||
CoverageStats { | ||
lines_covered, | ||
lines_valid, | ||
branches_covered, | ||
branches_valid, | ||
} | ||
}) | ||
} | ||
|
||
pub fn print_summary(results: &[(PathBuf, PathBuf, CovResult)]) { | ||
let stats = get_coverage_stats(results); | ||
let lines_percentage = if stats.lines_valid == 0 { | ||
0.0 | ||
} else { | ||
(stats.lines_covered as f64 / stats.lines_valid as f64) * 100.0 | ||
}; | ||
let branches_percentage = if stats.branches_valid == 0 { | ||
0.0 | ||
} else { | ||
(stats.branches_covered as f64 / stats.branches_valid as f64) * 100.0 | ||
}; | ||
println!( | ||
"lines: {:.1}% ({} out of {})", | ||
lines_percentage, stats.lines_covered, stats.lines_valid | ||
); | ||
println!( | ||
"branches: {:.1}% ({} out of {})", | ||
branches_percentage, stats.branches_covered, stats.branches_valid | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::{collections::BTreeMap, path::PathBuf}; | ||
|
||
use rustc_hash::FxHashMap; | ||
|
||
use crate::{CovResult, Function}; | ||
|
||
use super::get_coverage_stats; | ||
|
||
#[test] | ||
fn test_summary() { | ||
let results = vec![( | ||
PathBuf::from("src/main.rs"), | ||
PathBuf::from("src/main.rs"), | ||
CovResult { | ||
/* main.rs | ||
fn main() { | ||
let inp = "a"; | ||
if "a" == inp { | ||
println!("a"); | ||
} else if "b" == inp { | ||
println!("b"); | ||
} | ||
println!("what?"); | ||
} | ||
*/ | ||
lines: [ | ||
(1, 1), | ||
(2, 1), | ||
(3, 2), | ||
(4, 1), | ||
(5, 0), | ||
(6, 0), | ||
(8, 1), | ||
(9, 1), | ||
] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
branches: { | ||
let mut map = BTreeMap::new(); | ||
map.insert(3, vec![true, false]); | ||
map.insert(5, vec![false, false]); | ||
map | ||
}, | ||
functions: { | ||
let mut map = FxHashMap::default(); | ||
map.insert( | ||
"_ZN8cov_test4main17h7eb435a3fb3e6f20E".to_string(), | ||
Function { | ||
start: 1, | ||
executed: true, | ||
}, | ||
); | ||
map | ||
}, | ||
}, | ||
)]; | ||
|
||
let stats = get_coverage_stats(&results); | ||
assert_eq!(stats.lines_covered, 6); | ||
assert_eq!(stats.lines_valid, 8); | ||
assert_eq!(stats.branches_covered, 1); | ||
assert_eq!(stats.branches_valid, 4); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we have "summary" as one of the possible output types instead of adding a --print-summary option?