Skip to content

Visual Studio Code, rebuilt in Rust

Rust
8.2 KB
Raw Blame
use std::path::Path;

use exthost::proposed_api::{Environment, ProposedApi};
use exthost::scan::{ScannedExtension, scan_extension};
use gpui::TestAppContext;
use serde_json::json;

use crate::extension_host_start::{HostLaunch, HostProcess};
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;

/// relauncher.contribution.ts `onConfigurationChange`: an extension's new default of a restart setting does not ask
/// to restart (`ConfigurationTarget.DEFAULT`), a user's change does.
#[gpui::test]
fn a_changed_default_does_not_ask_to_restart(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("vsrustium-restart-default-{}", std::process::id()));
    let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx));
    workbench.update(cx, |workbench, _| workbench.settings.lock().unwrap().update_value("window.dialogStyle", Some(&json!("custom")), config::service::Target::User, None, &[]).unwrap());
    let render = |cx: &mut gpui::VisualTestContext| {
        cx.update(|window, _| {
            window.activate_window();
            window.refresh();
        });
        cx.run_until_parked();
    };
    render(cx);
    let description = json!({ "contributes": { "configurationDefaults": { "accessibility.verbosity.debug": false } } });
    workbench.update(cx, |workbench, cx| workbench.set_running_extensions_after_restart(vec![ScannedExtension { id: "pub.defaults".into(), location: dir.clone(), description }], cx));
    render(cx);
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.setting("accessibility.verbosity.debug")), json!(false));
    assert!(workbench.read_with(cx, |workbench, _| workbench.dialogs.is_empty()), "a new default does not ask");
    workbench.update(cx, |workbench, _| workbench.update_setting("accessibility.verbosity.debug", json!(true)));
    render(cx);
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.dialogs.len()), 1, "a user's change asks");
    let _ = std::fs::remove_dir_all(dir);
}

#[gpui::test]
fn a_host_restart_gives_the_extensions_the_proposals_its_window_allows(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-restart-proposals-{}", std::process::id()));
    let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx));
    let running = workbench.update(cx, |workbench, _| {
        let stable = Environment { is_built: true, is_extension_development: false, quality: Some("stable"), enabled_proposed_api: None };
        workbench.proposed_api = ProposedApi::new(&stable, &json!({ "vscode.sample": ["activeComment"] }));
        let description = json!({ "identifier": { "value": "vscode.sample" }, "isBuiltin": true, "enabledApiProposals": ["activeComment", "agentEditorComments"] });
        workbench.extensions_viewlet.builtins = vec![ScannedExtension { id: "vscode.sample".into(), location: dir.clone(), description }];
        workbench.extensions_to_run()
    });
    assert_eq!(running[0].description["enabledApiProposals"], json!(["activeComment"]));
}

/// abstractExtensionService.ts `_startExtensionHostsIfNecessary`: a restarted host starts as the window's first one,
/// with the window's user data folder (main.rs `--user-data-dir`), not one derived again from the settings path.
#[gpui::test]
fn a_host_restart_starts_the_host_as_the_window_did(cx: &mut TestAppContext) {
    // The exthost-session thread wakes the window's status loop.
    cx.executor().allow_parking();
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("vsrustium-restart-start-{}", std::process::id()));
    let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("settings/User"), None)));
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx));
    let (opened, host_opened) = std::sync::mpsc::channel();
    let user_data = dir.join("user-data");
    workbench.update(cx, |workbench, _| {
        workbench.extensions_viewlet.user = Some(exthost::extension_management::UserExtensions::new(dir.join("extensions"), "1.148.0"));
        workbench.host_launch = Some(HostLaunch { process: HostProcess::Silent(opened), user_data: user_data.clone() });
    });
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.restartExtensionHost", Vec::new(), window, cx)));
    cx.run_until_parked();
    let (init, peer) = host_opened.recv_timeout(std::time::Duration::from_secs(10)).expect("the restart starts a host");
    drop(peer);
    assert_eq!(init["environment"]["globalStorageHome"], exthost::scan::file_uri(&user_data.join("User/globalStorage")));
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.extension_host_starts), 1);
    let _ = std::fs::remove_dir_all(dir);
}

/// abstractExtensionService.ts `_doStopExtensionHostsWithVeto`: a running debug session (debugService.ts `onWillStop`)
/// vetoes Restart Extension Host; Cancel keeps the host, Restart Anyway stops and starts it.
#[gpui::test]
fn a_debug_session_vetoes_the_restart_until_confirmed(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("vsrustium-restart-veto-{}", std::process::id()));
    let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
    let (sender, receiver) = std::sync::mpsc::channel();
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(sender), cx));
    workbench.update(cx, |workbench, _| {
        workbench.settings.lock().unwrap().update_value("window.dialogStyle", Some(&json!("custom")), config::service::Target::User, None, &[]).unwrap();
        workbench.debug.sessions.push(crate::debug::session::Session::new("s".into(), 1, json!({ "name": "Launch", "type": "node", "request": "launch" }), json!({}), None, json!({})));
    });
    let restart = |cx: &mut gpui::VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.restartExtensionHost", Vec::new(), window, cx)));
        cx.run_until_parked();
    };
    let stopped = || receiver.try_iter().any(|message| message.method == "stopExtensionHost");
    let answer = |button: usize, cx: &mut gpui::VisualTestContext| {
        workbench.update(cx, |workbench, cx| {
            let dialog = &workbench.dialogs[0].dialog;
            assert_eq!((dialog.message.as_str(), dialog.detail.as_deref()), ("Please confirm restart of extensions.", Some("A debug session is still running that would terminate.")));
            workbench.close_custom_dialog(button, cx);
        });
        cx.run_until_parked();
    };
    restart(cx);
    answer(1, cx);
    assert!(!stopped(), "a veto keeps the host");
    restart(cx);
    answer(0, cx);
    assert!(stopped());
}