Skip to content

Commit 6d418c0

Browse files
authored
Fix calculated percentage of badges (mozilla#782)
This patch fixes an issue with the calculation of badges percentage. The issue is that all badges were being generated with a `0%` percentage on them, no matter the coverage. This was due to an integer division that always resulted in zero. Doing a floating-point division instead solves it.
1 parent 9f46172 commit 6d418c0

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/html.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::collections::HashMap;
55
use std::collections::{btree_map, BTreeMap};
66
use std::fs::{self, File};
77
use std::io::{BufRead, BufReader, Write};
8-
use std::ops::{Div, Mul};
98
use std::path::{Path, PathBuf};
109
use std::sync::{Arc, Mutex};
1110
use tera::try_get_value;
@@ -153,18 +152,13 @@ fn get_stats(result: &CovResult) -> HtmlStats {
153152
}
154153

155154
#[inline(always)]
156-
fn get_percentage<
157-
T: Div + Mul + PartialOrd + std::ops::Mul<Output = T> + std::ops::Div<Output = T> + From<u8>,
158-
>(
159-
x: T,
160-
y: T,
161-
) -> T {
162-
if y != T::from(0) {
163-
x / y * T::from(100)
155+
fn get_percentage_of_covered_lines(covered_lines: usize, total_lines: usize) -> f64 {
156+
if total_lines != 0 {
157+
covered_lines as f64 / total_lines as f64 * 100.0
164158
} else {
165159
// If the file is empty (no lines) then the coverage
166160
// must be 100% (0% means "bad" which is not the case).
167-
T::from(100)
161+
100.0
168162
}
169163
}
170164

@@ -174,7 +168,7 @@ fn percent(args: &HashMap<String, Value>) -> tera::Result<Value> {
174168
from_value::<usize>(n.clone()),
175169
from_value::<usize>(d.clone()),
176170
) {
177-
Ok(to_value(get_percentage(num as f64, den as f64)).unwrap())
171+
Ok(to_value(get_percentage_of_covered_lines(num, den)).unwrap())
178172
} else {
179173
Err(tera::Error::msg("Invalid arguments"))
180174
}
@@ -466,7 +460,7 @@ pub fn gen_badge(tera: &Tera, stats: &HtmlStats, conf: &Config, output: &Path, s
466460
let mut ctx = make_context();
467461
ctx.insert(
468462
"current",
469-
&get_percentage(stats.covered_lines, stats.total_lines),
463+
&(get_percentage_of_covered_lines(stats.covered_lines, stats.total_lines) as usize),
470464
);
471465
ctx.insert("hi_limit", &conf.hi_limit);
472466
ctx.insert("med_limit", &conf.med_limit);
@@ -511,7 +505,7 @@ pub fn gen_coverage_json(stats: &HtmlStats, conf: &Config, output: &Path) {
511505
Ok(f) => f,
512506
};
513507

514-
let coverage = get_percentage(stats.covered_lines, stats.total_lines);
508+
let coverage = get_percentage_of_covered_lines(stats.covered_lines, stats.total_lines) as usize;
515509

516510
let res = serde_json::to_writer(
517511
&mut output_stream,
@@ -533,3 +527,17 @@ pub fn gen_coverage_json(stats: &HtmlStats, conf: &Config, output: &Path) {
533527
eprintln!("cannot write the file {:?}", output_file);
534528
}
535529
}
530+
531+
#[cfg(test)]
532+
mod tests {
533+
use super::get_percentage_of_covered_lines;
534+
535+
#[test]
536+
fn test_get_percentage_of_covered_lines() {
537+
assert_eq!(get_percentage_of_covered_lines(5, 5), 100.0);
538+
assert_eq!(get_percentage_of_covered_lines(1, 2), 50.0);
539+
assert_eq!(get_percentage_of_covered_lines(200, 500), 40.0);
540+
assert_eq!(get_percentage_of_covered_lines(0, 0), 100.0);
541+
assert_eq!(get_percentage_of_covered_lines(5, 0), 100.0);
542+
}
543+
}

0 commit comments

Comments
 (0)