Visual Studio Code, rebuilt in Rust
Rust
use std::path::Path;
use std::sync::{Arc, Mutex};
use gpui::TestAppContext;
use serde_json::json;
use crate::menus::MenuAction;
use crate::testing::{GROUP_DEBUG, GROUP_RUN, Profile};
use crate::workbench::Workbench;
use crate::workspace_trust::{Startup, WorkspaceTrust};
fn profile(profile_id: i64, label: &str, group: u64, is_default: bool) -> Profile {
Profile { controller_id: "c".into(), profile_id, label: label.into(), group, is_default, tag: None, supports_continuous_run: true, has_configuration_handler: false }
}
/// testingExplorerView.ts `onContextMenu` on a test with a uri under a controller with two run profiles and a
/// debug one: testExplorerActions.ts's `MenuId.TestItem` actions but the inline group, run on the row's test.
#[gpui::test]
fn a_test_rows_context_menu_shows_and_runs_the_test_item_actions(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let (sender, host) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(sender), cx);
let mut storage = storage::Storage::in_memory();
let startup = Startup { enabled: false, workspace: None, empty_window_trusted: false, workspace_storage: &mut storage };
workbench.workspace_trust = WorkspaceTrust::new(Arc::new(Mutex::new(storage::Storage::in_memory())), startup);
let uri = json!({ "scheme": "file", "path": "/a.test.js" });
workbench.testing.collection.apply(&[json!({ "op": 0, "item": { "expand": 0, "item": { "extId": "c", "label": "c", "tags": [] } } }), json!({ "op": 0, "item": { "expand": 0, "item": { "extId": "c\0t", "label": "t", "tags": [], "uri": uri } } })]);
workbench.testing.profiles = vec![profile(1, "Run", GROUP_RUN, true), profile(2, "Run Slow", GROUP_RUN, false), profile(3, "Debug", GROUP_DEBUG, true)];
workbench
});
let (titles, args) = workbench.update_in(cx, |workbench, window, cx| {
workbench.show_test_item_menu("c\0t", gpui::Bounds::default(), window, cx);
let menu = workbench.context_menu.as_ref().expect("a context menu shows");
let titles: Vec<String> = menu.levels[0].actions.iter().map(|action| match action {
MenuAction::Command { title, .. } => title.clone(),
_ => "---".into(),
}).collect();
(titles, menu.args.clone())
});
assert_eq!(titles, ["Run Test", "Debug Test", "Go to Test", "Turn on Continuous Run", "---", "Hide Test", "Start Continous Run Using...", "Execute Using Profile..."]);
assert_eq!(args, [json!({ "$mid": 16, "tests": [{ "expand": 0, "item": { "extId": "c\0t", "label": "t", "tags": [], "uri": { "scheme": "file", "path": "/a.test.js" } } }] })]);
workbench.update_in(cx, |workbench, window, cx| {
workbench.hide_context_menu(window, cx);
drop(workbench.execute_command("testing.run", args, window, cx));
});
cx.run_until_parked();
let runs: Vec<_> = host.try_iter().filter(|message| message.method == "$runControllerTests").map(|message| message.args[0][0]["testIds"].clone()).collect();
assert_eq!(runs, [json!(["c\0t"])], "Run Test runs the row's test with the default run profile");
}
/// testExclusions.ts: Hide Test hides the row unless the filter has `@hidden`, its menu offers Unhide Test, runs
/// exclude it, the set is stored in the workspace, and Unhide All Tests brings it back.
#[gpui::test]
fn a_hidden_test_leaves_the_tree_and_runs_until_it_is_unhidden(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let (sender, host) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(sender), cx);
let mut storage = storage::Storage::in_memory();
let startup = Startup { enabled: false, workspace: None, empty_window_trusted: false, workspace_storage: &mut storage };
workbench.workspace_trust = WorkspaceTrust::new(Arc::new(Mutex::new(storage::Storage::in_memory())), startup);
let test = |id: &str| json!({ "op": 0, "item": { "expand": 0, "item": { "extId": id, "label": id, "tags": [] } } });
workbench.testing.collection.apply(&[test("c"), test("c\0t"), test("c\0u")]);
workbench.testing.profiles = vec![profile(1, "Run", GROUP_RUN, true)];
workbench
});
let menu_titles = |workbench: &mut Workbench, window: &mut gpui::Window, cx: &mut gpui::Context<Workbench>| {
workbench.show_test_item_menu("c\0t", gpui::Bounds::default(), window, cx);
let menu = workbench.context_menu.as_ref().expect("a context menu shows");
let titles: Vec<String> = menu.levels[0].actions.iter().filter_map(|action| match action {
MenuAction::Command { title, .. } => Some(title.clone()),
_ => None,
}).collect();
let args = menu.args.clone();
workbench.hide_context_menu(window, cx);
(titles, args)
};
workbench.update_in(cx, |workbench, window, cx| {
let (_, args) = menu_titles(workbench, window, cx);
drop(workbench.execute_command("testing.hideTest", args, window, cx));
});
let (titles, visible, stored) = workbench.update_in(cx, |workbench, window, cx| {
let (titles, _) = menu_titles(workbench, window, cx);
let visible = workbench.testing_filtered_tests().map(|(visible, _)| visible);
(titles, visible, workbench.workspace_storage.lock().unwrap().get("excludedTestItems").map(str::to_owned))
});
assert!(titles.contains(&"Unhide Test".to_owned()) && !titles.contains(&"Hide Test".to_owned()), "{titles:?}");
assert_eq!(visible, Some(["c\0u".to_owned()].into()), "the hidden test leaves the tree");
assert_eq!(stored.as_deref(), Some(r#"["c\u0000t"]"#));
// The row's Run Test leaves it out as hidden in the tree, a run by id as `excluded` (`runResolvedTests`).
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("testing.run", vec![json!("c")], window, cx)));
cx.run_until_parked();
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("vscode.runTestsById", vec![json!(GROUP_RUN), json!("c")], window, cx)));
cx.run_until_parked();
let runs: Vec<_> = host.try_iter().filter(|message| message.method == "$runControllerTests").map(|message| message.args[0][0]["excludeExtIds"].clone()).collect();
assert_eq!(runs, [json!(["c\0t"]), json!(["c\0t"])], "a run excludes the hidden test");
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("testing.filters.toggle@hidden", Vec::new(), window, cx)));
let visible = workbench.read_with(cx, |workbench, _| workbench.testing_filtered_tests().map(|(visible, _)| visible));
assert_eq!(visible, Some(["c\0t".to_owned(), "c\0u".to_owned()].into()), "`@hidden` shows it again");
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("testing.unhideAllTests", Vec::new(), window, cx)));
let excluded = workbench.read_with(cx, |workbench, _| workbench.testing.excluded.clone());
assert!(excluded.is_empty());
}
/// testingExplorerView.ts `getTreeIncludeExclude`: the view title's Run Tests and a row's Run Test run the tests
/// under them but exclude the ones the filter box hides.
#[gpui::test]
fn runs_from_the_explorer_exclude_the_tests_the_filter_hides(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let (sender, host) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(sender), cx);
let mut storage = storage::Storage::in_memory();
let startup = Startup { enabled: false, workspace: None, empty_window_trusted: false, workspace_storage: &mut storage };
workbench.workspace_trust = WorkspaceTrust::new(Arc::new(Mutex::new(storage::Storage::in_memory())), startup);
let test = |id: &str, label: &str| json!({ "op": 0, "item": { "expand": 0, "item": { "extId": id, "label": label, "tags": [] } } });
workbench.testing.collection.apply(&[test("c", "c"), test("c\0math", "Math"), test("c\0math\0add", "adds"), test("c\0math\0sub", "subtracts"), test("c\0other", "Other")]);
workbench.testing.profiles = vec![profile(1, "Run", GROUP_RUN, true)];
workbench
});
workbench.update_in(cx, |workbench, window, cx| workbench.edit_testing_filter(|input| input.set_value("add".into()), window, cx));
cx.executor().advance_clock(std::time::Duration::from_millis(400));
cx.run_until_parked();
let mut runs = Vec::new();
for (command, args) in [("testing.runSelected", Vec::new()), ("testing.run", vec![json!("c\0math")])] {
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(command, args, window, cx)));
cx.run_until_parked();
runs.extend(host.try_iter().filter(|message| message.method == "$runControllerTests").map(|message| (message.args[0][0]["testIds"].clone(), message.args[0][0]["excludeExtIds"].clone())));
}
assert_eq!(runs, [(json!(["c"]), json!(["c\0math\0sub", "c\0other"])), (json!(["c\0math"]), json!(["c\0math\0sub"]))]);
}
/// testingExplorerView.ts `fillActionBar`: a row's actions are `MenuId.TestItem`'s `inline` group, so a coverage
/// profile adds Run Test with Coverage, a controller without a debug profile has no Debug Test, and an extension's
/// `testing/item/context` `inline` contribution shows beside them.
#[gpui::test]
fn a_test_row_shows_the_test_item_menu_s_inline_group(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let (sender, _host) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(sender), cx);
workbench.testing.collection.apply(&[json!({ "op": 0, "item": { "expand": 0, "item": { "extId": "c", "label": "c", "tags": [] } } }), json!({ "op": 0, "item": { "expand": 0, "item": { "extId": "c\0t", "label": "t", "tags": [] } } })]);
workbench.testing.profiles = vec![profile(1, "Run", GROUP_RUN, true), profile(2, "Coverage", crate::testing::GROUP_COVERAGE, true)];
let entry = crate::menus::Entry::Command { id: "ext.pin".into(), precondition: None, toggled: None, alt: None };
workbench.menus.push(crate::menus::MenuItem { menu: crate::menus::MenuId::TestItem, entry, title: "Pin Test".into(), group: "inline".into(), order: 20., when: None });
workbench
});
let titles = workbench.update_in(cx, |workbench, window, _| {
workbench.test_row_inline_actions("c\0t", window).into_iter().map(|action| match action {
MenuAction::Command { title, .. } => title,
_ => "---".into(),
}).collect::<Vec<_>>()
});
assert_eq!(titles, ["Run Test", "Run Test with Coverage", "Pin Test", "Turn on Continuous Run"]);
}