use crate::cache::{self, DependencyJson, FixJson};
use crate::config::HtlConfig;
use crate::diagnostic::{Diagnostic, Severity};
use crate::testing::{FileReport, RunOptions, TestSession};
use crate::{CheckInfo, Fix, Htl};
use anyhow::Result;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::{Component, Path, PathBuf};
pub trait Output {
fn diagnostic(
&mut self,
severity: Severity,
text: &str,
fix: Option<&Fix>,
dependency: Option<&DependencyJson>,
);
}
#[derive(Debug, Default)]
pub struct Collect {
pub diagnostics: Vec<Diagnostic>,
}
impl Collect {
pub fn take(&mut self) -> Vec<Diagnostic> {
std::mem::take(&mut self.diagnostics)
}
}
impl Output for Collect {
fn diagnostic(
&mut self,
severity: Severity,
text: &str,
fix: Option<&Fix>,
dependency: Option<&DependencyJson>,
) {
self.diagnostics
.push(diagnostic_of(severity, text, fix, dependency));
}
}
pub fn diagnostic_of(
severity: Severity,
text: &str,
fix: Option<&Fix>,
dependency: Option<&DependencyJson>,
) -> Diagnostic {
let mut d = Diagnostic::parse(severity, text);
d.fix = fix.cloned();
if let Some(dep) = dependency {
d.required_by = Some(dep.required_by.clone());
d.origin = dep.origin.clone();
}
d
}
pub struct Sink<O: Output> {
out: O,
recorded: Vec<cache::Recorded>,
walked: HashSet<PathBuf>,
reported: HashSet<PathBuf>,
dependency_errors: usize,
levels: Option<crate::lint::Selection>,
denied: usize,
}
impl<O: Output> Sink<O> {
pub fn new(out: O) -> Self {
Self {
out,
recorded: Vec::new(),
walked: Default::default(),
reported: Default::default(),
dependency_errors: 0,
levels: None,
denied: 0,
}
}
pub fn judge_by(&mut self, sel: &crate::lint::Selection) {
self.levels = Some(sel.clone());
}
pub fn denied(&self) -> usize {
self.denied
}
pub fn out(&mut self) -> &mut O {
&mut self.out
}
pub fn walking(&mut self, files: &[PathBuf]) {
self.walked = files.iter().map(|f| canonical(f)).collect();
}
pub fn diag(&mut self, severity: Severity, text: &str) {
self.diag_with_fix(severity, text, None);
}
pub fn checkinfo(&mut self, c: &CheckInfo) {
for w in &c.warnings {
self.diag(Severity::Warning, w);
}
for (i, l) in c.lints.iter().enumerate() {
self.diag_with_fix(
Severity::Lint,
l,
c.lint_fixes.get(i).and_then(|f| f.as_ref()),
);
}
for (i, e) in c.errors.iter().enumerate() {
self.diag_with_fix(
Severity::Error,
e,
c.error_fixes.get(i).and_then(|f| f.as_ref()),
);
}
}
pub fn dependency_errors(
&mut self,
c: &CheckInfo,
origin_of: &dyn Fn(&Path) -> Option<&'static str>,
) {
for e in &c.dependency_errors {
let dep = DependencyJson {
file: e.file.display().to_string(),
required_by: e.required_by.display().to_string(),
origin: origin_of(&e.file).map(str::to_string),
};
self.recorded.push(cache::Recorded {
severity: Severity::Error.as_str().to_string(),
text: e.text.clone(),
fix: None,
dependency: Some(dep.clone()),
});
self.emit(Severity::Error, &e.text, None, Some(&dep));
}
}
pub fn dependency_error_count(&self) -> usize {
self.dependency_errors
}
fn diag_with_fix(&mut self, severity: Severity, text: &str, fix: Option<&Fix>) {
self.recorded.push(cache::Recorded {
severity: severity.as_str().to_string(),
text: text.to_string(),
fix: fix.map(FixJson::from_fix),
dependency: None,
});
self.emit(severity, text, fix, None);
}
fn emit(
&mut self,
severity: Severity,
text: &str,
fix: Option<&Fix>,
dependency: Option<&DependencyJson>,
) {
if let Some(d) = dependency {
let file = canonical(Path::new(&d.file));
if self.walked.contains(&file) || !self.reported.insert(file) {
return;
}
self.dependency_errors += 1;
}
let text = match dependency {
Some(_) => shown(text),
None => Cow::Borrowed(text),
};
if severity != Severity::Error
&& let Some(levels) = &self.levels
&& let Some(rule) = crate::diagnostic::rule_of(text.as_ref())
&& levels.level_of(rule) == crate::lint::Level::Deny
{
self.denied += 1;
}
self.out
.diagnostic(severity, text.as_ref(), fix, dependency);
}
pub fn replay(&mut self, recorded: &[cache::Recorded]) -> Result<()> {
let severities = recorded
.iter()
.map(|r| match Severity::parse(&r.severity) {
Some(s) => Ok(s),
None => anyhow::bail!("cache entry has an unknown severity: {}", r.severity),
})
.collect::<Result<Vec<Severity>>>()?;
for (severity, r) in severities.into_iter().zip(recorded) {
let fix = r.fix.as_ref().map(FixJson::to_fix);
self.emit(severity, &r.text, fix.as_ref(), r.dependency.as_ref());
}
Ok(())
}
pub fn take_recorded(&mut self) -> Vec<cache::Recorded> {
std::mem::take(&mut self.recorded)
}
}
fn canonical(p: &Path) -> PathBuf {
std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
}
fn shown(text: &str) -> Cow<'_, str> {
let Some((file, _, _, _)) = crate::diagnostic::position(text) else {
return Cow::Borrowed(text);
};
let shown = display_path(Path::new(file));
if shown == file {
return Cow::Borrowed(text);
}
Cow::Owned(format!("{shown}{}", &text[file.len()..]))
}
pub fn display_path(p: &Path) -> String {
let norm = lexical(p);
match std::env::current_dir()
.ok()
.and_then(|cwd| norm.strip_prefix(cwd).ok().map(Path::to_path_buf))
{
Some(rel) => rel.display().to_string(),
None => norm.display().to_string(),
}
}
fn lexical(p: &Path) -> PathBuf {
let mut out = PathBuf::new();
for c in p.components() {
match c {
Component::ParentDir => {
out.pop();
}
Component::CurDir => {}
c => out.push(c.as_os_str()),
}
}
out
}
pub type Config = Option<(PathBuf, PathBuf, HtlConfig)>;
pub fn config_of(first: &Path) -> Result<Config> {
Ok(HtlConfig::find(first)?.map(|(p, c)| (crate::parent_dir(&p), p, c)))
}
pub fn patched(paths: &[PathBuf]) -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = Vec::new();
for p in paths {
for d in crate::patched_dirs(p) {
if !out.contains(&d) {
out.push(d);
}
}
}
out
}
pub fn store_refusal(root: &Path, has_config: bool) -> Option<String> {
if !has_config {
return Some("no htl.toml".to_string());
}
cache::scratch_root(root).map(str::to_string)
}
pub fn store(
root: &Path,
opts: cache::Options,
refusal: Option<&str>,
tag: &str,
) -> Option<cache::Cache> {
if let Some(why) = refusal {
if opts.explain {
eprintln!("htl cache: not used by {tag} ({why}: {})", root.display());
}
return None;
}
cache::Cache::open(root, opts)
}
pub fn cache_options(
enabled: bool,
mode: Option<cache::Mode>,
cfg: &Config,
explain: bool,
) -> cache::Options {
let mode = mode
.or_else(|| {
cfg.as_ref()
.and_then(|(_, _, c)| c.cache.mode.as_deref())
.map(|m| {
cache::Mode::parse(m).unwrap_or_else(|| {
eprintln!("htl: unknown [cache] mode {m:?}, using per-module");
cache::Mode::PerModule
})
})
})
.unwrap_or_default();
let env = cache::Options::from_env();
cache::Options {
enabled: enabled && env.enabled,
mode,
explain: explain || env.explain,
max_entries: env.max_entries,
}
}
pub fn search_dirs(file: &Path, root: &Path, cfg: &Config) -> Vec<PathBuf> {
cache::search_dirs(file, root, cfg.as_ref().map(|(r, _, c)| (r.as_path(), c)))
}
pub fn checker(cfg: &Config, paths: &[PathBuf], sel: &crate::lint::Selection) -> Result<Htl> {
let h = Htl::new()?;
h.select_lints(sel)?;
if let Some(first) = paths.first()
&& let Some(p) = crate::pkg::Project::find(first)
{
h.apply_project(&p)?;
}
if let Some((root, _, c)) = cfg {
h.apply_config(root, c)?;
}
h.install_test_lib()?;
#[cfg(feature = "std")]
h.install_std()?;
Ok(h)
}
pub struct Origins {
dependency: Vec<PathBuf>,
external: Vec<PathBuf>,
}
impl Origins {
pub fn new(
start: &Path,
root: &Path,
cfg: &Config,
contracts: &[crate::contract::Resolved],
) -> Self {
let canon = |p: PathBuf| std::fs::canonicalize(&p).unwrap_or(p);
let mut dependency = Vec::new();
if let Some(p) = crate::pkg::Project::find(start) {
dependency.push(canon(p.pkgs_dir.clone()));
dependency.extend(p.target_dirs.iter().cloned().map(canon));
}
let mut external = Vec::new();
if let Some((r, _, c)) = cfg {
external.extend(
c.check
.paths
.iter()
.map(|p| canon(crate::config::resolve_path(r, p))),
);
for c in contracts {
external.extend(c.dirs(root).into_iter().map(canon));
}
}
Self {
dependency,
external,
}
}
pub fn of(&self, file: &Path) -> Option<&'static str> {
let file = std::fs::canonicalize(file).unwrap_or_else(|_| file.to_path_buf());
if self.dependency.iter().any(|d| file.starts_with(d)) {
Some("dependency")
} else if self.external.iter().any(|d| file.starts_with(d)) {
Some("external")
} else {
None
}
}
}
pub struct Walk<'a> {
pub cfg: &'a Config,
pub contracts: &'a [crate::contract::Resolved],
pub origins: &'a Origins,
pub host_modules: &'a [String],
pub lints: &'a crate::lint::Lints,
}
pub fn check_one<O: Output>(
h: &Htl,
sink: &mut Sink<O>,
f: &Path,
w: &Walk<'_>,
) -> Result<cache::Module> {
let (cfg, contracts, origins, host_modules, lints) =
(w.cfg, w.contracts, w.origins, w.host_modules, w.lints);
let saved = h.search_path()?;
h.add_layout_paths(f)?;
let c = h.check(f)?;
sink.checkinfo(&c);
let mut lints_said = c.lints.len();
if lints.on("duplicate-declaration") || lints.on("host-module-shadowed") {
for l in lints.keep(crate::declaration_conflict_lints(h, f, &c, host_modules)?) {
sink.diag(Severity::Lint, &l);
lints_said += 1;
}
}
if let Some((root, _, cfg)) = cfg
&& c.ok()
&& lints.on("contract")
{
for l in lints.keep(crate::contract_lints(h, root, cfg, contracts, f)?) {
sink.diag(Severity::Lint, &l);
lints_said += 1;
}
}
sink.dependency_errors(&c, &|p| origins.of(p));
h.set_search_path(&saved)?;
Ok(cache::Module {
diagnostics: sink.take_recorded(),
errors: c.errors.len(),
warnings: c.warnings.len(),
lints: lints_said,
deps: c.deps.iter().map(|p| cache::normal(p)).collect(),
requires: cache::requires_json(&c),
code: None,
check: None,
})
}
pub fn resolved_requires(requires: &[cache::RequireJson]) -> Vec<(String, PathBuf)> {
requires
.iter()
.filter_map(|r| {
r.path
.as_ref()
.map(|p| (r.module.clone(), PathBuf::from(p)))
})
.collect()
}
pub struct Harvest<'a> {
pub store: &'a cache::Cache,
pub session: &'a crate::testing::TestSession,
pub cfg_inputs: &'a [PathBuf],
pub root: &'a Path,
pub cfg: &'a Config,
pub lint: Option<&'a str>,
pub opts: cache::Options,
pub done: RefCell<HashSet<PathBuf>>,
}
pub fn harvest_modules(h: &Harvest<'_>, check: &CheckInfo, test_file: &Path) {
let Harvest {
store,
session,
cfg_inputs,
root,
cfg,
lint,
opts,
done,
} = h;
let (lint, opts) = (*lint, *opts);
let done = &mut *done.borrow_mut();
let saved = session.checker().search_path().ok();
let _ = session.checker().add_layout_paths(test_file);
let mut queue = resolved_requires(&cache::requires_json(check));
let (mut stored, mut skipped) = (0usize, 0usize);
while let Some((_, path)) = queue.pop() {
if !done.insert(path.clone()) {
continue;
}
if let Some(m) = store.lookup(&cache::module_gen_key(&path, lint))
&& m.code.is_some()
{
queue.extend(resolved_requires(&m.requires));
continue;
}
let Ok((Some(code), c)) = session.checker().gen_lua(&path) else {
skipped += 1;
continue;
};
let c = if c.requires.is_empty() && crate::link::mentions_require(&path) {
session.checker().check(&path).unwrap_or(c)
} else {
c
};
stored += 1;
let m = cache::Module::generated(&c, code);
queue.extend(resolved_requires(&m.requires));
store.store_module(
&cache::module_gen_key(&path, lint),
&path,
cfg_inputs,
&search_dirs(&path, root, cfg),
&m,
);
}
if let Some(s) = saved {
let _ = session.checker().set_search_path(&s);
}
if opts.explain {
eprintln!("htl cache: harvested {stored} modules, {skipped} could not be generated");
}
}
pub fn preloads_for(
store: &cache::Cache,
entry: &cache::Module,
lint: Option<&str>,
opts: cache::Options,
) -> Vec<(String, String, PathBuf)> {
let mut out = Vec::new();
let mut queue = resolved_requires(&entry.requires);
let mut seen: HashSet<PathBuf> = Default::default();
let mut absent = 0usize;
while let Some((name, path)) = queue.pop() {
if !seen.insert(path.clone()) {
continue;
}
let Some(m) = store.lookup(&cache::module_gen_key(&path, lint)) else {
absent += 1;
continue;
};
let Some(code) = m.code.clone() else { continue };
queue.extend(resolved_requires(&m.requires));
out.push((name, code, path));
}
if opts.explain {
let names: Vec<&str> = out.iter().map(|(n, _, _)| n.as_str()).collect();
eprintln!(
"htl cache: preloading {} [{}], {absent} not in the store",
out.len(),
names.join(" ")
);
}
out
}
pub fn explain_cache(store: Option<&cache::Cache>, opts: cache::Options) {
if let Some(c) = store
&& opts.explain
&& let Some(line) = c.stats().summary(opts.mode)
{
eprintln!("{line}");
}
}
pub struct Options<'a> {
pub paths: &'a [PathBuf],
pub config: &'a Config,
pub lint: Option<&'a str>,
pub cache: cache::Options,
}
#[derive(Debug, Clone)]
pub struct Report {
pub files: Vec<PathBuf>,
pub errors: usize,
pub warnings: usize,
pub lints: usize,
pub denied: usize,
pub replayed: usize,
pub requires: Vec<(PathBuf, Vec<cache::RequireJson>)>,
}
impl Report {
pub fn failed(&self, strict: bool) -> bool {
self.errors > 0 || self.denied > 0 || (strict && (self.warnings > 0 || self.lints > 0))
}
pub fn all_cached(&self) -> bool {
self.replayed == self.files.len() && !self.files.is_empty()
}
}
pub fn check<O: Output>(
sink: &mut Sink<O>,
files: &[PathBuf],
opts: &Options<'_>,
) -> Result<Report> {
let Options {
paths,
config: cfg,
lint,
cache: cache_opts,
} = opts;
let (cfg, cache_opts) = (*cfg, *cache_opts);
let files = files.to_vec();
let (contracts, contract_problems) = match cfg {
Some((r, _, c)) => crate::contract::resolve(r, c),
None => (Vec::new(), Vec::new()),
};
let root = cfg
.as_ref()
.map(|(r, _, _)| r.clone())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
let store = store(&root, cache_opts, None, "htl check");
let start = paths
.first()
.map(PathBuf::as_path)
.unwrap_or(Path::new("."));
let origins = Origins::new(start, &root, cfg, &contracts);
sink.walking(&files);
let file_spec = cfg
.as_ref()
.map(|(_, _, c)| c.lint_spec())
.unwrap_or_default();
let spec = crate::config::join_specs([file_spec.as_str(), lint.unwrap_or("")]);
let lints = crate::lint::Lints::parse(&spec)?;
sink.judge_by(lints.selection());
let cargo_root = crate::dts::find_cargo_package_root(start);
let host_modules = cargo_root
.as_deref()
.map(crate::dts::host_module_names)
.unwrap_or_default();
let key_spec = if host_modules.is_empty() {
spec.clone()
} else {
format!("{spec}\u{1}host={}", host_modules.join(","))
};
let keys: Vec<cache::Key> = files
.iter()
.map(|f| cache::module_key(f, Some(&key_spec)))
.collect();
let run_key = cache::run_key(&files, Some(&key_spec));
let hits: Vec<Option<cache::Module>> = match &store {
Some(c) => c.lookup_all(&keys, &run_key, files.len()),
None => vec![None; files.len()],
};
let to_check = hits.iter().filter(|h| h.is_none()).count();
let h = if to_check > 0 {
Some(checker(cfg, paths, lints.selection())?)
} else {
None
};
let cfg_inputs: Vec<PathBuf> = cfg.iter().map(|(_, p, _)| p.clone()).collect();
let (mut n_err, mut n_warn, mut n_lint) = (0usize, 0usize, 0usize);
let mut infos: Vec<(PathBuf, CheckInfo)> = Vec::with_capacity(files.len());
let mut requires: Vec<(PathBuf, Vec<cache::RequireJson>)> = Vec::with_capacity(files.len());
let mut modules: Vec<cache::Module> = Vec::with_capacity(files.len());
for ((f, key), hit) in files.iter().zip(&keys).zip(hits) {
let m = match hit {
Some(m) => {
sink.replay(&m.diagnostics)?;
m
}
None => {
let h = h.as_ref().expect("a module missed, so a checker was built");
let m = check_one(
h,
sink,
f,
&Walk {
cfg,
contracts: &contracts,
origins: &origins,
host_modules: &host_modules,
lints: &lints,
},
)?;
if let Some(c) = &store
&& c.mode() == cache::Mode::PerModule
{
c.store_module(key, f, &cfg_inputs, &search_dirs(f, &root, cfg), &m);
}
m
}
};
n_err += m.errors;
n_warn += m.warnings;
n_lint += m.lints;
requires.push((f.clone(), m.requires.clone()));
infos.push((f.clone(), m.requires_only()));
modules.push(m);
}
if let Some(c) = &store
&& c.mode() == cache::Mode::WholeRun
&& to_check > 0
{
let dirs: Vec<PathBuf> = files
.iter()
.flat_map(|f| search_dirs(f, &root, cfg))
.collect();
c.store_run(&run_key, &files, &cfg_inputs, &dirs, &modules);
}
if lints.on("require-cycle") {
for cyc in lints.keep(crate::require_cycles(&infos)) {
sink.diag(Severity::Lint, &cyc);
n_lint += 1;
}
}
let publish_problems = match cfg {
Some((r, _, _)) => crate::contract::publish(r, &contracts).1,
None => Vec::new(),
};
let problems: Vec<String> = contract_problems
.iter()
.chain(&publish_problems)
.cloned()
.collect();
for p in lints.keep(problems) {
sink.diag(Severity::Lint, &p);
n_lint += 1;
}
if let Some((_, cfg_path, _)) = cfg
&& lints.on("contract-unenforced")
{
for l in lints.keep(crate::contract_enforcement_lints(
cfg_path,
&contracts,
cargo_root.as_deref(),
)) {
sink.diag(Severity::Lint, &l);
n_lint += 1;
}
}
if let Some(c) = &store {
let keep = match c.mode() {
cache::Mode::PerModule => keys.clone(),
cache::Mode::WholeRun => vec![run_key.clone()],
};
c.sweep(&keep, files.len());
}
explain_cache(store.as_ref(), cache_opts);
n_err += sink.dependency_error_count();
let replayed = files.len() - to_check;
Ok(Report {
files,
errors: n_err,
warnings: n_warn,
lints: n_lint,
denied: sink.denied(),
replayed,
requires,
})
}
#[derive(Serialize, Debug, Clone)]
pub struct NeverRan {
pub name: String,
pub line: usize,
}
#[derive(Serialize, Debug, Clone)]
pub struct CoverageModule {
pub path: String,
pub executed: usize,
pub total: usize,
pub unexecuted: Vec<(usize, usize)>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub never_ran: Vec<NeverRan>,
#[serde(skip)]
pub source: PathBuf,
#[serde(skip)]
pub statements: Vec<(usize, bool)>,
#[serde(skip)]
pub functions: Vec<(String, usize, bool)>,
}
#[derive(Serialize, Debug, Clone, Default)]
pub struct CoverageReport {
pub modules: Vec<CoverageModule>,
pub executed: usize,
pub total: usize,
}
impl CoverageReport {
pub fn lcov(&self, root: &Path) -> String {
use std::collections::BTreeMap;
use std::fmt::Write as _;
let mut out = String::new();
for m in &self.modules {
let sf = m
.source
.strip_prefix(root)
.unwrap_or(&m.source)
.to_string_lossy();
out.push_str("TN:\n");
let _ = writeln!(out, "SF:{sf}");
for (name, line, _) in &m.functions {
let _ = writeln!(out, "FN:{line},{name}");
}
for (name, _, entered) in &m.functions {
let _ = writeln!(out, "FNDA:{},{name}", u8::from(*entered));
}
let _ = writeln!(out, "FNF:{}", m.functions.len());
let _ = writeln!(
out,
"FNH:{}",
m.functions.iter().filter(|(_, _, e)| *e).count()
);
let mut lines: BTreeMap<usize, bool> = BTreeMap::new();
for &(line, ran) in &m.statements {
*lines.entry(line).or_default() |= ran;
}
for (line, ran) in &lines {
let _ = writeln!(out, "DA:{line},{}", u8::from(*ran));
}
let _ = writeln!(out, "LF:{}", lines.len());
let _ = writeln!(out, "LH:{}", lines.values().filter(|r| **r).count());
out.push_str("end_of_record\n");
}
out
}
}
pub fn coverage_report(
checker: &Htl,
test_files: &[PathBuf],
hits: &HashMap<PathBuf, BTreeSet<usize>>,
deps: &BTreeSet<PathBuf>,
) -> Result<CoverageReport> {
let canon = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
let tests: HashSet<PathBuf> = test_files.iter().map(|p| canon(p)).collect();
let mut sources: BTreeSet<PathBuf> = deps.iter().map(|p| canon(p)).collect();
sources.extend(hits.keys().cloned());
let cwd = std::env::current_dir().unwrap_or_default();
let (mut tot_exec, mut tot_all) = (0usize, 0usize);
let mut rows: Vec<CoverageModule> = Vec::new();
for src in &sources {
let name = src.to_string_lossy();
if !name.ends_with(".tl")
|| name.ends_with(".d.tl")
|| tests.contains(src)
|| name.contains("/htl-lib-")
{
continue;
}
let (ranges, funcs) = checker.coverage_spans(src)?;
if ranges.is_empty() {
continue;
}
let empty = BTreeSet::new();
let ran = hits.get(src).unwrap_or(&empty);
let mut missed = Vec::new();
let mut statements = Vec::with_capacity(ranges.len());
let mut executed = 0usize;
for &(a, b) in &ranges {
let hit = ran.range(a..=b).next().is_some();
statements.push((a, hit));
if hit {
executed += 1;
} else {
missed.push((a, b));
}
}
let functions: Vec<(String, usize, bool)> = funcs
.into_iter()
.map(|f| {
let entered = ran.range(f.line + 1..=f.last - 1).next().is_some();
(f.name, f.line, entered)
})
.collect();
let never_ran = functions
.iter()
.filter(|(_, _, entered)| !entered)
.map(|(name, line, _)| NeverRan {
name: name.clone(),
line: *line,
})
.collect();
tot_exec += executed;
tot_all += ranges.len();
let shown = src
.strip_prefix(&cwd)
.unwrap_or(src)
.to_string_lossy()
.into_owned();
rows.push(CoverageModule {
path: shown,
executed,
total: ranges.len(),
unexecuted: missed,
never_ran,
source: src.clone(),
statements,
functions,
});
}
Ok(CoverageReport {
modules: rows,
executed: tot_exec,
total: tot_all,
})
}
pub struct TestOptions<'a> {
pub config: &'a Config,
pub lint: Option<&'a str>,
pub lib: &'a str,
pub filter: Option<&'a str>,
pub run: RunOptions,
pub cache: cache::Options,
}
#[derive(Debug)]
pub struct TestReport {
pub files: Vec<PathBuf>,
pub ran: usize,
pub passed: usize,
pub failed: usize,
pub files_with_errors: usize,
pub replayed: usize,
pub seed: u64,
pub duration_ms: f64,
pub coverage: Option<CoverageReport>,
}
impl TestReport {
pub fn ok(&self) -> bool {
self.files_with_errors == 0
}
pub fn skipped(&self) -> usize {
self.files.len() - self.ran
}
}
pub fn test<O: Output>(
sink: &mut Sink<O>,
files: &[PathBuf],
opts: &TestOptions<'_>,
each: &mut dyn FnMut(&FileReport, &mut Sink<O>),
) -> Result<TestReport> {
let TestOptions {
config: cfg,
lint,
lib,
filter,
run,
cache: cache_opts,
} = opts;
let (cfg, cache_opts) = (*cfg, *cache_opts);
let files = files.to_vec();
let seed = run.seed.unwrap_or_else(|| {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
.unwrap_or(0)
});
let run = RunOptions {
seed: Some(seed),
..run.clone()
};
let coverage_wanted = run.coverage;
let fail_fast = run.fail_fast;
let file_spec = cfg
.as_ref()
.map(|(_, _, c)| c.lint_spec())
.unwrap_or_default();
let spec = crate::config::join_specs([file_spec.as_str(), lint.unwrap_or("")]);
let lint = if spec.is_empty() {
None
} else {
Some(spec.as_str())
};
let mut cov_hits: HashMap<PathBuf, BTreeSet<usize>> = Default::default();
let mut cov_deps: BTreeSet<PathBuf> = Default::default();
let (mut passed, mut failed, mut bad_files, mut ran_files) = (0usize, 0usize, 0usize, 0usize);
let started = std::time::Instant::now();
let session = TestSession::new(lint, lib, *filter, run)?;
let root = cfg
.as_ref()
.map(|(r, _, _)| r.clone())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
let store = store(&root, cache_opts, None, "htl test");
let keys: Vec<cache::Key> = files.iter().map(|f| cache::gen_key(f, lint)).collect();
let cfg_inputs: Vec<PathBuf> = cfg.iter().map(|(_, p, _)| p.clone()).collect();
let mut replayed = 0usize;
let harvest = store.as_ref().map(|c| Harvest {
store: c,
session: &session,
cfg_inputs: &cfg_inputs,
root: &root,
cfg,
lint,
opts: cache_opts,
done: RefCell::new(Default::default()),
});
for (f, key) in files.iter().zip(&keys) {
let hit = store
.as_ref()
.and_then(|c| c.lookup(key))
.filter(|m| m.code.is_some() && m.check.is_some());
let rep = match &hit {
Some(m) => {
replayed += 1;
let check = m.check.as_ref().expect("filtered above").to_check();
let code = m.code.as_deref().expect("filtered above");
let pre = store
.as_ref()
.map(|c| preloads_for(c, m, lint, cache_opts))
.unwrap_or_default();
session.run_file_with(f, Some((code, &check)), &pre)?.0
}
None => {
let (rep, code) = session.run_file_with(f, None, &[])?;
if let (Some(c), Some(code)) = (&store, code) {
let m = cache::Module::generated(&rep.check, code);
c.store_module(key, f, &cfg_inputs, &search_dirs(f, &root, cfg), &m);
if let Some(h) = &harvest {
harvest_modules(h, &rep.check, f);
}
}
rep
}
};
if coverage_wanted {
for (source, lines) in &rep.coverage {
let Some(path) = source.strip_prefix('@') else {
continue;
};
let key = std::fs::canonicalize(path).unwrap_or_else(|_| PathBuf::from(path));
cov_hits
.entry(key)
.or_default()
.extend(lines.iter().copied());
}
cov_deps.extend(rep.check.deps.iter().cloned());
}
ran_files += 1;
sink.checkinfo(&rep.check);
each(&rep, sink);
passed += rep.passed;
failed += rep.failed;
if !rep.ok() {
bad_files += 1;
if fail_fast {
break;
}
}
}
let coverage = if coverage_wanted {
Some(coverage_report(
session.checker(),
&files,
&cov_hits,
&cov_deps,
)?)
} else {
None
};
explain_cache(store.as_ref(), cache_opts);
Ok(TestReport {
files,
ran: ran_files,
passed,
failed,
files_with_errors: bad_files,
replayed,
seed,
duration_ms: started.elapsed().as_secs_f64() * 1000.0,
coverage,
})
}