дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Plugin API 1.21 adds Database tool window context menus for data sources, schemas/databases and columns next to database.table.context, each item receiving the selected nodes, and database.open-console / database.open-table-data, which open the IDE's Query Console (SQL filled in, not run) or a table's data grid from a command the user invoked, after checking the source exists and the database capability is granted. The plugin UI reaches the Database feature through a new DatabaseService in the workbench contributions rather than importing it. Includes SDK methods and MockHost helpers, a fixture plugin with a host integration test, UI tests, regenerated bindings, rebuilt plugin components and README notes. Co-authored-by: RuyimgByCN <[email protected]>
114 рядки
4.7 KiB
Rust
114 рядки
4.7 KiB
Rust
//! API 1.21 `database.open-console` & `database.open-table-data`: capability gating,
|
|
//! unknown source rejection, and OpenDatabaseConsole / OpenDatabaseTable event emission.
|
|
mod common;
|
|
|
|
use ide_core::error::ErrorKind;
|
|
use ide_host::plugins::{Consent, HostWorkspace, PluginEvent, PluginHost};
|
|
use ide_host::{Host, HostConfig, ops};
|
|
use ide_plugin::{Invocation, Limits};
|
|
use std::{
|
|
path::{Path, PathBuf},
|
|
sync::{Arc, Mutex},
|
|
time::Duration,
|
|
};
|
|
|
|
/// An emitted plugin event and the window it went to.
|
|
type WindowEvent = (Option<String>, PluginEvent);
|
|
|
|
const ID: &str = "test.database-open";
|
|
|
|
fn fixture() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/database-open")
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn open_console_and_table_data_enforce_grants_and_emit_events() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
|
|
std::fs::create_dir(&root).unwrap();
|
|
let sqlite = root.join("db.sqlite");
|
|
std::fs::File::create(&sqlite).unwrap();
|
|
|
|
let host =
|
|
Arc::new(Host::new(HostConfig { local_data_dir: tmp.path().join("local"), ..common::host_config(tmp.path()) }));
|
|
host.open_project("main", &root).unwrap();
|
|
ops::project::project_trust_set(&host, root.clone(), true, false).await.unwrap();
|
|
let source = ide_db::DataSource {
|
|
id: "test-db".into(),
|
|
name: "Test DB".into(),
|
|
file: Some(ide_core::IpcPath(sqlite)),
|
|
..Default::default()
|
|
};
|
|
ops::db::db_save_sources(&host, "main", vec![source]).await.unwrap();
|
|
|
|
let inv = || Invocation { client: Some("main".into()), workspace_root: Some(root.clone()), ..Default::default() };
|
|
|
|
// 1. Denied without `database.schema` / `database.query`.
|
|
let denied = Arc::new(
|
|
PluginHost::new(
|
|
tmp.path().join("plugins-denied"),
|
|
Arc::new(|_: Option<&str>, _| {}),
|
|
Limits { timeout: Duration::from_secs(5), ..Default::default() },
|
|
)
|
|
.unwrap(),
|
|
);
|
|
denied.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
|
|
denied.install_from_dir(&fixture(), &Consent { optional: vec![], signer_change: true }).unwrap();
|
|
|
|
let (p, i) = (denied.clone(), inv());
|
|
let err_console =
|
|
tokio::task::spawn_blocking(move || p.run_command(ID, "open-console", None, i)).await.unwrap().unwrap_err();
|
|
assert_eq!(err_console.kind, ErrorKind::PermissionDenied);
|
|
|
|
let (p, i) = (denied.clone(), inv());
|
|
let err_table =
|
|
tokio::task::spawn_blocking(move || p.run_command(ID, "open-table", None, i)).await.unwrap().unwrap_err();
|
|
assert_eq!(err_table.kind, ErrorKind::PermissionDenied);
|
|
|
|
// 2. Granted capabilities: unknown source is NotFound; saved source emits UI open events.
|
|
let events: Arc<Mutex<Vec<WindowEvent>>> = Arc::new(Mutex::new(Vec::new()));
|
|
let sink_events = events.clone();
|
|
let granted = Arc::new(
|
|
PluginHost::new(
|
|
tmp.path().join("plugins-granted"),
|
|
Arc::new(move |w: Option<&str>, ev| {
|
|
sink_events.lock().unwrap().push((w.map(str::to_owned), ev));
|
|
}),
|
|
Limits { timeout: Duration::from_secs(5), ..Default::default() },
|
|
)
|
|
.unwrap(),
|
|
);
|
|
granted.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
|
|
granted.install_from_dir(&fixture(), &Consent::all()).unwrap();
|
|
|
|
let (p, i) = (granted.clone(), inv());
|
|
let missing = tokio::task::spawn_blocking(move || p.run_command(ID, "open-console", Some("unknown"), i))
|
|
.await
|
|
.unwrap()
|
|
.unwrap_err();
|
|
assert_eq!(missing.kind, ErrorKind::NotFound);
|
|
|
|
let (p, i) = (granted.clone(), inv());
|
|
tokio::task::spawn_blocking(move || p.run_command(ID, "open-console", Some("test-db"), i)).await.unwrap().unwrap();
|
|
let (p, i) = (granted.clone(), inv());
|
|
tokio::task::spawn_blocking(move || p.run_command(ID, "open-table", Some("test-db"), i)).await.unwrap().unwrap();
|
|
|
|
let recorded = events.lock().unwrap();
|
|
assert!(recorded.iter().any(|(w, e)| {
|
|
w.as_deref() == Some("main")
|
|
&& matches!(
|
|
e,
|
|
PluginEvent::OpenDatabaseConsole { plugin_id, source_id, sql, schema }
|
|
if plugin_id == ID && source_id == "test-db" && sql.as_deref() == Some("SELECT 1") && schema.as_deref() == Some("main")
|
|
)
|
|
}));
|
|
assert!(recorded.iter().any(|(w, e)| {
|
|
w.as_deref() == Some("main")
|
|
&& matches!(
|
|
e,
|
|
PluginEvent::OpenDatabaseTable { plugin_id, source_id, catalog, schema, table }
|
|
if plugin_id == ID && source_id == "test-db" && catalog.is_none() && schema == "main" && table == "users"
|
|
)
|
|
}));
|
|
}
|