attach_modal.rs

  1use crate::{attach_modal::Candidate, tests::start_debug_session_with, *};
  2use attach_modal::AttachModal;
  3use dap::{FakeAdapter, adapters::DebugTaskDefinition};
  4use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
  5use menu::Confirm;
  6use project::{FakeFs, Project};
  7use serde_json::json;
  8use task::{AttachRequest, TcpArgumentsTemplate};
  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 session = start_debug_session_with(
 30        &workspace,
 31        cx,
 32        DebugTaskDefinition {
 33            adapter: "fake-adapter".into(),
 34            request: dap::DebugRequest::Attach(AttachRequest {
 35                process_id: Some(10),
 36            }),
 37            label: "label".into(),
 38            initialize_args: None,
 39            tcp_connection: None,
 40            stop_on_entry: None,
 41        },
 42        |client| {
 43            client.on_request::<dap::requests::Attach, _>(move |_, args| {
 44                let raw = &args.raw;
 45                assert_eq!(raw["request"], "attach");
 46                assert_eq!(raw["process_id"], 10);
 47
 48                Ok(())
 49            });
 50        },
 51    )
 52    .unwrap();
 53
 54    cx.run_until_parked();
 55
 56    // assert we didn't show the attach modal
 57    workspace
 58        .update(cx, |workspace, _window, cx| {
 59            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
 60        })
 61        .unwrap();
 62
 63    let shutdown_session = project.update(cx, |project, cx| {
 64        project.dap_store().update(cx, |dap_store, cx| {
 65            dap_store.shutdown_session(session.read(cx).session_id(), cx)
 66        })
 67    });
 68
 69    shutdown_session.await.unwrap();
 70}
 71
 72#[gpui::test]
 73async fn test_show_attach_modal_and_select_process(
 74    executor: BackgroundExecutor,
 75    cx: &mut TestAppContext,
 76) {
 77    init_test(cx);
 78
 79    let fs = FakeFs::new(executor.clone());
 80
 81    fs.insert_tree(
 82        "/project",
 83        json!({
 84            "main.rs": "First line\nSecond line\nThird line\nFourth line",
 85        }),
 86    )
 87    .await;
 88
 89    let project = Project::test(fs, ["/project".as_ref()], cx).await;
 90    let workspace = init_test_workspace(&project, cx).await;
 91    let cx = &mut VisualTestContext::from_window(*workspace, cx);
 92    // Set up handlers for sessions spawned via modal.
 93    let _initialize_subscription =
 94        project::debugger::test::intercept_debug_sessions(cx, |client| {
 95            client.on_request::<dap::requests::Attach, _>(move |_, args| {
 96                let raw = &args.raw;
 97                assert_eq!(raw["request"], "attach");
 98                assert_eq!(raw["process_id"], 1);
 99
100                Ok(())
101            });
102        });
103    let attach_modal = workspace
104        .update(cx, |workspace, window, cx| {
105            let workspace_handle = cx.entity();
106            workspace.toggle_modal(window, cx, |window, cx| {
107                AttachModal::with_processes(
108                    workspace_handle,
109                    DebugTaskDefinition {
110                        adapter: FakeAdapter::ADAPTER_NAME.into(),
111
112                        request: dap::DebugRequest::Attach(AttachRequest::default()),
113                        label: "attach example".into(),
114                        initialize_args: None,
115                        tcp_connection: Some(TcpArgumentsTemplate::default()),
116                        stop_on_entry: None,
117                    },
118                    vec![
119                        Candidate {
120                            pid: 0,
121                            name: "fake-binary-1".into(),
122                            command: vec![],
123                        },
124                        Candidate {
125                            pid: 3,
126                            name: "real-binary-1".into(),
127                            command: vec![],
128                        },
129                        Candidate {
130                            pid: 1,
131                            name: "fake-binary-2".into(),
132                            command: vec![],
133                        },
134                    ]
135                    .into_iter()
136                    .collect(),
137                    true,
138                    window,
139                    cx,
140                )
141            });
142
143            workspace.active_modal::<AttachModal>(cx).unwrap()
144        })
145        .unwrap();
146
147    cx.run_until_parked();
148
149    // assert we got the expected processes
150    workspace
151        .update(cx, |_, window, cx| {
152            let names =
153                attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
154            // Initially all processes are visible.
155            assert_eq!(3, names.len());
156            attach_modal.update(cx, |this, cx| {
157                this.picker.update(cx, |this, cx| {
158                    this.set_query("fakb", window, cx);
159                })
160            })
161        })
162        .unwrap();
163    cx.run_until_parked();
164    // assert we got the expected processes
165    workspace
166        .update(cx, |_, _, cx| {
167            let names =
168                attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
169            // Initially all processes are visible.
170            assert_eq!(2, names.len());
171        })
172        .unwrap();
173    // select the only existing process
174    cx.dispatch_action(Confirm);
175
176    cx.run_until_parked();
177
178    // assert attach modal was dismissed
179    workspace
180        .update(cx, |workspace, _window, cx| {
181            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
182        })
183        .unwrap();
184}