sloc-core 1.5.4

Source line analysis tool with CLI, web UI, HTML/PDF reports, and CI/CD integration
Documentation
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Per-file coverage metrics parsed from an LCOV `.info` file.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FileCoverage {
    pub lines_found: u32,
    pub lines_hit: u32,
    pub functions_found: u32,
    pub functions_hit: u32,
    pub branches_found: u32,
    pub branches_hit: u32,
}

impl FileCoverage {
    #[must_use]
    pub fn line_pct(&self) -> f64 {
        if self.lines_found == 0 {
            0.0
        } else {
            (f64::from(self.lines_hit) / f64::from(self.lines_found)) * 100.0
        }
    }

    #[must_use]
    pub fn function_pct(&self) -> f64 {
        if self.functions_found == 0 {
            0.0
        } else {
            (f64::from(self.functions_hit) / f64::from(self.functions_found)) * 100.0
        }
    }

    #[must_use]
    pub fn branch_pct(&self) -> f64 {
        if self.branches_found == 0 {
            0.0
        } else {
            (f64::from(self.branches_hit) / f64::from(self.branches_found)) * 100.0
        }
    }
}

/// Parse an LCOV `.info` file and return a map from source file path to coverage metrics.
///
/// Paths in the map are normalised to forward-slash separators and stored as-is from the
/// `SF:` record — callers are responsible for matching against `FileRecord.relative_path`.
#[must_use]
pub fn parse_lcov(content: &str) -> HashMap<PathBuf, FileCoverage> {
    let mut result: HashMap<PathBuf, FileCoverage> = HashMap::new();

    let mut current_path: Option<PathBuf> = None;
    let mut lf: u32 = 0;
    let mut lh: u32 = 0;
    let mut fnf: u32 = 0;
    let mut fnh: u32 = 0;
    let mut brf: u32 = 0;
    let mut brh: u32 = 0;

    for line in content.lines() {
        let line = line.trim();
        if let Some(path_str) = line.strip_prefix("SF:") {
            current_path = Some(PathBuf::from(path_str.replace('\\', "/")));
            lf = 0;
            lh = 0;
            fnf = 0;
            fnh = 0;
            brf = 0;
            brh = 0;
        } else if line == "end_of_record" {
            if let Some(path) = current_path.take() {
                result.insert(
                    path,
                    FileCoverage {
                        lines_found: lf,
                        lines_hit: lh,
                        functions_found: fnf,
                        functions_hit: fnh,
                        branches_found: brf,
                        branches_hit: brh,
                    },
                );
            }
        } else if let Some(val) = line.strip_prefix("LF:") {
            lf = val.parse().unwrap_or(0);
        } else if let Some(val) = line.strip_prefix("LH:") {
            lh = val.parse().unwrap_or(0);
        } else if let Some(val) = line.strip_prefix("FNF:") {
            fnf = val.parse().unwrap_or(0);
        } else if let Some(val) = line.strip_prefix("FNH:") {
            fnh = val.parse().unwrap_or(0);
        } else if let Some(val) = line.strip_prefix("BRF:") {
            brf = val.parse().unwrap_or(0);
        } else if let Some(val) = line.strip_prefix("BRH:") {
            brh = val.parse().unwrap_or(0);
        }
    }

    result
}

/// Attempt to match a `FileRecord`'s `relative_path` against the coverage map.
///
/// LCOV paths are typically absolute (`/path/to/repo/src/foo.c`) while oxide-sloc stores
/// relative paths (`src/foo.c`). This function tries three strategies in order:
/// 1. Direct `PathBuf` key lookup (exact match or already-relative paths)
/// 2. Suffix match: find a coverage path whose components end with the relative path
/// 3. Filename-only fallback when the relative path is a bare filename
#[must_use]
#[allow(clippy::implicit_hasher)] // public API; callers always use the default hasher
pub fn lookup_coverage<'a>(
    map: &'a HashMap<PathBuf, FileCoverage>,
    relative_path: &str,
) -> Option<&'a FileCoverage> {
    let rel = PathBuf::from(relative_path.replace('\\', "/"));

    // Strategy 1: exact key
    if let Some(cov) = map.get(&rel) {
        return Some(cov);
    }

    // Strategy 2: any coverage path whose tail matches the relative path
    let rel_components: Vec<_> = rel.components().collect();
    for (cov_path, cov) in map {
        let cov_components: Vec<_> = cov_path.components().collect();
        if cov_components.len() >= rel_components.len()
            && cov_components[cov_components.len() - rel_components.len()..] == rel_components[..]
        {
            return Some(cov);
        }
    }

    // Strategy 3: filename-only fallback
    let filename = rel.file_name()?;
    for (cov_path, cov) in map {
        if cov_path.file_name() == Some(filename) {
            return Some(cov);
        }
    }

    None
}

