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)]
 28#[cfg(not(windows))]
 29mod new_session_modal;
 30#[cfg(test)]
 31mod persistence;
 32#[cfg(test)]
 33mod stack_frame_list;
 34#[cfg(test)]
 35mod variable_list;
 36
 37pub fn init_test(cx: &mut gpui::TestAppContext) {
 38    if std::env::var("RUST_LOG").is_ok() {
 39        env_logger::try_init().ok();
 40    }
 41
 42    cx.update(|cx| {
 43        let settings = SettingsStore::test(cx);
 44        cx.set_global(settings);
 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            window,
121            cx,
122        )
123    })?;
124    cx.run_until_parked();
125    let session = workspace.read_with(cx, |workspace, cx| {
126        workspace
127            .panel::<DebugPanel>(cx)
128            .and_then(|panel| panel.read(cx).active_session())
129            .map(|session| session.read(cx).running_state().read(cx).session())
130            .cloned()
131            .context("Failed to get active session")
132    })??;
133
134    Ok(session)
135}
136
137pub fn start_debug_session<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
138    workspace: &WindowHandle<Workspace>,
139    cx: &mut gpui::TestAppContext,
140    configure: T,
141) -> Result<Entity<Session>> {
142    use serde_json::json;
143
144    start_debug_session_with(
145        workspace,
146        cx,
147        DebugTaskDefinition {
148            adapter: "fake-adapter".into(),
149            label: "test".into(),
150            config: json!({
151                "request": "launch"
152            }),
153            tcp_connection: None,
154        },
155        configure,
156    )
157}