summaryrefslogtreecommitdiffstats
path: root/tests/layers.rs
blob: 18564620ab928af6c8aba0e2466192574ed521a0 (plain)
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
use std::path::{Path, PathBuf};

fn src_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("src")
}

fn rust_files(dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    let mut stack = vec![dir.to_path_buf()];
    while let Some(d) = stack.pop() {
        for entry in std::fs::read_dir(&d).expect("read dir") {
            let path = entry.expect("entry").path();
            if path.is_dir() {
                stack.push(path);
            } else if path.extension().is_some_and(|e| e == "rs") {
                out.push(path);
            }
        }
    }
    out.sort();
    out
}

fn edges(dir: &str, forbidden: &[&str]) -> Vec<String> {
    let mut hits = Vec::new();
    for file in rust_files(&src_root().join(dir)) {
        let text = std::fs::read_to_string(&file).expect("read file");
        for (n, line) in text.lines().enumerate() {
            let line = line.trim();
            if line.starts_with("//") {
                continue;
            }
            for m in forbidden {
                if line.contains(&format!("crate::{m}")) {
                    hits.push(format!(
                        "{}:{}: {line}",
                        file.strip_prefix(src_root()).unwrap().display(),
                        n + 1
                    ));
                }
            }
        }
    }
    hits
}

#[test]
fn persistence_names_nothing_above_itself() {
    let hits = edges("db", &["watch", "review", "pipeline", "http", "ops", "shims"]);
    assert!(hits.is_empty(), "src/db depends upward on the modules above it:\n{hits:#?}");
}

#[test]
fn review_does_not_call_back_into_the_watcher() {
    let hits = edges("review", &["watch", "pipeline", "http"]);
    assert!(hits.is_empty(), "src/review depends on modules beside or above it:\n{hits:#?}");
}

#[test]
fn the_watcher_does_not_reach_into_the_pipeline_or_the_ui() {
    let hits = edges("watch", &["pipeline", "http", "shims"]);
    assert!(hits.is_empty(), "src/watch depends on modules above it:\n{hits:#?}");
}

#[test]
fn the_recipe_leaf_depends_on_nothing_above_it() {
    let text = std::fs::read_to_string(src_root().join("recipe.rs")).expect("read recipe.rs");
    for m in ["watch", "review", "pipeline", "http", "ops", "shims"] {
        assert!(
            !text.contains(&format!("crate::{m}")),
            "src/recipe.rs must not name crate::{m}"
        );
    }
}