spvirit-tools 0.1.20

PVAccess client/server tools for EPICS
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
//! Verifies that the documentation in `docs/book` still matches the code.
//!
//! Chapters contain no copies of code — every snippet is an `{{#include}}`
//! against a real example file. This suite checks that every file, anchor,
//! tool, and example a chapter cites still exists, that no chapter includes
//! code it did not declare, that every shipped tool and example is documented
//! somewhere, and that the generated badge blocks tell the truth.
//!
//! Regenerate badge blocks after editing `verify.toml`:
//!
//!     UPDATE_DOCS=1 cargo test -p spvirit-tools --test docs_verify

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;

// ─── manifest ────────────────────────────────────────────────────────────

#[derive(Deserialize, Default)]
struct Verify {
    #[serde(default)]
    allow: Allow,
    #[serde(default)]
    chapters: BTreeMap<String, Chapter>,
}

#[derive(Deserialize, Default)]
struct Allow {
    #[serde(default)]
    undocumented_tools: Vec<String>,
    #[serde(default)]
    undocumented_examples: Vec<String>,
}

#[derive(Deserialize, Default)]
struct Chapter {
    #[serde(default)]
    rust_examples: Vec<String>,
    #[serde(default)]
    py_examples: Vec<String>,
    #[serde(default)]
    anchors: Vec<String>,
    #[serde(default)]
    tools: Vec<String>,
}

fn repo_root() -> PathBuf {
    // CARGO_MANIFEST_DIR is <root>/spvirit-tools
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("spvirit-tools has a parent directory")
        .to_path_buf()
}

fn load_verify() -> Verify {
    let path = repo_root().join("docs/book/verify.toml");
    let text =
        fs::read_to_string(&path).unwrap_or_else(|e| panic!("cannot read {}: {e}", path.display()));
    toml::from_str(&text).unwrap_or_else(|e| panic!("cannot parse {}: {e}", path.display()))
}

/// True if `src` contains a balanced `ANCHOR: name` / `ANCHOR_END: name` pair.
///
/// Matching to end-of-line keeps `sim` from matching `simulate`.
fn anchor_exists(src: &str, name: &str) -> bool {
    let start = format!("ANCHOR: {name}");
    let end = format!("ANCHOR_END: {name}");
    let has = |needle: &str| src.lines().any(|l| l.trim_end().ends_with(needle));
    has(&start) && has(&end)
}

// ─── citation checks ─────────────────────────────────────────────────────

#[test]
fn cited_files_exist() {
    let verify = load_verify();
    let root = repo_root();
    let mut missing = Vec::new();

    for (chapter, spec) in &verify.chapters {
        if !root.join("docs/book/src").join(chapter).is_file() {
            missing.push(format!("chapter itself: docs/book/src/{chapter}"));
        }
        for file in spec.rust_examples.iter().chain(spec.py_examples.iter()) {
            if !root.join(file).is_file() {
                missing.push(format!("{chapter} cites missing file {file}"));
            }
        }
    }

    assert!(
        missing.is_empty(),
        "verify.toml cites files that do not exist:\n  {}",
        missing.join("\n  ")
    );
}

#[test]
fn cited_anchors_resolve() {
    let verify = load_verify();
    let root = repo_root();
    let mut broken = Vec::new();

    for (chapter, spec) in &verify.chapters {
        for anchor in &spec.anchors {
            let (file, name) = anchor
                .rsplit_once(':')
                .unwrap_or_else(|| panic!("{chapter}: anchor {anchor:?} is not <path>:<name>"));
            let Ok(src) = fs::read_to_string(root.join(file)) else {
                broken.push(format!("{chapter}: cannot read {file}"));
                continue;
            };
            if !anchor_exists(&src, name) {
                broken.push(format!("{chapter}: no balanced ANCHOR {name} in {file}"));
            }
        }
    }

    assert!(
        broken.is_empty(),
        "unresolved anchors:\n  {}",
        broken.join("\n  ")
    );
}

