use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
use globset::GlobSet;
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::document::Document;
use crate::folder::{ServedFolder, lexical};
use crate::problem::Problem;
const CONSTRAINT: &str = "constraint";
const KINDS: &[(&str, &str)] = &[
(CONSTRAINT, "constraint"),
("review-rule", "review rule"),
("objective", "objective"),
];
const FOLDER: &str = "requirements";
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct Config {
tests: Vec<SuiteConfig>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct SuiteConfig {
name: String,
glob: String,
pattern: String,
run: String,
}
#[derive(Debug)]
pub struct Requirements {
pub suites: Vec<Suite>,
}
#[derive(Debug)]
pub struct Suite {
pub name: String,
pub run: String,
root: PathBuf,
files: GlobSet,
pattern: Regex,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pointer {
pub id: String,
pub file: PathBuf,
pub line: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Case {
pub name: String,
pub failed: bool,
}
impl Requirements {
pub fn read(config: &serde_yaml::Value, base: &Path, served: &ServedFolder) -> Result<Self> {
let config: Config = serde_yaml::from_value(config.clone())?;
if config.tests.is_empty() {
bail!("tests lists no suite");
}
let mut suites = Vec::new();
for suite in config.tests {
let name = &suite.name;
let absolute = lexical(&base.join(&suite.glob));
if !served.contains(&absolute) {
bail!(
"tests {name}: glob {} is outside the served folder",
suite.glob
);
}
let root = crate::code::literal_prefix(&absolute);
let rest = absolute.strip_prefix(&root).unwrap_or(&absolute);
let text = if rest.as_os_str().is_empty() {
globset::escape(&crate::code::glob_path(&root))
} else {
format!(
"{}/{}",
globset::escape(crate::code::glob_path(&root).trim_end_matches('/')),
crate::code::glob_path(rest)
)
};
let glob = globset::GlobBuilder::new(&text)
.literal_separator(true)
.build()
.with_context(|| format!("tests {name}: glob {}", suite.glob))?;
let pattern = Regex::new(&suite.pattern)
.with_context(|| format!("tests {name}: pattern {}", suite.pattern))?;
if pattern.captures_len() < 2 {
bail!(
"tests {name}: pattern {} has no group — its first group is the constraint's id",
suite.pattern
);
}
if suite.run.trim().is_empty() {
bail!("tests {name}: run is empty");
}
suites.push(Suite {
name: suite.name,
run: suite.run,
root,
files: GlobSet::builder().add(glob).build()?,
pattern,
});
}
Ok(Self { suites })
}
pub fn pointers(&self) -> Vec<Pointer> {
let mut found = Vec::new();
for suite in &self.suites {
let mut files = Vec::new();
let walk = ignore::WalkBuilder::new(&suite.root)
.hidden(true)
.require_git(false)
.build();
for entry in walk.flatten() {
let path = entry.path();
if entry.file_type().is_some_and(|t| t.is_file())
&& suite.files.is_match(crate::code::glob_path(path))
{
files.push(path.to_path_buf());
}
}
files.sort();
for file in files {
let Ok(source) = std::fs::read_to_string(&file) else {
continue;
};
for captures in suite.pattern.captures_iter(&source) {
let Some(id) = captures.get(1) else { continue };
found.push(Pointer {
id: id.as_str().to_string(),
file: file.clone(),
line: source[..id.start()].matches('\n').count() + 1,
});
}
}
}
found
}
}
struct Constraints {
by_id: BTreeMap<String, Vec<PathBuf>>,
unnamed: Vec<PathBuf>,
}
fn constraints(document: &Document) -> Constraints {
let mut found = Constraints {
by_id: BTreeMap::new(),
unnamed: Vec::new(),
};
let walk = ignore::WalkBuilder::new(&document.base)
.hidden(true)
.require_git(false)
.build();
let mut pages = Vec::new();
for entry in walk.flatten() {
let path = entry.path();
if entry.file_type().is_some_and(|t| t.is_file())
&& path.extension().is_some_and(|ext| ext == "md")
{
pages.push(path.to_path_buf());
}
}
pages.sort();
for page in pages {
let Ok(source) = std::fs::read_to_string(&page) else {
continue;
};
if crate::structure::page_type(&source).is_none_or(|(name, _)| name != CONSTRAINT) {
continue;
}
match crate::structure::front_matter(&source, "id") {
Some(id) => found.by_id.entry(id).or_default().push(page),
None => found.unnamed.push(page),
}
}
found
}
pub fn problems(document: &Document) -> Vec<Problem> {
let Some(requirements) = &document.requirements else {
return Vec::new();
};
let problem = |file: &Path, line: Option<usize>, message: String| {
document.name(file).map(|file| Problem {
file,
line,
column: None,
message,
target: None,
})
};
let constraints = constraints(document);
let pointers = requirements.pointers();
let mut problems = Vec::new();
for page in &constraints.unnamed {
problems.extend(problem(
page,
None,
"a constraint has an id — its front matter has no id".to_string(),
));
}
for (id, pages) in &constraints.by_id {
for page in &pages[1..] {
problems.extend(problem(
page,
None,
format!(
"constraint id {id} is taken by {}",
document.name(&pages[0]).unwrap_or_default()
),
));
}
if !pointers.iter().any(|pointer| pointer.id == *id) {
problems.extend(problem(
&pages[0],
None,
format!("no test points at this constraint — a test names it {id}"),
));
}
}
for pointer in &pointers {
if !constraints.by_id.contains_key(&pointer.id) {
problems.extend(problem(
&pointer.file,
Some(pointer.line),
format!("{} — no constraint has this id", pointer.id),
));
}
}
problems
}
pub fn junit(text: &str) -> Vec<Case> {
static CASE: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r"(?s)<testcase\b([^>]*?)(?:/>|>(.*?)</testcase>)").unwrap()
});
static ATTRIBUTE: std::sync::LazyLock<Regex> =
std::sync::LazyLock::new(|| Regex::new(r#"\b(classname|name)="([^"]*)""#).unwrap());
let unescaped = |value: &str| {
value
.replace("<", "<")
.replace(">", ">")
.replace(""", "\"")
.replace("'", "'")
.replace("&", "&")
};
CASE.captures_iter(text)
.map(|case| {
let mut name = String::new();
let mut class = String::new();
for attribute in ATTRIBUTE.captures_iter(&case[1]) {
match &attribute[1] {
"name" => name = unescaped(&attribute[2]),
_ => class = unescaped(&attribute[2]),
}
}
let body = case.get(2).map_or("", |body| body.as_str());
Case {
name: if class.is_empty() {
name
} else {
format!("{class}::{name}")
},
failed: body.contains("<failure") || body.contains("<error"),
}
})
.collect()
}
pub fn run_problems(
document: &Document,
suite: &Suite,
printed: &str,
exit: Option<i32>,
) -> Vec<Problem> {
let problem = |message: String| Problem {
file: document.part_name(&document.path()),
line: None,
column: None,
message,
target: None,
};
let cases = junit(printed);
if cases.is_empty() {
let exit = exit.map_or("no exit code".to_string(), |code| {
format!("exit code {code}")
});
return vec![problem(format!(
"tests {}: `{}` printed no JUnit report ({exit})",
suite.name, suite.run
))];
}
let failed: Vec<Problem> = cases
.into_iter()
.filter(|case| case.failed)
.map(|case| problem(format!("tests {}: {} failed", suite.name, case.name)))
.collect();
if failed.is_empty() && exit != Some(0) {
let exit = exit.map_or("no exit code".to_string(), |code| {
format!("exit code {code}")
});
return vec![problem(format!(
"tests {}: `{}` failed ({exit}) and its report names no failed test",
suite.name, suite.run
))];
}
failed
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Outcome {
Passed,
Failed,
NotRun,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Tested {
pub test: String,
pub result: Outcome,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Results {
pub checked: u64,
pub tests: BTreeMap<String, Vec<Tested>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Status {
Met,
NotMet,
NoTest,
NotChecked,
}
impl Results {
fn status(&self, id: &str) -> Status {
match self.tests.get(id) {
None => Status::NotChecked,
Some(tests) if tests.is_empty() => Status::NoTest,
Some(tests) if tests.iter().any(|t| t.result == Outcome::Failed) => Status::NotMet,
Some(tests) if tests.iter().any(|t| t.result == Outcome::NotRun) => Status::NotChecked,
Some(_) => Status::Met,
}
}
}
pub fn results(document: &Document, cases: &[Case], checked: u64) -> Results {
let mut tests: BTreeMap<String, Vec<Tested>> = constraints(document)
.by_id
.into_keys()
.map(|id| (id, Vec::new()))
.collect();
let Some(requirements) = &document.requirements else {
return Results { checked, tests };
};
let mut failed: HashMap<&str, bool> = HashMap::new();
for case in cases {
let short = case
.name
.rsplit([':', '.', ' '])
.next()
.unwrap_or(&case.name);
*failed.entry(short).or_default() |= case.failed;
}
let mut sources: HashMap<PathBuf, String> = HashMap::new();
for pointer in requirements.pointers() {
let Some(found) = tests.get_mut(&pointer.id) else {
continue;
};
let source = sources
.entry(pointer.file.clone())
.or_insert_with(|| std::fs::read_to_string(&pointer.file).unwrap_or_default());
let after: usize = source
.split_inclusive('\n')
.take(pointer.line.saturating_sub(1))
.map(str::len)
.sum();
let named = source[after..]
.split(|c: char| !(c.is_alphanumeric() || c == '_'))
.find(|word| failed.contains_key(word));
found.push(match named {
Some(name) => Tested {
test: name.to_string(),
result: if failed[name] {
Outcome::Failed
} else {
Outcome::Passed
},
},
None => Tested {
test: format!(
"{}:{}",
document.name(&pointer.file).unwrap_or_default(),
pointer.line
),
result: Outcome::NotRun,
},
});
}
Results { checked, tests }
}
fn kept(served: &ServedFolder, document: &str) -> PathBuf {
served
.path()
.join(".devpage")
.join(FOLDER)
.join(format!("{document}.json"))
}
pub fn save(document: &Document, results: &Results) -> Result<()> {
let name = document
.name(&document.path())
.context("the document file is outside the served folder")?;
let file = kept(&document.served, &name);
let root = document.served.path().join(".devpage");
std::fs::create_dir_all(file.parent().unwrap_or(&root))?;
let ignore = root.join(".gitignore");
if !ignore.exists() {
std::fs::write(ignore, "*\n")?;
}
std::fs::write(file, serde_json::to_string_pretty(results)?)?;
Ok(())
}
fn saved(served: &ServedFolder, document: &str) -> Option<Results> {
serde_json::from_str(&std::fs::read_to_string(kept(served, document)).ok()?).ok()
}
struct Listed {
file: PathBuf,
title: String,
kind: &'static str,
id: Option<String>,
}
fn listed(part: &Path) -> Vec<Listed> {
let mut files: Vec<PathBuf> = std::fs::read_dir(part.join(FOLDER))
.into_iter()
.flatten()
.flatten()
.map(|entry| entry.path())
.filter(|file| file.extension().is_some_and(|ext| ext == "md"))
.collect();
files.sort();
files
.into_iter()
.filter_map(|file| {
let source = std::fs::read_to_string(&file).ok()?;
let (name, _) = crate::structure::page_type(&source)?;
let (kind, called) = KINDS.iter().find(|(kind, _)| *kind == name)?;
Some(Listed {
title: crate::markup::title(&source, &file),
kind: called,
id: (*kind == CONSTRAINT)
.then(|| crate::structure::front_matter(&source, "id"))
.flatten(),
file,
})
})
.collect()
}
pub fn listing(served: &ServedFolder, base: &Path, document: &str, page: &Path) -> String {
let results = saved(served, document);
let Some(part) = page.parent().filter(|part| part.starts_with(base)) else {
return String::new();
};
let item = |listed: &Listed, up: &str, indent: &str| {
let status = match (&listed.id, listed.kind) {
(Some(id), _) => match results.as_ref().map(|r| r.status(id)) {
Some(Status::Met) => "met",
Some(Status::NotMet) => "not met",
Some(Status::NoTest) => "no test",
Some(Status::NotChecked) | None => "not checked",
},
(None, kind) => kind,
};
format!(
"{indent}- [{}]({up}{FOLDER}/{}) — {status}\n",
listed.title.replace('[', "\\[").replace(']', "\\]"),
listed
.file
.file_name()
.unwrap_or_default()
.to_string_lossy()
)
};
let mut out = String::new();
for listed in listed(part) {
out.push_str(&item(&listed, "", ""));
}
let mut up = String::new();
let mut above = part;
while above != base {
let Some(parent) = above.parent() else { break };
above = parent;
up.push_str("../");
let inherited = listed(above);
if inherited.is_empty() {
continue;
}
let top = above.join("README.md");
let title = std::fs::read_to_string(&top).map_or_else(
|_| {
above
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned()
},
|source| crate::markup::title(&source, &top),
);
out.push_str(&format!("- [{title}]({up}README.md)\n"));
for listed in inherited {
out.push_str(&item(&listed, &up, " "));
}
}
if out.is_empty() {
return String::new();
}
if let Some(results) = results {
out.push_str(&format!(
"\n<span data-checked=\"{}\"></span>\n",
results.checked
));
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn site(name: &str, files: &[(&str, &str)]) -> Document {
let dir = std::env::temp_dir().join(format!("devpage-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("devpage.yml"),
"kind: site\ntitle: T\nroot: README.md\nextensions:\n requirements:\n config:\n tests:\n - name: shell\n glob: \"tests/*.sh\"\n pattern: '# spec: ([a-z0-9-]+)'\n run: \"sh tests/run.sh\"\n",
)
.unwrap();
for (file, text) in files {
let file = dir.join(file);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(file, text).unwrap();
}
Document::load(&dir, &ServedFolder::new(&dir).unwrap()).unwrap()
}
fn found(document: &Document) -> Vec<String> {
let mut found: Vec<String> = problems(document).iter().map(ToString::to_string).collect();
found.sort();
found
}
fn constraint(id: &str) -> String {
format!("---\npage-type: constraint\nid: {id}\n---\n# C\n")
}
#[test]
fn a_constraint_every_test_of_which_is_found_has_no_problem() {
let doc = site(
"requirements-ok",
&[
("README.md", "# Home\n"),
("rules/saving.md", &constraint("a-save-is-kept")),
(
"rules/no-rework.md",
"---\npage-type: review-rule\n---\n# R\n",
),
(
"tests/save.sh",
"# spec: a-save-is-kept\none() { :; }\n# spec: a-save-is-kept\ntwo() { :; }\n",
),
],
);
assert_eq!(found(&doc), Vec::<String>::new());
}
#[test]
fn a_constraint_with_no_test_or_no_id_is_a_problem_on_its_page() {
let doc = site(
"requirements-untested",
&[
("README.md", "# Home\n"),
("rules/saving.md", &constraint("a-save-is-kept")),
("rules/unnamed.md", "---\npage-type: constraint\n---\n# C\n"),
("tests/save.sh", "one() { :; }\n"),
],
);
assert_eq!(
found(&doc),
[
"rules/saving.md: no test points at this constraint — a test names it a-save-is-kept",
"rules/unnamed.md: a constraint has an id — its front matter has no id",
]
);
}
#[test]
fn a_test_pointing_at_no_constraint_is_a_problem_at_its_line() {
let doc = site(
"requirements-orphan",
&[
("README.md", "# Home\n"),
(
"tests/save.sh",
"one() { :; }\n# spec: gone-rule\ntwo() { :; }\n",
),
],
);
assert_eq!(
found(&doc),
["tests/save.sh:2: gone-rule — no constraint has this id"]
);
}
#[test]
fn two_constraints_with_one_id_are_a_problem_on_the_second() {
let doc = site(
"requirements-twice",
&[
("README.md", "# Home\n"),
("a/one.md", &constraint("kept")),
("b/two.md", &constraint("kept")),
("tests/save.sh", "# spec: kept\none() { :; }\n"),
],
);
assert_eq!(
found(&doc),
["b/two.md: constraint id kept is taken by a/one.md"]
);
}
#[test]
fn a_site_with_no_requirements_has_none_of_these_problems() {
let dir =
std::env::temp_dir().join(format!("devpage-requirements-off-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("devpage.yml"),
"kind: site\ntitle: T\nroot: README.md\n",
)
.unwrap();
std::fs::write(dir.join("README.md"), "# Home\n").unwrap();
std::fs::write(dir.join("kept.md"), constraint("kept")).unwrap();
let doc = Document::load(&dir, &ServedFolder::new(&dir).unwrap()).unwrap();
assert!(doc.requirements.is_none());
assert_eq!(found(&doc), Vec::<String>::new());
}
#[test]
fn a_junit_report_gives_each_test_and_whether_it_failed() {
let cases = junit(
"<?xml version=\"1.0\"?>\n<testsuites><testsuite name=\"a\">\
<testcase classname=\"save\" name=\"one\"/>\
<testcase classname=\"save\" name=\"two\"><failure message=\"x < y\">at line 3</failure></testcase>\
<testcase name=\"three\"><error/></testcase>\
</testsuite></testsuites>\n\
#1 [internal] load <build definition> & more\n\
<?xml version=\"1.0\"?>\n<testsuites><testsuite name=\"b\"><testcase name=\"four\"/></testsuite></testsuites>",
);
let read: Vec<(&str, bool)> = cases.iter().map(|c| (c.name.as_str(), c.failed)).collect();
assert_eq!(
read,
[
("save::one", false),
("save::two", true),
("three", true),
("four", false)
]
);
}
#[test]
fn a_run_reports_each_failed_test_or_that_nothing_was_printed() {
let doc = site("requirements-run", &[("README.md", "# Home\n")]);
let suite = &doc.requirements.as_ref().unwrap().suites[0];
let report = "<testsuite><testcase name=\"one\"/><testcase name=\"two\"><failure/></testcase></testsuite>";
let shown = |printed: &str, exit| -> Vec<String> {
run_problems(&doc, suite, printed, exit)
.iter()
.map(ToString::to_string)
.collect()
};
assert_eq!(
shown(report, Some(101)),
["devpage.yml: tests shell: two failed"]
);
assert_eq!(
shown("<testsuite><testcase name=\"one\"/></testsuite>", Some(101)),
[
"devpage.yml: tests shell: `sh tests/run.sh` failed (exit code 101) and its report names no failed test"
]
);
assert!(shown("<testsuite><testcase name=\"one\"/></testsuite>", Some(0)).is_empty());
assert_eq!(
shown("error: could not compile", Some(101)),
["devpage.yml: tests shell: `sh tests/run.sh` printed no JUnit report (exit code 101)"]
);
}
#[test]
fn a_wrong_configuration_stops_the_site_loading_and_names_the_field() {
let dir =
std::env::temp_dir().join(format!("devpage-requirements-bad-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("README.md"), "# Home\n").unwrap();
let load = |tests: &str| {
std::fs::write(
dir.join("devpage.yml"),
format!("kind: site\ntitle: T\nroot: README.md\nextensions:\n requirements:\n config:\n tests:\n{tests}"),
)
.unwrap();
format!(
"{:#}",
Document::load(&dir, &ServedFolder::new(&dir).unwrap()).unwrap_err()
)
};
assert!(
load(" - {name: sh, glob: \"*.sh\", pattern: 'spec', run: \"x\"}\n")
.contains("tests sh: pattern spec has no group"),
);
assert!(
load(" - {name: sh, glob: \"*.sh\", pattern: '(', run: \"x\"}\n")
.contains("extensions: requirements: tests sh: pattern ("),
);
}
#[test]
fn a_run_is_kept_and_a_parts_page_lists_its_requirements_with_what_the_run_showed() {
let page = |kind: &str, id: &str, title: &str| {
let id = if id.is_empty() {
String::new()
} else {
format!("id: {id}\n")
};
format!("---\npage-type: {kind}\n{id}---\n# {title}\n")
};
let doc = site(
"requirements-kept",
&[
("README.md", "# Home\n"),
(
"requirements/kept.md",
&page("constraint", "a-save-is-kept", "A save is kept"),
),
(
"requirements/lost.md",
&page("constraint", "nothing-is-lost", "Nothing is lost"),
),
(
"requirements/rule.md",
&page("review-rule", "", "No rework"),
),
(
"requirements/untested.md",
&page("constraint", "it-is-fast", "It is fast"),
),
("requirements/steps.md", &page("process", "", "Steps")),
("part/README.md", "# The part\n"),
(
"part/requirements/own.md",
&page("constraint", "the-part-works", "The part works"),
),
(
"tests/save.sh",
"# spec: a-save-is-kept\nsaves() { :; }\n# spec: nothing-is-lost\n# spec: a-save-is-kept\nloses() { :; }\n# spec: the-part-works\nnever_ran() { :; }\n",
),
],
);
let case = |name: &str, failed| Case {
name: name.to_string(),
failed,
};
let results = results(&doc, &[case("shell::saves", false), case("loses", true)], 7);
let outcomes = |id: &str| -> Vec<(String, Outcome)> {
results.tests[id]
.iter()
.map(|t| (t.test.clone(), t.result))
.collect()
};
assert_eq!(
outcomes("a-save-is-kept"),
[
("saves".to_string(), Outcome::Passed),
("loses".to_string(), Outcome::Failed)
]
);
assert_eq!(
outcomes("the-part-works"),
[("tests/save.sh:6".to_string(), Outcome::NotRun)]
);
assert!(results.tests["it-is-fast"].is_empty());
let listing =
|page: &str| listing(&doc.served, &doc.base, "devpage.yml", &doc.base.join(page));
assert_eq!(
listing("part/README.md").lines().next(),
Some("- [The part works](requirements/own.md) — not checked")
);
save(&doc, &results).unwrap();
assert_eq!(
std::fs::read_to_string(doc.base.join(".devpage/.gitignore")).unwrap(),
"*\n"
);
assert_eq!(
listing("part/README.md"),
"- [The part works](requirements/own.md) — not checked\n\
- [Home](../README.md)\n \
- [A save is kept](../requirements/kept.md) — not met\n \
- [Nothing is lost](../requirements/lost.md) — not met\n \
- [No rework](../requirements/rule.md) — review rule\n \
- [It is fast](../requirements/untested.md) — no test\n\
\n<span data-checked=\"7\"></span>\n"
);
assert!(listing("README.md").starts_with("- [A save is kept](requirements/kept.md)"));
}
}