tests.rs

  1use std::sync::Arc;
  2
  3use anyhow::{Context as _, Result};
  4use dap::adapters::DebugTaskDefinition;
  5use dap::client::DebugAdapterClient;
  6use gpui::{Entity, TestAppContext, WindowHandle};
  7use project::{Project, debugger::session::Session};
  8use settings::SettingsStore;
  9use task::TaskContext;
 10use terminal_view::terminal_panel::TerminalPanel;
 11use workspace::Workspace;
 12
 13use crate::{debugger_panel::DebugPanel, session::DebugSession};
 14
 15#[cfg(test)]
 16mod attach_modal;
 17#[cfg(test)]
 18mod console;
 19#[cfg(test)]
 20mod dap_logger;
 21#[cfg(test)]
 22mod debugger_panel;
 23#[cfg(test)]
 24mod inline_values;
 25#[cfg(test)]
 26mod module_list;
 27#[cfg(test)]
 28mod new_process_modal;
 29#[cfg(test)]
 30mod persistence;
 31#[cfg(test)]
 32mod stack_frame_list;
 33#[cfg(test)]
 34mod variable_list;
 35
 36pub fn init_test(cx: &mut gpui::TestAppContext) {
 37    #[cfg(test)]
 38    zlog::init_test();
 39
 40    cx.update(|cx| {
 41        let settings = SettingsStore::test(cx);
 42        cx.set_global(settings);
 43        SettingsStore::load_registered_settings(cx);
 44
 45        terminal_view::init(cx);
 46        theme::init(theme::LoadThemes::JustBase, cx);
 47        command_palette_hooks::init(cx);
 48        language::init(cx);
 49        workspace::init_settings(cx);
 50        Project::init_settings(cx);
 51        editor::init(cx);
 52        crate::init(cx);
 53        dap_adapters::init(cx);
 54    });
 55}
 56
 57pub async fn init_test_workspace(
 58    project: &Entity<Project>,
 59    cx: &mut TestAppContext,
 60) -> WindowHandle<Workspace> {
 61    let workspace_handle =
 62        cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
 63
 64    let debugger_panel = workspace_handle
 65        .update(cx, |_, window, cx| {
 66            cx.spawn_in(window, async move |this, cx| {
 67                DebugPanel::load(this, cx).await
 68            })
 69        })
 70        .unwrap()
 71        .await
 72        .expect("Failed to load debug panel");
 73
 74    let terminal_panel = workspace_handle
 75        .update(cx, |_, window, cx| {
 76            cx.spawn_in(window, async |this, cx| {
 77                TerminalPanel::load(this, cx.clone()).await
 78            })
 79        })
 80        .unwrap()
 81        .await
 82        .expect("Failed to load terminal panel");
 83
 84    workspace_handle
 85        .update(cx, |workspace, window, cx| {
 86            workspace.add_panel(debugger_panel, window, cx);
 87            workspace.add_panel(terminal_panel, window, cx);
 88        })
 89        .unwrap();
 90    workspace_handle
 91}
 92
 93#[track_caller]
 94pub fn active_debug_session_panel(
 95    workspace: WindowHandle<Workspace>,
 96    cx: &mut TestAppContext,
 97) -> Entity<DebugSession> {
 98    workspace
 99        .update(cx, |workspace, _window, cx| {
100            let debug_panel = workspace.panel::<DebugPanel>(cx).unwrap();
101            debug_panel
102                .update(cx, |this, _| this.active_session())
103                .unwrap()
104        })
105        .unwrap()
106}
107
108pub fn start_debug_session_with<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
109    workspace: &WindowHandle<Workspace>,
110    cx: &mut gpui::TestAppContext,
111    config: DebugTaskDefinition,
112    configure: T,
113) -> Result<Entity<Session>> {
114    let _subscription = project::debugger::test::intercept_debug_sessions(cx, configure);
115    workspace.update(cx, |workspace, window, cx| {
116        workspace.start_debug_session(
117            config.to_scenario(),
118            TaskContext::default(),
119            None,
120            None,
121            window,
122            cx,
123        )
124    })?;
125    cx.run_until_parked();
126    let session = workspace.read_with(cx, |workspace, cx| {
127        workspace
128            .panel::<DebugPanel>(cx)
129            .and_then(|panel| panel.read(cx).active_session())
130            .map(|session| session.read(cx).running_state().read(cx).session())
131            .cloned()
132            .context("Failed to get active session")
133    })??;
134
135    Ok(session)
136}
137
138pub fn start_debug_session<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
139    workspace: &WindowHandle<Workspace>,
140    cx: &mut gpui::TestAppContext,
141    configure: T,
142) -> Result<Entity<Session>> {
143    use serde_json::json;
144
145    start_debug_session_with(
146        workspace,
147        cx,
148        DebugTaskDefinition {
149            adapter: "fake-adapter".into(),
150            label: "test".into(),
151            config: json!({
152                "request": "launch"
153            }),
154            tcp_connection: None,
155        },
156        configure,
157    )
158}