#[test]
fn chapters_declare_every_include() {
    let verify = load_verify();
    let root = repo_root();
    let src_dir = root.join("docs/book/src");
    let mut undeclared = Vec::new();

    for (chapter, spec) in &verify.chapters {
        let chapter_path = src_dir.join(chapter);
        let Ok(text) = fs::read_to_string(&chapter_path) else {
            continue;
        };
        let chapter_dir = chapter_path.parent().unwrap().to_path_buf();

        for line in text.lines() {
            let Some(rest) = line.split_once("{{#include ").map(|(_, r)| r) else {
                continue;
            };
            let Some(target) = rest.split_once("}}").map(|(t, _)| t.trim()) else {
                continue;
            };
            // strip the ":anchor" suffix if present
            let raw = target.rsplit_once(':').map_or(target, |(p, _)| p);
            let resolved = chapter_dir.join(raw);
            let Ok(canonical) = resolved.canonicalize() else {
                undeclared.push(format!("{chapter}: include target does not exist: {raw}"));
                continue;
            };
            let declared = spec
                .rust_examples
                .iter()
                .chain(spec.py_examples.iter())
                .any(|d| root.join(d).canonicalize().ok().as_deref() == Some(&canonical));
            if !declared {
                undeclared.push(format!(
                    "{chapter}: includes {raw}, not declared in verify.toml"
                ));
            }
        }
    }

    assert!(
        undeclared.is_empty(),
        "chapters include code they did not declare:\n  {}",
        undeclared.join("\n  ")
    );
}

// ─── reverse coverage ────────────────────────────────────────────────────

/// Every `[[bin]]` name declared in spvirit-tools/Cargo.toml.
fn shipped_tools() -> Vec<String> {
    let text = fs::read_to_string(repo_root().join("spvirit-tools/Cargo.toml"))
        .expect("read spvirit-tools/Cargo.toml");
    let manifest: toml::Value = text.parse().expect("parse spvirit-tools/Cargo.toml");
    manifest
        .get("bin")
        .and_then(|b| b.as_array())
        .expect("spvirit-tools/Cargo.toml has [[bin]] entries")
        .iter()
        .filter_map(|b| b.get("name")?.as_str().map(str::to_owned))
        .collect()
}

/// Every example target in the workspace, as repo-relative paths.
fn shipped_examples() -> Vec<String> {
    let root = repo_root();
    let mut found = Vec::new();
    for crate_dir in [
        "spvirit-client",
        "spvirit-server",
        "spvirit-codec",
        "spvirit-types",
    ] {
        let dir = root.join(crate_dir).join("examples");
        let Ok(entries) = fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "rs") {
                found.push(format!(
                    "{crate_dir}/examples/{}",
                    path.file_name().unwrap().to_string_lossy()
                ));
            }
        }
    }
    found.sort();
    found
}

#[test]
fn every_tool_is_documented() {
    let verify = load_verify();
    let documented: Vec<&str> = verify
        .chapters
        .values()
        .flat_map(|c| c.tools.iter().map(String::as_str))
        .collect();

    let mut undocumented = Vec::new();
    for tool in shipped_tools() {
        if documented.contains(&tool.as_str()) {
            continue;
        }
        if verify.allow.undocumented_tools.contains(&tool) {
            continue;
        }
        undocumented.push(tool);
    }

    assert!(
        undocumented.is_empty(),
        "these tools ship but no chapter documents them: {undocumented:?}\n\
         Write a chapter for each, or add it to [allow].undocumented_tools in \
         docs/book/verify.toml with a reason."
    );
}

#[test]
fn every_example_is_documented() {
    let verify = load_verify();
    let documented: Vec<&str> = verify
        .chapters
        .values()
        .flat_map(|c| c.rust_examples.iter().map(String::as_str))
        .collect();

    let mut undocumented = Vec::new();
    for example in shipped_examples() {
        if documented.contains(&example.as_str()) {
            continue;
        }
        if verify.allow.undocumented_examples.contains(&example) {
            continue;
        }
        undocumented.push(example);
    }

    assert!(
        undocumented.is_empty(),
        "these examples ship but no chapter documents them: {undocumented:?}\n\
         Cite each from a chapter, or add it to [allow].undocumented_examples in \
         docs/book/verify.toml with a reason."
    );
}

/// The [allow] lists are a migration aid and must shrink to empty. This test
/// fails if an entry is stale — either the thing no longer exists, or a
/// chapter now documents it and the entry should have been deleted.
#[test]
fn allow_list_has_no_stale_entries() {
    let verify = load_verify();
    let tools = shipped_tools();
    let examples = shipped_examples();
    let mut stale = Vec::new();

    let documented_tools: Vec<&str> = verify
        .chapters
        .values()
        .flat_map(|c| c.tools.iter().map(String::as_str))
        .collect();
    let documented_examples: Vec<&str> = verify
        .chapters
        .values()
        .flat_map(|c| c.rust_examples.iter().map(String::as_str))
        .collect();

    for tool in &verify.allow.undocumented_tools {
        if !tools.contains(tool) {
            stale.push(format!(
                "allow.undocumented_tools has {tool:?}, which is not a [[bin]]"
            ));
        } else if documented_tools.contains(&tool.as_str()) {
            stale.push(format!(
                "{tool:?} is now documented — delete it from allow.undocumented_tools"
            ));
        }
    }
    for example in &verify.allow.undocumented_examples {
        if !examples.contains(example) {
            stale.push(format!(
                "allow.undocumented_examples has {example:?}, which does not exist"
            ));
        } else if documented_examples.contains(&example.as_str()) {
            stale.push(format!(
                "{example:?} is now documented — delete it from allow.undocumented_examples"
            ));
        }
    }

    assert!(
        stale.is_empty(),
        "stale [allow] entries:\n  {}",
        stale.join("\n  ")
    );
}

