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;
  9use tests::{init_test, init_test_workspace};
 10use util::path;
 11
 12#[gpui::test]
 13async fn test_direct_attach_to_process(executor: BackgroundExecutor, cx: &mut TestAppContext) {
 14    init_test(cx);
 15
 16    let fs = FakeFs::new(executor.clone());
 17
 18    fs.insert_tree(
 19        path!("/project"),
 20        json!({
 21            "main.rs": "First line\nSecond line\nThird line\nFourth line",
 22        }),
 23    )
 24    .await;
 25
 26    let project = Project::test(fs, [path!("/project").as_ref()], cx).await;
 27    let workspace = init_test_workspace(&project, cx).await;
 28    let cx = &mut VisualTestContext::from_window(*workspace, cx);
 29
 30    let session = start_debug_session_with(
 31        &workspace,
 32        cx,
 33        DebugTaskDefinition {
 34            adapter: "fake-adapter".into(),
 35            label: "label".into(),
 36            config: json!({
 37               "request": "attach",
 38              "process_id": 10,
 39            }),
 40            tcp_connection: 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        path!("/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, [path!("/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.weak_entity();
106            workspace.toggle_modal(window, cx, |window, cx| {
107                AttachModal::with_processes(
108                    workspace_handle,
109                    task::ZedDebugConfig {
110                        adapter: FakeAdapter::ADAPTER_NAME.into(),
111                        request: dap::DebugRequest::Attach(AttachRequest::default()),
112                        label: "attach example".into(),
113                        stop_on_entry: None,
114                    },
115                    vec![
116                        Candidate {
117                            pid: 0,
118                            name: "fake-binary-1".into(),
119                            command: vec![],
120                        },
121                        Candidate {
122                            pid: 3,
123                            name: "real-binary-1".into(),
124                            command: vec![],
125                        },
126                        Candidate {
127                            pid: 1,
128                            name: "fake-binary-2".into(),
129                            command: vec![],
130                        },
131                    ]
132                    .into_iter()
133                    .collect(),
134                    true,
135                    window,
136                    cx,
137                )
138            });
139
140            workspace.active_modal::<AttachModal>(cx).unwrap()
141        })
142        .unwrap();
143
144    cx.run_until_parked();
145
146    // assert we got the expected processes
147    workspace
148        .update(cx, |_, window, cx| {
149            let names =
150                attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
151            // Initially all processes are visible.
152            assert_eq!(3, names.len());
153            attach_modal.update(cx, |this, cx| {
154                this.picker.update(cx, |this, cx| {
155                    this.set_query("fakb", window, cx);
156                })
157            })
158        })
159        .unwrap();
160    cx.run_until_parked();
161    // assert we got the expected processes
162    workspace
163        .update(cx, |_, _, cx| {
164            let names =
165                attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
166            // Initially all processes are visible.
167            assert_eq!(2, names.len());
168        })
169        .unwrap();
170    // select the only existing process
171    cx.dispatch_action(Confirm);
172
173    cx.run_until_parked();
174
175    // assert attach modal was dismissed
176    workspace
177        .update(cx, |workspace, _window, cx| {
178            assert!(workspace.active_modal::<AttachModal>(cx).is_none());
179        })
180        .unwrap();
181}