debugger.rs

  1mod go_locator {
  2    use collections::HashMap;
  3    use dap::{DapLocator, adapters::DebugAdapterName};
  4    use gpui::TestAppContext;
  5    use project::debugger::locators::go::{DelveLaunchRequest, GoLocator};
  6    use task::{HideStrategy, RevealStrategy, RevealTarget, SaveStrategy, Shell, TaskTemplate};
  7    #[gpui::test]
  8    async fn test_create_scenario_for_go_build(_: &mut TestAppContext) {
  9        let locator = GoLocator;
 10        let task = TaskTemplate {
 11            label: "go build".into(),
 12            command: "go".into(),
 13            args: vec!["build".into(), ".".into()],
 14            env: Default::default(),
 15            cwd: Some("${ZED_WORKTREE_ROOT}".into()),
 16            use_new_terminal: false,
 17            allow_concurrent_runs: false,
 18            reveal: RevealStrategy::Always,
 19            reveal_target: RevealTarget::Dock,
 20            hide: HideStrategy::Never,
 21            shell: Shell::System,
 22            tags: vec![],
 23            show_summary: true,
 24            show_command: true,
 25            save: SaveStrategy::default(),
 26            hooks: Default::default(),
 27        };
 28
 29        let scenario = locator
 30            .create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
 31            .await;
 32
 33        assert!(scenario.is_none());
 34    }
 35
 36    #[gpui::test]
 37    async fn test_skip_non_go_commands_with_non_delve_adapter(_: &mut TestAppContext) {
 38        let locator = GoLocator;
 39        let task = TaskTemplate {
 40            label: "cargo build".into(),
 41            command: "cargo".into(),
 42            args: vec!["build".into()],
 43            env: Default::default(),
 44            cwd: Some("${ZED_WORKTREE_ROOT}".into()),
 45            use_new_terminal: false,
 46            allow_concurrent_runs: false,
 47            reveal: RevealStrategy::Always,
 48            reveal_target: RevealTarget::Dock,
 49            hide: HideStrategy::Never,
 50            shell: Shell::System,
 51            tags: vec![],
 52            show_summary: true,
 53            show_command: true,
 54            save: SaveStrategy::default(),
 55            hooks: Default::default(),
 56        };
 57
 58        let scenario = locator
 59            .create_scenario(
 60                &task,
 61                "test label",
 62                &DebugAdapterName("SomeOtherAdapter".into()),
 63            )
 64            .await;
 65        assert!(scenario.is_none());
 66
 67        let scenario = locator
 68            .create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
 69            .await;
 70        assert!(scenario.is_none());
 71    }
 72    #[gpui::test]
 73    async fn test_go_locator_run(_: &mut TestAppContext) {
 74        let locator = GoLocator;
 75        let delve = DebugAdapterName("Delve".into());
 76
 77        let task = TaskTemplate {
 78            label: "go run with flags".into(),
 79            command: "go".into(),
 80            args: vec![
 81                "run".to_string(),
 82                "-race".to_string(),
 83                "-ldflags".to_string(),
 84                "-X main.version=1.0".to_string(),
 85                "./cmd/myapp".to_string(),
 86                "--config".to_string(),
 87                "production.yaml".to_string(),
 88                "--verbose".to_string(),
 89            ],
 90            env: {
 91                let mut env = HashMap::default();
 92                env.insert("GO_ENV".to_string(), "production".to_string());
 93                env
 94            },
 95            cwd: Some("/project/root".into()),
 96            ..Default::default()
 97        };
 98
 99        let scenario = locator
100            .create_scenario(&task, "test run label", &delve)
101            .await
102            .unwrap();
103
104        let config: DelveLaunchRequest = serde_json::from_value(scenario.config).unwrap();
105
106        assert_eq!(
107            config,
108            DelveLaunchRequest {
109                request: "launch".to_string(),
110                mode: "debug".to_string(),
111                program: "./cmd/myapp".to_string(),
112                build_flags: vec![
113                    "-race".to_string(),
114                    "-ldflags".to_string(),
115                    "-X main.version=1.0".to_string()
116                ],
117                args: vec![
118                    "--config".to_string(),
119                    "production.yaml".to_string(),
120                    "--verbose".to_string(),
121                ],
122                env: {
123                    let mut env = HashMap::default();
124                    env.insert("GO_ENV".to_string(), "production".to_string());
125                    env
126                },
127                cwd: Some("/project/root".to_string()),
128            }
129        );
130    }
131
132    #[gpui::test]
133    async fn test_go_locator_test(_: &mut TestAppContext) {
134        let locator = GoLocator;
135        let delve = DebugAdapterName("Delve".into());
136
137        // Test with tags and run flag
138        let task_with_tags = TaskTemplate {
139            label: "test".into(),
140            command: "go".into(),
141            args: vec![
142                "test".to_string(),
143                "-tags".to_string(),
144                "integration,unit".to_string(),
145                "-run".to_string(),
146                "Foo".to_string(),
147                ".".to_string(),
148            ],
149            ..Default::default()
150        };
151        let result = locator
152            .create_scenario(&task_with_tags, "", &delve)
153            .await
154            .unwrap();
155
156        let config: DelveLaunchRequest = serde_json::from_value(result.config).unwrap();
157
158        assert_eq!(
159            config,
160            DelveLaunchRequest {
161                request: "launch".to_string(),
162                mode: "test".to_string(),
163                program: ".".to_string(),
164                build_flags: vec!["-tags".to_string(), "integration,unit".to_string(),],
165                args: vec![
166                    "-test.run".to_string(),
167                    "Foo".to_string(),
168                    "-test.v".to_string()
169                ],
170                env: Default::default(),
171                cwd: None,
172            }
173        );
174    }
175
176    #[gpui::test]
177    async fn test_skip_unsupported_go_commands(_: &mut TestAppContext) {
178        let locator = GoLocator;
179        let task = TaskTemplate {
180            label: "go clean".into(),
181            command: "go".into(),
182            args: vec!["clean".into()],
183            env: Default::default(),
184            cwd: Some("${ZED_WORKTREE_ROOT}".into()),
185            use_new_terminal: false,
186            allow_concurrent_runs: false,
187            reveal: RevealStrategy::Always,
188            reveal_target: RevealTarget::Dock,
189            hide: HideStrategy::Never,
190            shell: Shell::System,
191            tags: vec![],
192            show_summary: true,
193            show_command: true,
194            save: SaveStrategy::default(),
195            hooks: Default::default(),
196        };
197
198        let scenario = locator
199            .create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
200            .await;
201        assert!(scenario.is_none());
202    }
203}
204
205mod python_locator {
206    use dap::{DapLocator, adapters::DebugAdapterName};
207    use serde_json::json;
208
209    use project::debugger::locators::python::*;
210    use task::{DebugScenario, TaskTemplate};
211
212    #[gpui::test]
213    async fn test_python_locator() {
214        let adapter = DebugAdapterName("Debugpy".into());
215        let build_task = TaskTemplate {
216            label: "run module '$ZED_FILE'".into(),
217            command: "$ZED_CUSTOM_PYTHON_ACTIVE_ZED_TOOLCHAIN".into(),
218            args: vec!["-m".into(), "$ZED_CUSTOM_PYTHON_MODULE_NAME".into()],
219            env: Default::default(),
220            cwd: Some("$ZED_WORKTREE_ROOT".into()),
221            use_new_terminal: false,
222            allow_concurrent_runs: false,
223            reveal: task::RevealStrategy::Always,
224            reveal_target: task::RevealTarget::Dock,
225            hide: task::HideStrategy::Never,
226            tags: vec!["python-module-main-method".into()],
227            shell: task::Shell::System,
228            show_summary: false,
229            show_command: false,
230            save: task::SaveStrategy::default(),
231            hooks: Default::default(),
232        };
233
234        let expected_scenario = DebugScenario {
235            adapter: "Debugpy".into(),
236            label: "run module 'main.py'".into(),
237            build: None,
238            config: json!({
239                "request": "launch",
240                "python": "$ZED_CUSTOM_PYTHON_ACTIVE_ZED_TOOLCHAIN",
241                "args": [],
242                "cwd": "$ZED_WORKTREE_ROOT",
243                "module": "$ZED_CUSTOM_PYTHON_MODULE_NAME",
244            }),
245            tcp_connection: None,
246        };
247
248        assert_eq!(
249            PythonLocator
250                .create_scenario(&build_task, "run module 'main.py'", &adapter)
251                .await
252                .expect("Failed to create a scenario"),
253            expected_scenario
254        );
255    }
256}
257
258mod memory {
259    use project::debugger::{
260        MemoryCell,
261        memory::{MemoryIterator, PageAddress, PageContents},
262    };
263
264    #[test]
265    fn iterate_over_unmapped_memory() {
266        let empty_iterator = MemoryIterator::new(0..=127, Default::default());
267        let actual = empty_iterator.collect::<Vec<_>>();
268        let expected = vec![MemoryCell(None); 128];
269        assert_eq!(actual.len(), expected.len());
270        assert_eq!(actual, expected);
271    }
272
273    #[test]
274    fn iterate_over_partially_mapped_memory() {
275        let it = MemoryIterator::new(
276            0..=127,
277            vec![(PageAddress(5), PageContents::mapped(vec![1]))].into_iter(),
278        );
279        let actual = it.collect::<Vec<_>>();
280        let expected = std::iter::repeat_n(MemoryCell(None), 5)
281            .chain(std::iter::once(MemoryCell(Some(1))))
282            .chain(std::iter::repeat_n(MemoryCell(None), 122))
283            .collect::<Vec<_>>();
284        assert_eq!(actual.len(), expected.len());
285        assert_eq!(actual, expected);
286    }
287
288    #[test]
289    fn reads_from_the_middle_of_a_page() {
290        let partial_iter = MemoryIterator::new(
291            20..=30,
292            vec![(PageAddress(0), PageContents::mapped((0..255).collect()))].into_iter(),
293        );
294        let actual = partial_iter.collect::<Vec<_>>();
295        let expected = (20..=30)
296            .map(|val| MemoryCell(Some(val)))
297            .collect::<Vec<_>>();
298        assert_eq!(actual.len(), expected.len());
299        assert_eq!(actual, expected);
300    }
301}