Files
Liber-public/crates/liber-format/tests/purity.rs
T

68 lines
1.8 KiB
Rust

//! The core is pure: no file system, no network, no clock, no processes in either crate's sources, and no network
//! crate anywhere in Cargo.lock. This is the "network-blocked run" for a library that cannot open a socket at all.
use std::path::Path;
const FORBIDDEN: &[&str] = &[
"std::fs",
"std::net",
"TcpStream",
"UdpSocket",
"std::process",
"Command::new",
"SystemTime",
"Instant::now",
"File::open",
"File::create",
"env::var",
"std::env",
];
fn scan(dir: &Path, hits: &mut Vec<String>) {
for e in std::fs::read_dir(dir).unwrap() {
let p = e.unwrap().path();
if p.is_dir() {
scan(&p, hits);
} else if p.extension().is_some_and(|x| x == "rs") {
let s = std::fs::read_to_string(&p).unwrap();
for f in FORBIDDEN {
if s.contains(f) {
hits.push(format!("{}: {f}", p.display()));
}
}
}
}
}
#[test]
fn sources_have_no_io() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
let mut hits = Vec::new();
scan(&root.join("liber-format/src"), &mut hits);
scan(&root.join("liber-library/src"), &mut hits);
scan(&root.join("liber-habit/src"), &mut hits);
scan(&root.join("liber-lang/src"), &mut hits);
assert!(hits.is_empty(), "{hits:#?}");
}
#[test]
fn no_network_crate_in_the_lockfile() {
let lock = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("../../Cargo.lock")).unwrap();
for c in [
"reqwest",
"ureq",
"hyper",
"curl",
"tokio",
"isahc",
"attohttpc",
"native-tls",
"rustls",
"openssl",
"socket2",
"mio",
] {
assert!(!lock.contains(&format!("name = \"{c}\"")), "{c} is in Cargo.lock");
}
}