Document coverage feature; Convenience query functions (#152)

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2024-02-22 14:59:06 -08:00
committed by GitHub
parent d3d5367fd4
commit 3a86c83827
4 changed files with 149 additions and 4 deletions
+19
View File
@@ -3,6 +3,7 @@
// Use README.md as crate documentation.
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
#![cfg_attr(docsrs, feature(doc_cfg))]
use serde::Serialize;
@@ -340,21 +341,39 @@ impl std::fmt::Debug for dyn Extension {
}
#[cfg(feature = "coverage")]
#[cfg_attr(docsrs, doc(cfg(feature = "coverage")))]
pub mod coverage {
#[derive(Default, serde::Serialize, serde::Deserialize)]
/// Coverage information about a rego policy file.
pub struct File {
/// Path of the policy file.
pub path: String,
/// The rego policy.
pub code: String,
/// Lines that were evaluated.
pub covered: std::collections::BTreeSet<u32>,
/// Lines that were not evaluated.
pub not_covered: std::collections::BTreeSet<u32>,
}
#[derive(Default, serde::Serialize, serde::Deserialize)]
/// Policy coverage report.
pub struct Report {
/// Coverage information for files.
pub files: Vec<File>,
}
impl Report {
/// Produce an ANSI color encoded version of the report.
///
/// Covered lines are green.
/// Lines that are not covered are red.
///
/// <img src="https://github.com/microsoft/regorus/blob/main/docs/coverage.png?raw=true">
pub fn to_colored_string(&self) -> anyhow::Result<String> {
use std::io::Write;
let mut s = Vec::new();