tests.rs

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