attach_modal.rs

  1use crate::*;
  2use attach_modal::AttachModal;
  3use dap::client::SessionId;
  4use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
  5use menu::Confirm;
  6use project::{FakeFs, Project};
  7use serde_json::json;
  8use task::AttachConfig;
  9use tests::{init_test, init_test_workspace};
 10
 11#[gpui::test]
 12async fn test_direct_attach_to_process(executor: BackgroundExecutor, cx: &mut TestAppContext) {
 13    init_test(cx);
 14
 15    let fs = FakeFs::new(executor.clone());
 16
 17    fs.insert_tree(
 18        "/project",
 19        json!({
 20            "main.rs": "First line\nSecond line\nThird line\nFourth line",
 21        }),
 22    )
 23    .await;
 24
 25    let project = Project::test(fs, ["/project".as_ref()], cx).await;
 26    let workspace = init_test_workspace(&project, cx).await;
 27    let cx = &mut VisualTestContext::from_window(*workspace, cx);
 28
 29    let task = project.update(cx, |project, cx| {
 30        project.start_debug_session(
 31            dap::test_config(
 32                dap::DebugRequestType::Attach(AttachConfig {
 33                    process_id: Some(10),
 34                }),
 35                None,
 36                None,
 37            ),
 38            cx,
 39        )
 40    });
 41
 42    let session = task.await.unwrap();
 43
 44    cx.run_until_parked();
 45
 46    // assert we didn't show the attach modal
 47    workspace
 48        .update(cx, |workspace, _window, cx| {
 49            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
 50        })
 51        .unwrap();
 52
 53    let shutdown_session = project.update(cx, |project, cx| {
 54        project.dap_store().update(cx, |dap_store, cx| {
 55            dap_store.shutdown_session(session.read(cx).session_id(), cx)
 56        })
 57    });
 58
 59    shutdown_session.await.unwrap();
 60}
 61
 62#[gpui::test]
 63async fn test_show_attach_modal_and_select_process(
 64    executor: BackgroundExecutor,
 65    cx: &mut TestAppContext,
 66) {
 67    init_test(cx);
 68
 69    let fs = FakeFs::new(executor.clone());
 70
 71    fs.insert_tree(
 72        "/project",
 73        json!({
 74            "main.rs": "First line\nSecond line\nThird line\nFourth line",
 75        }),
 76    )
 77    .await;
 78
 79    let project = Project::test(fs, ["/project".as_ref()], cx).await;
 80    let workspace = init_test_workspace(&project, cx).await;
 81    let cx = &mut VisualTestContext::from_window(*workspace, cx);
 82
 83    let attach_modal = workspace
 84        .update(cx, |workspace, window, cx| {
 85            workspace.toggle_modal(window, cx, |window, cx| {
 86                AttachModal::new(
 87                    project.clone(),
 88                    dap::test_config(
 89                        dap::DebugRequestType::Attach(AttachConfig { process_id: None }),
 90                        None,
 91                        None,
 92                    ),
 93                    window,
 94                    cx,
 95                )
 96            });
 97
 98            workspace.active_modal::<AttachModal>(cx).unwrap()
 99        })
100        .unwrap();
101
102    cx.run_until_parked();
103
104    // assert we got the expected processes
105    workspace
106        .update(cx, |_, _, cx| {
107            let names =
108                attach_modal.update(cx, |modal, cx| attach_modal::process_names(&modal, cx));
109
110            // we filtered out all processes that are not the current process(zed itself)
111            assert_eq!(1, names.len());
112        })
113        .unwrap();
114
115    // select the only existing process
116    cx.dispatch_action(Confirm);
117
118    cx.run_until_parked();
119
120    // assert attach modal was dismissed
121    workspace
122        .update(cx, |workspace, _window, cx| {
123            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
124        })
125        .unwrap();
126
127    let shutdown_session = project.update(cx, |project, cx| {
128        project.dap_store().update(cx, |dap_store, cx| {
129            let session = dap_store.session_by_id(SessionId(0)).unwrap();
130
131            dap_store.shutdown_session(session.read(cx).session_id(), cx)
132        })
133    });
134
135    shutdown_session.await.unwrap();
136}