Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/Skidbladnir

tests/one_tls_setup.rs

//! **One reqwest TLS setup, and no dead-code allows hiding drift (stinky #26).**
//!
//! `tls_roots_blocking` (ring provider plus the bundled Mozilla roots) was pasted into
//! five modules, three of them under `#[allow(dead_code)]`, and the test-only
//! `mockhttp` module carried a module-wide `#[allow(dead_code)]`. A fix to one copy
//! (a new root store, a provider change) would silently miss the others, and the allows
//! meant the compiler could not say when a copy went unused.

use std::path::Path;

fn sources() -> Vec<(String, String)> {
    let mut out = Vec::new();
    for e in std::fs::read_dir(Path::new(env!("CARGO_MANIFEST_DIR")).join("src")).unwrap().flatten() {
        let p = e.path();
        if p.extension().is_some_and(|x| x == "rs") {
            out.push((p.display().to_string(), std::fs::read_to_string(&p).unwrap()));
        }
    }
    out
}

#[test]
fn tls_roots_blocking_is_defined_once() {
    let defs: Vec<String> = sources()
        .into_iter()
        .filter(|(_, s)| s.contains("fn tls_roots_blocking("))
        .map(|(p, _)| p)
        .collect();
    assert_eq!(defs.len(), 1, "tls_roots_blocking is defined in: {defs:#?}");
}

#[test]
fn no_dead_code_allow_on_the_tls_helper_or_mockhttp() {
    let mut bad = Vec::new();
    for (path, src) in sources() {
        let lines: Vec<&str> = src.lines().collect();
        for (i, l) in lines.iter().enumerate() {
            let guarded = l.contains("fn tls_roots_blocking(") || l.contains("mod mockhttp");
            if !guarded {
                continue;
            }
            let attrs = lines[..i]
                .iter()
                .rev()
                .take_while(|a| a.trim_start().starts_with("#[") || a.trim_start().starts_with("//"));
            if attrs.into_iter().any(|a| a.contains("allow(dead_code)")) {
                bad.push(format!("{path}:{}: {}", i + 1, l.trim()));
            }
        }
    }
    assert!(bad.is_empty(), "allow(dead_code) hides drift on:\n{}", bad.join("\n"));
}