// ─── generated badges ────────────────────────────────────────────────────

const BADGE_BEGIN: &str = "<!-- verify:begin -->";
const BADGE_END: &str = "<!-- verify:end -->";
const BLOB: &str = "https://github.com/ISISNeutronMuon/spvirit/blob/main";
const CI: &str = "https://github.com/ISISNeutronMuon/spvirit/actions/workflows/ci.yml";

/// Renders the verification badge for a chapter. Deterministic: the same
/// manifest entry always produces byte-identical output.
fn badge_block(spec: &Chapter) -> String {
    let mut parts = Vec::new();
    for file in spec.rust_examples.iter().chain(spec.py_examples.iter()) {
        let name = file.rsplit('/').next().unwrap_or(file);
        parts.push(format!("[`{name}`]({BLOB}/{file})"));
    }
    parts.push(format!(
        "check [`docs_verify`]({BLOB}/spvirit-tools/tests/docs_verify.rs)"
    ));

    let sources = if parts.len() == 1 {
        String::from("no code on this page")
    } else {
        parts.join(" · ")
    };

    format!(
        "{BADGE_BEGIN}\n\
         > ✅ **Verified** · {sources} · \
         [![docs-verify]({CI}/badge.svg)]({CI})\n\
         >\n\
         > The badge reports the whole `docs-verify` suite, not this chapter alone.\n\
         {BADGE_END}"
    )
}

fn splice_badge(text: &str, badge: &str) -> Option<String> {
    let start = text.find(BADGE_BEGIN)?;
    let end = text.find(BADGE_END)? + BADGE_END.len();
    Some(format!("{}{}{}", &text[..start], badge, &text[end..]))
}

#[test]
fn badges_match_the_manifest() {
    let verify = load_verify();
    let src_dir = repo_root().join("docs/book/src");
    let update = std::env::var_os("UPDATE_DOCS").is_some();
    let mut wrong = Vec::new();

    for (chapter, spec) in &verify.chapters {
        let path = src_dir.join(chapter);
        let Ok(text) = fs::read_to_string(&path) else {
            continue;
        };
        let expected = badge_block(spec);

        if !text.contains(BADGE_BEGIN) || !text.contains(BADGE_END) {
            wrong.push(format!(
                "{chapter}: missing the verify:begin/verify:end markers"
            ));
            continue;
        }
        if text.contains(&expected) {
            continue;
        }
        if update {
            let fixed = splice_badge(&text, &expected).expect("markers present");
            fs::write(&path, fixed).expect("rewrite chapter");
        } else {
            wrong.push(format!("{chapter}: badge is stale"));
        }
    }

    assert!(
        wrong.is_empty(),
        "badge blocks disagree with verify.toml:\n  {}\n\n\
         Regenerate with:  UPDATE_DOCS=1 cargo test -p spvirit-tools --test docs_verify",
        wrong.join("\n  ")
    );
}

/// Python examples are syntax-checked, not executed — running them needs a
/// built wheel, which the docs job does not have.
#[test]
fn python_examples_compile() {
    let verify = load_verify();
    let root = repo_root();
    let mut files: Vec<&String> = verify
        .chapters
        .values()
        .flat_map(|c| c.py_examples.iter())
        .collect();
    files.sort();
    files.dedup();
    if files.is_empty() {
        return;
    }

    let mut cmd = std::process::Command::new("python");
    cmd.arg("-m").arg("py_compile").current_dir(&root);
    for f in &files {
        cmd.arg(f);
    }

    match cmd.output() {
        Ok(out) => assert!(
            out.status.success(),
            "py_compile failed:\n{}",
            String::from_utf8_lossy(&out.stderr)
        ),
        // No python on this machine — skip rather than fail the whole suite.
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            eprintln!("skipping python_examples_compile: no `python` on PATH");
        }
        Err(e) => panic!("failed to run python: {e}"),
    }
}