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        terminal_view::init(cx);
 44        theme::init(theme::LoadThemes::JustBase, cx);
 45        command_palette_hooks::init(cx);
 46        editor::init(cx);
 47        crate::init(cx);
 48        dap_adapters::init(cx);
 49    });
 50}
 51
 52pub async fn init_test_workspace(
 53    project: &Entity<Project>,
 54    cx: &mut TestAppContext,
 55) -> WindowHandle<Workspace> {
 56    let workspace_handle =
 57        cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
 58
 59    let debugger_panel = workspace_handle
 60        .update(cx, |_, window, cx| {
 61            cx.spawn_in(window, async move |this, cx| {
 62                DebugPanel::load(this, cx).await
 63            })
 64        })
 65        .unwrap()
 66        .await
 67        .expect("Failed to load debug panel");
 68
 69    let terminal_panel = workspace_handle
 70        .update(cx, |_, window, cx| {
 71            cx.spawn_in(window, async |this, cx| {
 72                TerminalPanel::load(this, cx.clone()).await
 73            })
 74        })
 75        .unwrap()
 76        .await
 77        .expect("Failed to load terminal panel");
 78
 79    workspace_handle
 80        .update(cx, |workspace, window, cx| {
 81            workspace.add_panel(debugger_panel, window, cx);
 82            workspace.add_panel(terminal_panel, window, cx);
 83        })
 84        .unwrap();
 85    workspace_handle
 86}
 87
 88#[track_caller]
 89pub fn active_debug_session_panel(
 90    workspace: WindowHandle<Workspace>,
 91    cx: &mut TestAppContext,
 92) -> Entity<DebugSession> {
 93    workspace
 94        .update(cx, |workspace, _window, cx| {
 95            let debug_panel = workspace.panel::<DebugPanel>(cx).unwrap();
 96            debug_panel
 97                .update(cx, |this, _| this.active_session())
 98                .unwrap()
 99        })
100        .unwrap()
101}
102
103pub fn start_debug_session_with<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
104    workspace: &WindowHandle<Workspace>,
105    cx: &mut gpui::TestAppContext,
106    config: DebugTaskDefinition,
107    configure: T,
108) -> Result<Entity<Session>> {
109    let _subscription = project::debugger::test::intercept_debug_sessions(cx, configure);
110    workspace.update(cx, |workspace, window, cx| {
111        workspace.start_debug_session(
112            config.to_scenario(),
113            TaskContext::default(),
114            None,
115            None,
116            window,
117            cx,
118        )
119    })?;
120    cx.run_until_parked();
121    let session = workspace.read_with(cx, |workspace, cx| {
122        workspace
123            .panel::<DebugPanel>(cx)
124            .and_then(|panel| panel.read(cx).active_session())
125            .map(|session| session.read(cx).running_state().read(cx).session())
126            .cloned()
127            .context("Failed to get active session")
128    })??;
129
130    Ok(session)
131}
132
133pub fn start_debug_session<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
134    workspace: &WindowHandle<Workspace>,
135    cx: &mut gpui::TestAppContext,
136    configure: T,
137) -> Result<Entity<Session>> {
138    use serde_json::json;
139
140    start_debug_session_with(
141        workspace,
142        cx,
143        DebugTaskDefinition {
144            adapter: "fake-adapter".into(),
145            label: "test".into(),
146            config: json!({
147                "request": "launch"
148            }),
149            tcp_connection: None,
150        },
151        configure,
152    )
153}