vsg-bridge-ls/tests/packaged_vsg.rs

102 lines
3.6 KiB
Rust

//! Opt-in integration check against an installed companion VSG package.
use serde_json::Value;
use std::path::PathBuf;
use std::time::Duration;
use vsg_bridge_ls::{ProcessVsgRunner, VsgRunner};
/// Exercises diagnostics, one scoped fix, and fix-all against a real wheel.
#[tokio::test]
#[ignore = "set VSG_EXECUTABLE to an installed companion VSG executable"]
async fn packaged_vsg_supports_bridge_protocol() {
let executable = std::env::var_os("VSG_EXECUTABLE")
.map(PathBuf::from)
.expect("VSG_EXECUTABLE must identify the packaged VSG executable");
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let source = std::fs::read_to_string(root.join("showcase/one-line-entity.vhd"))
.expect("read showcase VHDL");
let configuration = root.join("showcase/vsg.yaml");
let arguments = vec![
"-c".to_owned(),
configuration.to_string_lossy().into_owned(),
];
let runner = ProcessVsgRunner::new(executable, Duration::from_secs(30));
let diagnostic_output = runner
.lint(&source, &arguments)
.await
.expect("packaged VSG diagnostic request");
let document: Value =
serde_json::from_str(&diagnostic_output).expect("versioned diagnostic JSON");
assert_eq!(document["schema"], "vsg.diagnostics");
assert_eq!(document["schema_version"], 1);
assert!(document["diagnostics"]
.as_array()
.is_some_and(|diagnostics| diagnostics.len() >= 4));
let scoped = runner
.fix_rules(&source, &arguments, &[("entity_012".to_owned(), 1)])
.await
.expect("packaged VSG scoped fix");
assert_ne!(scoped, source);
assert!(scoped.contains("entity TINY"));
let fixed = runner
.fix_all(&source, &arguments)
.await
.expect("packaged VSG fix-all");
assert_ne!(fixed, source);
assert!(fixed.lines().count() > source.lines().count());
let diagnosed_fixed: Value = serde_json::from_str(
&runner
.lint(&fixed, &arguments)
.await
.expect("diagnose fix-all result"),
)
.expect("valid diagnostics after fix-all");
assert_eq!(diagnosed_fixed["diagnostics"], serde_json::json!([]));
let multi_selectors = vec![
("entity_008".to_owned(), 1),
("entity_012".to_owned(), 1),
("entity_021".to_owned(), 1),
("entity_026".to_owned(), 1),
];
let multi = runner
.fix_rules(&source, &arguments, &multi_selectors)
.await
.expect("packaged VSG multi-rule line fix");
let diagnosed_multi: Value = serde_json::from_str(
&runner
.lint(&multi, &arguments)
.await
.expect("diagnose multi-rule result"),
)
.expect("valid diagnostics after multi-rule fix");
assert_eq!(diagnosed_multi["schema_version"], 1);
let after = diagnosed_multi["diagnostics"]
.as_array()
.expect("diagnostic array after multi-rule fix");
let selected_remaining = after
.iter()
.filter(|diagnostic| {
diagnostic["rule"]
.as_str()
.is_some_and(|rule| multi_selectors.iter().any(|(selected, _)| selected == rule))
})
.count();
assert!(selected_remaining < multi_selectors.len());
assert!(
after
.iter()
.any(|diagnostic| diagnostic["rule"] == "entity_009"),
"the known phase-one newline currently exposes an indentation finding"
);
let repeated = runner
.fix_rules(&multi, &arguments, &multi_selectors)
.await
.expect("repeat packaged VSG multi-rule line fix");
assert_eq!(repeated, multi, "second identical request must be a no-op");
}