Skip to content

Commit

Permalink
Set up module list test to see if Modules request is called
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Feb 6, 2025
1 parent 172c834 commit 5b3e696
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/debugger_ui/src/debugger_panel_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ pub enum DebugPanelItemEvent {
}

#[derive(Clone, PartialEq, Eq)]
#[cfg(any(test, feature = "test-support"))]
pub enum ThreadItem {
Console,
LoadedSource,
Modules,
Variables,
}

#[derive(Clone, PartialEq, Eq)]
#[cfg(not(any(test, feature = "test-support")))]
enum ThreadItem {
Console,
LoadedSource,
Expand Down Expand Up @@ -486,6 +496,11 @@ impl DebugPanelItem {
self.thread_id
}

#[cfg(any(test, feature = "test-support"))]
pub fn set_thread_item(&mut self, thread_item: ThreadItem) {
self.active_thread_item = thread_item;
}

#[cfg(any(test, feature = "test-support"))]
pub fn stack_frame_list(&self) -> &Entity<StackFrameList> {
&self.stack_frame_list
Expand Down
3 changes: 3 additions & 0 deletions crates/debugger_ui/src/module_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ impl Render for ModuleList {
}
}

#[cfg(any(test, feature = "test-support"))]
use dap::Module;

#[cfg(any(test, feature = "test-support"))]
impl ModuleList {
pub fn modules(&self, cx: &mut Context<Self>) -> Vec<Module> {
Expand Down
1 change: 1 addition & 0 deletions crates/debugger_ui/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{debugger_panel::DebugPanel, debugger_panel_item::DebugPanelItem};
mod attach_modal;
mod console;
mod debugger_panel;
mod module_list;
mod stack_frame_list;
mod variable_list;

Expand Down
150 changes: 150 additions & 0 deletions crates/debugger_ui/src/tests/module_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use crate::{
debugger_panel_item::ThreadItem,
tests::{active_debug_panel_item, init_test, init_test_workspace},
};
use dap::{
requests::{Disconnect, Initialize, Launch, Modules, StackTrace},
StoppedEvent,
};
use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
use project::{FakeFs, Project};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};

#[gpui::test]
async fn test_module_list(executor: BackgroundExecutor, cx: &mut TestAppContext) {
init_test(cx);

let fs = FakeFs::new(executor.clone());

let project = Project::test(fs, ["/project".as_ref()], cx).await;
let workspace = init_test_workspace(&project, cx).await;
let cx = &mut VisualTestContext::from_window(*workspace, cx);

let task = project.update(cx, |project, cx| {
project.start_debug_session(
task::DebugAdapterConfig {
label: "test config".into(),
kind: task::DebugAdapterKind::Fake,
request: task::DebugRequestType::Launch,
program: None,
cwd: None,
initialize_args: None,
},
cx,
)
});

let (session, client) = task.await.unwrap();

client
.on_request::<Initialize, _>(move |_, _| {
Ok(dap::Capabilities {
supports_modules_request: Some(true),
..Default::default()
})
})
.await;

client.on_request::<Launch, _>(move |_, _| Ok(())).await;

client
.on_request::<StackTrace, _>(move |_, _| {
Ok(dap::StackTraceResponse {
stack_frames: Vec::default(),
total_frames: None,
})
})
.await;

let called_modules = Arc::new(AtomicBool::new(false));
let modules = vec![
dap::Module {
id: dap::ModuleId::Number(1),
name: "First Module".into(),
address_range: None,
date_time_stamp: None,
path: None,
symbol_file_path: None,
symbol_status: None,
version: None,
is_optimized: None,
is_user_code: None,
},
dap::Module {
id: dap::ModuleId::Number(2),
name: "Second Module".into(),
address_range: None,
date_time_stamp: None,
path: None,
symbol_file_path: None,
symbol_status: None,
version: None,
is_optimized: None,
is_user_code: None,
},
];

client
.on_request::<Modules, _>({
let called_modules = called_modules.clone();
let modules = modules.clone();
move |_, _| unsafe {
static mut REQUEST_COUNT: i32 = 1;
assert_eq!(
1, REQUEST_COUNT,
"This request should only be called once from the host"
);
REQUEST_COUNT += 1;
called_modules.store(true, Ordering::SeqCst);

Ok(dap::ModulesResponse {
modules: modules.clone(),
total_modules: Some(2u64),
})
}
})
.await;

client
.fake_event(dap::messages::Events::Stopped(dap::StoppedEvent {
reason: dap::StoppedEventReason::Pause,
description: None,
thread_id: Some(1),
preserve_focus_hint: None,
text: None,
all_threads_stopped: None,
hit_breakpoint_ids: None,
}))
.await;

client.on_request::<Disconnect, _>(move |_, _| Ok(())).await;

cx.run_until_parked();

assert!(
!called_modules.load(std::sync::atomic::Ordering::SeqCst),
"Request Modules shouldn't be called before it's needed"
);

active_debug_panel_item(workspace, cx).update(cx, |item, _cx| {
item.set_thread_item(ThreadItem::Modules);
});

cx.run_until_parked();

assert!(
called_modules.load(std::sync::atomic::Ordering::SeqCst),
"Request Modules should be called because a user clicked on the module list"
);

let shutdown_session = project.update(cx, |project, cx| {
project.dap_store().update(cx, |dap_store, cx| {
dap_store.shutdown_session(&session.read(cx).id(), cx)
})
});

shutdown_session.await.unwrap();
}

0 comments on commit 5b3e696

Please sign in to comment.