/// Compute a weighted-average line coverage percentage across all files that have coverage data.
/// Returns `None` if no files have coverage data or if total `lines_found` is zero.
#[must_use]
pub fn aggregate_line_coverage(records: &[&FileCoverage]) -> Option<f64> {
    let total_found: u64 = records.iter().map(|c| u64::from(c.lines_found)).sum();
    if total_found == 0 {
        return None;
    }
    let total_hit: u64 = records.iter().map(|c| u64::from(c.lines_hit)).sum();
    // ratio/percentage display, precision loss acceptable
    #[allow(clippy::cast_precision_loss)]
    Some((total_hit as f64 / total_found as f64) * 100.0)
}

/// Auto-detect coverage file format from path extension and content, then dispatch to the
/// appropriate parser. Falls back to LCOV for unknown extensions.
#[must_use]
pub fn parse_coverage_auto(path: &Path, content: &str) -> HashMap<PathBuf, FileCoverage> {
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_ascii_lowercase();
    match ext.as_str() {
        "xml" => {
            let snip = &content[..content.len().min(512)];
            if snip.contains("<coverage") {
                parse_cobertura(content)
            } else if snip.contains("<report") {
                parse_jacoco(content)
            } else {
                HashMap::new()
            }
        }
        "json" => parse_istanbul(content),
        _ => parse_lcov(content),
    }
}

/// Parse a Cobertura XML coverage file (`coverage.xml`) into a per-file coverage map.
///
/// Cobertura is produced by pytest-cov (`--cov-report xml`), Maven Cobertura plugin, and others.
/// The `filename` attribute on `<class>` is already relative to the project root.
#[must_use]
pub fn parse_cobertura(content: &str) -> HashMap<PathBuf, FileCoverage> {
    let mut result: HashMap<PathBuf, FileCoverage> = HashMap::new();
    let mut remaining = content;
    while let Some(class_start) = remaining.find("<class ") {
        remaining = &remaining[class_start + 7..];
        let Some(filename) = extract_attr(remaining, "filename") else {
            continue;
        };
        let class_end = remaining.find("</class>").unwrap_or(remaining.len());
        let class_block = &remaining[..class_end];
        let (lines_found, lines_hit, branch_found, branch_hit) = cobertura_scan_lines(class_block);
        let (method_found, method_hit) = cobertura_scan_methods(class_block);
        let entry = result
            .entry(PathBuf::from(&filename))
            .or_insert(FileCoverage {
                lines_found: 0,
                lines_hit: 0,
                functions_found: 0,
                functions_hit: 0,
                branches_found: 0,
                branches_hit: 0,
            });
        entry.lines_found += lines_found;
        entry.lines_hit += lines_hit;
        entry.functions_found += method_found;
        entry.functions_hit += method_hit;
        entry.branches_found += branch_found;
        entry.branches_hit += branch_hit;
    }
    result
}

/// Count `<line>` hits and branch coverage within a Cobertura `<class>` block.
fn cobertura_scan_lines(class_block: &str) -> (u32, u32, u32, u32) {
    let mut lines_found: u32 = 0;
    let mut lines_hit: u32 = 0;
    let mut branch_found: u32 = 0;
    let mut branch_hit: u32 = 0;
    let mut scan = class_block;
    while let Some(pos) = scan.find("<line ") {
        scan = &scan[pos + 6..];
        lines_found += 1;
        if extract_attr(scan, "hits").is_some_and(|h| h.trim() != "0") {
            lines_hit += 1;
        }
        if extract_attr(scan, "branch").as_deref() == Some("true") {
            let (hit, found) = parse_cobertura_branch_fraction(scan);
            branch_hit += hit;
            branch_found += found;
        }
    }
    (lines_found, lines_hit, branch_found, branch_hit)
}

