дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-25 02:44:37 +02:00
`HostConfig` (9 fields) was written out as a full struct literal in about 100 tests of `ide-host`, almost all of them the same boilerplate, so a new field meant editing ~100 places. This adds one base constructor for tests and migrates every literal that is a plain variant of it. Tests that differ keep struct-update overrides (`HostConfig { sink_for: .., ..HostConfig::for_test(&base) }`). No builder.
- Unit tests (`src/`): `#[cfg(test)] pub(crate) fn HostConfig::for_test(base: &Path)` in `lib.rs`.
- Integration tests (`tests/`): `tests/common/mod.rs` with `host_config(base: &Path)`, pulled in with `mod common;`. It builds the same value as `for_test`.
Both build: `config`, `data`, and `data` again as the machine-local dir (the layout most tests used), `LanguageRegistry::load(None)`, empty syntax, a sink that drops events, identity owner, `Handle::current()`, `project_environment: false`.
Why not one constructor for both: integration tests compile against the crate without `cfg(test)`, so a shared one needs a new `testing` cargo feature plus a dev-dependency on the crate itself (nothing in `ide-host` does that today). Two small definitions need no feature, no `Cargo.toml` change and no public item.
Co-authored-by: Mr0Bread <[email protected]>
63 рядки
2.5 KiB
Rust
63 рядки
2.5 KiB
Rust
//! Binary command arguments over the remote protocol: base64 strings, not arrays of numbers.
|
|
|
|
mod common;
|
|
|
|
use ide_core::IpcBytes;
|
|
use ide_host::Host;
|
|
use ide_host::rpc::{self, Dispatcher, Invoke};
|
|
use ide_jsonrpc::{Handler, Peer};
|
|
use serde_json::{Value, json};
|
|
use std::sync::{Arc, OnceLock};
|
|
|
|
struct Client;
|
|
|
|
impl Handler for Client {
|
|
fn on_notification(&self, _: &str, _: Value) {}
|
|
fn on_request(&self, method: &str, _: Value) -> Result<Value, (i64, String)> {
|
|
Err((ide_jsonrpc::codes::METHOD_NOT_FOUND, method.to_owned()))
|
|
}
|
|
}
|
|
|
|
async fn connect(base: &std::path::Path) -> Peer {
|
|
let host = Arc::new(Host::new(common::host_config(base)));
|
|
let (client_io, server_io) = tokio::io::duplex(1 << 20);
|
|
let (sr, sw) = tokio::io::split(server_io);
|
|
tokio::spawn(rpc::serve(host, Dispatcher::standard(), Arc::new(OnceLock::new()), sr, sw));
|
|
let (cr, cw) = tokio::io::split(client_io);
|
|
Peer::connect(cr, cw, Arc::new(Client))
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn file_bytes_cross_the_protocol_as_base64() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let peer = connect(tmp.path()).await;
|
|
let bytes: Vec<u8> = (0..=255).cycle().take(3 * 1024 * 1024 + 1).collect();
|
|
let path = tmp.path().join("image.bin");
|
|
|
|
let args = json!({ "path": path, "data": IpcBytes(bytes.clone()) });
|
|
assert!(args["data"].is_string());
|
|
// Measure the envelope separately so the assertion is independent of the temp directory.
|
|
let envelope = serde_json::to_vec(&json!({ "path": path, "data": "" })).unwrap();
|
|
let body = serde_json::to_vec(&args).unwrap();
|
|
assert_eq!(body.len(), envelope.len() + bytes.len().div_ceil(3) * 4);
|
|
|
|
let call = Invoke { window: "main".into(), cmd: "fs_create_file_bytes".into(), args };
|
|
peer.request::<_, Value>("invoke", call, None).await.map_err(rpc::error_from_reply).unwrap();
|
|
assert_eq!(std::fs::read(&path).unwrap(), bytes);
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn an_array_of_numbers_is_not_accepted_as_bytes() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let peer = connect(tmp.path()).await;
|
|
let path = tmp.path().join("old.bin");
|
|
let call = Invoke {
|
|
window: "main".into(),
|
|
cmd: "fs_create_file_bytes".into(),
|
|
args: json!({ "path": path, "data": [1, 2, 3] }),
|
|
};
|
|
let error = peer.request::<_, Value>("invoke", call, None).await.map_err(rpc::error_from_reply).unwrap_err();
|
|
assert!(error.message.contains("argument data"), "{error:?}");
|
|
assert!(!path.exists());
|
|
}
|