use super::*;
#[test]
fn cmd_k_shift_enter_debugs_instead_of_running_the_test_at_cursor() {
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("t.rs");
std::fs::write(&f, "#[test]\nfn my_case() { assert!(true); }\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_pinned(&f).unwrap();
app.editor.cursor_row = 1;
assert!(app.handle_cmd_k_chord(key(KeyCode::Enter, KeyModifiers::SHIFT)));
assert_eq!(app.status, "No test runner detected in this workspace");
assert!(
!app.testing.is_busy(),
"the debug chord must not start a run"
);
}
#[test]
fn debug_stop_immediately_deactivates_the_run_debug_panel() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.run_debug.debug_active = true;
app.run_debug.feedback = Some("Paused (breakpoint)".into());
app.run_debug.debug_rows = vec![crate::widgets::run_debug::DebugRow {
indent: 0,
kind: crate::widgets::run_debug::DebugRowKind::Header {
title: "CALL STACK".into(),
},
}];
app.debug_stop();
assert!(
!app.run_debug.debug_active,
"panel must deactivate the instant the session stops"
);
assert!(
app.run_debug.debug_rows.is_empty(),
"stale call-stack/variables rows must be cleared on stop"
);
assert!(
app.run_debug.feedback.is_none(),
"stale 'Paused' feedback must be cleared so the empty state is clean"
);
}
#[test]
fn lldb_dap_rank_prefers_unversioned_then_highest_version() {
assert_eq!(lldb_dap_rank("lldb"), None);
assert_eq!(lldb_dap_rank("lldb-dap-"), None);
assert_eq!(lldb_dap_rank("lldb-dapper"), None);
assert_eq!(lldb_dap_rank("clangd"), None);
let unversioned = lldb_dap_rank("lldb-dap").expect("bare name must rank");
let v18 = lldb_dap_rank("lldb-dap-18").expect("versioned name must rank");
let v17 = lldb_dap_rank("lldb-dap-17").expect("versioned name must rank");
let legacy_v18 = lldb_dap_rank("lldb-vscode-18").expect("legacy name must rank");
assert!(
unversioned > v18,
"unversioned lldb-dap outranks any versioned"
);
assert!(v18 > v17, "lldb-dap-18 outranks lldb-dap-17");
assert!(
v18 > legacy_v18,
"lldb-dap-18 is preferred over the legacy lldb-vscode-18 name"
);
}
#[test]
fn lldb_dap_missing_message_is_platform_appropriate() {
let msg = lldb_dap_missing_message();
assert!(
msg.contains("lldb-dap not found"),
"message must name the missing adapter: {msg}"
);
if cfg!(target_os = "macos") {
assert!(
msg.contains("Xcode"),
"macOS hint should mention Xcode: {msg}"
);
} else {
assert!(
!msg.contains("Xcode"),
"non-macOS hint must not mention Xcode: {msg}"
);
}
}
#[test]
fn f5_failure_reaches_the_status_bar_when_run_debug_panel_is_hidden() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let file = tmp.path().join("notes.txt");
std::fs::write(&file, b"hi\n").unwrap();
app.editor.path = Some(file);
app.set_sidebar_view(SidebarView::Explorer);
app.debug_start_or_continue();
assert!(
app.status.contains("No debugger"),
"F5 failure must explain itself in the status bar, not just the hidden panel: {}",
app.status
);
}
#[test]
fn run_debug_icon_clears_when_a_paused_session_takes_over_the_panel() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.show_tree = true;
app.set_sidebar_view(SidebarView::RunDebug);
app.overlays
.run_debug
.set_image("\x1b]1337;File=inline=1:AAAA\x07".to_string());
app.overlays.run_debug.mark_emitted_at((5, 5));
app.run_debug.last_icon_cell = None;
app.flush_run_debug_icon_overlay(&ratatui::buffer::Buffer::empty(app.last_frame_area));
assert!(
app.consume_run_debug_image_clear(),
"a one-shot terminal clear must be armed to evict the ghosted debug icon"
);
assert!(
!app.overlays.run_debug.was_emitted(),
"emitted-position tracking must reset so the clear fires exactly once"
);
}
#[test]
fn a_left_click_in_the_breakpoint_lane_toggles_a_breakpoint_on_that_line() {
let (mut app, _tmp, f) = app_with_painted_file("l1\nl2\nl3\nl4\nl5\n");
let lane = app.editor.last_inner.x;
let row = app.editor.last_inner.y + 2;
click_and_release(&mut app, lane, row);
assert_eq!(app.editor.breakpoint_lines(&f), vec![3u32]);
assert_eq!(app.status, "Breakpoint set at line 3");
assert_eq!(app.editor.cursor_row, 0, "the caret stays on line 1");
app.editor_click.clear();
click_and_release(&mut app, lane, row);
assert!(app.editor.breakpoint_lines(&f).is_empty());
assert_eq!(app.status, "Breakpoint removed from line 3");
assert_eq!(app.editor.cursor_row, 0);
}
#[test]
fn drags_after_a_breakpoint_lane_press_neither_move_the_caret_nor_select() {
use crossterm::event::{MouseButton, MouseEventKind};
let (mut app, _tmp, f) = app_with_painted_file("l1\nl2\nl3\nl4\nl5\n");
let lane = app.editor.last_inner.x;
let y = app.editor.last_inner.y;
let text_x = lane + app.editor.last_gutter_width + 1;
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), lane, y + 2));
app.handle_mouse(mouse(MouseEventKind::Drag(MouseButton::Left), lane, y + 2));
app.handle_mouse(mouse(
MouseEventKind::Drag(MouseButton::Left),
text_x,
y + 3,
));
app.handle_mouse(mouse(MouseEventKind::Up(MouseButton::Left), text_x, y + 3));
assert_eq!(app.editor.breakpoint_lines(&f), vec![3u32]);
assert_eq!(app.editor.cursor_row, 0, "the caret stays on line 1");
assert!(
app.editor.selection.is_none_or(|s| !s.has_area()),
"no selection from the caret to the clicked line"
);
app.editor_click.clear();
app.handle_mouse(mouse(
MouseEventKind::Down(MouseButton::Left),
text_x,
y + 1,
));
app.handle_mouse(mouse(
MouseEventKind::Drag(MouseButton::Left),
text_x + 1,
y + 1,
));
app.handle_mouse(mouse(
MouseEventKind::Up(MouseButton::Left),
text_x + 1,
y + 1,
));
assert!(
app.editor.selection.is_some_and(|s| s.has_area()),
"a text drag after the lane gesture still selects"
);
}
#[test]
fn add_conditional_breakpoint_opens_a_popup_not_the_status_line() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("prog.py");
std::fs::write(&f, "l1\nl2\nl3\nl4\nl5\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_pinned(&f).unwrap();
let backend = ratatui::backend::TestBackend::new(100, 30);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let gutter_col = app.editor.last_inner.x;
let row = app.editor.last_inner.y + 2;
app.handle_mouse(mouse(
MouseEventKind::Down(MouseButton::Right),
gutter_col,
row,
));
let idx = app
.context_menu
.as_ref()
.unwrap()
.items
.iter()
.position(|e| menu_label(e) == "Add Conditional Breakpoint\u{2026}")
.expect("menu must offer Add Conditional Breakpoint");
app.context_menu.as_mut().unwrap().selected = idx;
app.handle_menu_key(key(KeyCode::Enter, KeyModifiers::NONE));
let prompt = app
.prompt
.as_ref()
.expect("conditional breakpoint must open a popup prompt, not a status-line input");
assert!(
matches!(prompt.kind, PromptKind::BreakpointCondition { line: 3, .. }),
"popup must target the clicked line (3)"
);
for c in "x > 1".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(app.prompt.is_none(), "committing must close the popup");
assert_eq!(
app.editor.breakpoint_lines(&f),
vec![3u32],
"committing a condition must create the breakpoint on line 3"
);
let lines = app.editor.breakpoints.get(&f).unwrap();
let specs = app.editor.source_breakpoints(&f, lines);
assert_eq!(
specs[0].condition.as_deref(),
Some("x > 1"),
"the typed expression must be attached as the breakpoint condition"
);
}
#[test]
fn add_logpoint_attaches_a_log_message_to_the_line() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("prog.py");
std::fs::write(&f, "l1\nl2\nl3\nl4\nl5\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_pinned(&f).unwrap();
let backend = ratatui::backend::TestBackend::new(100, 30);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let gutter_col = app.editor.last_inner.x;
let row = app.editor.last_inner.y + 2; app.handle_mouse(mouse(
MouseEventKind::Down(MouseButton::Right),
gutter_col,
row,
));
let idx = app
.context_menu
.as_ref()
.unwrap()
.items
.iter()
.position(|e| menu_label(e) == "Add Logpoint")
.expect("menu must offer Add Logpoint");
app.context_menu.as_mut().unwrap().selected = idx;
app.handle_menu_key(key(KeyCode::Enter, KeyModifiers::NONE));
let prompt = app.prompt.as_ref().expect("logpoint must open a popup");
assert!(
matches!(prompt.kind, PromptKind::Logpoint { line: 3, .. }),
"popup must target the clicked line (3)"
);
for c in "x is {x}".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(app.prompt.is_none(), "committing must close the popup");
assert_eq!(
app.editor.breakpoint_lines(&f),
vec![3u32],
"committing a logpoint must create the breakpoint on line 3"
);
let lines = app.editor.breakpoints.get(&f).unwrap();
let specs = app.editor.source_breakpoints(&f, lines);
assert_eq!(
specs[0].log_message.as_deref(),
Some("x is {x}"),
"the typed message must ride the breakpoint as logMessage"
);
assert_eq!(
specs[0].condition, None,
"a logpoint sets no condition of its own"
);
}
#[test]
fn leaving_run_debug_after_emitting_the_icon_arms_a_one_shot_terminal_clear() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.overlays
.run_debug
.set_image("\x1b]1337;File=...\x07".to_string());
app.set_sidebar_view(SidebarView::RunDebug);
app.run_debug.last_icon_cell = Some((10, 5));
app.flush_run_debug_icon_overlay(&ratatui::buffer::Buffer::empty(app.last_frame_area));
assert!(
app.overlays.run_debug.was_emitted(),
"test setup: the flush must record the emit position"
);
app.set_sidebar_view(SidebarView::Explorer);
assert!(
app.consume_run_debug_image_clear(),
"leaving Run-Debug after emitting the icon must arm exactly one terminal.clear() so iTerm2's image cells are evicted",
);
assert!(
!app.consume_run_debug_image_clear(),
"the clear request must be one-shot — consuming twice in a row returns false",
);
}
#[test]
fn flush_run_debug_icon_overlay_is_silent_when_panel_is_not_visible() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.overlays
.run_debug
.set_image("\x1b]1337;File=...\x07".to_string());
app.set_sidebar_view(SidebarView::RunDebug);
app.run_debug.last_icon_cell = Some((10, 5));
app.flush_run_debug_icon_overlay(&ratatui::buffer::Buffer::empty(app.last_frame_area));
assert_eq!(app.overlays.run_debug.last_emitted(), Some((10, 5)));
app.set_sidebar_view(SidebarView::Explorer);
app.flush_run_debug_icon_overlay(&ratatui::buffer::Buffer::empty(app.last_frame_area));
assert_eq!(
app.overlays.run_debug.last_emitted(),
None,
"flush on a hidden panel must reset last_emitted so a re-entry primes a fresh emit",
);
}
#[test]
fn run_debug_icon_moving_within_a_visible_panel_arms_a_one_shot_terminal_clear() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.overlays
.run_debug
.set_image("\x1b]1337;File=...\x07".to_string());
app.set_sidebar_view(SidebarView::RunDebug);
app.run_debug.last_icon_cell = Some((10, 5));
app.flush_run_debug_icon_overlay(&ratatui::buffer::Buffer::empty(app.last_frame_area));
assert_eq!(app.overlays.run_debug.last_emitted(), Some((10, 5)));
app.run_debug.last_icon_cell = Some((3, 2));
app.flush_run_debug_icon_overlay(&ratatui::buffer::Buffer::empty(app.last_frame_area));
assert!(
app.consume_run_debug_image_clear(),
"an icon that moved within the visible panel must arm one terminal.clear() to evict the stale image cells",
);
assert!(
!app.consume_run_debug_image_clear(),
"the clear request must be one-shot",
);
assert_eq!(
app.overlays.run_debug.last_emitted(),
None,
"the move branch must reset last_emitted so the post-clear frame re-emits a single clean icon",
);
}
#[test]
fn toggle_inline_values_clears_trailers_on_disable() {
let mut app = editor_app_with_lines(&["def f():", " x = 1"]);
assert!(
app.inline_values_enabled,
"inline values are on by default (VS Code debug.inlineValues: auto)"
);
app.editor.inline_values.insert(1, String::from("x = 1"));
app.run_command(crate::widgets::command_palette::Command::ToggleInlineValues);
assert!(!app.inline_values_enabled);
assert!(
app.editor
.editors
.iter()
.all(|e| e.inline_values.is_empty()),
"disabling clears the trailers in every editor"
);
app.run_command(crate::widgets::command_palette::Command::ToggleInlineValues);
assert!(app.inline_values_enabled, "round trip");
}
#[test]
fn cmd_shift_d_jumps_to_run_and_debug_from_any_pane() {
let mut app = editor_app_with_lines(&["x"]);
app.focus_pane(Pane::Editor);
app.handle_key(key(
KeyCode::Char('d'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
))
.unwrap();
assert_eq!(app.sidebar_view, SidebarView::RunDebug);
}
#[test]
fn click_on_run_debug_icon_switches_sidebar_to_run_debug() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let backend = ratatui::backend::TestBackend::new(80, 30);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
let run = app.sidebar_areas.activity_icons[ActivityIcon::RunDebug];
assert!(run.height > 0, "run-debug icon must be laid out");
app.handle_mouse(crossterm::event::MouseEvent {
kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
column: run.x,
row: run.y,
modifiers: KeyModifiers::NONE,
});
assert_eq!(app.sidebar_view, SidebarView::RunDebug);
}
fn recording_member(log: &std::path::Path) -> crate::dap::session::DapSession {
crate::dap::session::DapSession::launch_with(
"sh",
&[String::from("-c"), format!("cat > '{}'", log.display())],
std::path::Path::new("."),
serde_json::json!({"seq": 2, "type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("sh spawns")
}
fn debug_toolbar_app(
paused: bool,
) -> (
tempfile::TempDir,
App,
ratatui::Terminal<ratatui::backend::TestBackend>,
std::path::PathBuf,
) {
use crate::dap::session::SessionPhase;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let log = tmp.path().join("requests.log");
let mut session = recording_member(&log);
session.stopped_thread = Some(1);
session.phase = if paused {
SessionPhase::Stopped
} else {
SessionPhase::Running
};
app.debug_sessions.push("app.py", session);
app.set_sidebar_view(SidebarView::RunDebug);
app.refresh_debug_panel();
let term = draw_terminal(&mut app, 120, 32);
(tmp, app, term, log)
}
fn debug_control_cell(
app: &App,
control: crate::widgets::run_debug::DebugControl,
) -> Option<(u16, u16)> {
let area = app.run_debug.last_area;
(area.y..area.y + area.height)
.flat_map(|y| (area.x..area.x + area.width).map(move |x| (x, y)))
.find(|&(x, y)| app.run_debug.debug_control_painted_at(x, y) == Some(control))
}
fn adapter_got(log: &std::path::Path, command: &str) -> bool {
let needle = format!("\"command\":\"{command}\"");
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while std::time::Instant::now() < deadline {
if std::fs::read_to_string(log).is_ok_and(|s| s.contains(&needle)) {
return true;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
false
}
#[test]
fn the_debug_toolbar_steps_and_continues_a_paused_session() {
use crate::widgets::run_debug::DebugControl;
use crossterm::event::{MouseButton, MouseEventKind};
for (control, command) in [
(DebugControl::StepOver, "next"),
(DebugControl::StepInto, "stepIn"),
(DebugControl::StepOut, "stepOut"),
(DebugControl::Continue, "continue"),
] {
let (_tmp, mut app, _term, log) = debug_toolbar_app(true);
let (x, y) = debug_control_cell(&app, control)
.unwrap_or_else(|| panic!("{control:?} is on the toolbar"));
assert!(
debug_control_cell(&app, DebugControl::Pause).is_none(),
"a paused session offers Continue, not Pause"
);
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), x, y));
assert!(adapter_got(&log, command), "{control:?} sends {command}");
}
}
#[test]
fn a_running_sessions_toolbar_pauses_and_its_steps_do_nothing() {
use crate::widgets::run_debug::DebugControl;
use crossterm::event::{MouseButton, MouseEventKind};
let (_tmp, mut app, _term, log) = debug_toolbar_app(false);
assert!(debug_control_cell(&app, DebugControl::Continue).is_none());
let (sx, sy) = debug_control_cell(&app, DebugControl::StepOver).expect("Step Over is shown");
assert_eq!(app.run_debug.debug_control_at(sx, sy), None, "greyed out");
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), sx, sy));
let (x, y) = debug_control_cell(&app, DebugControl::Pause).expect("Pause leads while running");
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), x, y));
assert!(adapter_got(&log, "pause"));
let sent = std::fs::read_to_string(&log).unwrap_or_default();
assert!(!sent.contains("\"command\":\"next\""), "{sent}");
}
#[test]
fn the_debug_toolbar_stops_and_names_its_keys() {
use crate::widgets::run_debug::DebugControl;
use crossterm::event::{MouseButton, MouseEventKind};
let (_tmp, mut app, _term, log) = debug_toolbar_app(true);
for (control, tip) in [
(DebugControl::Continue, "Continue (F5)"),
(DebugControl::StepOver, "Step Over (F10)"),
(DebugControl::StepInto, "Step Into (F11)"),
(DebugControl::StepOut, "Step Out (Shift+F11)"),
(DebugControl::Restart, "Restart (Ctrl+Shift+F5)"),
(DebugControl::Stop, "Stop (Shift+F5)"),
] {
let (x, y) = debug_control_cell(&app, control).unwrap();
assert_eq!(app.ui_tooltip_at(x, y), Some(tip));
}
let (x, y) = debug_control_cell(&app, DebugControl::Stop).unwrap();
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), x, y));
assert!(adapter_got(&log, "disconnect"));
}
#[test]
fn the_debug_toolbar_wraps_at_the_minimum_sidebar_width() {
use crate::widgets::run_debug::DebugControl;
use crossterm::event::{MouseButton, MouseEventKind};
let (_tmp, mut app, mut term, log) = debug_toolbar_app(true);
app.sidebar_width = SIDEBAR_WIDTH_MIN;
term.draw(|f| app.render(f)).unwrap();
let cells: Vec<_> = DebugControl::toolbar(true)
.into_iter()
.map(|control| {
debug_control_cell(&app, control)
.unwrap_or_else(|| panic!("{control:?} is on the narrow toolbar"))
})
.collect();
assert!(
cells.iter().any(|&(_, y)| y != cells[0].1),
"the buttons wrap onto a second row: {cells:?}"
);
let (x, y) = debug_control_cell(&app, DebugControl::Stop).unwrap();
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), x, y));
assert!(adapter_got(&log, "disconnect"));
}
#[test]
fn no_session_no_debug_toolbar() {
use crate::widgets::run_debug::DebugControl;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.set_sidebar_view(SidebarView::RunDebug);
app.refresh_debug_panel();
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 32)).unwrap();
term.draw(|f| app.render(f)).unwrap();
for control in DebugControl::toolbar(true)
.into_iter()
.chain([DebugControl::Pause])
{
assert_eq!(debug_control_cell(&app, control), None, "{control:?}");
}
}
#[test]
fn a_debugged_script_runs_under_the_interpreter_run_uses() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let pkg = root.join("src").join("pkg");
std::fs::create_dir_all(&pkg).unwrap();
let file = pkg.join("main.py");
std::fs::write(&file, b"import requests\n").unwrap();
let bare = super::project_python_for(&pkg, root);
assert_eq!(
bare.file_name().and_then(|n| n.to_str()),
Some("python3"),
"no venv: python3, got {}",
bare.display()
);
assert!(!bare.to_string_lossy().contains("debug-venv"));
let venv = make_venv(root, ".venv");
assert_eq!(super::project_python_for(&pkg, root), venv);
assert_eq!(
super::debuggee_python(&pkg, root, std::env::var_os("PATH").as_deref()),
Some(venv.clone()),
"F5's debuggee is Run's interpreter"
);
let req =
crate::dap::session::launch_request(&file, &super::project_python_for(&pkg, root), false);
assert_eq!(
req["arguments"]["python"][0],
&*venv.to_string_lossy(),
"the launch names the project's venv"
);
let spec = App::run_spec_for(&file, root).unwrap();
assert_eq!(spec.program, venv.to_string_lossy());
}
#[test]
fn a_launch_json_python_config_runs_under_the_project_venv_unless_it_names_one() {
use crate::dap::configs::{SubstCtx, debugpy_request, parse_launch_json, resolve};
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let ctx = SubstCtx {
workspace_folder: root.to_path_buf(),
file: None,
inputs: Default::default(),
folders: Vec::new(),
};
let python_of = |json: &str| {
let cfg = parse_launch_json(json, ".vscode/launch.json").remove(0);
let rc = resolve(&cfg, &ctx).unwrap();
let cwd = rc.cwd.clone().unwrap_or_else(|| root.to_path_buf());
let adapter = Path::new("/home/u/.croft/debug-venv/bin/python");
let req = debugpy_request(&rc, &super::config_project_python(&rc, &cwd, root, adapter));
req["arguments"]["python"].clone()
};
let program = r#"[{"name":"P","type":"python","program":"${workspaceFolder}/app/main.py"}]"#;
let fallback = python_of(program);
let fallback = fallback[0].as_str().unwrap();
assert!(
fallback.ends_with("python3") && !fallback.contains("debug-venv"),
"no venv: python3, got {fallback}"
);
let venv = make_venv(root, ".venv");
assert_eq!(python_of(program)[0], &*venv.to_string_lossy());
let svc = root.join("svc");
std::fs::create_dir_all(&svc).unwrap();
let svc_venv = make_venv(&svc, "venv");
assert_eq!(
python_of(r#"[{"name":"M","type":"python","module":"svc.main","cwd":"svc"}]"#)[0],
&*svc_venv.to_string_lossy()
);
assert_eq!(
python_of(
r#"[{"name":"E","type":"python","program":"app/main.py","python":"/opt/py/bin/python3.9"}]"#
),
"/opt/py/bin/python3.9",
"an explicit python wins"
);
}
#[test]
fn a_module_launch_that_also_names_a_program_runs_under_its_cwds_venv() {
use crate::dap::configs::{SubstCtx, debugpy_request, parse_launch_json, resolve};
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let ctx = SubstCtx {
workspace_folder: root.to_path_buf(),
file: None,
inputs: Default::default(),
folders: Vec::new(),
};
let request_of = |json: &str| {
let cfg = parse_launch_json(json, ".vscode/launch.json").remove(0);
let rc = resolve(&cfg, &ctx).unwrap();
let cwd = rc.cwd.clone().unwrap_or_else(|| root.to_path_buf());
let adapter = Path::new("/home/u/.croft/debug-venv/bin/python");
debugpy_request(&rc, &super::config_project_python(&rc, &cwd, root, adapter))
};
let root_venv = make_venv(root, ".venv");
let tools = root.join("tools");
std::fs::create_dir_all(&tools).unwrap();
let tools_venv = make_venv(&tools, ".venv");
let both = request_of(
r#"[{"name":"T","type":"python","module":"pytest","program":"tools/helper.py"}]"#,
);
assert_eq!(
both["arguments"]["module"], "pytest",
"the module is launched"
);
assert!(
both["arguments"].get("program").is_none(),
"the program is dropped"
);
assert_eq!(
both["arguments"]["python"][0],
&*root_venv.to_string_lossy(),
"the module runs under its cwd's venv, not the dropped program's"
);
let program = request_of(r#"[{"name":"P","type":"python","program":"tools/helper.py"}]"#);
assert_eq!(
program["arguments"]["python"][0],
&*tools_venv.to_string_lossy(),
"a program launch keeps the venv beside its program"
);
}
#[test]
fn a_venv_above_the_workspace_is_not_the_debuggee_interpreter() {
let tmp = tempfile::tempdir().unwrap();
let outside = make_venv(tmp.path(), ".venv");
let root = tmp.path().join("proj");
let pkg = root.join("pkg");
std::fs::create_dir_all(&pkg).unwrap();
assert_eq!(
super::debuggee_python(&pkg, &root, None),
None,
"a venv above the workspace is not the project's"
);
let py = super::project_python_for(&pkg, &root);
assert_ne!(py, outside, "nor Run's");
assert!(!py.starts_with(tmp.path()), "got {}", py.display());
}
#[test]
fn a_nearer_venv_beats_the_workspace_roots_for_the_debuggee() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let root_venv = make_venv(root, ".venv");
let svc = root.join("svc");
std::fs::create_dir_all(&svc).unwrap();
let svc_venv = make_venv(&svc, "venv");
assert_eq!(
super::debuggee_python(&svc, root, None),
Some(svc_venv.clone())
);
assert_eq!(super::project_python_for(&svc, root), svc_venv);
assert_eq!(
super::debuggee_python(root, root, None),
Some(root_venv.clone())
);
assert_eq!(super::project_python_for(root, root), root_venv);
}
fn make_venv_of(dir: &Path, venv_name: &str, version: &str) -> PathBuf {
let py = make_venv(dir, venv_name);
std::fs::write(
dir.join(venv_name).join("pyvenv.cfg"),
format!("home = /usr/bin\ninclude-system-site-packages = false\nversion = {version}\n"),
)
.unwrap();
py
}
#[test]
fn a_debuggee_older_than_3_14_is_named_with_the_attach_floor() {
let tmp = tempfile::tempdir().unwrap();
let py = make_venv_of(tmp.path(), ".venv", "3.12.4");
assert_eq!(
super::debuggee_python_note(&py),
Ok(Some(String::from(
"under Python 3.12 (.venv); attach needs 3.14+"
)))
);
}
#[test]
fn a_3_14_debuggee_or_an_unreadable_one_gets_no_note() {
let tmp = tempfile::tempdir().unwrap();
let py314 = make_venv_of(tmp.path(), "py314", "3.14.0");
assert_eq!(super::debuggee_python_note(&py314), Ok(None));
let py315 = make_venv_of(tmp.path(), "py315", "3.15.1");
assert_eq!(super::debuggee_python_note(&py315), Ok(None));
let unknown = make_venv(tmp.path(), "unknown");
assert_eq!(super::debuggee_python_note(&unknown), Ok(None));
}
#[test]
fn a_debuggee_debugpy_cannot_run_under_is_refused() {
let tmp = tempfile::tempdir().unwrap();
let old = make_venv_of(tmp.path(), ".venv", "3.9.18");
assert_eq!(
super::debuggee_python_note(&old),
Err(String::from(
"Python 3.9 (.venv) is too old to debug: debugpy needs 3.10+"
))
);
let floor = make_venv_of(tmp.path(), "venv", "3.10.0");
assert_eq!(
super::debuggee_python_note(&floor),
Ok(Some(String::from(
"under Python 3.10 (venv); attach needs 3.14+"
)))
);
}
#[test]
fn a_debug_test_configs_interpreter_is_the_one_checked() {
use crate::dap::configs::{SubstCtx, parse_launch_json, resolve, with_debug_test_config};
let root = Path::new("/work/proj");
let ctx = SubstCtx {
workspace_folder: root.to_path_buf(),
file: None,
inputs: Default::default(),
folders: Vec::new(),
};
let base = |py: &str| {
crate::dap::session::pytest_debug_launch_request(Path::new(py), root, "test_mode")
};
let cfgs = parse_launch_json(
r#"[{ "name": "T", "type": "debugpy", "purpose": ["debug-test"],
"python": "/opt/py314/bin/python" }]"#,
".vscode/launch.json",
);
let rc = resolve(&cfgs[0], &ctx).unwrap();
let adapter = Path::new("/debug-venv/bin/python");
let req = with_debug_test_config(base("/work/proj/.venv/bin/python"), &rc);
assert_eq!(
super::debug_test_python(&req, root, adapter, true),
Some(PathBuf::from("/opt/py314/bin/python"))
);
let req = with_debug_test_config(base("/debug-venv/bin/python"), &rc);
assert_eq!(
super::debug_test_python(&req, root, adapter, false),
Some(PathBuf::from("/opt/py314/bin/python")),
"a config's python is checked even when no project one was found"
);
assert_eq!(
super::debug_test_python(&base("/debug-venv/bin/python"), root, adapter, false),
None
);
assert_eq!(
super::debug_test_python(&base("/work/proj/.venv/bin/python"), root, adapter, true),
Some(PathBuf::from("/work/proj/.venv/bin/python"))
);
}
#[test]
fn a_launch_requests_own_interpreter_is_the_one_checked() {
use serde_json::json;
let python = |args: serde_json::Value| {
super::launch_request_python(&json!({ "arguments": args }), Path::new("/work/proj"))
};
assert_eq!(
python(json!({ "python": "/opt/py/bin/python3.9" })),
Some(PathBuf::from("/opt/py/bin/python3.9"))
);
assert_eq!(
python(json!({ "python": ["/p/.venv/bin/python", "-X", "dev"] })),
Some(PathBuf::from("/p/.venv/bin/python"))
);
assert_eq!(
python(json!({ "pythonPath": "/legacy/python" })),
Some(PathBuf::from("/legacy/python"))
);
assert_eq!(python(json!({ "program": "a.py" })), None);
}
#[test]
fn a_relative_launch_python_is_checked_where_debugpy_runs_it() {
use serde_json::json;
let python = |args: serde_json::Value| {
super::launch_request_python(&json!({ "arguments": args }), Path::new("/work/proj"))
};
assert_eq!(
python(json!({ "python": ".venv/bin/python", "cwd": "/work/proj/api" })),
Some(PathBuf::from("/work/proj/api/.venv/bin/python")),
"from the request's cwd"
);
assert_eq!(
python(json!({
"python": ["venv/bin/python", "-X", "dev"],
"program": "/work/proj/tools/run.py"
})),
Some(PathBuf::from("/work/proj/tools/venv/bin/python")),
"from the program's folder when the request names no cwd"
);
assert_eq!(
python(json!({ "python": "./py/bin/python", "module": "pytest" })),
Some(PathBuf::from("/work/proj/py/bin/python")),
"from the adapter's folder for a module launch"
);
}
#[test]
fn a_bare_or_absolute_launch_python_is_checked_as_written() {
use serde_json::json;
let python = |args: serde_json::Value| {
super::launch_request_python(&json!({ "arguments": args }), Path::new("/work/proj"))
};
assert_eq!(
python(json!({ "python": "python3", "cwd": "/work/proj/api" })),
Some(PathBuf::from("python3"))
);
assert_eq!(
python(json!({ "python": "/opt/py/bin/python3.12", "cwd": "/work/proj/api" })),
Some(PathBuf::from("/opt/py/bin/python3.12"))
);
}
#[test]
fn f5_refuses_a_project_venv_too_old_for_debugpy() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
make_venv_of(&root, ".venv", "3.9.18");
let file = root.join("main.py");
std::fs::write(&file, "print('hi')\n").unwrap();
let mut app = App::new(root.clone()).unwrap();
app.editor.open(&file).unwrap();
app.debug_start_or_continue();
let started = !app.debug_sessions.is_empty();
if started {
app.debug_stop();
}
assert_eq!(
app.status, "Python 3.9 (.venv) is too old to debug: debugpy needs 3.10+",
"a session started: {started}"
);
assert!(!started);
assert!(app.run_debug.feedback_is_error);
}
#[test]
fn a_debuggee_with_no_venv_and_no_python3_is_left_to_the_debug_venv() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("proj");
let pkg = root.join("pkg");
std::fs::create_dir_all(&pkg).unwrap();
let empty = tmp.path().join("empty-bin");
std::fs::create_dir_all(&empty).unwrap();
let no_python = std::env::join_paths([&empty]).unwrap();
assert_eq!(super::debuggee_python(&pkg, &root, Some(&no_python)), None);
assert_eq!(super::debuggee_python(&pkg, &root, None), None, "no PATH");
let bin = tmp.path().join("bin");
std::fs::create_dir_all(&bin).unwrap();
std::fs::write(bin.join("python3"), b"#!/bin/sh\n").unwrap();
let with_python = std::env::join_paths([&empty, &bin]).unwrap();
assert_eq!(
super::debuggee_python(&pkg, &root, Some(&with_python)),
Some(bin.join("python3"))
);
}
#[test]
fn a_project_venv_is_the_debuggee_whatever_path_holds() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let venv = make_venv(root, ".venv");
let pkg = root.join("pkg");
std::fs::create_dir_all(&pkg).unwrap();
let empty = std::env::join_paths([root.join("empty-bin")]).unwrap();
assert_eq!(super::debuggee_python(&pkg, root, Some(&empty)), Some(venv));
}
#[test]
fn f5_saves_the_unsaved_buffer_before_launching() {
let (_tmp, mut app, file) = app_with_unsaved_pinned_file("notes.txt");
app.debug_start_or_continue();
assert_eq!(std::fs::read_to_string(&file).unwrap(), "new\nold\n");
assert!(!app.editor.dirty);
assert!(
app.status.starts_with("No debugger for .txt files"),
"the launch itself still runs and reports: {}",
app.status
);
}
#[test]
fn f5_saves_every_unsaved_tab_not_only_the_active_one() {
let (tmp, mut app, helper) = app_with_unsaved_pinned_file("helper.txt");
let main = tmp.path().canonicalize().unwrap().join("main.txt");
std::fs::write(&main, "main\n").unwrap();
app.editor.open_pinned(&main).unwrap();
app.debug_start_or_continue();
assert_eq!(std::fs::read_to_string(&helper).unwrap(), "new\nold\n");
assert_eq!(app.unsaved_count(), 0);
}
#[test]
fn debug_test_saves_the_unsaved_buffer_before_launching() {
let (_tmp, mut app, file) = app_with_unsaved_pinned_file("test_x.txt");
app.debug_named_test(String::from("test_x"));
assert_eq!(std::fs::read_to_string(&file).unwrap(), "new\nold\n");
assert_eq!(app.status, "No test runner detected in this workspace");
}
#[test]
fn with_save_before_debug_off_f5_leaves_the_disk_and_warns() {
let (_tmp, mut app, file) = app_with_unsaved_pinned_file("notes.txt");
app.save_before_debug = false;
app.debug_start_or_continue();
assert_eq!(std::fs::read_to_string(&file).unwrap(), "old\n");
assert!(app.editor.dirty);
assert!(
app.status
.starts_with("notes.txt has unsaved changes: the launch uses the file on disk"),
"{}",
app.status
);
assert!(
app.status.contains("No debugger for .txt files"),
"{}",
app.status
);
}
#[test]
fn f5_never_overwrites_a_file_changed_on_disk() {
let (_tmp, mut app, file) = app_with_unsaved_pinned_file("notes.txt");
std::thread::sleep(std::time::Duration::from_millis(20));
std::fs::write(&file, "theirs, changed elsewhere\n").unwrap();
app.debug_start_or_continue();
assert_eq!(
std::fs::read_to_string(&file).unwrap(),
"theirs, changed elsewhere\n"
);
assert!(app.editor.dirty);
assert!(
app.status.contains("notes.txt has unsaved changes"),
"{}",
app.status
);
}
#[test]
fn f5_with_nothing_unsaved_writes_nothing() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
let file = root.join("notes.txt");
std::fs::write(&file, "old\n").unwrap();
let stamp = std::fs::metadata(&file).unwrap().modified().unwrap();
let mut app = App::new(root).unwrap();
app.editor.open_pinned(&file).unwrap();
std::thread::sleep(std::time::Duration::from_millis(20));
app.debug_start_or_continue();
assert_eq!(std::fs::metadata(&file).unwrap().modified().unwrap(), stamp);
assert!(!app.status.contains("unsaved"), "{}", app.status);
}
#[test]
fn f9_toggles_a_breakpoint_whatever_the_updater_is_doing() {
let tmp = tempfile::tempdir().unwrap();
let file = tmp.path().join("app.py");
std::fs::write(&file, "x = 1\ny = 2\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&file).unwrap();
let marked = |app: &App| {
app.editor
.breakpoints
.get(&file)
.is_some_and(|lines| lines.contains(&1))
};
app.local_drift = Some(String::from("def456"));
assert!(app.update_armed());
let _ = app.handle_key(key(KeyCode::F(9), KeyModifiers::NONE));
assert!(marked(&app), "F9 set a breakpoint: {}", app.status);
assert!(app.self_install.is_none(), "and started no rebuild");
app.self_install = Some(crate::update_watch::SelfInstall::preloaded(&[]));
let _ = app.handle_key(key(KeyCode::F(9), KeyModifiers::NONE));
assert!(!marked(&app), "F9 removed it again: {}", app.status);
app.self_install = None;
app.update_status = UpdateStatus::Ready;
let _ = app.handle_key(key(KeyCode::F(9), KeyModifiers::NONE));
assert!(marked(&app), "{}", app.status);
assert!(
!app.pending_reexec && !app.quit,
"F9 never relaunches croft"
);
}
#[test]
fn bare_f10_reaches_the_shell_when_terminal_is_focused_with_no_debug_session() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let script = "stty raw -echo; dd bs=1 count=5 2>/dev/null | cat -v; sleep 60";
app.terminals[0] = crate::widgets::terminal::PtyTerminal::new_running(
"/bin/sh",
&[String::from("-c"), String::from(script)],
tmp.path(),
)
.unwrap();
app.focus_pane(Pane::Terminal);
let _ = app.handle_key(key(KeyCode::F(10), KeyModifiers::NONE));
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE,
"F10 to reach the PTY rather than being swallowed",
|| {
app.terminals[0]
.grid_lines()
.0
.iter()
.any(|l| l.contains("^[[21~"))
},
);
}
#[test]
fn debug_a_cargo_test_builds_off_the_ui_thread() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("Cargo.toml"),
"[package]\nname = \"x\"\nversion = \"0.0.0\"\n",
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.start_test_binary_build(tmp.path().to_path_buf(), String::from("my_case"), None);
assert_eq!(
app.status, "Building test binary for my_case",
"the request returns with the progress status showing"
);
app.start_test_binary_build(tmp.path().to_path_buf(), String::from("other"), None);
assert_eq!(app.status, "A test binary is already building");
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
while !app.drain_pending_test_debug() {
assert!(
std::time::Instant::now() < deadline,
"the build result never arrived through the drain"
);
std::thread::sleep(std::time::Duration::from_millis(20));
}
assert!(
app.status.starts_with("Test build failed"),
"a broken crate surfaces the build error, got: {}",
app.status
);
assert!(
app.pending_test_debug.is_none(),
"the pending slot frees for the next request"
);
}
#[test]
fn debug_end_message_flags_a_program_that_exited_without_hitting_a_breakpoint() {
assert_eq!(
debug_end_message(true, false),
"Program exited without hitting a breakpoint"
);
assert_eq!(debug_end_message(true, true), "Debug session ended");
assert_eq!(debug_end_message(false, false), "Debug session ended");
}
#[test]
fn debug_console_is_retained_after_the_session_ends() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_console = vec![
crate::ansi_text::AnsiLine::plain("hello from program"),
crate::ansi_text::AnsiLine::plain("all done"),
];
app.run_debug.session_ended = true;
app.refresh_debug_panel();
assert!(
app.run_debug
.console_tail
.contains(&crate::ansi_text::AnsiLine::plain("all done")),
"the ended session's console output must stay visible"
);
app.run_debug.session_ended = false;
app.refresh_debug_panel();
assert!(
app.run_debug.console_tail.is_empty(),
"the idle Run panel must not show stale console output"
);
}
#[test]
fn add_watch_via_the_palette_prompt_then_clear_all() {
use crate::widgets::input_prompt::InputPurpose;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.run_command(crate::widgets::command_palette::Command::DebugAddWatch);
let prompt = app
.input_prompt
.as_ref()
.expect("Debug: Add Watch Expression must open an input prompt");
assert_eq!(prompt.purpose, InputPurpose::AddWatch);
append_and_submit_prompt(&mut app, "len(cart)");
assert!(app.input_prompt.is_none(), "submitting closes the prompt");
assert_eq!(app.watch_exprs, vec!["len(cart)"]);
assert!(app.status.contains("Watching len(cart)"));
app.add_watch_expression(String::from(" len(cart) "));
assert_eq!(app.watch_exprs.len(), 1, "no duplicate watches");
app.add_watch_expression(String::from("t"));
app.watch_vals
.insert(String::from("t"), (String::from("9.99"), true));
app.remove_watch_expression(0);
assert_eq!(
app.watch_exprs,
vec!["t"],
"removal is by display index, later rows keep their values"
);
assert!(app.watch_vals.contains_key("t"));
app.run_command(crate::widgets::command_palette::Command::DebugClearWatch);
assert!(app.watch_exprs.is_empty() && app.watch_vals.is_empty());
assert!(app.status.contains("Removed 1 watch expression"));
}
#[test]
fn lldb_program_resolves_against_the_session_cwd_not_crofts() {
let tmp = tempfile::tempdir().unwrap();
let cwd = tmp.path().join("proj");
std::fs::create_dir_all(cwd.join("target/debug")).unwrap();
std::fs::write(cwd.join("target/debug/app"), b"bin").unwrap();
let resolved = resolve_lldb_program("target/debug/app", &cwd).expect("exists under cwd");
assert_eq!(resolved, cwd.join("target/debug/app"));
assert!(resolved.is_absolute());
let err = resolve_lldb_program("target/release/app", &cwd).unwrap_err();
assert!(
err.contains(&cwd.join("target/release/app").display().to_string()),
"{err}"
);
let abs = cwd.join("target/debug/app");
let abs_str = abs.to_string_lossy().into_owned();
assert_eq!(
resolve_lldb_program(&abs_str, Path::new("/somewhere/else")).unwrap(),
abs
);
}
#[test]
fn debug_config_picker_lists_configs_and_selection_drives_f5() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "API", "type": "python", "request": "launch", "program": "api.py" },
{ "name": "Javan", "type": "java", "program": "Main.java" }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let picker = app.list_picker.as_ref().expect("picker open");
assert_eq!(
picker.rows.len(),
4,
"zero-config entry + the two configs + Add Configuration"
);
assert!(picker.rows[0].label.contains("active file"));
assert!(picker.rows[1].label.contains("API"));
app.list_picker.as_mut().unwrap().selected = 2;
app.confirm_list_picker();
assert_eq!(app.selected_debug_config.as_deref(), Some("Javan"));
assert!(
app.run_debug.feedback_is_error,
"unsupported type is an error: {:?}",
app.run_debug.feedback
);
assert!(
app.status.contains("unsupported type \"java\""),
"{}",
app.status
);
app.open_debug_config_picker();
app.list_picker.as_mut().unwrap().selected = 0;
app.confirm_list_picker();
assert_eq!(app.selected_debug_config, None);
}
#[test]
fn the_config_row_counts_compounds_so_a_compounds_only_workspace_still_has_one() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"{"configurations":[{"name":"Server","type":"python","program":"s.py"}],
"compounds":[{"name":"Full Stack","configurations":["Server"]}]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.refresh_run_debug();
assert_eq!(
app.run_debug.config_count, 2,
"one configuration plus one compound — the row counts what the picker lists"
);
let only = tempfile::tempdir().unwrap();
std::fs::create_dir_all(only.path().join(".croft")).unwrap();
std::fs::write(
only.path().join(".croft/launch.json"),
r#"{"configurations":[],
"compounds":[{"name":"Orphan","configurations":["Nothing"]}]}"#,
)
.unwrap();
let mut app2 = App::new(only.path().to_path_buf()).unwrap();
app2.refresh_run_debug();
assert_eq!(
app2.run_debug.config_count, 1,
"a compounds-only workspace must still get a row, or the documented \
route to the picker does not exist for it"
);
}
#[test]
fn refresh_run_debug_syncs_the_config_row() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"[ { "name": "One", "type": "python", "program": "a.py" } ]"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.selected_debug_config = Some(String::from("One"));
app.refresh_run_debug();
assert_eq!(app.run_debug.config_count, 1);
assert_eq!(app.run_debug.selected_config.as_deref(), Some("One"));
}
#[test]
fn a_member_that_fails_to_start_is_not_counted_as_started() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch", "program": "s.py" },
{ "name": "Broken", "type": "no-such-debugger", "request": "launch", "program": "b.py" }
],
"compounds": [
{ "name": "Mixed", "configurations": ["Server", "Broken"] }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let row = app
.list_picker
.as_ref()
.unwrap()
.rows
.iter()
.position(|r| r.label.starts_with("Mixed"))
.expect("the compound is listed");
app.list_picker.as_mut().unwrap().selected = row;
app.confirm_list_picker();
let live = app.debug_sessions.names();
assert!(
!live.contains(&"Broken".to_string()),
"the unresolvable member never joins the set: {live:?}"
);
let reported = app
.run_debug
.feedback
.clone()
.unwrap_or_else(|| app.status.clone());
if live.is_empty() {
assert!(
reported.contains("started no sessions"),
"an empty set is reported as empty, never as live sessions: {reported}"
);
} else {
assert!(
reported.contains(&format!("{} of 2", live.len())),
"the count names the sessions that exist ({}), not the members attempted: {reported}",
live.len()
);
}
assert!(
!reported.contains("showing ?"),
"no session is described that cannot be named: {reported}"
);
}
#[test]
fn a_compound_member_skipped_for_its_own_task_is_named_in_the_feedback() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch", "program": "s.py" },
{ "name": "Client", "type": "python", "request": "launch", "program": "c.py",
"preLaunchTask": "build-client" }
],
"compounds": [
{ "name": "Pair", "configurations": ["Server", "Client"] }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let row = app
.list_picker
.as_ref()
.unwrap()
.rows
.iter()
.position(|r| r.label.starts_with("Pair"))
.expect("the compound is listed");
app.list_picker.as_mut().unwrap().selected = row;
app.confirm_list_picker();
let reported = app
.run_debug
.feedback
.clone()
.unwrap_or_else(|| app.status.clone());
assert!(
reported.contains("Client"),
"the skipped member is named: {reported}"
);
assert!(
reported.contains("preLaunchTask"),
"and why it was skipped: {reported}"
);
assert!(
!app.debug_sessions.names().contains(&"Client".to_string()),
"the skipped member is not running: {:?}",
app.debug_sessions.names()
);
}
#[test]
fn a_single_member_compound_launches_rather_than_citing_the_multi_session_limit() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch", "program": "s.py" },
{ "name": "Client", "type": "python", "request": "launch", "program": "c.py" }
],
"compounds": [
{ "name": "Just Server", "configurations": ["Server"] },
{ "name": "Both", "configurations": ["Server", "Client"] },
{ "name": "Tasked", "configurations": ["Server"], "preLaunchTask": "build" }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let idx = app
.list_picker
.as_ref()
.expect("picker open")
.rows
.iter()
.position(|r| r.label.starts_with("Just Server"))
.expect("the one-member compound is listed");
app.list_picker.as_mut().unwrap().selected = idx;
app.confirm_list_picker();
assert!(
!app.status.contains("#310"),
"a one-member compound needs ONE session, so the multi-session \
deferral does not apply to it: {}",
app.status
);
assert_eq!(app.selected_debug_compound.as_deref(), Some("Just Server"));
assert_eq!(app.selected_debug_config, None);
if let Some(feedback) = app.run_debug.feedback.as_deref() {
assert!(
!feedback.contains("#310") && !feedback.contains("several debug sessions"),
"a one-member compound must not be refused for needing several \
sessions; any other environment-dependent failure is not this \
test's business: {feedback}"
);
}
app.open_debug_config_picker();
let multi = app
.list_picker
.as_ref()
.unwrap()
.rows
.iter()
.position(|r| r.label.starts_with("Both"))
.expect("the two-member compound is listed");
app.list_picker.as_mut().unwrap().selected = multi;
app.confirm_list_picker();
let live = app.debug_sessions.names();
if live.is_empty() {
assert!(
app.status.contains("started no sessions"),
"no adapter: the compound says so rather than claiming sessions: {}",
app.status
);
} else {
assert_eq!(
live,
vec!["Server", "Client"],
"with an adapter, both members run in launch.json order: {}",
app.status
);
assert_eq!(
app.debug_sessions.focused_name(),
Some("Server"),
"the FIRST member takes focus"
);
assert!(
app.status.contains(&format!("{} session(s)", live.len())),
"the count names the sessions that exist: {}",
app.status
);
assert!(
!app.status.contains("showing ?"),
"and the focused session is identified: {}",
app.status
);
}
assert!(
!app.status.contains("#310"),
"and no longer defers to the session limit: {}",
app.status
);
let mut fresh = App::new(tmp.path().to_path_buf()).unwrap();
fresh.open_debug_config_picker();
let tasked = fresh
.list_picker
.as_ref()
.expect("picker open")
.rows
.iter()
.position(|r| r.label.starts_with("Tasked"))
.expect("the task-carrying compound is listed");
fresh.list_picker.as_mut().unwrap().selected = tasked;
fresh.confirm_list_picker();
assert!(
fresh.status.contains("\"build\" not found"),
"the error names the task the file asked for, so the user can declare \
it rather than guessing: {}",
fresh.status
);
assert!(
!fresh.status.contains("#318"),
"and it no longer cites the unbuilt-feature issue: croft honours this \
key now, so naming #318 would report a limitation it does not have: {}",
fresh.status
);
assert!(
fresh.pending_debug_launch.is_none(),
"nothing parked behind a task that does not exist"
);
assert!(
fresh.debug_sessions.is_empty(),
"and nothing launched — starting the member anyway would debug stale \
artifacts, which is what running the task exists to prevent"
);
}
#[test]
fn a_compounds_own_pre_launch_task_runs_before_its_member_starts() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "build", "type": "shell", "command": "true" }
]}"#,
)
.unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch", "program": "s.py" },
{ "name": "Client", "type": "python", "request": "launch", "program": "c.py" }
],
"compounds": [
{ "name": "Tasked", "configurations": ["Server"], "preLaunchTask": "build" },
{ "name": "Presented", "configurations": ["Server"],
"presentation": { "hidden": "true" } },
{ "name": "PresentedPair", "configurations": ["Server", "Client"],
"presentation": { "hidden": "true" } }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let select = |app: &mut App, label: &str| {
app.open_debug_config_picker();
let idx = app
.list_picker
.as_ref()
.expect("picker open")
.rows
.iter()
.position(|r| r.label.starts_with(label))
.unwrap_or_else(|| panic!("{label} is listed"));
app.list_picker.as_mut().unwrap().selected = idx;
app.confirm_list_picker();
};
select(&mut app, "Tasked");
assert!(
app.pending_debug_launch.is_some(),
"the launch waits behind the compound's task rather than starting \
alongside it: status was {:?}",
app.status
);
assert!(
!app.run_debug.feedback_is_error,
"and this is progress, not a refusal: {:?}",
app.run_debug.feedback
);
assert!(
app.status.contains("build") && app.status.contains("exits 0"),
"the status names the task and the condition the launch waits on: {}",
app.status
);
assert!(
app.debug_sessions.is_empty(),
"nothing has been debugged yet — that is the whole point of the wait"
);
let mut fresh = App::new(tmp.path().to_path_buf()).unwrap();
select(&mut fresh, "Presented");
assert!(
fresh.run_debug.feedback_is_error,
"presentation is still not honoured, so it still refuses: {:?}",
fresh.run_debug.feedback
);
assert!(
fresh.status.contains("presentation.hidden"),
"and names the key it cannot read: {}",
fresh.status
);
assert!(
!fresh
.status
.replace("presentation.hidden", "")
.contains("presentation"),
"the parent is named only as part of the qualified path: {}",
fresh.status
);
assert!(
!fresh.status.contains("preLaunchTask"),
"without naming the one it now can — reporting a limitation croft no \
longer has is the false claim this whole path exists to avoid: {}",
fresh.status
);
assert!(
fresh.pending_debug_launch.is_none() && fresh.debug_sessions.is_empty(),
"and nothing ran"
);
let mut pair = App::new(tmp.path().to_path_buf()).unwrap();
select(&mut pair, "PresentedPair");
assert!(
pair.run_debug.feedback_is_error,
"a malformed key still refuses at two members: {:?}",
pair.run_debug.feedback
);
assert!(
pair.status.contains("Server") && pair.status.contains("Client"),
"and the remedy names EVERY member, not just the first: {}",
pair.status
);
assert!(pair.debug_sessions.is_empty(), "and still nothing ran");
}
#[test]
fn a_pre_launch_task_with_a_vs_code_label_runs_the_detected_task() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path();
std::fs::write(
p.join("package.json"),
r#"{ "name": "ts1", "scripts": { "build": "tsc" } }"#,
)
.unwrap();
std::fs::write(p.join("tsconfig.json"), "{}").unwrap();
std::fs::create_dir_all(p.join(".vscode")).unwrap();
std::fs::write(
p.join(".vscode/launch.json"),
r#"{ "configurations": [
{ "type": "node", "request": "launch", "name": "Npm",
"program": "${workspaceFolder}/dist/main.js", "preLaunchTask": "npm: build" },
{ "type": "node", "request": "launch", "name": "Tsc",
"program": "${workspaceFolder}/dist/main.js", "preLaunchTask": "tsc: build - tsconfig.json" },
{ "type": "node", "request": "launch", "name": "Bare",
"program": "${workspaceFolder}/dist/main.js", "preLaunchTask": "build" }
]}"#,
)
.unwrap();
let launch = |name: &str| {
let mut app = App::new(p.to_path_buf()).unwrap();
let cfg = crate::dap::configs::discover_configs(p)
.into_iter()
.find(|c| c.name == name)
.unwrap();
app.launch_debug_config(&cfg);
app
};
let app = launch("Npm");
assert_eq!(
app.pending_debug_launch
.as_ref()
.map(|l| l.command.as_str()),
Some("npm run build"),
"{}",
app.status
);
let app = launch("Tsc");
assert_eq!(
app.pending_debug_launch
.as_ref()
.map(|l| l.command.as_str()),
Some("tsc -p tsconfig.json"),
"{}",
app.status
);
let app = launch("Bare");
assert!(app.pending_debug_launch.is_none());
assert!(
app.status
.contains("preLaunchTask \"build\" not found (did you mean \"npm run build\"?)"),
"{}",
app.status
);
}
#[test]
fn a_finished_compound_task_starts_its_member_and_chains_the_members_own_task() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "compound-build", "type": "shell", "command": "true 1" },
{ "label": "member-build", "type": "shell", "command": "true 2" }
]}"#,
)
.unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch",
"program": "s.py", "preLaunchTask": "member-build" }
],
"compounds": [
{ "name": "Tasked", "configurations": ["Server"], "preLaunchTask": "compound-build" }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let idx = app
.list_picker
.as_ref()
.expect("picker open")
.rows
.iter()
.position(|r| r.label.starts_with("Tasked"))
.expect("the compound is listed");
app.list_picker.as_mut().unwrap().selected = idx;
app.confirm_list_picker();
let parked = app
.pending_debug_launch
.take()
.expect("the compound's task parked the launch");
assert!(
app.status.contains("compound-build"),
"staging: it is the COMPOUND's task that ran first: {}",
app.status
);
app.resume_pending_launch(Some(0), parked.step);
let second = app
.pending_debug_launch
.as_ref()
.expect("the member's own preLaunchTask parks in turn rather than being skipped");
assert!(
app.status.contains("member-build"),
"and it is the MEMBER's task waiting now: {}",
app.status
);
assert!(
!app.run_debug.feedback_is_error,
"still progress, not a refusal: {:?}",
app.run_debug.feedback
);
assert!(
matches!(second.step, crate::app::PendingLaunchStep::Resolved(_)),
"the second park is the member's own launch, so the chain ends here \
rather than looping"
);
let second = app.pending_debug_launch.take().expect("still parked");
app.resume_pending_launch(Some(1), second.step);
assert!(
app.run_debug.feedback_is_error && app.status.contains("exited 1"),
"a non-zero task aborts the launch and says so: {}",
app.status
);
assert!(app.debug_sessions.is_empty(), "and nothing was debugged");
}
fn watch_task_workspace(background: bool, build: &str) -> (tempfile::TempDir, App) {
let script = format!(
"sleep 1; echo 'Starting compilation in watch mode...'; {build} echo 'Found 0 errors. Watching for file changes.'; sleep 30"
);
watch_task_workspace_with(background, &script, serde_json::json!("$tsc-watch"))
}
fn watch_task_workspace_with(
background: bool,
script: &str,
matcher: serde_json::Value,
) -> (tempfile::TempDir, App) {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
let task = serde_json::json!({
"version": "2.0.0",
"tasks": [{
"label": "watch", "type": "shell", "command": "sh", "args": ["-c", script],
"isBackground": background, "problemMatcher": matcher
}]
});
std::fs::write(tmp.path().join(".vscode/tasks.json"), task.to_string()).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [ { "name": "Built", "type": "lldb", "request": "launch",
"program": "/nope/bin", "preLaunchTask": "watch" } ] }"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let idx = app
.list_picker
.as_ref()
.unwrap()
.rows
.iter()
.position(|r| r.label.starts_with("Built"))
.expect("Built is listed");
app.list_picker.as_mut().unwrap().selected = idx;
app.confirm_list_picker();
(tmp, app)
}
#[test]
fn a_watch_pre_launch_task_launches_once_its_first_build_ends() {
let (_tmp, mut app) = watch_task_workspace(true, "");
assert!(app.pending_debug_launch.is_some(), "{}", app.status);
assert!(
app.status.contains("when its first build ends"),
"{}",
app.status
);
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE * 4,
"the watcher's first build to settle the launch",
|| {
app.drain_terminal_bells();
app.pending_debug_launch.is_none()
},
);
assert!(
app.status.contains("program /nope/bin does not exist"),
"the launch went ahead: {}",
app.status
);
assert!(
app.terminals.iter().any(|t| t.label().contains("watch")),
"the watcher's pane is left open"
);
}
#[test]
fn a_watch_pre_launch_task_with_build_errors_does_not_launch() {
let (_tmp, mut app) = watch_task_workspace(
true,
r#"echo "src/index.ts(1,5): error TS2322: Type 'string' is not assignable to type 'number'.";"#,
);
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE * 4,
"the watcher's first build to settle the launch",
|| {
app.drain_terminal_bells();
app.pending_debug_launch.is_none()
},
);
assert!(app.run_debug.feedback_is_error);
assert!(
app.status.contains("finished with 1 error"),
"{}",
app.status
);
assert!(!app.status.contains("does not exist"), "{}", app.status);
}
#[test]
fn an_active_on_start_watch_pre_launch_task_launches_on_its_first_end() {
let (_tmp, mut app) = watch_task_workspace_with(
true,
"echo 'Found 0 errors. Watching for file changes.'; sleep 300",
serde_json::json!({
"base": "$tsc",
"background": {
"activeOnStart": true,
"beginsPattern": "^File change detected",
"endsPattern": "Watching for file changes\\."
}
}),
);
assert!(
app.status.contains("when its first build ends"),
"{}",
app.status
);
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE * 4,
"the watcher's first build end to settle the launch",
|| {
app.drain_terminal_bells();
app.pending_debug_launch.is_none()
},
);
assert!(
app.status.contains("program /nope/bin does not exist"),
"the launch went ahead: {}",
app.status
);
}
#[test]
fn an_ordinary_pre_launch_task_still_waits_for_its_exit() {
let (_tmp, mut app) = watch_task_workspace(
false,
r#"echo "src/index.ts(1,5): error TS2322: Type 'string' is not assignable to type 'number'.";"#,
);
assert!(app.status.contains("when it exits 0"), "{}", app.status);
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE * 4,
"the watcher's build to reach PROBLEMS",
|| {
app.drain_terminal_bells();
!app.build_diagnostics.is_empty()
},
);
assert!(
app.pending_debug_launch.is_some(),
"still waiting: {}",
app.status
);
assert!(
!app.run_debug.feedback_is_error,
"{:?}",
app.run_debug.feedback
);
}
#[test]
fn a_pre_launch_wait_follows_is_background_and_the_matcher() {
let task = |background: bool, matcher: Option<serde_json::Value>| crate::tasks::Task {
label: String::from("t"),
command: String::from("t"),
source: String::from("tasks.json"),
is_build: false,
is_default: false,
problem_matcher: matcher,
is_background: background,
vscode: None,
};
let watch = Some(serde_json::json!("$tsc-watch"));
assert_eq!(
PreLaunchWait::of(&task(false, watch.clone())),
PreLaunchWait::Exit
);
assert_eq!(
PreLaunchWait::of(&task(true, watch)),
PreLaunchWait::FirstBuild
);
assert_eq!(
PreLaunchWait::of(&task(true, Some(serde_json::json!("$tsc")))),
PreLaunchWait::Untracked
);
assert_eq!(
PreLaunchWait::of(&task(true, None)),
PreLaunchWait::Untracked
);
assert_eq!(PreLaunchWait::of(&task(false, None)), PreLaunchWait::Exit);
}
#[test]
fn debugging_a_test_breaks_at_its_last_failure_and_cleans_up_after() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
let src = tmp.path().join("src/lib.rs");
std::fs::write(&src, "fn a() {}\nfn b() {}\nassert!(false);\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
crate::output::clear(crate::output::CHANNEL_TESTS);
for line in [
"---- tests::adds stdout ----",
"thread 'tests::adds' panicked at src/lib.rs:3:5:",
"assertion failed: false",
] {
crate::output::push(
crate::output::CHANNEL_TESTS,
crate::output::OutputLevel::Info,
line,
);
}
let site = app
.failure_site_of("tests::adds")
.expect("the panic line names a place in the user's own code");
assert_eq!(site.line, 3);
assert_eq!(site.resolve(tmp.path()), Some((src.clone(), 3)));
assert!(app.editor.breakpoints.is_empty());
app.arm_failure_breakpoint("tests::adds", tmp.path());
assert_eq!(
app.editor.breakpoints.get(&src).map(|l| l.contains(&3)),
Some(true),
"the assertion's line carries a breakpoint the user never set"
);
assert!(
app.with_failure_note(String::from("Building"))
.contains("breaking at lib.rs:3 where it last failed"),
"{}",
app.with_failure_note(String::from("Building"))
);
app.disarm_failure_breakpoint();
assert_eq!(app.with_failure_note(String::from("Building")), "Building");
assert!(
app.editor.breakpoints.is_empty(),
"the temporary breakpoint does not outlive its session"
);
app.editor
.breakpoints
.entry(src.clone())
.or_default()
.insert(3);
app.arm_failure_breakpoint("tests::adds", tmp.path());
app.disarm_failure_breakpoint();
assert_eq!(
app.editor.breakpoints.get(&src).map(|l| l.contains(&3)),
Some(true),
"the user's own breakpoint survives a debug session that shared its line"
);
app.editor.breakpoints.clear();
crate::output::clear(crate::output::CHANNEL_TESTS);
for line in [
"---- tests::deps stdout ----",
"thread 'tests::deps' panicked at /Users/me/.cargo/registry/src/x-1.0/lib.rs:9:1:",
] {
crate::output::push(
crate::output::CHANNEL_TESTS,
crate::output::OutputLevel::Info,
line,
);
}
assert!(app.failure_site_of("tests::deps").is_none());
app.arm_failure_breakpoint("tests::deps", tmp.path());
assert!(
app.editor.breakpoints.is_empty(),
"a location inside a dependency is not offered as a breakpoint"
);
assert!(
crate::widgets::command_palette::ALL_COMMANDS
.contains(&crate::widgets::command_palette::Command::DebugTestAtCursor)
);
}
#[test]
fn the_config_row_does_not_count_a_hidden_compound() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"{
"configurations": [
{ "name": "A", "type": "lldb", "request": "launch", "program": "/bin/ls" }
],
"compounds": [
{ "name": "OnlyHidden", "configurations": ["A"],
"presentation": { "hidden": true } }
]
}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.refresh_run_debug();
assert_eq!(
crate::dap::configs::discover_configs(tmp.path()).len(),
1,
"fixture: exactly one configuration, so the counts isolate the compound"
);
assert_eq!(
app.run_debug.config_count, 1,
"the visible configuration counts; the hidden compound does not"
);
app.open_debug_config_picker();
let picker = app.list_picker.as_ref().expect("the picker opened");
assert!(
!picker.rows.iter().any(|r| r.id.starts_with("compound:")),
"no compound rows: {:?}",
picker.rows.iter().map(|r| &r.id).collect::<Vec<_>>()
);
app.list_picker = None;
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"{
"configurations": [
{ "name": "A", "type": "lldb", "request": "launch", "program": "/bin/ls" }
],
"compounds": [
{ "name": "OnlyHidden", "configurations": ["A"],
"presentation": { "hidden": false } }
]
}"#,
)
.unwrap();
let mut shown = App::new(tmp.path().to_path_buf()).unwrap();
shown.refresh_run_debug();
assert_eq!(
shown.run_debug.config_count, 2,
"the same compound, unhidden, IS an entry the row offers alongside A"
);
shown.open_debug_config_picker();
let picker = shown.list_picker.as_ref().expect("the picker opened");
assert!(
picker.rows.iter().any(|r| r.id.starts_with("compound:")),
"and the picker lists it: {:?}",
picker.rows.iter().map(|r| &r.id).collect::<Vec<_>>()
);
}
#[test]
fn a_hidden_configuration_leaves_both_the_picker_and_the_count() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"{
"configurations": [
{ "name": "Shown", "type": "lldb", "request": "launch", "program": "/bin/ls" },
{ "name": "Hid", "type": "lldb", "request": "launch", "program": "/bin/ls",
"presentation": { "hidden": true } }
]
}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.refresh_run_debug();
assert_eq!(
crate::dap::configs::discover_configs(tmp.path()).len(),
2,
"fixture: both configurations parse, so the count below isolates hiding"
);
assert_eq!(
app.run_debug.config_count, 1,
"the hidden configuration is not an entry the row offers"
);
app.open_debug_config_picker();
let labels: Vec<String> = app
.list_picker
.as_ref()
.expect("the picker opened")
.rows
.iter()
.map(|r| r.label.clone())
.collect();
assert!(
labels.iter().any(|l| l.starts_with("Shown")),
"the visible one is listed: {labels:?}"
);
assert!(
!labels.iter().any(|l| l.starts_with("Hid")),
"and the hidden one is not: {labels:?}"
);
}
#[test]
fn a_reordering_presentation_leaves_every_row_naming_its_own_entry() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"{
"configurations": [
{ "name": "First", "type": "lldb", "request": "launch", "program": "/bin/ls" },
{ "name": "Second", "type": "lldb", "request": "launch", "program": "/bin/ls" },
{ "name": "Last", "type": "lldb", "request": "launch", "program": "/bin/ls",
"presentation": { "order": 1 } }
]
}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let rows: Vec<(String, String)> = app
.list_picker
.as_ref()
.expect("the picker opened")
.rows
.iter()
.map(|r| (r.id.clone(), r.label.clone()))
.collect();
let configs = crate::dap::configs::discover_configs(tmp.path());
let ordered: Vec<&str> = rows
.iter()
.filter(|(id, _)| id != "active" && id != "add")
.map(|(_, label)| label.split(' ').next().unwrap_or_default())
.collect();
assert_eq!(
ordered,
vec!["Last", "First", "Second"],
"precondition: the presentation actually moved a row"
);
for (id, label) in rows.iter().filter(|(id, _)| id != "active" && id != "add") {
let idx: usize = id.parse().expect("a configuration row's id is its index");
assert!(
label.starts_with(&configs[idx].name),
"row {id:?} says {label:?} but index {idx} is {:?}",
configs[idx].name
);
}
}
#[test]
fn a_hidden_compound_is_not_listed_and_the_rest_keep_their_ids() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"{
"configurations": [
{ "name": "A", "type": "lldb", "request": "launch", "program": "/bin/ls" }
],
"compounds": [
{ "name": "First", "configurations": ["A"] },
{ "name": "Hidden", "configurations": ["A"], "presentation": { "hidden": true } },
{ "name": "Third", "configurations": ["A"] }
]
}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let picker = app.list_picker.as_ref().expect("the picker opened");
let rows: Vec<(String, String)> = picker
.rows
.iter()
.filter(|r| r.id.starts_with("compound:"))
.map(|r| (r.id.clone(), r.label.clone()))
.collect();
assert_eq!(rows.len(), 2, "the hidden compound is not listed: {rows:?}");
assert!(
!rows.iter().any(|(_, l)| l.contains("Hidden")),
"and it is the hidden one that went: {rows:?}"
);
assert_eq!(rows[0].0, "compound:0", "First keeps its index");
assert!(rows[0].1.contains("First"));
assert_eq!(
rows[1].0, "compound:2",
"Third keeps index 2 across the hidden row, not 1"
);
assert!(rows[1].1.contains("Third"));
}
fn pick_row_id(app: &mut App, id: &str) {
let picker = app.list_picker.as_mut().expect("a picker is open");
picker.selected = picker
.rows
.iter()
.position(|r| r.id == id)
.unwrap_or_else(|| panic!("no row {id}"));
app.confirm_list_picker();
}
fn replace_and_submit_prompt(app: &mut App, value: &str) {
app.input_prompt.as_mut().expect("a prompt is open").value = value.to_string();
app.submit_input_prompt();
}
#[test]
fn add_debug_configuration_writes_a_launch_entry() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
pick_row_id(&mut app, "add");
pick_row_id(&mut app, "python");
pick_row_id(&mut app, "launch");
let prompt = app.input_prompt.as_ref().expect("the name is asked");
assert_eq!(prompt.value, "Launch Python", "seeded with a name");
replace_and_submit_prompt(&mut app, "Serve");
assert_eq!(app.input_prompt.as_ref().unwrap().value, "${file}");
replace_and_submit_prompt(&mut app, "${workspaceFolder}/api.py");
replace_and_submit_prompt(&mut app, "--port \"80 00");
assert!(app.status.contains("quote"), "{}", app.status);
assert_eq!(
app.input_prompt.as_ref().unwrap().value,
"--port \"80 00",
"the refused value is offered back to fix"
);
replace_and_submit_prompt(&mut app, "--port 8000");
replace_and_submit_prompt(&mut app, "");
replace_and_submit_prompt(&mut app, "DEBUG=1");
replace_and_submit_prompt(&mut app, "");
assert!(app.input_prompt.is_none(), "the draft is complete");
assert!(app.status.contains("Added \"Serve\""), "{}", app.status);
assert_eq!(app.selected_debug_config.as_deref(), Some("Serve"));
let cfgs = crate::dap::configs::discover_configs(tmp.path());
assert_eq!(cfgs.len(), 1);
assert_eq!(cfgs[0].source, ".croft/launch.json");
let written: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(tmp.path().join(".croft/launch.json")).unwrap(),
)
.unwrap();
let entry = &written["configurations"][0];
assert_eq!(entry["program"], "${workspaceFolder}/api.py");
assert_eq!(entry["args"], serde_json::json!(["--port", "8000"]));
assert_eq!(entry["env"]["DEBUG"], "1");
assert!(entry.get("cwd").is_none() && entry.get("preLaunchTask").is_none());
app.open_debug_config_picker();
assert!(
app.list_picker
.as_ref()
.unwrap()
.rows
.iter()
.any(|r| r.label.starts_with("Serve")),
"and the picker lists it"
);
}
#[test]
fn add_debug_configuration_attaches_by_port() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.run_command(crate::widgets::command_palette::Command::AddDebugConfig);
pick_row_id(&mut app, "node");
pick_row_id(&mut app, "attach");
replace_and_submit_prompt(&mut app, "Web");
replace_and_submit_prompt(&mut app, "nope");
assert!(app.status.contains("Not a port"), "{}", app.status);
replace_and_submit_prompt(&mut app, "9229");
assert!(app.input_prompt.is_none());
let cfgs = crate::dap::configs::discover_configs(tmp.path());
assert_eq!(
(cfgs.len(), cfgs[0].request),
(1, crate::dap::configs::RequestKind::Attach)
);
}
#[test]
fn a_compound_member_from_another_folder_launches_from_that_folder() {
let tmp = tempfile::tempdir().unwrap();
let api = tmp.path().join("api");
let web = tmp.path().join("web");
for d in [&api, &web] {
std::fs::create_dir_all(d.join(".vscode")).unwrap();
}
std::fs::write(
api.join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Run api", "type": "python", "request": "launch", "program": "${workspaceFolder}/main.py" }
],
"compounds": [
{ "name": "Web only", "configurations": [{ "folder": "web", "name": "Run web" }] }
]}"#,
)
.unwrap();
std::fs::write(
web.join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Run web", "type": "python", "request": "launch", "program": "${workspaceFolder}/main.py", "preLaunchTask": "serve" }
]}"#,
)
.unwrap();
std::fs::write(
web.join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "serve", "type": "shell", "command": "sleep 30" }
]}"#,
)
.unwrap();
let mut app = App::new(api.clone()).unwrap();
app.roots.add(web.clone());
pick_debug_row(&mut app, "Web only");
assert!(
!app.status.contains("no launch.json declares"),
"status: {}",
app.status
);
let pending = app
.pending_debug_launch
.as_ref()
.expect("web's preLaunchTask parks the launch");
let PendingLaunchStep::Resolved(rc) = &pending.step else {
panic!("the member itself is parked behind its task");
};
assert_eq!(rc.name, "Run web");
assert_eq!(
rc.program.as_deref(),
Some(web.join("main.py").to_string_lossy().as_ref())
);
}
fn inputs_workspace_1424() -> tempfile::TempDir {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "build", "type": "shell", "command": "echo", "args": ["${input:envName}"] },
{ "label": "greet", "type": "shell", "command": "echo", "args": ["${input:who}"] }
],
"inputs": [
{ "id": "who", "type": "promptString", "description": "Who", "default": "world" }
]}"#,
)
.unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Run with args", "type": "python", "request": "launch",
"program": "${workspaceFolder}/app.py", "preLaunchTask": "build",
"args": ["--env", "${input:envName}", "--user", "${input:userName}", "${input:envName}"] },
{ "name": "Typo", "type": "python", "request": "launch", "program": "app.py",
"args": ["${input:nope}"] },
{ "name": "Secret", "type": "python", "request": "launch", "program": "app.py",
"args": ["${input:token}"] }
],
"inputs": [
{ "id": "envName", "type": "pickString", "description": "Environment",
"options": ["dev", "staging", "prod"], "default": "staging" },
{ "id": "userName", "type": "promptString", "description": "User to run as",
"default": "alice" },
{ "id": "token", "type": "promptString", "description": "Token", "password": true }
]}"#,
)
.unwrap();
tmp
}
fn launch_named_1424(app: &mut App, name: &str) {
let cfg = crate::dap::configs::discover_configs(&app.active_workspace_root())
.into_iter()
.find(|c| c.name == name)
.expect("config declared");
app.launch_debug_config(&cfg);
}
#[test]
fn a_launch_asks_each_input_once_and_uses_the_answers() {
use crate::widgets::input_prompt::InputPurpose;
use crate::widgets::list_picker::ListPurpose;
let tmp = inputs_workspace_1424();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
launch_named_1424(&mut app, "Run with args");
let picker = app.list_picker.as_ref().expect("the pick list is open");
assert_eq!(picker.purpose, ListPurpose::LaunchInput);
assert_eq!(picker.title, "Environment");
let labels: Vec<_> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["dev", "staging", "prod"]);
assert_eq!(
picker.selected_row().map(|r| r.id.as_str()),
Some("staging"),
"the default is selected"
);
assert!(app.pending_debug_launch.is_none(), "nothing starts yet");
app.list_picker.as_mut().unwrap().selected = 2;
app.confirm_list_picker();
let prompt = app.input_prompt.as_ref().expect("then the text box");
assert!(matches!(prompt.purpose, InputPurpose::LaunchInput));
assert_eq!(prompt.title, "User to run as");
assert_eq!(prompt.value, "alice", "prefilled with the default");
assert!(!prompt.masked);
app.input_prompt.as_mut().unwrap().value = String::from("bob");
app.submit_input_prompt();
assert!(
app.input_prompt.is_none() && app.list_picker.is_none(),
"envName, used three times, was asked for once"
);
let pending = app
.pending_debug_launch
.as_ref()
.unwrap_or_else(|| panic!("the launch waits on its task: {}", app.status));
assert_eq!(pending.command, "echo prod", "the task got the answer too");
let PendingLaunchStep::Resolved(rc) = &pending.step else {
panic!("a resolved config waits on the task");
};
assert_eq!(rc.args, ["--env", "prod", "--user", "bob", "prod"]);
}
#[test]
fn esc_on_an_input_starts_nothing_and_an_unknown_input_is_named() {
let tmp = inputs_workspace_1424();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
launch_named_1424(&mut app, "Run with args");
assert!(app.list_picker.is_some(), "precondition: asking");
app.handle_list_picker_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
assert!(app.list_picker.is_none() && app.input_prompt.is_none());
assert!(
app.pending_debug_launch.is_none() && app.debug_sessions.is_empty(),
"nothing started"
);
assert!(
!app.run_debug.feedback_is_error && !app.status.contains("input"),
"and no error: {:?} / {}",
app.run_debug.feedback,
app.status
);
launch_named_1424(&mut app, "Typo");
assert!(app.list_picker.is_none() && app.input_prompt.is_none());
assert!(app.run_debug.feedback_is_error);
assert!(
app.status.contains("${input:nope}") && app.status.contains("no \"inputs\" entry"),
"{}",
app.status
);
assert!(app.pending_debug_launch.is_none() && app.debug_sessions.is_empty());
launch_named_1424(&mut app, "Secret");
let prompt = app.input_prompt.as_ref().expect("asks for the token");
assert!(prompt.masked, "a password is not shown");
}
#[test]
fn a_task_asks_its_input_and_quotes_the_answer() {
let tmp = inputs_workspace_1424();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let task = crate::tasks::discover_tasks(tmp.path())
.into_iter()
.find(|t| t.label == "greet")
.unwrap();
assert!(app.run_project_task(task).is_none(), "asks first");
assert_eq!(
app.input_prompt.as_ref().map(|p| p.value.as_str()),
Some("world")
);
app.input_prompt.as_mut().unwrap().value = String::from("a b; touch x");
app.submit_input_prompt();
let ran = app.last_task.clone().expect("the task ran");
assert_eq!(
app.task_command_line(&ran).unwrap(),
"echo 'a b; touch x'",
"{}",
app.status
);
}
#[test]
fn rerunning_a_task_asks_its_inputs_again() {
let tmp = inputs_workspace_1424();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let task = crate::tasks::discover_tasks(tmp.path())
.into_iter()
.find(|t| t.label == "greet")
.unwrap();
app.run_project_task(task);
app.input_prompt.as_mut().unwrap().value = String::from("first");
app.submit_input_prompt();
assert!(app.last_task.is_some(), "precondition: the task ran");
app.rerun_last_task();
assert_eq!(
app.input_prompt.as_ref().map(|p| p.value.as_str()),
Some("world"),
"asked again, prefilled with the default: {}",
app.status
);
}
#[test]
fn compound_members_resolve_with_the_compound_answers() {
let tmp = inputs_workspace_1424();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let mut cfg = crate::dap::configs::discover_configs(tmp.path())
.into_iter()
.find(|c| c.name == "Run with args")
.unwrap();
cfg.answers
.insert(String::from("envName"), String::from("dev"));
cfg.answers
.insert(String::from("userName"), String::from("bob"));
assert!(
matches!(app.launch_member_into_set(&cfg), MemberOutcome::TaskUnrun),
"the member saw its answers: {}",
app.status
);
}
fn pick_debug_row(app: &mut App, label: &str) {
app.open_debug_config_picker();
let idx = app
.list_picker
.as_ref()
.expect("picker open")
.rows
.iter()
.position(|r| r.label.starts_with(label))
.unwrap_or_else(|| panic!("{label} is listed"));
app.list_picker.as_mut().unwrap().selected = idx;
app.confirm_list_picker();
}
fn compound_workspace() -> tempfile::TempDir {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "build", "type": "shell", "command": "true" }
]}"#,
)
.unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch", "program": "s.py" },
{ "name": "Client", "type": "python", "request": "launch", "program": "c.py" }
],
"compounds": [
{ "name": "Tasked", "configurations": ["Server"], "preLaunchTask": "build" },
{ "name": "Both", "configurations": ["Server", "Client"] }
]}"#,
)
.unwrap();
tmp
}
#[test]
fn restart_relaunches_the_selected_compound_not_a_stale_config() {
let tmp = compound_workspace();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.selected_debug_config = Some(String::from("Client"));
pick_debug_row(&mut app, "Tasked");
assert_eq!(app.selected_debug_compound.as_deref(), Some("Tasked"));
assert_eq!(
app.selected_debug_config, None,
"the compound replaces the old target"
);
app.refresh_run_debug();
assert_eq!(app.run_debug.selected_config.as_deref(), Some("Tasked"));
app.pending_debug_launch = None;
app.debug_restart();
assert!(
app.pending_debug_launch.is_some() && app.status.contains("compound preLaunchTask"),
"restart re-runs the compound behind its own task: {}",
app.status
);
pick_debug_row(&mut app, "Server");
assert_eq!(app.selected_debug_compound, None);
assert_eq!(app.selected_debug_config.as_deref(), Some("Server"));
}
#[test]
fn a_direct_compound_launch_drops_an_older_parked_task() {
let tmp = compound_workspace();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
pick_debug_row(&mut app, "Tasked");
assert!(
app.pending_debug_launch.is_some(),
"the task-gated launch is parked"
);
pick_debug_row(&mut app, "Both");
assert!(
app.pending_debug_launch.is_none(),
"the newer compound launch supersedes the parked one"
);
}
#[test]
fn a_zero_config_launch_drops_an_older_parked_task() {
let tmp = compound_workspace();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
pick_debug_row(&mut app, "Tasked");
assert!(app.pending_debug_launch.is_some());
pick_debug_row(&mut app, "Debug active file");
assert!(app.pending_debug_launch.is_none());
}
fn stub_emitting(messages: &[&str]) -> crate::dap::session::DapSession {
let mut script = String::new();
for m in messages {
script.push_str(&format!(
"printf 'Content-Length: %d\\r\\n\\r\\n%s' {} '{}'; ",
m.len(),
m
));
}
script.push_str("sleep 30");
crate::dap::session::DapSession::launch_with(
"sh",
&[String::from("-c"), script],
std::path::Path::new("."),
serde_json::json!({"seq": 2, "type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("sh spawns")
}
fn poll_until(app: &mut App, done: impl Fn(&App) -> bool) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
while !done(app) && std::time::Instant::now() < deadline {
app.poll_dap();
std::thread::sleep(std::time::Duration::from_millis(20));
}
}
#[test]
fn the_raised_exceptions_toggle_follows_the_adapters_filters() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"Node",
stub_emitting(&[
r#"{"seq":1,"type":"response","request_seq":1,"success":true,"command":"initialize","body":{"exceptionBreakpointFilters":[{"filter":"all","label":"Caught Exceptions"},{"filter":"uncaught","label":"Uncaught Exceptions"}]}}"#,
]),
);
poll_until(&mut app, |a| {
a.debug_sessions
.focused()
.is_some_and(|s| !s.capabilities.is_null())
});
app.debug_toggle_raised_exceptions();
assert_eq!(app.status, "Breaking on raised exceptions (and uncaught)");
assert_eq!(
app.debug_sessions.focused().unwrap().exception_filters,
vec![String::from("uncaught"), String::from("all")]
);
app.debug_toggle_raised_exceptions();
assert_eq!(app.status, "Breaking on uncaught exceptions only");
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"Go",
stub_emitting(&[
r#"{"seq":1,"type":"response","request_seq":1,"success":true,"command":"initialize","body":{"supportsConditionalBreakpoints":true}}"#,
]),
);
poll_until(&mut app, |a| {
a.debug_sessions
.focused()
.is_some_and(|s| !s.capabilities.is_null())
});
app.debug_toggle_raised_exceptions();
assert_eq!(
app.status,
"This debugger has no filter for raised exceptions; exception breakpoints are unchanged"
);
assert_eq!(
app.debug_sessions.focused().unwrap().exception_filters,
vec![String::from("uncaught")]
);
}
#[test]
fn the_raised_exceptions_toggle_before_initialize_uses_the_adapters_ids() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"LLDB",
stub_emitting(&[
r#"{"seq":1,"type":"response","request_seq":1,"success":true,"command":"initialize","body":{"exceptionBreakpointFilters":[{"filter":"cpp_catch","label":"C++ Catch"},{"filter":"cpp_throw","label":"C++ Throw"},{"filter":"objc_catch","label":"Objective-C Catch"},{"filter":"objc_throw","label":"Objective-C Throw"}]}}"#,
]),
);
app.debug_toggle_raised_exceptions();
assert!(app.status.starts_with("Breaking on raised exceptions"));
poll_until(&mut app, |a| {
a.debug_sessions
.focused()
.is_some_and(|s| !s.capabilities.is_null())
});
assert_eq!(
app.debug_sessions.focused().unwrap().exception_filters,
vec![String::from("cpp_throw"), String::from("objc_throw")]
);
}
#[test]
fn an_expensive_scope_shows_collapsed_and_opens_on_click() {
use crate::widgets::run_debug::DebugRowKind;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"Node",
stub_emitting(&[
r#"{"seq":1,"type":"event","event":"stopped","body":{"reason":"breakpoint","threadId":1}}"#,
r#"{"seq":2,"type":"response","request_seq":9,"success":true,"command":"scopes","body":{"scopes":[{"name":"Local: work","variablesReference":10,"expensive":false},{"name":"Global","variablesReference":20,"expensive":true}]}}"#,
]),
);
poll_until(&mut app, |a| {
a.debug_sessions
.focused()
.is_some_and(|s| s.scopes.len() == 2)
});
app.refresh_debug_panel();
let scope_rows = |app: &App| -> Vec<(String, Option<bool>)> {
app.run_debug
.debug_rows
.iter()
.filter_map(|r| match &r.kind {
DebugRowKind::Scope { name, open, .. } => Some((name.clone(), *open)),
_ => None,
})
.collect()
};
assert_eq!(
scope_rows(&app),
vec![
(String::from("Local: work"), None),
(String::from("Global"), Some(false)),
]
);
let global = app
.run_debug
.debug_rows
.iter()
.position(|r| matches!(&r.kind, DebugRowKind::Scope { name, .. } if name == "Global"))
.unwrap();
app.debug_panel_click(global);
assert_eq!(
scope_rows(&app),
vec![
(String::from("Local: work"), None),
(String::from("Global"), Some(true)),
]
);
assert!(
app.debug_sessions
.focused()
.unwrap()
.open_expensive_scopes
.contains(&(String::from("Global"), 0))
);
app.debug_panel_click(global);
assert_eq!(scope_rows(&app)[1], (String::from("Global"), Some(false)));
let local = app
.run_debug
.debug_rows
.iter()
.position(|r| matches!(&r.kind, DebugRowKind::Scope { name, .. } if name == "Local: work"))
.unwrap();
app.debug_panel_click(local);
assert_eq!(scope_rows(&app)[0], (String::from("Local: work"), None));
}
#[test]
fn expensive_scopes_sharing_a_name_open_separately() {
use crate::widgets::run_debug::DebugRowKind;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"Node",
stub_emitting(&[
r#"{"seq":1,"type":"event","event":"stopped","body":{"reason":"breakpoint","threadId":1}}"#,
r#"{"seq":2,"type":"response","request_seq":9,"success":true,"command":"scopes","body":{"scopes":[{"name":"Global","variablesReference":20,"expensive":true},{"name":"Global","variablesReference":30,"expensive":true}]}}"#,
]),
);
poll_until(&mut app, |a| {
a.debug_sessions
.focused()
.is_some_and(|s| s.scopes.len() == 2)
});
app.refresh_debug_panel();
let scope_rows = |app: &App| -> Vec<Option<bool>> {
app.run_debug
.debug_rows
.iter()
.filter_map(|r| match &r.kind {
DebugRowKind::Scope { open, .. } => Some(*open),
_ => None,
})
.collect()
};
assert_eq!(scope_rows(&app), vec![Some(false), Some(false)]);
let second = app
.run_debug
.debug_rows
.iter()
.position(|r| matches!(&r.kind, DebugRowKind::Scope { index: 1, .. }))
.unwrap();
app.debug_panel_click(second);
assert_eq!(scope_rows(&app), vec![Some(false), Some(true)]);
}
fn copy_value_adapter(dir: &Path, clipboard: bool) -> crate::dap::session::DapSession {
let script = format!(
r#"
import json, sys
CLIP = {clip}
seq = [1000]
def read():
length = None
while True:
line = sys.stdin.buffer.readline()
if not line:
return None
line = line.strip()
if not line:
break
if line.lower().startswith(b"content-length:"):
length = int(line.split(b":")[1])
return json.loads(sys.stdin.buffer.read(length))
def send(msg):
seq[0] += 1
msg["seq"] = seq[0]
body = json.dumps(msg).encode()
sys.stdout.buffer.write(b"Content-Length: %d\r\n\r\n" % len(body) + body)
sys.stdout.buffer.flush()
def reply(req, body=None):
send({{"type": "response", "request_seq": req["seq"], "success": True,
"command": req["command"], "body": body or {{}}}})
while True:
req = read()
if req is None:
break
c = req["command"]
if c == "initialize":
reply(req, {{"supportsClipboardContext": CLIP}})
send({{"type": "event", "event": "initialized"}})
elif c == "configurationDone":
reply(req)
send({{"type": "event", "event": "stopped", "body": {{"reason": "breakpoint", "threadId": 1}}}})
elif c == "threads":
reply(req, {{"threads": [{{"id": 1, "name": "MainThread"}}]}})
elif c == "stackTrace":
reply(req, {{"stackFrames": [
{{"id": 11, "name": "main", "source": {{"path": "/w/s.py"}}, "line": 6}},
{{"id": 12, "name": "<module>", "source": {{"path": "/w/s.py"}}, "line": 9}}]}})
elif c == "scopes":
reply(req, {{"scopes": [{{"name": "Locals", "variablesReference": 5}}]}})
elif c == "variables":
reply(req, {{"variables": [{{"name": "payload", "value": "'{{\"user\": \"ada\"...", "type": "str",
"variablesReference": 0, "evaluateName": "payload"}}]}})
elif c == "evaluate":
ctx = req["arguments"].get("context")
expr = req["arguments"].get("expression")
if ctx != "clipboard":
result = "CUT"
elif expr == "payload":
result = "'{{\"user\": \"ada\", \"token\": \"FULL\"}}'"
else:
result = "FULL " + expr
reply(req, {{"result": result, "variablesReference": 0}})
elif c == "disconnect":
reply(req)
break
else:
reply(req)
"#,
clip = if clipboard { "True" } else { "False" }
);
let path = dir.join("fake_dap.py");
std::fs::write(&path, script).unwrap();
crate::dap::session::DapSession::launch_with(
"python3",
&[path.display().to_string()],
dir,
serde_json::json!({"type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("python3 spawns")
}
fn app_paused_with_a_payload(dir: &Path, clipboard: bool) -> Option<App> {
if std::process::Command::new("python3")
.arg("--version")
.output()
.is_err()
{
return None;
}
let mut app = App::new(dir.to_path_buf()).unwrap();
app.debug_sessions
.push("P", copy_value_adapter(dir, clipboard));
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the stop and its variables",
|| {
app.poll_dap();
app.refresh_debug_panel();
debug_row_index(&app, "payload").is_some()
},
);
Some(app)
}
fn debug_row_index(app: &App, name: &str) -> Option<usize> {
use crate::widgets::run_debug::DebugRowKind;
app.run_debug.debug_rows.iter().position(|r| match &r.kind {
DebugRowKind::Variable { name: n, .. } => n == name,
DebugRowKind::Watch { expression, .. } => expression == name,
DebugRowKind::Frame { name: n, .. } => n == name,
_ => false,
})
}
fn debug_menu_labels(items: &[(String, MenuAction)]) -> Vec<&str> {
items.iter().map(|(label, _)| label.as_str()).collect()
}
fn run_debug_menu_entry(app: &mut App, idx: usize, label: &str) {
let action = app
.debug_row_menu(idx)
.into_iter()
.find(|(l, _)| l == label)
.unwrap_or_else(|| panic!("no {label} entry"))
.1;
let root = app.tree.root.clone();
app.dispatch_menu_action(action, root);
}
#[test]
fn a_variable_row_offers_copy_value_copy_as_expression_and_add_to_watch() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), true) else {
return;
};
let idx = debug_row_index(&app, "payload").unwrap();
let items = app.debug_row_menu(idx);
app.debug_stop();
assert_eq!(
debug_menu_labels(&items),
["Copy Value", "Copy as Expression", "Add to Watch"]
);
}
#[test]
fn copy_value_copies_the_whole_value_from_the_adapter() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), true) else {
return;
};
crate::clipboard::test_clip::mock_write("before");
let idx = debug_row_index(&app, "payload").unwrap();
run_debug_menu_entry(&mut app, idx, "Copy Value");
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the clipboard evaluate",
|| {
app.poll_dap();
crate::clipboard::test_clip::mock_read().as_deref() != Some("before")
},
);
let copied = crate::clipboard::test_clip::mock_read();
let status = app.status.clone();
app.debug_stop();
assert_eq!(
copied.as_deref(),
Some(r#"'{"user": "ada", "token": "FULL"}'"#)
);
assert!(status.starts_with("Copied value"), "status: {status}");
}
#[test]
fn the_latest_copy_value_wins_over_an_earlier_pending_one() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), true) else {
return;
};
crate::clipboard::test_clip::mock_write("before");
let idx = debug_row_index(&app, "payload").unwrap();
run_debug_menu_entry(&mut app, idx, "Copy Value");
app.copy_debug_value(Some(String::from("other")), String::from("shown"));
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"both clipboard evaluates",
|| {
app.poll_dap();
crate::clipboard::test_clip::mock_read().as_deref() != Some("before")
},
);
let copied = crate::clipboard::test_clip::mock_read();
app.debug_stop();
assert_eq!(copied.as_deref(), Some("FULL other"));
}
#[test]
fn copy_value_without_clipboard_support_copies_the_shown_value() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), false) else {
return;
};
crate::clipboard::test_clip::mock_write("before");
let idx = debug_row_index(&app, "payload").unwrap();
run_debug_menu_entry(&mut app, idx, "Copy Value");
let copied = crate::clipboard::test_clip::mock_read();
app.debug_stop();
assert_eq!(copied.as_deref(), Some(r#"'{"user": "ada"..."#));
}
#[test]
fn copy_as_expression_and_add_to_watch_use_the_evaluate_name() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), true) else {
return;
};
crate::clipboard::test_clip::mock_write("before");
let idx = debug_row_index(&app, "payload").unwrap();
run_debug_menu_entry(&mut app, idx, "Copy as Expression");
let copied = crate::clipboard::test_clip::mock_read();
run_debug_menu_entry(&mut app, idx, "Add to Watch");
let watching = app.watch_exprs.clone();
let watch_row = debug_row_index(&app, "payload");
app.debug_stop();
assert_eq!(copied.as_deref(), Some("payload"));
assert_eq!(watching, ["payload"]);
assert!(watch_row.is_some(), "the WATCH section lists it");
}
#[test]
fn a_watch_row_can_be_copied_or_removed() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), true) else {
return;
};
app.add_watch_expression(String::from("len(payload)"));
app.refresh_debug_panel();
let idx = debug_row_index(&app, "len(payload)").unwrap();
let labels: Vec<String> = app
.debug_row_menu(idx)
.into_iter()
.map(|(l, _)| l)
.collect();
crate::clipboard::test_clip::mock_write("before");
run_debug_menu_entry(&mut app, idx, "Copy Expression");
let copied = crate::clipboard::test_clip::mock_read();
run_debug_menu_entry(&mut app, idx, "Remove Expression");
let watching = app.watch_exprs.clone();
app.debug_stop();
assert_eq!(
labels,
["Copy Value", "Copy Expression", "Remove Expression"]
);
assert_eq!(copied.as_deref(), Some("len(payload)"));
assert!(watching.is_empty());
}
#[test]
fn copy_call_stack_copies_every_frame() {
let tmp = tempfile::tempdir().unwrap();
let Some(mut app) = app_paused_with_a_payload(tmp.path(), true) else {
return;
};
crate::clipboard::test_clip::mock_write("before");
let idx = debug_row_index(&app, "<module>").unwrap();
run_debug_menu_entry(&mut app, idx, "Copy Call Stack");
let copied = crate::clipboard::test_clip::mock_read();
app.debug_stop();
assert_eq!(
copied.as_deref(),
Some("main (/w/s.py:6)\n<module> (/w/s.py:9)")
);
}
#[test]
fn rows_without_an_expression_offer_less_or_nothing() {
use crate::widgets::run_debug::{DebugRow, DebugRowKind};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.run_debug.debug_rows = vec![
DebugRow {
indent: 0,
kind: DebugRowKind::Header {
title: String::from("VARIABLES"),
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::Scope {
name: String::from("Locals"),
index: 0,
open: None,
},
},
DebugRow {
indent: 2,
kind: DebugRowKind::Variable {
reference: 0,
expandable: false,
expanded: false,
name: String::from("n"),
value: String::from("42"),
type_name: String::from("int"),
evaluate_name: None,
},
},
];
assert!(app.debug_row_menu(0).is_empty());
assert!(app.debug_row_menu(1).is_empty());
assert!(app.debug_row_menu(9).is_empty(), "past the last row");
assert_eq!(debug_menu_labels(&app.debug_row_menu(2)), ["Copy Value"]);
crate::clipboard::test_clip::mock_write("before");
run_debug_menu_entry(&mut app, 2, "Copy Value");
assert_eq!(
crate::clipboard::test_clip::mock_read().as_deref(),
Some("42"),
"no session: the shown value is copied as it is"
);
}
#[test]
fn a_background_members_output_reaches_the_console_with_its_name() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"A",
stub_emitting(&[
r#"{"seq":1,"type":"event","event":"output","body":{"category":"stdout","output":"hello from A\n"}}"#,
]),
);
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(1);
poll_until(&mut app, |a| {
a.debug_console
.iter()
.any(|l| l.text.contains("hello from A"))
});
assert!(
app.debug_console
.iter()
.any(|l| l.text == "[A] hello from A"),
"console: {:?}",
app.debug_console
);
assert_eq!(
app.debug_sessions.focused_name(),
Some("B"),
"output alone does not steal focus"
);
app.debug_stop();
}
#[test]
fn a_background_stop_moves_the_view_to_the_stopped_member() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"A",
stub_emitting(&[
r#"{"seq":1,"type":"event","event":"stopped","body":{"reason":"breakpoint","threadId":1}}"#,
]),
);
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(1);
poll_until(&mut app, |a| a.debug_sessions.focused_name() == Some("A"));
assert_eq!(app.debug_sessions.focused_name(), Some("A"));
assert!(app.status.contains("A stopped"), "status: {}", app.status);
assert_eq!(
app.run_debug.feedback.as_deref(),
Some("Paused (breakpoint)"),
"the stop replays through the normal handler once A is focused"
);
app.debug_stop();
}
const STOPPED_EVENT: &str =
r#"{"seq":1,"type":"event","event":"stopped","body":{"reason":"breakpoint","threadId":1}}"#;
#[test]
fn a_stop_shows_the_member_that_stopped_even_when_names_repeat() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(false));
app.debug_sessions
.push("A", stub_emitting(&[STOPPED_EVENT]));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(2);
poll_until(&mut app, |a| a.debug_sessions.focused_index() != 2);
assert_eq!(
app.debug_sessions.focused_index(),
1,
"the second A stopped"
);
app.debug_stop();
}
#[test]
fn a_stop_lands_on_the_right_member_when_an_earlier_one_ends_alongside() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push(
"Gone",
stub_emitting(&[r#"{"seq":1,"type":"event","event":"terminated"}"#]),
);
app.debug_sessions
.push("Stops", stub_emitting(&[STOPPED_EVENT]));
app.debug_sessions.push("Watched", stub_member(false));
app.debug_sessions.focus(2);
poll_until(&mut app, |a| {
a.debug_sessions.len() == 2 && a.debug_sessions.focused_name() == Some("Stops")
});
assert_eq!(app.debug_sessions.names(), vec!["Stops", "Watched"]);
assert_eq!(app.debug_sessions.focused_name(), Some("Stops"));
app.debug_stop();
}
#[test]
fn switch_debug_session_cycles_the_members() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(false));
app.switch_debug_session();
assert_eq!(app.status, "Only one debug session is running");
app.debug_sessions.push("B", stub_member(false));
assert_eq!(app.debug_sessions.focused_name(), Some("B"));
app.switch_debug_session();
assert_eq!(app.debug_sessions.focused_name(), Some("A"));
assert!(
app.status.starts_with("Debugging A"),
"status: {}",
app.status
);
app.switch_debug_session();
assert_eq!(app.debug_sessions.focused_name(), Some("B"));
assert!(is_switch_debug_session_key(key(
KeyCode::Char('g'),
KeyModifiers::SUPER | KeyModifiers::ALT | KeyModifiers::SHIFT,
)));
assert!(!is_switch_debug_session_key(key(
KeyCode::Char('g'),
KeyModifiers::SUPER | KeyModifiers::ALT,
)));
app.debug_stop();
}
#[test]
fn a_switched_to_member_replays_its_queued_stop() {
use crate::dap::session::DapEvent;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(false));
app.debug_sessions.push("B", stub_member(false));
let (_, a) = app
.debug_sessions
.iter_named_mut_indexed()
.find(|(i, _)| *i == 0)
.unwrap();
a.backlog.push(DapEvent::Stopped {
thread_id: 1,
reason: String::from("exception"),
});
app.poll_dap();
assert_ne!(
app.run_debug.feedback.as_deref(),
Some("Paused (exception)"),
"not replayed while A is in the background"
);
app.switch_debug_session();
app.poll_dap();
assert_eq!(
app.run_debug.feedback.as_deref(),
Some("Paused (exception)")
);
app.debug_stop();
}
#[test]
fn the_debug_console_is_mirrored_to_an_output_channel() {
use crate::dap::session::DapEvent;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let tag = format!("mirror-{}", std::process::id());
let printed = format!("{tag} {}", "wide ".repeat(40));
let event = format!(
r#"{{"seq":1,"type":"event","event":"output","body":{{"category":"stdout","output":"{printed}\n"}}}}"#
);
app.debug_sessions.push("P", stub_emitting(&[&event]));
let mirrored = |text: &str| {
crate::output::snapshot(crate::output::CHANNEL_DEBUG_CONSOLE)
.unwrap_or_default()
.iter()
.any(|l| l.text == text)
};
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the program's output to reach the Debug Console channel",
|| {
app.poll_dap();
mirrored(&printed)
},
);
let (_, p) = app
.debug_sessions
.iter_named_mut_indexed()
.find(|(i, _)| *i == 0)
.unwrap();
p.backlog.push(DapEvent::Evaluated {
context: String::from("repl"),
expression: format!("describe('{tag}')"),
result: format!("'{tag} described'"),
success: true,
});
app.poll_dap();
assert!(mirrored(&format!("❯ describe('{tag}')")), "the REPL echo");
assert!(mirrored(&format!("'{tag} described'")), "and its result");
app.debug_stop();
}
#[test]
fn the_debug_console_mirror_skips_telemetry_and_copies_each_line_once() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let tag = format!("mirror-once-{}", std::process::id());
let telemetry = format!(
r#"{{"seq":1,"type":"event","event":"output","body":{{"category":"telemetry","output":"{tag} ptvsd"}}}}"#
);
let printed = format!(
r#"{{"seq":2,"type":"event","event":"output","body":{{"category":"stdout","output":"{tag} printed\n"}}}}"#
);
app.debug_sessions
.push("P", stub_emitting(&[&telemetry, &printed]));
let count = |text: &str| {
crate::output::snapshot(crate::output::CHANNEL_DEBUG_CONSOLE)
.unwrap_or_default()
.iter()
.filter(|l| l.text == text)
.count()
};
let line = format!("{tag} printed");
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the printed line to reach the Debug Console channel",
|| {
app.poll_dap();
count(&line) > 0
},
);
for _ in 0..5 {
app.poll_dap();
}
let channel = crate::output::snapshot(crate::output::CHANNEL_DEBUG_CONSOLE).unwrap_or_default();
let in_panel = app.debug_console.iter().filter(|l| l.text == line).count();
app.debug_stop();
assert_eq!(count(&line), 1, "once");
assert!(
!channel
.iter()
.any(|l| l.text.contains(&format!("{tag} ptvsd"))),
"telemetry is not mirrored"
);
assert_eq!(in_panel, 1, "and the panel keeps its own copy");
}
fn exception_stop_adapter(dir: &Path, answer_at_once: bool) -> crate::dap::session::DapSession {
let script = format!(
r#"
import json, sys
AT_ONCE = {at_once}
seq = [1000]
held = []
def read():
length = None
while True:
line = sys.stdin.buffer.readline()
if not line:
return None
line = line.strip()
if not line:
break
if line.lower().startswith(b"content-length:"):
length = int(line.split(b":")[1])
return json.loads(sys.stdin.buffer.read(length))
def send(msg):
seq[0] += 1
msg["seq"] = seq[0]
body = json.dumps(msg).encode()
sys.stdout.buffer.write(b"Content-Length: %d\r\n\r\n" % len(body) + body)
sys.stdout.buffer.flush()
def reply(req, body=None):
send({{"type": "response", "request_seq": req["seq"], "success": True,
"command": req["command"], "body": body or {{}}}})
def answer(req):
reply(req, {{"exceptionId": "KeyError", "description": "'x'"}})
while True:
req = read()
if req is None:
break
c = req["command"]
if c == "initialize":
reply(req, {{"supportsExceptionInfoRequest": True}})
send({{"type": "event", "event": "initialized"}})
elif c == "configurationDone":
reply(req)
send({{"type": "event", "event": "stopped", "body": {{"reason": "exception", "threadId": 1}}}})
elif c == "exceptionInfo":
if AT_ONCE:
answer(req)
else:
held.append(req)
elif c == "continue":
reply(req)
for h in held:
answer(h)
send({{"type": "event", "event": "output", "body": {{"category": "stdout", "output": "MARKER\n"}}}})
elif c == "disconnect":
reply(req)
break
else:
reply(req)
"#,
at_once = if answer_at_once { "True" } else { "False" }
);
let path = dir.join("fake_dap.py");
std::fs::write(&path, script).unwrap();
crate::dap::session::DapSession::launch_with(
"python3",
&[path.display().to_string()],
dir,
serde_json::json!({"type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("python3 spawns")
}
#[test]
fn an_exception_answer_after_continue_does_not_say_paused() {
use crate::dap::session::SessionPhase;
if std::process::Command::new("python3")
.arg("--version")
.output()
.is_err()
{
return;
}
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions
.push("P", exception_stop_adapter(tmp.path(), false));
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the exception stop",
|| {
app.poll_dap();
app.debug_sessions
.focused()
.is_some_and(|s| s.phase == SessionPhase::Stopped)
},
);
app.debug_start_or_continue();
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the late answer and the marker after it",
|| {
app.poll_dap();
app.debug_console.iter().any(|l| l.text == "MARKER")
},
);
let status = app.status.clone();
let feedback = app.run_debug.feedback.clone().unwrap_or_default();
app.debug_stop();
assert!(!status.contains("Paused"), "status: {status}");
assert!(!feedback.contains("KeyError"), "feedback: {feedback}");
}
#[test]
fn the_step_commands_are_in_the_palette_and_bindable() {
use crate::widgets::command_palette::{ALL_COMMANDS, Command};
for (title, id) in [
("Debug: Continue", "continue"),
("Debug: Step Into", "step_into"),
("Debug: Step Out", "step_out"),
("Debug: Step Into Target", "step_into_target"),
] {
let cmd = Command::from_id(id).unwrap_or_else(|| panic!("no command {id}"));
assert_eq!(cmd.title(), title);
assert!(ALL_COMMANDS.contains(&cmd));
}
assert_eq!(
Command::from_id("step_into").unwrap().keybinding_hint(),
"F11"
);
assert_eq!(
Command::from_id("step_out").unwrap().keybinding_hint(),
"Shift+F11"
);
}
#[test]
fn step_into_target_steps_into_the_picked_call() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let log = tmp.path().join("requests.log");
let mut session = crate::dap::session::DapSession::launch_with(
"sh",
&[String::from("-c"), format!("cat > '{}'", log.display())],
std::path::Path::new("."),
serde_json::json!({"seq": 2, "type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("sh spawns");
session.phase = crate::dap::session::SessionPhase::Stopped;
session.stopped_thread = Some(1);
app.debug_sessions.push("app.py", session);
let sent = |needle: &str| {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while std::time::Instant::now() < deadline {
if std::fs::read_to_string(&log).is_ok_and(|s| s.contains(needle)) {
return true;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
false
};
assert!(run_palette_row_titled(&mut app, "Debug: Step Out"));
assert!(sent("\"command\":\"stepOut\""));
app.offer_step_in_targets(vec![
(1, String::from("load('data.csv')")),
(2, String::from("scale(2)")),
(3, String::from("transform(...)")),
]);
let picker = app
.list_picker
.as_ref()
.expect("several calls open a picker");
assert_eq!(picker.title, "Step Into Target");
let labels: Vec<&str> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["load('data.csv')", "scale(2)", "transform(...)"]);
app.handle_key(key(KeyCode::Down, KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(
sent("\"targetId\":2"),
"{:?}",
std::fs::read_to_string(&log)
);
}
#[test]
fn one_or_no_step_in_target_needs_no_picker() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let log = tmp.path().join("requests.log");
let mut session = crate::dap::session::DapSession::launch_with(
"sh",
&[String::from("-c"), format!("cat > '{}'", log.display())],
std::path::Path::new("."),
serde_json::json!({"seq": 2, "type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("sh spawns");
session.phase = crate::dap::session::SessionPhase::Stopped;
session.stopped_thread = Some(1);
app.debug_sessions.push("app.py", session);
app.offer_step_in_targets(vec![(9, String::from("only()"))]);
assert!(app.list_picker.is_none());
app.offer_step_in_targets(Vec::new());
assert!(app.list_picker.is_none());
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
let mut text = String::new();
while std::time::Instant::now() < deadline {
text = std::fs::read_to_string(&log).unwrap_or_default();
if text.matches("\"command\":\"stepIn\"").count() >= 2 {
break;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
assert!(text.contains("\"targetId\":9"), "{text}");
assert_eq!(text.matches("\"command\":\"stepIn\"").count(), 2, "{text}");
}
#[test]
fn continue_with_no_session_starts_nothing() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
assert!(run_palette_row_titled(&mut app, "Debug: Continue"));
assert!(app.debug_sessions.is_empty());
assert_eq!(app.status, "No debug session to continue");
}
#[test]
fn an_exception_answer_while_stopped_names_the_exception() {
if std::process::Command::new("python3")
.arg("--version")
.output()
.is_err()
{
return;
}
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions
.push("P", exception_stop_adapter(tmp.path(), true));
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the exception to be named",
|| {
app.poll_dap();
app.status.contains("KeyError")
},
);
let feedback = app.run_debug.feedback.clone();
let status = app.status.clone();
app.debug_stop();
assert_eq!(status, "Paused on exception: KeyError: 'x'");
assert_eq!(
feedback.as_deref(),
Some("Paused (exception): KeyError: 'x'")
);
}
#[test]
fn breakpoint_items_list_every_breakpoint_by_file_then_line() {
let (_tmp, app, _a, _b) = app_with_breakpoints();
let items = app.breakpoint_items();
let shown: Vec<(String, Option<String>)> = items
.iter()
.map(|i| (i.label.clone(), i.detail.clone()))
.collect();
assert_eq!(
shown,
vec![
(String::from("a.rs:1"), None),
(String::from("a.rs:3"), Some(String::from("if n > 2"))),
(String::from("b.rs:4"), Some(String::from("log at {i}"))),
]
);
}
#[test]
fn removing_from_the_list_drops_the_breakpoint_and_its_condition() {
let (_tmp, mut app, a, _b) = app_with_breakpoints();
app.sync_breakpoint_list();
let idx = app
.run_debug
.breakpoints
.iter()
.position(|i| i.label == "a.rs:3")
.unwrap();
app.breakpoint_list_click(idx, true);
assert!(!app.editor.breakpoints[&a].contains(&3));
assert!(
app.editor.breakpoints[&a].contains(&1),
"only that one goes"
);
assert!(!app.editor.breakpoint_conditions[&a].contains_key(&3));
assert_eq!(app.run_debug.breakpoints.len(), 2, "the list follows");
}
#[test]
fn clicking_a_breakpoint_opens_its_file_at_the_line() {
let (_tmp, mut app, a, _b) = app_with_breakpoints();
app.sync_breakpoint_list();
let idx = app
.run_debug
.breakpoints
.iter()
.position(|i| i.label == "a.rs:1")
.unwrap();
app.breakpoint_list_click(idx, false);
assert_eq!(app.editor.path.as_deref(), Some(a.as_path()));
assert_eq!(app.editor.cursor_row, 0, "line 1, 0-based");
}
#[test]
fn a_go_connect_to_server_attach_reaches_the_headless_delve() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let rc = resolved_attach(
r#"{ "configurations": [ { "name": "Bare", "type": "go", "request": "attach" } ] }"#,
);
app.launch_resolved_config(rc);
assert!(
app.status.contains(
r#"config "Bare": a go attach needs a "processId", or "mode": "remote" with a "port""#
),
"{}",
app.status
);
let rc = resolved_attach(
r#"{ "configurations": [ { "name": "No port", "type": "go", "request": "attach", "mode": "remote" } ] }"#,
);
app.launch_resolved_config(rc);
assert!(
app.status
.contains(r#"config "No port": a go remote attach needs a "port""#),
"{}",
app.status
);
assert!(app.debug_sessions.is_empty());
let (port, server) = crate::dap::session::tests::headless_delve(2);
let rc = resolved_attach(&format!(
r#"{{ "configurations": [ {{ "name": "Connect to server", "type": "go", "request": "attach",
"mode": "remote", "host": "127.0.0.1", "port": {port} }} ] }}"#
));
app.launch_resolved_config(rc);
let got = server.join().unwrap();
assert_eq!(got[1]["command"], "attach");
assert_eq!(got[1]["arguments"]["mode"], "remote");
assert!(!app.debug_sessions.is_empty(), "{}", app.status);
}
#[test]
fn a_python_remote_attach_talks_to_the_listening_port() {
let (port, server) = crate::dap::session::tests::listening_adapter(1);
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
format!(
r#"{{ "configurations": [ {{ "name": "Python: Remote Attach", "type": "debugpy",
"request": "attach", "connect": {{ "host": "127.0.0.1", "port": {port} }} }} ] }}"#
),
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let cfg = crate::dap::configs::discover_configs(tmp.path()).remove(0);
app.launch_debug_config(&cfg);
assert!(
!app.debug_sessions.is_empty() && !app.run_debug.feedback_is_error,
"{}",
app.status
);
assert!(
app.status.contains("Attached: Python: Remote Attach"),
"{}",
app.status
);
let got = server.join().unwrap();
assert_eq!(
got.first().map(|m| m["command"].clone()),
Some(serde_json::json!("initialize")),
"croft's own initialize reached the port"
);
app.debug_stop();
}
fn resolved_attach(json: &str) -> crate::dap::configs::ResolvedConfig {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(tmp.path().join(".vscode/launch.json"), json).unwrap();
let cfg = crate::dap::configs::discover_configs(tmp.path())
.into_iter()
.next()
.expect("one config");
let ctx = crate::dap::configs::SubstCtx {
workspace_folder: tmp.path().to_path_buf(),
file: None,
inputs: Default::default(),
folders: Vec::new(),
};
crate::dap::configs::resolve(&cfg, &ctx).expect("resolves")
}
#[test]
fn an_lldb_attach_needs_a_pid_a_program_or_attach_commands() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let rc = resolved_attach(
r#"{ "configurations": [ { "name": "Nothing", "type": "lldb", "request": "attach" } ] }"#,
);
app.launch_resolved_config(rc);
if lldb_dap_path().is_none() {
assert!(app.debug_sessions.is_empty());
return;
}
assert!(
app.status.contains(
r#"config "Nothing": an lldb attach needs a numeric "processId", a "program" or "attachCommands""#
),
"{}",
app.status
);
assert!(app.debug_sessions.is_empty());
for config in [
r#"{ "name": "By name", "type": "lldb", "request": "attach", "program": "/no/such/app", "waitFor": true }"#,
r#"{ "name": "Remote", "type": "lldb", "request": "attach", "attachCommands": ["gdb-remote 127.0.0.1:9"] }"#,
] {
let rc = resolved_attach(&format!(r#"{{ "configurations": [ {config} ] }}"#));
app.launch_resolved_config(rc);
assert!(!app.status.contains("needs a numeric"), "{}", app.status);
assert!(!app.debug_sessions.is_empty(), "{config} starts a session");
app.debug_stop();
}
}
#[test]
fn a_pick_process_attach_opens_the_process_picker() {
use crate::widgets::list_picker::ListPurpose;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let rc = resolved_attach(
r#"{ "configurations": [ { "name": "Attach", "type": "lldb", "request": "attach",
"processId": "${command:pickProcess}" } ] }"#,
);
app.launch_resolved_config(rc);
let picker = app.list_picker.as_ref().expect("the picker opens");
assert!(matches!(picker.purpose, ListPurpose::AttachProcess));
assert!(
!picker.rows.is_empty(),
"this test process's siblings are listed"
);
assert!(
picker.rows.iter().all(|r| r.id.parse::<i64>().is_ok()),
"every row id is a pid"
);
assert!(
!picker
.rows
.iter()
.any(|r| r.id == std::process::id().to_string()),
"croft does not offer itself"
);
assert!(
app.debug_sessions.is_empty(),
"nothing launched before the pick"
);
let (rc, _) = app.take_pending_attach(4242).expect("the attach is parked");
assert_eq!(rc.process_id, Some(4242));
assert!(
app.take_pending_attach(4242).is_none(),
"taken once, not twice"
);
}
#[test]
fn pick_process_on_a_non_lldb_config_is_refused() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let rc = resolved_attach(
r#"{ "configurations": [ { "name": "Py", "type": "python", "request": "attach",
"processId": "${command:pickProcess}" } ] }"#,
);
app.launch_resolved_config(rc);
assert!(app.list_picker.is_none());
assert!(
app.status.contains("pickProcess")
|| app
.run_debug
.feedback
.as_deref()
.unwrap_or("")
.contains("pickProcess"),
"status: {} / feedback: {:?}",
app.status,
app.run_debug.feedback
);
}
#[test]
fn the_internal_stop_before_a_relaunch_runs_no_post_debug_task() {
let (_tmp, mut app) = app_with_cleanup_task();
app.debug_sessions.push("A", stub_member(false));
app.debug_post_tasks = vec![String::from("cleanup")];
app.debug_stop();
assert_eq!(last_task_label(&app), None);
}
#[test]
fn stop_all_true_stops_the_set_when_a_background_member_ends() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(true));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(1);
app.debug_stop_all = true;
poll_until_shrinks(&mut app, 2);
assert!(
app.debug_sessions.is_empty(),
"A ending takes B with it: {:?}",
app.debug_sessions.names()
);
assert!(
app.status.contains("stopAll"),
"and says why: {}",
app.status
);
}
#[test]
fn stop_all_false_leaves_the_siblings_running() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(true));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(1);
poll_until_shrinks(&mut app, 2);
assert_eq!(app.debug_sessions.names(), vec!["B"]);
app.debug_stop();
}
#[test]
fn a_session_ending_on_its_own_says_so_on_the_status_line() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("out.py", stub_member(true));
app.status = String::from("Debugging out.py — F5 continue · F10 step over · Shift+F5 stop");
poll_until_shrinks(&mut app, 1);
assert!(app.debug_sessions.is_empty(), "the stub terminated");
assert_eq!(
app.run_debug.feedback.as_deref(),
Some("Debug session ended")
);
assert_eq!(app.status, "Debug session ended");
}
fn loop_stop_adapter(dir: &Path) -> crate::dap::session::DapSession {
stop_adapter(dir, false)
}
fn stop_adapter(dir: &Path, sourceless_step: bool) -> crate::dap::session::DapSession {
let app_py = dir.join("app.py");
std::fs::write(
&app_py,
"total = 0\nfor n in range(3):\n total += n\nprint(total)\n",
)
.unwrap();
let script = format!(
r#"
import json, sys
seq = [1000]
stepped = [False]
sourceless = {sourceless}
def read():
length = None
while True:
line = sys.stdin.buffer.readline()
if not line:
return None
line = line.strip()
if not line:
break
if line.lower().startswith(b"content-length:"):
length = int(line.split(b":")[1])
return json.loads(sys.stdin.buffer.read(length))
def send(msg):
seq[0] += 1
msg["seq"] = seq[0]
body = json.dumps(msg).encode()
sys.stdout.buffer.write(b"Content-Length: %d\r\n\r\n" % len(body) + body)
sys.stdout.buffer.flush()
def reply(req, body=None):
send({{"type": "response", "request_seq": req["seq"], "success": True,
"command": req["command"], "body": body or {{}}}})
def stop(reason):
send({{"type": "event", "event": "stopped", "body": {{"reason": reason, "threadId": 1}}}})
while True:
req = read()
if req is None:
break
c = req["command"]
if c == "initialize":
reply(req)
send({{"type": "event", "event": "initialized"}})
elif c == "configurationDone":
reply(req)
stop("breakpoint")
elif c == "threads":
reply(req, {{"threads": [{{"id": 1, "name": "main"}}]}})
elif c == "stackTrace":
if sourceless and stepped[0]:
reply(req, {{"stackFrames": [{{"id": 2, "name": "memcpy", "line": 0, "column": 0}}],
"totalFrames": 1}})
else:
reply(req, {{"stackFrames": [{{"id": 1, "name": "<module>", "line": 3, "column": 1,
"source": {{"path": {path}}}}}], "totalFrames": 1}})
elif c == "next":
stepped[0] = True
reply(req)
stop("step")
elif c == "continue":
reply(req)
stop("breakpoint")
elif c == "disconnect":
reply(req)
break
else:
reply(req)
"#,
path = serde_json::to_string(&app_py.display().to_string()).unwrap(),
sourceless = if sourceless_step { "True" } else { "False" },
);
let path = dir.join("loop_dap.py");
std::fs::write(&path, script).unwrap();
crate::dap::session::DapSession::launch_with(
"python3",
&[path.display().to_string()],
dir,
serde_json::json!({"type": "request", "command": "launch", "arguments": {}}),
std::collections::BTreeMap::new(),
)
.expect("python3 spawns")
}
fn has_python3() -> bool {
std::process::Command::new("python3")
.arg("--version")
.output()
.is_ok()
}
fn await_a11y_event(app: &mut App, seen: Option<u64>) -> String {
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the pause's announcement",
|| {
app.poll_dap();
app.a11y_event.as_ref().map(|(n, _)| *n) > seen
},
);
app.update_announcer();
app.announcer.line.clone()
}
#[test]
fn a_breakpoint_pause_is_read_out_with_its_file_line_and_text() {
if !has_python3() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = true;
app.debug_sessions.push("P", loop_stop_adapter(tmp.path()));
let said = await_a11y_event(&mut app, None);
app.debug_stop();
assert!(
said.contains("Paused on breakpoint, app.py line 3: total += n"),
"said: {said}"
);
}
#[test]
fn every_stop_is_read_even_on_the_same_line() {
if !has_python3() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = true;
app.debug_sessions.push("P", loop_stop_adapter(tmp.path()));
await_a11y_event(&mut app, None);
let first = app.a11y_event.as_ref().map(|(n, _)| *n);
app.debug_step("next");
let said = await_a11y_event(&mut app, first);
assert!(
said.contains("Paused on step, app.py line 3"),
"said: {said}"
);
let stepped = app.a11y_event.as_ref().map(|(n, _)| *n);
app.debug_start_or_continue();
assert_eq!(
app.a11y_event.as_ref().map(|(_, e)| e.as_str()),
Some("Continued")
);
app.update_announcer();
assert!(
app.announcer.line.contains("Continued"),
"{}",
app.announcer.line
);
let continued = app.a11y_event.as_ref().map(|(n, _)| *n);
assert!(continued > stepped);
let said = await_a11y_event(&mut app, continued);
app.debug_stop();
assert!(
said.contains("Paused on breakpoint, app.py line 3: total += n"),
"said: {said}"
);
}
#[test]
fn a_sourceless_stop_is_not_read_with_the_last_stops_location() {
if !has_python3() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = true;
app.debug_sessions.push("P", stop_adapter(tmp.path(), true));
await_a11y_event(&mut app, None);
let first = app.a11y_event.as_ref().map(|(n, _)| *n);
app.debug_step("next");
let said = await_a11y_event(&mut app, first);
app.debug_stop();
assert!(said.contains("Paused on step, in memcpy"), "said: {said}");
assert!(!said.contains("app.py"), "said: {said}");
}
#[test]
fn a_pause_with_screen_reader_mode_off_is_not_kept() {
if !has_python3() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = false;
app.debug_sessions.push("P", loop_stop_adapter(tmp.path()));
crate::test_budget::await_spawned(std::time::Duration::from_millis(500), "the stop", || {
app.poll_dap();
app.editor.stop_line.is_some()
});
app.debug_stop();
assert_eq!(app.a11y_event, None);
}
#[test]
fn a_finished_run_all_is_read_out_with_its_first_failure() {
use crate::testing::model::{Activity, TestCase, TestStatus};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = true;
app.testing.on_busy_started(Activity::Running);
for (name, status) in [
("m::a", TestStatus::Passed),
("m::b", TestStatus::Failed),
("m::c", TestStatus::Skipped),
] {
app.testing.apply_case(TestCase {
name: name.to_string(),
status,
});
}
app.testing.on_finished(Some(false));
app.tick_test_watch();
app.update_announcer();
assert!(
app.announcer
.line
.contains("Tests finished: 1 failed, 1 passed, 1 skipped. First failure: m::b"),
"said: {}",
app.announcer.line
);
}
#[test]
fn a_run_with_a_status_outcome_adds_only_its_first_failure() {
use crate::testing::model::{TestCase, TestStatus};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = true;
app.testing.start_filter("m::");
app.status = String::from("Running suite m");
app.await_test_run_outcome(String::from("suite m"));
app.testing.apply_case(TestCase {
name: String::from("m::b"),
status: TestStatus::Failed,
});
app.testing.on_finished(Some(false));
app.tick_test_watch();
assert!(app.status.starts_with("suite m failed"), "{}", app.status);
assert_eq!(
app.a11y_event.as_ref().map(|(_, e)| e.as_str()),
Some("First failure: m::b")
);
}
#[test]
fn a_run_that_never_hit_its_breakpoint_says_so_on_the_status_line() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor
.breakpoints
.entry(tmp.path().join("out.py"))
.or_default()
.insert(1);
app.debug_sessions.push("out.py", stub_member(true));
app.status = String::from("Debugging out.py — F5 continue · F10 step over · Shift+F5 stop");
poll_until_shrinks(&mut app, 1);
assert!(app.debug_sessions.is_empty(), "the stub terminated");
assert_eq!(app.status, "Program exited without hitting a breakpoint");
}
#[test]
fn a_status_set_after_the_debug_session_ended_is_kept() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("out.py", stub_member(true));
poll_until_shrinks(&mut app, 1);
assert!(app.debug_sessions.is_empty(), "the stub terminated");
app.status = String::from("Saved out.py");
for _ in 0..5 {
app.poll_dap();
}
assert_eq!(app.status, "Saved out.py");
}
#[test]
fn stop_all_keeps_its_explanation_when_the_focused_member_ends() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(true));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(0);
app.debug_stop_all = true;
poll_until_shrinks(&mut app, 2);
assert!(
app.debug_sessions.is_empty(),
"{:?}",
app.debug_sessions.names()
);
assert_eq!(
app.status,
"A ended — stopAll stopped the rest of the compound"
);
}
#[test]
fn a_refused_compound_does_not_hand_its_stop_all_to_the_running_set() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{"version":"0.2.0","configurations":[],
"compounds":[{"name":"Bad","configurations":["Missing"],"stopAll":true}]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(true));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(1);
let compound = crate::dap::configs::discover_compounds(tmp.path())
.into_iter()
.find(|c| c.name == "Bad")
.expect("the compound parses");
assert!(compound.stop_all);
app.launch_compound(&compound);
assert_eq!(
app.debug_sessions.len(),
2,
"the refusal leaves the set running"
);
poll_until_shrinks(&mut app, 2);
assert_eq!(
app.debug_sessions.names(),
vec!["B"],
"A ending must not take B: the running set never asked for stopAll"
);
app.debug_stop();
}
#[test]
fn a_background_member_ending_rebuilds_the_report_from_the_live_set() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(true));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(1);
app.debug_compound = Some(String::from("Stack"));
app.run_debug.feedback = Some(String::from("Debugging compound Stack: A, B"));
poll_until_shrinks(&mut app, 2);
assert_eq!(app.debug_sessions.names(), vec!["B"]);
assert_eq!(
app.run_debug.feedback.as_deref(),
Some("Debugging compound Stack: B (A ended)")
);
assert!(
app.status.starts_with("A ended — showing B; running: B"),
"{}",
app.status
);
app.debug_stop();
assert_eq!(app.debug_compound, None, "a full stop forgets the compound");
}
#[test]
fn the_focused_member_ending_names_the_session_now_shown() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("A", stub_member(true));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.focus(0);
app.debug_compound = Some(String::from("Stack"));
poll_until_shrinks(&mut app, 2);
assert_eq!(app.debug_sessions.names(), vec!["B"]);
assert!(
app.status.starts_with("A ended — showing B; running: B"),
"{}",
app.status
);
app.debug_stop();
}
#[test]
fn a_hit_count_prompt_sets_the_breakpoints_hit_condition() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.py", "x = 1\ny = 2\n");
app.editor.cursor_row = 1;
app.run_command(crate::widgets::command_palette::Command::DebugAddHitCountBreakpoint);
let path = app.editor.path.clone().unwrap();
assert!(matches!(
app.prompt.as_ref().map(|p| &p.kind),
Some(PromptKind::HitCondition { line: 2, .. })
));
app.commit_hit_condition(path.clone(), 2, " 5 ");
let lines = app.editor.breakpoints.get(&path).cloned().unwrap();
assert!(lines.contains(&2), "the breakpoint exists");
let specs = app.editor.source_breakpoints(&path, &lines);
assert_eq!(specs[0].hit_condition.as_deref(), Some("5"));
app.commit_hit_condition(path.clone(), 2, "");
let specs = app.editor.source_breakpoints(&path, &lines);
assert_eq!(specs[0].hit_condition, None, "blank clears it");
}
#[test]
fn function_breakpoints_are_kept_for_the_next_session() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.commit_function_breakpoint(String::from("main"));
app.commit_function_breakpoint(String::from("main"));
assert_eq!(
app.function_breakpoints,
vec![String::from("main")],
"no duplicates"
);
app.debug_clear_function_breakpoints();
assert!(app.function_breakpoints.is_empty());
}
#[test]
fn every_new_debug_session_carries_the_function_breakpoints() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.commit_function_breakpoint(String::from("apply_discount"));
app.adopt_debug_session("orders.py", stub_member(false), LaunchSlot::Replace);
let want = vec![String::from("apply_discount")];
assert_eq!(
app.debug_sessions.focused().unwrap().function_breakpoints,
want
);
app.adopt_debug_session("worker", stub_member(false), LaunchSlot::Add);
assert_eq!(app.debug_sessions.len(), 2, "a compound member is added");
for session in app.debug_sessions.iter_mut() {
assert_eq!(session.function_breakpoints, want);
}
}
#[test]
fn run_to_cursor_without_a_session_says_so() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.py", "x = 1\n");
let ctrl_f10 = crossterm::event::KeyEvent::new(KeyCode::F(10), KeyModifiers::CONTROL);
assert!(is_run_to_cursor_key(ctrl_f10));
assert!(!is_run_to_cursor_key(crossterm::event::KeyEvent::new(
KeyCode::F(10),
KeyModifiers::NONE
)));
app.debug_run_to_cursor();
assert_eq!(app.status, "Run to Cursor: no debug session");
}
const EXITED_EVENT: &str = r#"{"seq":1,"type":"event","event":"exited","body":{"exitCode":0}}"#;
const TERMINATED_EVENT: &str = r#"{"seq":2,"type":"event","event":"terminated"}"#;
#[test]
fn a_member_that_exits_and_terminates_in_one_poll_is_removed_once() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions
.push("A", stub_emitting(&[EXITED_EVENT, TERMINATED_EVENT]));
app.debug_sessions.push("B", stub_member(false));
app.debug_sessions.push("C", stub_member(false));
app.debug_sessions.focus(2);
std::thread::sleep(std::time::Duration::from_millis(300));
poll_until(&mut app, |a| {
!a.debug_sessions.names().contains(&String::from("A"))
});
assert_eq!(
app.debug_sessions.names(),
vec![String::from("B"), String::from("C")]
);
app.debug_stop();
}
#[test]
fn a_focused_member_that_ends_as_another_stops_is_still_removed() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions
.push("S", stub_emitting(&[STOPPED_EVENT]));
app.debug_sessions
.push("F", stub_emitting(&[TERMINATED_EVENT]));
app.debug_sessions.focus(1);
std::thread::sleep(std::time::Duration::from_millis(300));
poll_until(&mut app, |a| {
a.debug_sessions.names() == vec![String::from("S")]
});
assert_eq!(
app.debug_sessions.names(),
vec![String::from("S")],
"F ended and must go"
);
app.debug_stop();
}
#[cfg(unix)]
#[test]
fn debugging_a_codeql_query_stops_at_its_first_breakpoint() {
use crate::widgets::command_palette::Command;
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let mut app = codeql_query_fixture(
tmp.path(),
"import python\n\npredicate isCall(Call c) { exists(c) }\n\nfrom Call c\nwhere isCall(c)\nselect c\n",
);
app.codeql_program = fake_query_server(
bin.path(),
r#"{"resultType": 0, "message": "", "evaluationTime": 1}"#,
"\"c\"\n",
);
let query = tmp.path().join("q.ql");
app.editor
.breakpoints
.entry(query.clone())
.or_default()
.insert(7);
app.run_command(Command::CodeqlDebugQuery);
assert!(
app.status
.starts_with("No predicate at the breakpoint on line 7"),
"{}",
app.status
);
app.editor
.breakpoints
.entry(query.clone())
.or_default()
.insert(6);
app.run_command(Command::CodeqlDebugQuery);
assert_eq!(
app.status,
"Running Quick evaluation of isCall (breakpoint on line 6) on app\u{2026}"
);
wait_for_codeql(&mut app);
let requests = std::fs::read_to_string(bin.path().join("requests.jsonl")).unwrap();
let run: serde_json::Value =
serde_json::from_str(requests.lines().last().unwrap()).unwrap();
assert_eq!(
run["params"]["body"]["target"]["quickEval"]["quickEvalPos"]["line"],
6
);
assert_eq!(
run["params"]["body"]["target"]["quickEval"]["quickEvalPos"]["column"],
7
);
app.editor.open(&query).unwrap();
app.editor.breakpoints.clear();
app.run_command(Command::CodeqlDebugQuery);
assert_eq!(app.status, "Running q.ql on app\u{2026}");
wait_for_codeql(&mut app);
});
}
#[test]
fn a_refused_debug_launch_says_why() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let refused = r#"{"seq":1,"type":"response","request_seq":2,"success":false,"command":"launch","message":"Failed to launch","body":{"error":{"id":3000,"format":"Failed to launch /w: Go version go1.24.7 is too old for this version of Delve"}}}"#;
app.debug_sessions.push("go", stub_emitting(&[refused]));
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"the refusal to end the session",
|| {
app.poll_dap();
app.debug_sessions.is_empty()
},
);
assert!(
app.status
.starts_with("Could not start debugging: Failed to launch /w: Go version"),
"{}",
app.status
);
assert!(app.run_debug.feedback_is_error);
assert!(
app.run_debug
.feedback
.as_deref()
.is_some_and(|f| f.contains("too old for this version of Delve"))
);
}
fn debug_until_local(app: &mut App, local: &str, secs: u64) -> Option<String> {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(secs);
while std::time::Instant::now() < deadline {
app.drain_terminal_bells();
app.poll_dap();
if let Some(v) = app
.debug_sessions
.focused()
.and_then(|s| s.lookup_local(local))
{
return Some(v.value.clone());
}
std::thread::sleep(std::time::Duration::from_millis(50));
}
None
}
#[test]
#[ignore = "requires ~/.croft/debug-venv (uv + debugpy)"]
fn a_launch_json_python_config_passes_args_and_env_file() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
std::fs::write(
root.join("app.py"),
"import os, sys\nwho = sys.argv[1]\ngreeting = os.environ[\"GREETING\"]\nprint(f\"{greeting}, {who}\")\n",
)
.unwrap();
std::fs::write(root.join(".env"), "GREETING=hi from envFile\n").unwrap();
std::fs::create_dir_all(root.join(".vscode")).unwrap();
std::fs::write(
root.join(".vscode/launch.json"),
r#"{ "version": "0.2.0", "configurations": [
// Written for VS Code: comments, and keys croft does not map.
{ "name": "Py", "type": "debugpy", "request": "launch",
"program": "${workspaceFolder}/app.py", "args": ["croft"],
"envFile": "${workspaceFolder}/.env", "console": "integratedTerminal",
"justMyCode": true }
]}"#,
)
.unwrap();
let mut app = App::new(root.clone()).unwrap();
app.editor
.breakpoints
.entry(root.join("app.py"))
.or_default()
.insert(4);
pick_debug_row(&mut app, "Py");
let greeting = debug_until_local(&mut app, "greeting", 90);
let who = app
.debug_sessions
.focused()
.and_then(|s| s.lookup_local("who"))
.map(|v| v.value.clone());
app.debug_stop();
assert_eq!(
greeting.as_deref(),
Some("'hi from envFile'"),
"status {:?}, feedback {:?}",
app.status,
app.run_debug.feedback
);
assert_eq!(who.as_deref(), Some("'croft'"));
}
#[test]
#[ignore = "requires ~/.croft/debug-venv (uv + debugpy)"]
fn f5_debugs_a_script_against_the_project_venv_and_names_its_exception() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
let made = std::process::Command::new("python3")
.args(["-m", "venv", "--without-pip"])
.arg(root.join(".venv"))
.output()
.unwrap();
assert!(
made.status.success(),
"{}",
String::from_utf8_lossy(&made.stderr)
);
let site = std::process::Command::new(root.join(".venv/bin/python"))
.args([
"-c",
"import sysconfig; print(sysconfig.get_path('purelib'))",
])
.output()
.unwrap();
let site = PathBuf::from(String::from_utf8(site.stdout).unwrap().trim());
std::fs::write(site.join("croftdep.py"), "VALUE = 42\n").unwrap();
let file = root.join("main.py");
std::fs::write(
&file,
"import croftdep\nvalue = croftdep.VALUE\nraise ValueError(f\"boom {value}\")\n",
)
.unwrap();
let mut app = App::new(root.clone()).unwrap();
app.editor.open(&file).unwrap();
app.editor
.breakpoints
.entry(file.clone())
.or_default()
.insert(3);
app.debug_start_or_continue();
let value = debug_until_local(&mut app, "value", 90);
assert_eq!(
value.as_deref(),
Some("42"),
"status {:?}, feedback {:?}",
app.status,
app.run_debug.feedback
);
app.debug_start_or_continue();
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
while std::time::Instant::now() < deadline
&& !app
.run_debug
.feedback
.as_deref()
.is_some_and(|f| f.contains("ValueError"))
{
app.poll_dap();
std::thread::sleep(std::time::Duration::from_millis(50));
}
let feedback = app.run_debug.feedback.clone();
let status = app.status.clone();
app.debug_stop();
assert_eq!(
feedback.as_deref(),
Some("Paused (exception): ValueError: boom 42"),
"status {status:?}"
);
assert!(status.contains("ValueError: boom 42"), "{status}");
}
fn real_venv(dir: &Path) -> PathBuf {
let made = std::process::Command::new("python3")
.args(["-m", "venv", "--without-pip"])
.arg(dir)
.output()
.unwrap();
assert!(
made.status.success(),
"{}",
String::from_utf8_lossy(&made.stderr)
);
let site = std::process::Command::new(dir.join("bin/python"))
.args([
"-c",
"import sysconfig; print(sysconfig.get_path('purelib'))",
])
.output()
.unwrap();
PathBuf::from(String::from_utf8(site.stdout).unwrap().trim())
}
#[test]
#[ignore = "requires ~/.croft/debug-venv (uv + debugpy)"]
fn f5_names_a_project_python_older_than_3_14() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
real_venv(&root.join(".venv"));
let version = std::process::Command::new(root.join(".venv/bin/python"))
.args(["-c", "import sys; print('%d.%d' % sys.version_info[:2])"])
.output()
.unwrap();
let version = String::from_utf8(version.stdout)
.unwrap()
.trim()
.to_string();
let (major, minor) = version.split_once('.').unwrap();
let minor: u32 = minor.parse().unwrap();
if major != "3" || !(10..14).contains(&minor) {
eprintln!("python3 is {version}: nothing to name");
return;
}
let file = root.join("main.py");
std::fs::write(&file, "x = 1\n").unwrap();
let mut app = App::new(root.clone()).unwrap();
app.editor.open(&file).unwrap();
app.debug_start_or_continue();
let started = !app.debug_sessions.is_empty();
let status = app.status.clone();
let console = app.debug_console.clone();
app.debug_stop();
let want = format!("Debugging main.py under Python {version} (.venv); attach needs 3.14+");
assert!(started, "{status}");
assert!(status.starts_with(&want), "{status}");
assert!(
status.contains("F10 step over"),
"the key hints stay: {status}"
);
assert_eq!(
console.first().map(|l| l.text.as_str()),
Some(want.as_str()),
"{console:?}"
);
}
#[test]
#[ignore = "requires ~/.croft/debug-venv (uv + debugpy)"]
fn debug_test_at_cursor_runs_pytest_under_the_nearest_venv() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
std::fs::write(root.join("pytest.ini"), "[pytest]\n").unwrap();
let svc = root.join("svc");
std::fs::create_dir_all(&svc).unwrap();
let site = real_venv(&svc.join("venv"));
let pytest = site.join("pytest");
std::fs::create_dir_all(&pytest).unwrap();
std::fs::write(pytest.join("__init__.py"), "").unwrap();
let main = pytest.join("__main__.py");
std::fs::write(&main, "import sys\nprefix = sys.prefix\nprint(prefix)\n").unwrap();
let test = svc.join("test_app.py");
std::fs::write(&test, "def test_prefix():\n assert True\n").unwrap();
let mut app = App::new(root.clone()).unwrap();
app.editor.open(&test).unwrap();
app.editor.cursor_row = 1;
app.editor
.breakpoints
.entry(main.clone())
.or_default()
.insert(3);
app.debug_test_at_cursor();
let prefix = debug_until_local(&mut app, "prefix", 90);
let status = app.status.clone();
app.debug_stop();
let prefix = prefix.unwrap_or_else(|| panic!("no stop in pytest; status {status:?}"));
assert!(prefix.contains("svc/venv"), "sys.prefix {prefix}");
}
#[test]
#[ignore = "requires cargo and lldb-dap"]
fn a_launch_json_rust_config_builds_first_and_passes_args_and_env() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
std::fs::create_dir_all(root.join("src")).unwrap();
std::fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"t\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[workspace]\n",
)
.unwrap();
std::fs::write(
root.join("src/main.rs"),
"fn main() {\n let n: i32 = std::env::args().nth(1).unwrap().parse().unwrap();\n \
let k: i32 = std::env::var(\"K\").unwrap().parse().unwrap();\n \
println!(\"{}\", n + k);\n}\n",
)
.unwrap();
std::fs::create_dir_all(root.join(".vscode")).unwrap();
std::fs::write(
root.join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "cargo build", "type": "shell", "command": "cargo build" }
]}"#,
)
.unwrap();
std::fs::write(
root.join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Rs", "type": "lldb", "request": "launch",
"program": "${workspaceFolder}/target/debug/t", "args": ["35"],
"env": { "K": "7" }, "cwd": "${workspaceFolder}",
"preLaunchTask": "cargo build", "sourceLanguages": ["rust"] }
]}"#,
)
.unwrap();
let mut app = App::new(root.clone()).unwrap();
app.editor
.breakpoints
.entry(root.join("src/main.rs"))
.or_default()
.insert(4);
pick_debug_row(&mut app, "Rs");
assert!(
app.pending_debug_launch.is_some(),
"the launch waits for the build: {}",
app.status
);
let k = debug_until_local(&mut app, "k", 240);
let n = app
.debug_sessions
.focused()
.and_then(|s| s.lookup_local("n"))
.map(|v| v.value.clone());
app.debug_stop();
assert!(
root.join("target/debug/t").is_file(),
"the preLaunchTask built the binary"
);
assert_eq!(
k.as_deref(),
Some("7"),
"status {:?}, feedback {:?}",
app.status,
app.run_debug.feedback
);
assert_eq!(n.as_deref(), Some("35"));
}
#[test]
#[ignore = "requires node and ~/.croft/js-debug"]
fn a_launch_json_node_attach_config_attaches_by_port() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
std::fs::write(
root.join("server.js"),
"let n = 0;\nsetInterval(() => {\n n += 1;\n const label = `tick ${n}`;\n if (label === '') console.log(label);\n}, 100);\n",
)
.unwrap();
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let mut node = std::process::Command::new("node")
.arg(format!("--inspect=127.0.0.1:{port}"))
.arg(root.join("server.js"))
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("node runs");
std::fs::create_dir_all(root.join(".vscode")).unwrap();
std::fs::write(
root.join(".vscode/launch.json"),
format!(
r#"{{ "configurations": [
{{ "name": "Attach", "type": "node", "request": "attach",
"port": {port}, "skipFiles": ["<node_internals>/**"] }}
]}}"#
),
)
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(500));
let mut app = App::new(root.clone()).unwrap();
app.editor
.breakpoints
.entry(root.join("server.js"))
.or_default()
.insert(5);
pick_debug_row(&mut app, "Attach");
let label = debug_until_local(&mut app, "label", 90);
app.debug_stop();
let _ = node.kill();
let _ = node.wait();
let label = label.unwrap_or_else(|| {
panic!(
"no stop: status {:?}, feedback {:?}",
app.status, app.run_debug.feedback
)
});
assert!(label.contains("tick "), "{label}");
}
#[test]
fn a_parked_js_launch_keeps_the_workspace_folder_it_was_resolved_against() {
use crate::dap::configs;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("alpha");
let b = tmp.path().join("beta");
std::fs::create_dir_all(a.join(".vscode")).unwrap();
std::fs::create_dir(&b).unwrap();
std::fs::write(b.join("other.js"), "1;\n").unwrap();
std::fs::write(
a.join(".vscode/tasks.json"),
r#"{ "version": "2.0.0", "tasks": [
{ "label": "tsc", "type": "shell", "command": "true" }
]}"#,
)
.unwrap();
let mut app = App::new(a.clone()).unwrap();
app.add_workspace_folder(b.clone());
let root = app.active_workspace_root();
let cfg = configs::parse_launch_json(
r#"{ "configurations": [
{ "name": "TS", "type": "node", "request": "launch",
"program": "${workspaceFolder}/dist/main.js", "preLaunchTask": "tsc" }
]}"#,
"launch.json",
)
.remove(0);
app.launch_debug_config(&cfg);
let parked = app
.pending_debug_launch
.take()
.expect("the preLaunchTask parked the launch");
let crate::app::PendingLaunchStep::Resolved(rc) = parked.step else {
panic!("a config's own task parks the resolved config");
};
app.editor.path = Some(b.canonicalize().unwrap().join("other.js"));
assert_ne!(
app.active_workspace_root(),
root,
"staging: the active root moved"
);
let req =
configs::with_workspace_folder(configs::js_request(&rc), &app.active_workspace_root());
assert_eq!(
req["arguments"]["__workspaceFolder"],
serde_json::json!(root.to_string_lossy()),
"the launch carries the root the config was resolved against"
);
}
fn debug_console_after(outputs: &[&str], done: impl Fn(&App) -> bool) -> App {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let events: Vec<String> = outputs
.iter()
.enumerate()
.map(|(i, out)| {
format!(
r#"{{"seq":{},"type":"event","event":"output","body":{{"category":"stdout","output":"{out}"}}}}"#,
i + 1
)
})
.collect();
let refs: Vec<&str> = events.iter().map(String::as_str).collect();
app.debug_sessions.push("P", stub_emitting(&refs));
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the program's output to reach the Debug Console",
|| {
app.poll_dap();
done(&app)
},
);
app.debug_stop();
app
}
fn console_line<'a>(app: &'a App, text: &str) -> &'a crate::ansi_text::AnsiLine {
app.debug_console
.iter()
.find(|l| l.text == text)
.unwrap_or_else(|| panic!("no {text:?} in {:?}", app.debug_console))
}
fn console_fg(line: &crate::ansi_text::AnsiLine, at: usize) -> Option<crate::ansi_text::AnsiColor> {
line.spans
.iter()
.find(|s| s.start <= at && at < s.end)
.and_then(|s| s.style.fg)
}
#[test]
fn the_debug_console_shows_colour_codes_as_colours() {
let tag = format!("ansi-{}", std::process::id());
let printed = format!("FAILED tests/test_a.py::test_x - {tag}");
let app = debug_console_after(
&[&format!(
r"\u001b[31mFAILED\u001b[0m tests/test_a.py::\u001b[1mtest_x\u001b[0m - {tag}\n"
)],
|a| a.debug_console.iter().any(|l| l.text.contains(&tag)),
);
let line = console_line(&app, &printed);
use crate::ansi_text::AnsiColor;
assert_eq!(
console_fg(line, 0),
Some(AnsiColor::Indexed(1)),
"FAILED is red"
);
assert_eq!(console_fg(line, 7), None, "the reset ends it");
let bold = line
.spans
.iter()
.find(|s| &line.text[s.start..s.end] == "test_x")
.expect("test_x is its own run");
assert!(bold.style.bold, "{:?}", line.spans);
assert!(
crate::output::snapshot(crate::output::CHANNEL_DEBUG_CONSOLE)
.unwrap_or_default()
.iter()
.any(|l| l.text == printed),
"OUTPUT's Debug Console channel gets the plain text"
);
}
#[test]
fn debug_console_colour_carries_across_lines_and_cut_events() {
let tag = format!("carry-{}", std::process::id());
let app = debug_console_after(
&[r"\u001b[3", &format!(r"2m{tag} ok\nstill\u001b[0m plain\n")],
|a| a.debug_console.iter().any(|l| l.text.contains("plain")),
);
use crate::ansi_text::AnsiColor;
let first = console_line(&app, &format!("{tag} ok"));
assert_eq!(console_fg(first, 0), Some(AnsiColor::Indexed(2)));
let second = console_line(&app, "still plain");
assert_eq!(
console_fg(second, 0),
Some(AnsiColor::Indexed(2)),
"carried"
);
assert_eq!(console_fg(second, 6), None, "until the reset");
assert!(
!app.debug_console
.iter()
.any(|l| l.text.contains('\u{1b}') || l.text.contains("[3")),
"{:?}",
app.debug_console
);
}
#[test]
fn debug_console_plain_output_and_croft_lines_stay_plain() {
use crate::dap::session::DapEvent;
let tag = format!("plain-{}", std::process::id());
let mut app = debug_console_after(
&[&format!(r"[31m is just text {tag}\n\u001b[35mleft on\n")],
|a| a.debug_console.iter().any(|l| l.text.ends_with("left on")),
);
let plain = console_line(&app, &format!("[31m is just text {tag}"));
assert_eq!(
plain.spans.iter().map(|s| s.style).collect::<Vec<_>>(),
vec![crate::ansi_text::AnsiStyle::default()]
);
app.debug_sessions.push("Q", stub_emitting(&[]));
let (_, q) = app
.debug_sessions
.iter_named_mut_indexed()
.find(|(i, _)| *i == 0)
.unwrap();
q.backlog.push(DapEvent::Evaluated {
context: String::from("repl"),
expression: String::from("1 + 1"),
result: String::from("2"),
success: true,
});
app.poll_dap();
app.debug_stop();
let echo = console_line(&app, "❯ 1 + 1");
assert_eq!(console_fg(echo, 0), None, "{:?}", echo.spans);
assert_eq!(console_fg(console_line(&app, "2"), 0), None);
}
#[test]
fn debug_console_sessions_keep_their_own_colour_and_cut_escape() {
let tag = format!("streams-{}", std::process::id());
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let a_event = format!(
r#"{{"seq":1,"type":"event","event":"output","body":{{"category":"stdout","output":"\u001b[31mred from A {tag}\n\u001b[3"}}}}"#
);
app.debug_sessions.push("A", stub_emitting(&[&a_event]));
let a_line = format!("red from A {tag}");
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"session A's output to reach the Debug Console",
|| {
app.poll_dap();
app.debug_console.iter().any(|l| l.text == a_line)
},
);
let b_event = format!(
r#"{{"seq":1,"type":"event","event":"output","body":{{"category":"stdout","output":"hello from B {tag}\n"}}}}"#
);
app.debug_sessions.push("B", stub_emitting(&[&b_event]));
let b_line = format!("hello from B {tag}");
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"session B's output to reach the Debug Console",
|| {
app.poll_dap();
app.debug_console
.iter()
.any(|l| l.text.contains(&format!("from B {tag}")))
},
);
app.debug_stop();
let b = console_line(&app, &b_line);
assert_eq!(console_fg(b, 0), None, "A's red stays in A: {:?}", b.spans);
use crate::ansi_text::AnsiColor;
assert_eq!(
console_fg(console_line(&app, &a_line), 0),
Some(AnsiColor::Indexed(1)),
"A's own line is red"
);
}
fn console_from<'a>(app: &'a App, tag: &str) -> Vec<&'a str> {
let lines: Vec<&str> = app.debug_console.iter().map(|l| l.text.as_str()).collect();
let start = lines
.iter()
.position(|l| l.contains(tag))
.unwrap_or_else(|| panic!("no {tag:?} in {lines:?}"));
lines[start..].to_vec()
}
fn mirrored(text: &str) -> usize {
crate::output::snapshot(crate::output::CHANNEL_DEBUG_CONSOLE)
.unwrap_or_default()
.iter()
.filter(|l| l.text == text)
.count()
}
#[test]
fn debug_console_joins_a_line_cut_across_events_and_keeps_blank_lines() {
let tag = format!("chunks-{}", std::process::id());
let app = debug_console_after(
&[
&format!("Loading {tag}..."),
r" ok\n",
r"\n",
r"Section 2\n",
"print: i =",
r" 0\n",
],
|a| a.debug_console.iter().any(|l| l.text == "print: i = 0"),
);
let loaded = format!("Loading {tag}... ok");
assert_eq!(
console_from(&app, &tag),
[loaded.as_str(), "", "Section 2", "print: i = 0"]
);
assert_eq!(mirrored(&loaded), 1, "OUTPUT gets the whole line");
assert_eq!(mirrored(&format!("Loading {tag}...")), 0);
}
#[test]
fn debug_console_redraws_a_progress_line_in_place() {
let tag = format!("progress-{}", std::process::id());
let app = debug_console_after(
&[
&format!(r"\r 10% {tag}"),
&format!(r"\r 60% {tag}"),
&format!(r"\r100% {tag}\n"),
&format!("waiting {tag}"),
],
|a| {
a.debug_console
.iter()
.any(|l| l.text.starts_with("waiting"))
},
);
let done = format!("100% {tag}");
let waiting = format!("waiting {tag}");
assert_eq!(console_from(&app, &tag), [done.as_str(), waiting.as_str()]);
assert_eq!(
mirrored(&format!(" 60% {tag}")),
0,
"a redraw is not a line"
);
assert_eq!(mirrored(&waiting), 1, "the open line is flushed at the end");
}
#[test]
fn debug_console_keeps_categories_apart_in_a_background_member() {
let tag = format!("categories-{}", std::process::id());
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let event = |seq: usize, category: &str, output: &str| {
format!(
r#"{{"seq":{seq},"type":"event","event":"output","body":{{"category":"{category}","output":"{output}"}}}}"#
)
};
let events = [
event(1, "stdout", &format!("Loading {tag}...")),
event(2, "stderr", &format!(r"warning {tag}\n")),
event(3, "stdout", r" ok\n"),
];
let refs: Vec<&str> = events.iter().map(String::as_str).collect();
app.debug_sessions.push("A", stub_emitting(&refs));
app.debug_sessions.push("B", stub_emitting(&[]));
let loaded = format!("[A] Loading {tag}... ok");
crate::test_budget::await_spawned(
std::time::Duration::from_millis(500),
"the background member's line to be finished",
|| {
app.poll_dap();
app.debug_console.iter().any(|l| l.text == loaded)
},
);
app.debug_stop();
let warning = format!("[A] warning {tag}");
assert_eq!(
console_from(&app, &tag),
[loaded.as_str(), warning.as_str()]
);
}
#[test]
fn debug_console_shows_every_line_of_a_repl_result() {
use crate::dap::session::DapEvent;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.debug_sessions.push("P", stub_emitting(&[]));
let (_, p) = app.debug_sessions.iter_named_mut_indexed().next().unwrap();
p.backlog.push(DapEvent::Evaluated {
context: String::from("repl"),
expression: String::from("inventory['missing']"),
result: String::from(
"Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\n\nKeyError: 'missing'\n",
),
success: false,
});
app.poll_dap();
app.debug_stop();
assert_eq!(
console_from(&app, "inventory"),
[
"❯ inventory['missing']",
"Traceback (most recent call last):",
" File \"<string>\", line 1, in <module>",
"",
"KeyError: 'missing'",
]
);
}
#[test]
fn a_node_launch_with_nothing_to_run_names_what_it_needs() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [ { "name": "Bare", "type": "node", "request": "launch" } ] }"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let cfg = crate::dap::configs::discover_configs(tmp.path()).remove(0);
app.launch_debug_config(&cfg);
assert!(app.run_debug.feedback_is_error);
assert!(
app.status.contains(
"config \"Bare\": a node launch needs a \"program\", a \"runtimeExecutable\" or a \"command\""
),
"{}",
app.status
);
assert!(app.debug_sessions.is_empty());
}
#[test]
fn a_browser_runs_headless_only_where_there_is_no_display() {
let none = |_: &str| false;
let x11 = |k: &str| k == "DISPLAY";
let wayland = |k: &str| k == "WAYLAND_DISPLAY";
assert!(crate::app::no_display(true, true, x11), "over SSH");
assert!(
crate::app::no_display(false, true, none),
"Linux, no display"
);
assert!(!crate::app::no_display(false, true, x11));
assert!(!crate::app::no_display(false, true, wayland));
assert!(
!crate::app::no_display(false, false, none),
"macOS has a display with no DISPLAY set"
);
}
#[test]
fn a_cargo_launch_builds_first_and_a_bare_one_is_refused() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "type": "lldb", "request": "launch", "name": "Debug executable 'rc'",
"cargo": { "args": ["build", "--bin=rc", "--package=rc"],
"filter": { "name": "rc", "kind": "bin" } },
"cwd": "${workspaceFolder}" },
{ "type": "lldb", "request": "launch", "name": "Bare" }
]}"#,
)
.unwrap();
let launch = |name: &str| {
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let cfg = crate::dap::configs::discover_configs(tmp.path())
.into_iter()
.find(|c| c.name == name)
.unwrap();
app.launch_debug_config(&cfg);
app
};
let app = launch("Debug executable 'rc'");
assert!(
!app.pending_cargo_launches.is_empty() && !app.run_debug.feedback_is_error,
"{}",
app.status
);
assert!(
app.status
.contains("building with cargo build --bin=rc --package=rc"),
"{}",
app.status
);
let app = launch("Bare");
assert!(app.pending_cargo_launches.is_empty());
assert!(app.run_debug.feedback_is_error);
assert!(
app.status.contains("an lldb launch needs a \"program\""),
"{}",
app.status
);
}
fn cmake_debug_project(root: &Path) -> PathBuf {
for (rel, text) in [
(
"include/geom.hpp",
"#pragma once\nnamespace geom { double twice(double); }\n",
),
(
"src/geom.cpp",
"#include \"geom.hpp\"\nnamespace geom { double twice(double x) { return 2*x; } }\n",
),
(
"src/main.cpp",
"#include <cstdio>\n#include \"geom.hpp\"\nint main() { std::printf(\"%f\\n\", geom::twice(2)); }\n",
),
(
"CMakeLists.txt",
"cmake_minimum_required(VERSION 3.10)\nproject(cp CXX)\nset(CMAKE_EXPORT_COMPILE_COMMANDS ON)\nadd_executable(app src/main.cpp src/geom.cpp)\ntarget_include_directories(app PRIVATE include)\n",
),
] {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(&p, text).unwrap();
}
root.join("src/main.cpp")
}
fn finished_debug_build(
app: &mut App,
) -> (
PathBuf,
Result<crate::cpp_build::Built, crate::cpp_build::Failed>,
) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(300);
loop {
if let Some(done) = app.take_debug_build() {
return done;
}
assert!(
std::time::Instant::now() < deadline,
"the debug build never finished"
);
std::thread::sleep(std::time::Duration::from_millis(50));
}
}
#[test]
fn f5_in_a_cmake_project_debugs_the_target_that_builds_the_file() {
use crate::lsp::manager::is_on_path;
if !(is_on_path("cmake") && is_on_path("c++")) {
return;
}
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("proj");
let main = cmake_debug_project(&root);
let configured = std::process::Command::new("cmake")
.arg("-S")
.arg(&root)
.arg("-B")
.arg(root.join("build"))
.output()
.unwrap();
assert!(configured.status.success(), "{configured:?}");
with_relay_home(tmp.path(), || {
let mut app = App::new(root.clone()).unwrap();
app.begin_debug_build(&main);
assert_eq!(app.status, "Building main.cpp for debugging…");
let (source, built) = finished_debug_build(&mut app);
assert_eq!(source, main);
let built = built.unwrap_or_else(|f| panic!("{}\n{}", f.summary, f.output));
assert_eq!(
built.binary.canonicalize().unwrap(),
root.join("build/app").canonicalize().unwrap()
);
assert!(built.how.starts_with("cmake --build"), "{}", built.how);
assert!(built.how.ends_with("(target app)"), "{}", built.how);
let ran = std::process::Command::new(&built.binary).output().unwrap();
assert_eq!(String::from_utf8_lossy(&ran.stdout).trim(), "4.000000");
assert!(!root.join("src/main.dbg").exists(), "nothing lands in src/");
});
}
#[test]
fn a_failed_debug_build_says_why_and_keeps_its_output() {
use crate::lsp::manager::is_on_path;
if !is_on_path("c++") {
return;
}
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("proj");
let main = cmake_debug_project(&root);
with_relay_home(tmp.path(), || {
let mut app = App::new(root.clone()).unwrap();
app.begin_debug_build(&main);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120);
while !app.poll_debug_build() {
assert!(
std::time::Instant::now() < deadline,
"the build never ended"
);
std::thread::sleep(std::time::Duration::from_millis(50));
}
assert!(
app.status.starts_with("Build for debug failed: c++ failed"),
"{}",
app.status
);
assert!(app.status.contains("geom.hpp"), "{}", app.status);
assert!(
app.status
.ends_with("(full output in OUTPUT › Debug Build)")
);
assert!(app.run_debug.feedback_is_error);
let lines = crate::output::snapshot("Debug Build").expect("the build's output is kept");
assert!(lines.iter().any(|l| l.text.contains("geom.hpp")));
assert!(!root.join("src/main.dbg").exists());
assert!(!app.poll_debug_build(), "a finished build reports once");
app.begin_debug_build(&main);
app.editor.path = None;
app.start_debug_session();
assert!(
app.debug_build.is_none(),
"a new F5 drops the running build"
);
assert_eq!(app.status, "Open a file to debug");
app.begin_debug_build(&main);
app.debug_stop_by_user();
assert!(app.debug_build.is_none(), "Stop drops the running build");
assert!(!app.poll_debug_build());
});
}
#[test]
fn f5_builds_the_project_of_the_files_own_workspace_folder() {
use crate::lsp::manager::is_on_path;
if !(is_on_path("cmake") && is_on_path("c++")) {
return;
}
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let tmp = tempfile::tempdir().unwrap();
let primary = tmp.path().join("primary");
std::fs::create_dir_all(&primary).unwrap();
let second = tmp.path().join("second");
let main = cmake_debug_project(&second);
let configured = std::process::Command::new("cmake")
.arg("-S")
.arg(&second)
.arg("-B")
.arg(second.join("build"))
.output()
.unwrap();
assert!(configured.status.success(), "{configured:?}");
with_relay_home(tmp.path(), || {
let mut app = App::new(primary.clone()).unwrap();
app.roots.add(second.clone());
app.begin_debug_build(&main);
let (_, built) = finished_debug_build(&mut app);
let built = built.unwrap_or_else(|f| panic!("{}\n{}", f.summary, f.output));
assert_eq!(
built.binary.canonicalize().unwrap(),
second.join("build/app").canonicalize().unwrap()
);
});
}
#[test]
fn cargo_launches_queue_per_compound_member_and_stop_drops_them() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "type": "lldb", "request": "launch", "name": "A",
"cargo": { "args": ["build", "--bin=a"] } },
{ "type": "lldb", "request": "launch", "name": "B",
"cargo": { "args": ["build", "--bin=b"] } }
],
"compounds": [ { "name": "Both", "configurations": ["A", "B"] } ] }"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let compound = crate::dap::configs::discover_compounds(tmp.path())
.into_iter()
.find(|c| c.name == "Both")
.unwrap();
app.launch_compound(&compound);
assert_eq!(app.pending_cargo_launches.len(), 2, "{}", app.status);
assert!(!app.run_debug.feedback_is_error, "{}", app.status);
assert!(app.status.contains("building A, B"), "{}", app.status);
app.debug_stop_by_user();
assert!(app.pending_cargo_launches.is_empty());
}