attach_modal.rs

  1use crate::{attach_modal::Candidate, *};
  2use attach_modal::AttachModal;
  3use dap::{FakeAdapter, client::SessionId};
  4use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
  5use menu::Confirm;
  6use project::{FakeFs, Project};
  7use serde_json::json;
  8use task::{AttachConfig, DebugTaskDefinition, TCPHost};
  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.fake_debug_session(
 31            dap::DebugRequestType::Attach(AttachConfig {
 32                process_id: Some(10),
 33            }),
 34            None,
 35            false,
 36            cx,
 37        )
 38    });
 39
 40    let session = task.await.unwrap();
 41
 42    cx.run_until_parked();
 43
 44    // assert we didn't show the attach modal
 45    workspace
 46        .update(cx, |workspace, _window, cx| {
 47            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
 48        })
 49        .unwrap();
 50
 51    let shutdown_session = project.update(cx, |project, cx| {
 52        project.dap_store().update(cx, |dap_store, cx| {
 53            dap_store.shutdown_session(session.read(cx).session_id(), cx)
 54        })
 55    });
 56
 57    shutdown_session.await.unwrap();
 58}
 59
 60#[gpui::test]
 61async fn test_show_attach_modal_and_select_process(
 62    executor: BackgroundExecutor,
 63    cx: &mut TestAppContext,
 64) {
 65    init_test(cx);
 66
 67    let fs = FakeFs::new(executor.clone());
 68
 69    fs.insert_tree(
 70        "/project",
 71        json!({
 72            "main.rs": "First line\nSecond line\nThird line\nFourth line",
 73        }),
 74    )
 75    .await;
 76
 77    let project = Project::test(fs, ["/project".as_ref()], cx).await;
 78    let workspace = init_test_workspace(&project, cx).await;
 79    let cx = &mut VisualTestContext::from_window(*workspace, cx);
 80
 81    let attach_modal = workspace
 82        .update(cx, |workspace, window, cx| {
 83            workspace.toggle_modal(window, cx, |window, cx| {
 84                AttachModal::with_processes(
 85                    project.clone(),
 86                    DebugTaskDefinition {
 87                        adapter: FakeAdapter::ADAPTER_NAME.into(),
 88                        request: dap::DebugRequestType::Attach(AttachConfig::default()),
 89                        label: "attach example".into(),
 90                        initialize_args: None,
 91                        tcp_connection: Some(TCPHost::default()),
 92                        locator: None,
 93                        args: Default::default(),
 94                    },
 95                    vec![
 96                        Candidate {
 97                            pid: 0,
 98                            name: "fake-binary-1".into(),
 99                            command: vec![],
100                        },
101                        Candidate {
102                            pid: 3,
103                            name: "non-fake-binary-1".into(),
104                            command: vec![],
105                        },
106                        Candidate {
107                            pid: 1,
108                            name: "fake-binary-2".into(),
109                            command: vec![],
110                        },
111                    ],
112                    window,
113                    cx,
114                )
115            });
116
117            workspace.active_modal::<AttachModal>(cx).unwrap()
118        })
119        .unwrap();
120
121    cx.run_until_parked();
122
123    // assert we got the expected processes
124    workspace
125        .update(cx, |_, _, cx| {
126            let names =
127                attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
128
129            // we filtered out all processes that are not starting with `fake-binary`
130            assert_eq!(2, names.len());
131        })
132        .unwrap();
133
134    // select the only existing process
135    cx.dispatch_action(Confirm);
136
137    cx.run_until_parked();
138
139    // assert attach modal was dismissed
140    workspace
141        .update(cx, |workspace, _window, cx| {
142            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
143        })
144        .unwrap();
145
146    let shutdown_session = project.update(cx, |project, cx| {
147        project.dap_store().update(cx, |dap_store, cx| {
148            let session = dap_store.session_by_id(SessionId(0)).unwrap();
149
150            dap_store.shutdown_session(session.read(cx).session_id(), cx)
151        })
152    });
153
154    shutdown_session.await.unwrap();
155}