Visual Studio Code, rebuilt in Rust
Rust
use std::path::{Path, PathBuf};
use exthost::scan::scan_extension;
use gpui::{App, AppContext, BorrowAppContext, TestAppContext, WindowHandle, WindowOptions};
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::user_data_profile::UserDataProfilesService;
use crate::workbench::Workbench;
use crate::WindowRequest;
/// main.rs `open_window` without the extensions and the extension host: a workbench window on the profile the
/// app's profiles service resolves for the request.
pub(crate) fn open_window(request: WindowRequest, cx: &mut App) -> WindowHandle<Workbench> {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let profile = cx.update_global(|service: &mut UserDataProfilesService, _| service.resolve(None, request.profile.name.as_deref(), request.profile.temp, false));
cx.open_window(WindowOptions::default(), |_, cx| {
cx.new(|cx| {
let mut workbench = Workbench::new(theme, app_root, None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.set_user_data_profiles(profile, cx);
workbench
})
})
.unwrap()
}
/// A fresh user data folder with the app's profiles service on it.
pub(crate) fn profiles_service(name: &str, cx: &mut TestAppContext) -> PathBuf {
let data = std::env::temp_dir().join(format!("code-gpui-profiles-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&data);
cx.update(|cx| cx.set_global(UserDataProfilesService::load(&data, &data.join("extensions"))));
data
}
pub(crate) fn profile_names(handle: WindowHandle<Workbench>, cx: &mut TestAppContext) -> Vec<String> {
let commands = handle.read_with(cx, |workbench, _| workbench.commands.ids().filter(|id| id.starts_with("workbench.action.openProfile.")).map(str::to_owned).collect::<Vec<_>>()).unwrap();
let mut names: Vec<String> = commands.into_iter().map(|id| id.trim_start_matches("workbench.action.openProfile.").to_owned()).collect();
names.sort();
names
}
/// userDataProfileActions.ts `NewWindowWithProfileAction` and `CreateTransientProfileAction`: the window opens in this
/// process (hostService.openWindow), not as a new app instance, on the profile it was asked for.
#[gpui::test]
fn profile_windows_open_in_this_process(cx: &mut TestAppContext) {
profiles_service("in-process", cx);
let main = cx.update(|cx| open_window(WindowRequest::default(), cx));
main.update(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.profiles.actions.createTemporaryProfile", vec![], window, cx))).unwrap();
cx.run_until_parked();
let windows: Vec<WindowHandle<Workbench>> = cx.update(|cx| cx.windows().into_iter().filter_map(|handle| handle.downcast::<Workbench>()).collect());
assert_eq!(windows.len(), 2, "the temporary profile's window is a window of this process");
let current = windows.iter().find(|handle| **handle != main).unwrap().read_with(cx, |workbench, _| workbench.user_data_profiles.state.as_ref().unwrap().current.clone()).unwrap();
assert!(current.is_transient && current.name == "Temp 1", "{current:?}");
}
/// extensionsResource.ts `apply` through `installGalleryExtensions`: a template's extensions of two untrusted
/// publishers ask for trust once, in one dialog that names both.
#[gpui::test]
fn template_extensions_ask_for_publisher_trust_once(cx: &mut TestAppContext) {
profiles_service("template-trust", cx);
let gallery_url = crate::workbench::settings_import::tests::two_publisher_gallery();
let handle = cx.update(|cx| open_window(WindowRequest::default(), cx));
handle
.update(cx, |workbench, window, cx| {
workbench.gallery_url = gallery_url;
let profile_id = workbench.user_data_profiles.state.as_ref().unwrap().current.id.clone();
let ids = vec!["redhat.vscode-yaml".to_owned(), "beta.vscode-yaml".to_owned()];
cx.global_mut::<UserDataProfilesService>().pending_installs.insert(profile_id, ids);
workbench.install_template_extensions(window, cx);
})
.unwrap();
cx.run_until_parked();
let messages = handle.read_with(cx, |workbench, _| workbench.dialogs.iter().map(|shown| shown.dialog.message.clone()).collect::<Vec<_>>()).unwrap();
assert_eq!(messages, ["Do you trust publishers \"Red Hat\" and \"Beta\"?"]);
}
/// userDataProfile.ts `onDidChangeProfiles`: a profile one window creates is in every window's Profiles menus and
/// commands, and one it removes leaves them.
#[gpui::test]
fn every_window_sees_a_profile_another_window_creates_and_removes(cx: &mut TestAppContext) {
profiles_service("shared", cx);
let first = cx.update(|cx| open_window(WindowRequest::default(), cx));
let second = cx.update(|cx| open_window(WindowRequest::default(), cx));
let created = first
.update(cx, |workbench, _, cx| workbench.edit_profiles(cx, |profiles, storage| profiles.create_named_profile("Work", Default::default(), None, storage)).unwrap().0)
.unwrap();
cx.run_until_parked();
assert_eq!(profile_names(second, cx), ["Default", "Work"]);
first.update(cx, |workbench, window, cx| workbench.remove_profile(&created, window, cx)).unwrap();
cx.run_until_parked();
assert_eq!(profile_names(second, cx), ["Default"]);
}