- crates/lsp toolchain: PHP from php.executable or PATH, `php -v` with a 5 s timeout and capped output, version parsing (proptest), PHP >= 8.2. - Pinned Phpactor 2026.06.25.0 (SHA-256 832510fe...59e9); newer releases need ext-posix and do not start on Windows. - Download with ureq + rustls (ring, OS roots via rustls-native-certs): HTTPS only, 32 MiB cap, SHA-256 verified before an atomic write, retry with backoff. Tests use a local HTTP server only. - Engine: settings php.executable, phpactor.path, phpactor.autoDownload (bool and string setting kinds), toolchain.status, toolchain.downloadPhpactor, toolchainChanged, notifications; worker thread off the engine lock, results via the pump. Downloads only after the host calls Engine::allow_downloads (the desktop does). - Docs: architecture Toolchain section (network access, D15), G6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
4.8 KiB
Rust
159 lines
4.8 KiB
Rust
//! The toolchain over a real TCP control connection: `toolchain.status`
|
|
//! answers at once, the check runs off the engine lock and its result
|
|
//! arrives as `toolchainChanged` and a notification from the pump. No
|
|
//! network: the test backend never allows downloads.
|
|
|
|
#![allow(
|
|
clippy::unwrap_used,
|
|
clippy::expect_used,
|
|
clippy::indexing_slicing,
|
|
clippy::panic
|
|
)]
|
|
|
|
use std::io::{BufRead, BufReader, Write};
|
|
use std::net::TcpStream;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::time::Duration;
|
|
|
|
use lutra_testkit::EngineBackend;
|
|
use serde_json::{Value, json};
|
|
|
|
/// A client that keeps responses and event lines apart.
|
|
struct Client {
|
|
reader: BufReader<TcpStream>,
|
|
writer: TcpStream,
|
|
events: Vec<Value>,
|
|
}
|
|
|
|
impl Client {
|
|
fn connect(addr: std::net::SocketAddr) -> Self {
|
|
let stream = TcpStream::connect(addr).unwrap();
|
|
stream
|
|
.set_read_timeout(Some(Duration::from_secs(30)))
|
|
.unwrap();
|
|
Self {
|
|
writer: stream.try_clone().unwrap(),
|
|
reader: BufReader::new(stream),
|
|
events: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn line(&mut self) -> Value {
|
|
let mut line = String::new();
|
|
self.reader.read_line(&mut line).expect("line within 30 s");
|
|
serde_json::from_str(&line).unwrap()
|
|
}
|
|
|
|
fn call(&mut self, request: Value) -> Value {
|
|
self.writer
|
|
.write_all(format!("{request}\n").as_bytes())
|
|
.unwrap();
|
|
loop {
|
|
let line = self.line();
|
|
if line.get("event").is_some() {
|
|
self.events.push(line["event"].clone());
|
|
} else {
|
|
return line;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The next event (queued or read) for which `pick` holds.
|
|
fn event(&mut self, pick: impl Fn(&Value) -> bool) -> Value {
|
|
if let Some(i) = self.events.iter().position(&pick) {
|
|
return self.events.remove(i);
|
|
}
|
|
loop {
|
|
let line = self.line();
|
|
if let Some(event) = line.get("event") {
|
|
if pick(event) {
|
|
return event.clone();
|
|
}
|
|
self.events.push(event.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn execute(command: &str, params: Value) -> Value {
|
|
json!({"id": command, "method": "engine.execute", "params": {"command": command, "params": params}})
|
|
}
|
|
|
|
/// Pumps the backend every 20 ms, like the desktop's pump thread, until
|
|
/// dropped.
|
|
struct Pump(Arc<AtomicBool>);
|
|
|
|
impl Pump {
|
|
fn start(backend: Arc<EngineBackend>) -> Self {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let flag = stop.clone();
|
|
std::thread::spawn(move || {
|
|
while !flag.load(Ordering::Relaxed) {
|
|
backend.pump();
|
|
std::thread::sleep(Duration::from_millis(20));
|
|
}
|
|
});
|
|
Self(stop)
|
|
}
|
|
}
|
|
|
|
impl Drop for Pump {
|
|
fn drop(&mut self) {
|
|
self.0.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn status_and_check_over_tcp() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let missing = dir.path().join("no-php").join("php");
|
|
let backend = Arc::new(EngineBackend::new());
|
|
let server = lutra_control::start(0, backend.clone()).unwrap();
|
|
let mut client = Client::connect(server.local_addr());
|
|
client.call(json!({"id": 1, "method": "events.subscribe"}));
|
|
let r = client.call(execute(
|
|
"settings.set",
|
|
json!({"key": "php.executable", "value": missing.display().to_string()}),
|
|
));
|
|
assert!(r["error"].is_null(), "{r}");
|
|
let _pump = Pump::start(backend.clone());
|
|
|
|
let r = client.call(execute("toolchain.status", Value::Null));
|
|
let status = &r["result"];
|
|
assert_eq!(status["php"]["found"], false, "{r}");
|
|
assert!(status["php"].get("path").is_some());
|
|
assert!(status["php"].get("version").is_some());
|
|
assert_eq!(status["phpactor"]["state"], "missing");
|
|
|
|
let changed = client.event(|e| e["kind"] == "toolchainChanged");
|
|
assert_eq!(changed["php"]["found"], false, "{changed}");
|
|
assert!(
|
|
changed["php"]["error"]
|
|
.as_str()
|
|
.unwrap()
|
|
.contains("php.executable"),
|
|
"{changed}"
|
|
);
|
|
assert_eq!(changed["phpactor"]["state"], "missing");
|
|
let note = client.event(|e| e["kind"] == "notification");
|
|
assert_eq!(note["messageKey"], "notification.toolchain.phpMissing");
|
|
assert_eq!(note["level"], "warning");
|
|
assert_eq!(note["params"]["min"], "8.2.0");
|
|
|
|
let r = client.call(execute("toolchain.status", Value::Null));
|
|
assert_eq!(
|
|
r["result"],
|
|
changed
|
|
.as_object()
|
|
.map(|o| {
|
|
let mut o = o.clone();
|
|
o.remove("kind");
|
|
Value::Object(o)
|
|
})
|
|
.unwrap()
|
|
);
|
|
|
|
let r = client.call(execute("toolchain.downloadPhpactor", Value::Null));
|
|
assert_eq!(r["error"]["code"], "failed", "{r}");
|
|
}
|