no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{
    current_observer_with, has_matching_disable_directive_with, EngineHooks, MetricsObserver,
};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

struct CountingObserver {
    verbose: bool,
    total: AtomicU64,
}

impl MetricsObserver for CountingObserver {
    fn increment(&self, _metric: &'static str, amount: u64) {
        self.total.fetch_add(amount, Ordering::SeqCst);
    }

    fn verbose(&self) -> bool {
        self.verbose
    }
}

struct StubHooks {
    observer: Arc<CountingObserver>,
    suppressed: bool,
}

impl EngineHooks for StubHooks {
    fn current_observer(&self) -> Option<Arc<dyn MetricsObserver>> {
        Some(Arc::clone(&self.observer) as Arc<dyn MetricsObserver>)
    }

    fn has_matching_disable_directive(&self, _source: &str, _line: u32, _rule_id: &str) -> bool {
        self.suppressed
    }
}

#[test]
fn unregistered_hooks_default_to_absent_observer_and_unsuppressed() {
    assert!(current_observer_with(None).is_none());
    assert!(!has_matching_disable_directive_with(
        None,
        "// no-mistakes-disable-file r",
        1,
        "r"
    ));
}

#[test]
fn registered_hooks_forward_observer_and_suppression_queries() {
    let observer = Arc::new(CountingObserver {
        verbose: true,
        total: AtomicU64::new(0),
    });
    let hooks = StubHooks {
        observer: Arc::clone(&observer),
        suppressed: true,
    };
    let active = current_observer_with(Some(&hooks)).expect("observer");
    assert!(active.verbose());
    active.increment("postgres.sql.variants_composed", 3);
    assert_eq!(observer.total.load(Ordering::SeqCst), 3);
    assert!(has_matching_disable_directive_with(
        Some(&hooks),
        "code",
        1,
        "postgres-no-offset"
    ));
}

#[test]
fn test_observer_and_fs_sources_cover_hook_and_catalog_helpers() {
    use no_mistakes_ts_analysis::source_access::SourceAccess;
    let observer = super::test_observer::TestObserver::new(true);
    super::test_observer::with_observer(Some(Arc::clone(&observer)), || {
        assert!(!super::has_matching_disable_directive("code", 1, "rule"));
        let current = super::current_observer().expect("active test observer");
        assert!(current.verbose());
        current.increment("test.metric", 2);
    });
    assert_eq!(observer.snapshot().work["test.metric"], 2);

    let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../fixtures/postgres/catalog");
    let valid = root.join("wrong-version.json");
    let invalid = root.join("invalid.json");
    assert!(crate::test_support::FsSources
        .read_source_path(&valid)
        .is_ok());
    assert!(crate::test_support::FsSources
        .parse_json_path(&valid)
        .is_ok());
    assert!(crate::test_support::FsSources
        .parse_json_path(&invalid)
        .is_err());
    assert!(crate::test_support::FsSources
        .parse_json_path(&root.join("missing.json"))
        .is_err());
    assert!(crate::SchemaCatalog::from_json("{not json").is_err());
    assert!(crate::SchemaCatalog::from_json(r#"{"formatVersion": 1}"#).is_err());
}