Visual Studio Code, rebuilt in Rust
Rust
use std::path::Path;
use std::sync::{Arc, Mutex};
use config::registry::Registry;
use config::service::{Settings, Target};
use serde_json::json;
use super::*;
use crate::quick_input::Entry;
fn workbench<'a>(dir: &Path, cx: &'a mut gpui::TestAppContext) -> (gpui::Entity<Workbench>, &'a mut gpui::VisualTestContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();
let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), None)));
let (sender, _messages) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|_, cx| {
Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(sender), cx)
});
workbench.update(cx, |workbench, _| {
drop(workbench.settings.lock().unwrap().update_value("files.simpleDialog.enable", Some(&json!(true)), Target::User, None, &[]).unwrap());
});
(workbench, cx)
}
fn shown(workbench: &gpui::Entity<Workbench>, cx: &mut gpui::VisualTestContext) -> (String, Option<String>, Vec<String>) {
workbench.read_with(cx, |workbench, _| {
let input = workbench.quick_inputs.current().expect("the simple file dialog");
let labels = input.items().iter().filter_map(|entry| match entry {
Entry::Item(item) => Some(item.label.clone()),
_ => None,
});
(input.input_box.value.clone(), input.title.clone(), labels.collect())
})
}
fn type_text(workbench: &gpui::Entity<Workbench>, cx: &mut gpui::VisualTestContext, text: &str) {
workbench.update_in(cx, |workbench, window, cx| {
workbench.with_current_quick_input(window, cx, |input| input.edit_input(|input_box| input_box.insert(None, text)));
});
cx.run_until_parked();
}
fn accept(workbench: &gpui::Entity<Workbench>, cx: &mut gpui::VisualTestContext) {
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("workbench.action.acceptSelectedQuickOpenItem", vec![], window, cx));
cx.run_until_parked();
}
#[gpui::test]
fn the_simple_dialog_lists_a_folder_and_picks_a_typed_file(cx: &mut gpui::TestAppContext) {
let dir = std::env::temp_dir().join(format!("code-gpui-simple-dialog-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("sub")).unwrap();
std::fs::write(dir.join("a.txt"), "").unwrap();
std::fs::write(dir.join(".hidden"), "").unwrap();
std::fs::write(dir.join("sub/f.txt"), "").unwrap();
let folder = dir.to_string_lossy().into_owned();
let settings = std::env::temp_dir().join(format!("code-gpui-simple-dialog-settings-{}", std::process::id()));
let (workbench, cx) = workbench(&settings, cx);
let dialog = FileDialog { files: true, default_path: Some(dir.clone()), ..FileDialog::default() };
let picked = workbench.update(cx, |workbench, cx| workbench.show_open_dialog(dialog, cx));
cx.run_until_parked();
assert_eq!(shown(&workbench, cx), (format!("{folder}/"), None, vec!["..".into(), "sub".into(), ".hidden".into(), "a.txt".into()]));
type_text(&workbench, cx, "sub/");
assert_eq!(shown(&workbench, cx).2, ["..", "f.txt"], "typing a folder with a slash lists it");
type_text(&workbench, cx, "f.t");
accept(&workbench, cx);
assert_eq!(futures::executor::block_on(picked), Ok(vec![dir.join("sub/f.txt")]));
assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_none()));
let dialog = FileDialog { title: Some("Save As".into()), default_path: Some(dir.join("a.txt")), ..FileDialog::default() };
let saved = workbench.update(cx, |workbench, cx| workbench.show_save_dialog(dialog, cx));
cx.run_until_parked();
assert_eq!(shown(&workbench, cx).0, format!("{folder}/a.txt"));
accept(&workbench, cx);
assert_eq!(shown(&workbench, cx).1.as_deref(), Some("a.txt already exists. Are you sure you want to overwrite it?"));
accept(&workbench, cx);
assert_eq!(futures::executor::block_on(saved), Ok(Some(dir.join("a.txt"))));
std::fs::remove_dir_all(dir).unwrap();
let _ = std::fs::remove_dir_all(settings);
}
#[test]
fn paths_split_and_join_as_posix_uris() {
assert_eq!(dirname("/a/b/"), "/a");
assert_eq!(basename("/a/b/"), "b");
assert_eq!(extname("/a/.hidden"), "");
assert_eq!(join_path("/a/b/", "../c"), "/a/c");
assert_eq!(relative_path("/a/b", "/a/c/d"), "../c/d");
assert_eq!(basename_with_trailing_slash("/a/b/"), "b/");
assert!(is_equal_or_parent_ignore_case("/A/b", "/a"));
assert_eq!(root("/a/b"), "/");
}