1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::path::{Path, PathBuf}; use crate::error::*; #[derive(Clone, Debug, PartialEq)] pub struct LineCoverage { pub line_number: usize, pub count: u32, } #[derive(Clone, Debug, PartialEq)] pub struct BranchCoverage { pub line_number: Option<usize>, pub block_number: Option<usize>, pub taken: bool, } #[derive(Debug, PartialEq)] pub struct FileCoverage { path: PathBuf, #[doc(hidden)] pub line_coverages: Vec<LineCoverage>, #[doc(hidden)] pub branch_coverages: Vec<BranchCoverage>, } impl FileCoverage { #[cfg_attr(not(feature = "noinline"), inline)] pub fn new<P: Into<PathBuf>>( path: P, line_coverages: Vec<LineCoverage>, branch_coverages: Vec<BranchCoverage>, ) -> Self { Self { path: path.into(), line_coverages, branch_coverages, } } #[cfg_attr(not(feature = "noinline"), inline)] pub fn path(&self) -> &Path { &self.path } #[cfg_attr(not(feature = "noinline"), inline)] pub fn line_coverages(&self) -> &[LineCoverage] { &self.line_coverages } #[cfg_attr(not(feature = "noinline"), inline)] pub fn branch_coverages(&self) -> &[BranchCoverage] { &self.branch_coverages } } #[derive(Debug, PartialEq)] pub struct PackageCoverage { name: String, #[doc(hidden)] pub file_coverages: Vec<FileCoverage>, } impl PackageCoverage { pub fn new(file_coverages: Vec<FileCoverage>) -> Self { Self::with_test_name("", file_coverages) } pub fn with_test_name<T: Into<String>>(name: T, file_coverages: Vec<FileCoverage>) -> Self { Self { name: name.into(), file_coverages, } } pub fn name(&self) -> &str { &self.name } pub fn file_coverages(&self) -> &[FileCoverage] { &self.file_coverages } } #[doc(hidden)] pub trait TotalCoverage { fn line_executed(&self) -> usize; fn line_total(&self) -> usize; fn branch_executed(&self) -> usize; fn branch_total(&self) -> usize; } #[doc(hidden)] impl TotalCoverage for FileCoverage { #[cfg_attr(not(feature = "noinline"), inline)] fn line_executed(&self) -> usize { self.line_coverages.iter().filter(|&v| v.count > 0).count() } #[cfg_attr(not(feature = "noinline"), inline)] fn line_total(&self) -> usize { self.line_coverages.len() } #[cfg_attr(not(feature = "noinline"), inline)] fn branch_executed(&self) -> usize { self.branch_coverages.iter().filter(|&v| v.taken).count() } #[cfg_attr(not(feature = "noinline"), inline)] fn branch_total(&self) -> usize { self.branch_coverages.len() } } #[doc(hidden)] impl TotalCoverage for PackageCoverage { #[cfg_attr(not(feature = "noinline"), inline)] fn line_executed(&self) -> usize { self.file_coverages .iter() .fold(0, |sum, a| sum + a.line_executed()) } #[cfg_attr(not(feature = "noinline"), inline)] fn line_total(&self) -> usize { self.file_coverages .iter() .fold(0, |sum, a| sum + a.line_total()) } #[cfg_attr(not(feature = "noinline"), inline)] fn branch_executed(&self) -> usize { self.file_coverages .iter() .fold(0, |sum, a| sum + a.branch_executed()) } #[cfg_attr(not(feature = "noinline"), inline)] fn branch_total(&self) -> usize { self.file_coverages .iter() .fold(0, |sum, a| sum + a.branch_total()) } } pub trait CoverageReader { fn read<R: BufRead>(&self, reader: &mut R) -> Result<PackageCoverage, Error>; fn read_from_file(&self, path: &Path) -> Result<PackageCoverage, Error> { let f = fs::File::open(path) .chain_err(|| format!("Failed to open coverage file {:?}", path))?; let capacity = f.metadata().map(|m| m.len() as usize + 1).unwrap_or(8192); let mut reader = BufReader::with_capacity(capacity, f); self.read(&mut reader) } } pub trait CoverageWriter { fn write<W: Write>(&self, data: &PackageCoverage, writer: &mut W) -> Result<(), Error>; fn write_to_file(&self, data: &PackageCoverage, path: &Path) -> Result<(), Error> { let f = fs::File::create(path) .chain_err(|| format!("Failed to save coverage into file {:?}", path))?; let mut writer = BufWriter::new(f); self.write(&data, &mut writer) } }