/// Parse `condition-coverage="50% (1/2)"` → `(hit=1, found=2)`.
fn parse_cobertura_branch_fraction(scan: &str) -> (u32, u32) {
    let Some(cond) = extract_attr(scan, "condition-coverage") else {
        return (0, 0);
    };
    let Some(frac_start) = cond.find('(') else {
        return (0, 0);
    };
    let frac_str = &cond[frac_start + 1..];
    let Some(slash) = frac_str.find('/') else {
        return (0, 0);
    };
    let num: u32 = frac_str[..slash].trim().parse().unwrap_or(0);
    let den_end = frac_str[slash + 1..].find(')').unwrap_or(0);
    let den: u32 = frac_str[slash + 1..slash + 1 + den_end]
        .trim()
        .parse()
        .unwrap_or(0);
    (num, den)
}

/// Count `<method>` elements and how many have a non-zero line-rate in a Cobertura class block.
fn cobertura_scan_methods(class_block: &str) -> (u32, u32) {
    let mut method_found: u32 = 0;
    let mut method_hit: u32 = 0;
    let mut mscan = class_block;
    while let Some(pos) = mscan.find("<method ") {
        mscan = &mscan[pos + 8..];
        method_found += 1;
        let rate: f64 = extract_attr(mscan, "line-rate")
            .and_then(|lr| lr.parse().ok())
            .unwrap_or(0.0);
        if rate > 0.0 {
            method_hit += 1;
        }
    }
    (method_found, method_hit)
}

/// Parse a `JaCoCo` XML report (`jacoco.xml`) into a per-file coverage map.
///
/// `JaCoCo` is produced by the Gradle `jacocoTestReport` task and the Maven `JaCoCo` plugin.
/// Paths are reconstructed as `package/sourcefile` (e.g. `com/example/Main.java`).
#[must_use]
pub fn parse_jacoco(content: &str) -> HashMap<PathBuf, FileCoverage> {
    let mut result: HashMap<PathBuf, FileCoverage> = HashMap::new();
    let mut scan = content;
    while let Some(pkg_start) = scan.find("<package ") {
        scan = &scan[pkg_start + 9..];
        let pkg_name = extract_attr(scan, "name").unwrap_or_default();
        let pkg_end = scan.find("</package>").unwrap_or(scan.len());
        parse_jacoco_package(&scan[..pkg_end], &pkg_name, &mut result);
        if pkg_end < scan.len() {
            scan = &scan[pkg_end..];
        } else {
            break;
        }
    }
    result
}

fn parse_jacoco_package(
    pkg_block: &str,
    pkg_name: &str,
    result: &mut HashMap<PathBuf, FileCoverage>,
) {
    let mut sf_scan = pkg_block;
    while let Some(sf_start) = sf_scan.find("<sourcefile ") {
        sf_scan = &sf_scan[sf_start + 12..];
        let Some(sf_name) = extract_attr(sf_scan, "name") else {
            continue;
        };
        let sf_end = sf_scan.find("</sourcefile>").unwrap_or(sf_scan.len());
        let cov = parse_jacoco_counters(&sf_scan[..sf_end]);
        let path = if pkg_name.is_empty() {
            PathBuf::from(&sf_name)
        } else {
            PathBuf::from(format!("{pkg_name}/{sf_name}"))
        };
        result.insert(path, cov);
    }
}

