tests.rs

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