fn parse_jacoco_counters(sf_block: &str) -> FileCoverage {
    let mut lines_found: u32 = 0;
    let mut lines_hit: u32 = 0;
    let mut fn_found: u32 = 0;
    let mut fn_hit: u32 = 0;
    let mut br_found: u32 = 0;
    let mut br_hit: u32 = 0;
    let mut cscan = sf_block;
    while let Some(cpos) = cscan.find("<counter ") {
        cscan = &cscan[cpos + 9..];
        let ctype = extract_attr(cscan, "type").unwrap_or_default();
        let missed: u32 = extract_attr(cscan, "missed")
            .and_then(|v| v.parse().ok())
            .unwrap_or(0);
        let covered: u32 = extract_attr(cscan, "covered")
            .and_then(|v| v.parse().ok())
            .unwrap_or(0);
        match ctype.as_str() {
            "LINE" => {
                lines_found = missed + covered;
                lines_hit = covered;
            }
            "METHOD" => {
                fn_found = missed + covered;
                fn_hit = covered;
            }
            "BRANCH" => {
                br_found = missed + covered;
                br_hit = covered;
            }
            _ => {}
        }
    }
    FileCoverage {
        lines_found,
        lines_hit,
        functions_found: fn_found,
        functions_hit: fn_hit,
        branches_found: br_found,
        branches_hit: br_hit,
    }
}

/// Parse an Istanbul/NYC `coverage-summary.json` file into a per-file coverage map.
///
/// Istanbul is produced by `nyc --reporter=json-summary` and by many Jest configurations.
/// The top-level keys are absolute file paths.
#[must_use]
pub fn parse_istanbul(content: &str) -> HashMap<PathBuf, FileCoverage> {
    let mut result: HashMap<PathBuf, FileCoverage> = HashMap::new();

    let Ok(root) = serde_json::from_str::<serde_json::Value>(content) else {
        return result;
    };
    let Some(obj) = root.as_object() else {
        return result;
    };

    for (path_str, file_val) in obj {
        // Skip the top-level "total" key
        if path_str == "total" {
            continue;
        }
        // Line/function/branch counts are always small; truncation is not possible in practice.
        #[allow(clippy::cast_possible_truncation)]
        let lines_total: u32 = file_val["lines"]["total"].as_u64().unwrap_or(0) as u32;
        #[allow(clippy::cast_possible_truncation)]
        let lines_covered: u32 = file_val["lines"]["covered"].as_u64().unwrap_or(0) as u32;
        #[allow(clippy::cast_possible_truncation)]
        let fn_total: u32 = file_val["functions"]["total"].as_u64().unwrap_or(0) as u32;
        #[allow(clippy::cast_possible_truncation)]
        let fn_covered: u32 = file_val["functions"]["covered"].as_u64().unwrap_or(0) as u32;
        #[allow(clippy::cast_possible_truncation)]
        let br_total: u32 = file_val["branches"]["total"].as_u64().unwrap_or(0) as u32;
        #[allow(clippy::cast_possible_truncation)]
        let br_covered: u32 = file_val["branches"]["covered"].as_u64().unwrap_or(0) as u32;

        result.insert(
            PathBuf::from(path_str.replace('\\', "/")),
            FileCoverage {
                lines_found: lines_total,
                lines_hit: lines_covered,
                functions_found: fn_total,
                functions_hit: fn_covered,
                branches_found: br_total,
                branches_hit: br_covered,
            },
        );
    }

    result
}

/// Extract the value of a named XML attribute from a fragment of XML text.
/// Handles both `attr="value"` and `attr='value'` quoting.
fn extract_attr(fragment: &str, attr: &str) -> Option<String> {
    let needle = format!("{attr}=");
    let pos = fragment.find(&needle)?;
    let after = &fragment[pos + needle.len()..];
    let quote = after.chars().next()?;
    if quote == '"' || quote == '\'' {
        let inner = &after[1..];
        let end = inner.find(quote)?;
        Some(inner[..end].to_string())
    } else {
        None
    }
}

/// Resolve a coverage file path from the environment variable `SLOC_COVERAGE_FILE` or the
/// provided config path, normalising to an absolute `PathBuf`.
#[must_use]
pub fn resolve_coverage_file(config_path: Option<&Path>) -> Option<PathBuf> {
    if let Ok(env_path) = std::env::var("SLOC_COVERAGE_FILE") {
        if !env_path.is_empty() {
            return Some(PathBuf::from(env_path));
        }
    }
    config_path.map(PathBuf::from)
}