Visual Studio Code, rebuilt in Rust
Rust
use std::path::{Path, PathBuf};
use exthost::scan::scan_extension;
use gpui::{TestAppContext, VisualTestContext};
use super::CommandLine;
use crate::argv;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;
fn workbench_in<'a>(name: &str, cx: &'a mut TestAppContext) -> (PathBuf, gpui::Entity<Workbench>, &'a mut VisualTestContext) {
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 dir = std::env::temp_dir().join(format!("code-gpui-cli-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("folder")).unwrap();
for file in ["a.txt", "b.txt", "c.txt", "d.txt"] {
std::fs::write(dir.join(file), "one\ntwo\nthree").unwrap();
}
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));
(dir, workbench, cx)
}
fn command_line(args: &[&str], cwd: &Path) -> CommandLine {
let args: Vec<String> = args.iter().map(|arg| arg.to_string()).collect();
CommandLine::from_args(&argv::parse_and_validate(&args).unwrap().0, cwd)
}
/// `//var/...` is `/var/...` on macOS and Linux, and a path on UNC host `var` to the check.
#[cfg(unix)]
fn on_unc_host(path: &Path) -> (PathBuf, String) {
(PathBuf::from(format!("/{}", path.display())), path.components().nth(1).unwrap().as_os_str().to_str().unwrap().to_owned())
}
#[cfg(unix)]
#[test]
fn a_command_line_path_on_a_refused_unc_host_is_left_out() {
let dir = std::env::temp_dir().join(format!("code-gpui-cli-unc-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("a.txt"), "").unwrap();
let (unc, host) = on_unc_host(&dir.join("a.txt"));
files::unc::restrict_unc_access_on_this_thread(Some(&[]));
assert!(command_line(&[unc.to_str().unwrap()], &dir).files.open.is_empty());
files::unc::restrict_unc_access_on_this_thread(Some(&[&host]));
assert_eq!(command_line(&[unc.to_str().unwrap()], &dir).files.open.len(), 1);
files::unc::restrict_unc_access_on_this_thread(None);
std::fs::remove_dir_all(&dir).unwrap();
}
/// windowsMainService.ts `onUNCHostNotAllowed`: Cancel opens nothing, Learn More asks again, Allow opens the path.
#[cfg(unix)]
#[gpui::test]
fn a_path_on_a_refused_unc_host_asks_before_it_opens(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("unc", cx);
let (unc, host) = on_unc_host(&dir.join("a.txt"));
files::unc::restrict_unc_access_on_this_thread(Some(&[]));
workbench.update_in(cx, |workbench, window, cx| {
workbench.update_user_setting("window.dialogStyle", "custom".into());
workbench.open_window(vec![super::super::workspace_actions::Openable::of(unc.clone())], None, false, window, cx)
});
cx.run_until_parked();
let shown = |cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.dialogs.iter().map(|shown| shown.dialog.message.clone()).collect::<Vec<_>>());
assert_eq!(shown(cx), [format!("The host '{host}' was not found in the list of allowed hosts. Do you want to allow it anyway?")]);
workbench.update(cx, |workbench, cx| workbench.close_custom_dialog(2, cx));
cx.run_until_parked();
assert!(shown(cx).is_empty());
assert!(workbench.read_with(cx, |workbench, _| workbench.editors.active().is_none()));
workbench.update_in(cx, |workbench, window, cx| workbench.open_window(vec![super::super::workspace_actions::Openable::of(unc.clone())], None, false, window, cx));
cx.run_until_parked();
workbench.update(cx, |workbench, cx| workbench.close_custom_dialog(1, cx));
cx.run_until_parked();
assert_eq!(shown(cx).len(), 1, "Learn More shows the dialog again");
workbench.update(cx, |workbench, cx| workbench.close_custom_dialog(0, cx));
cx.run_until_parked();
assert!(shown(cx).is_empty());
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().path.clone()), unc);
assert!(files::unc::is_unc_access_allowed(&unc));
files::unc::restrict_unc_access_on_this_thread(None);
std::fs::remove_dir_all(&dir).unwrap();
}
fn open(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, command_line: CommandLine) {
workbench.update_in(cx, |workbench, window, cx| workbench.open_command_line_files(command_line.files, None, window, cx));
cx.run_until_parked();
}
fn run(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, command: &str) {
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(command, Vec::new(), window, cx)));
cx.run_until_parked();
}
#[test]
fn the_command_line_sorts_folders_files_and_the_wait_marker() {
let dir = std::env::temp_dir().join(format!("code-gpui-cli-sort-{}", std::process::id()));
std::fs::create_dir_all(dir.join("folder")).unwrap();
for file in ["a.txt", "b.txt"] {
std::fs::write(dir.join(file), "").unwrap();
}
let opened = command_line(&["folder", "a.txt", "missing.txt", "missing.code-workspace", "-r", "--wait", "--waitMarkerFilePath", "/tmp/marker"], &dir);
assert_eq!(opened.workspaces, vec![dir.join("folder")]);
let files = ["a.txt", "missing.txt", "missing.code-workspace"].map(|file| dir.join(file));
assert_eq!(opened.files.open.iter().map(|file| file.path.clone()).collect::<Vec<_>>(), files);
assert_eq!(opened.files.wait_marker, Some(PathBuf::from("/tmp/marker")));
assert!(opened.reuse_window);
let added = command_line(&["-a", "folder"], &dir);
assert_eq!((added.workspaces, added.folders_to_add), (Vec::new(), vec![dir.join("folder")]));
let diffed = command_line(&["-d", "a.txt", "b.txt"], &dir);
assert_eq!((diffed.files.diff, diffed.files.open), (vec![dir.join("a.txt"), dir.join("b.txt")], Vec::new()));
// cli.ts sets the marker only with `--wait`.
assert_eq!(command_line(&["a.txt", "--waitMarkerFilePath", "/tmp/marker"], &dir).files.wait_marker, None);
std::fs::remove_dir_all(&dir).unwrap();
}
#[gpui::test]
fn goto_opens_the_file_at_its_line_and_column(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("goto", cx);
open(&workbench, cx, command_line(&["-g", "b.txt:2:3"], &dir));
let (path, head) = workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
(editor.document().path.clone(), editor.selections[0].head)
});
assert_eq!(path, dir.join("b.txt"));
assert_eq!((head.line, head.column), (1, 2));
}
#[gpui::test]
fn diff_and_merge_open_their_editors(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("diff-merge", cx);
open(&workbench, cx, command_line(&["--diff", "a.txt", "b.txt"], &dir));
let diff = workbench.read_with(cx, |workbench, _| workbench.editors.active_diff().map(|diff| (diff.original.path.clone(), diff.modified.path.clone())));
assert_eq!(diff, Some((dir.join("a.txt"), dir.join("b.txt"))));
open(&workbench, cx, command_line(&["--merge", "a.txt", "b.txt", "c.txt", "d.txt"], &dir));
let result = workbench.read_with(cx, |workbench, _| workbench.editors.active_merge().map(|merge| merge.result.path.clone()));
assert_eq!(result, Some(dir.join("d.txt")));
}
#[gpui::test]
fn the_wait_marker_is_deleted_once_its_editors_close(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("wait", cx);
let marker = dir.join("marker");
std::fs::write(&marker, "").unwrap();
open(&workbench, cx, command_line(&["--wait", "--waitMarkerFilePath", &marker.to_string_lossy(), "a.txt", "b.txt"], &dir));
run(&workbench, cx, "workbench.action.closeActiveEditor");
assert!(marker.exists(), "one waited file is still open");
run(&workbench, cx, "workbench.action.closeActiveEditor");
assert!(!marker.exists());
}
/// editor.ts `whenEditorClosed`: the first close of an editor of the file releases the wait, a move does not.
#[gpui::test]
fn the_wait_marker_is_deleted_when_one_of_two_editors_of_its_file_closes(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("wait-groups", cx);
let marker = dir.join("marker");
std::fs::write(&marker, "").unwrap();
open(&workbench, cx, command_line(&["--wait", "--waitMarkerFilePath", &marker.to_string_lossy(), "a.txt"], &dir));
run(&workbench, cx, "workbench.action.moveEditorToRightGroup");
assert!(marker.exists(), "a move is not a close");
run(&workbench, cx, "workbench.action.splitEditor");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.groups_in_grid_order().len()), 2);
run(&workbench, cx, "workbench.action.closeActiveEditor");
assert!(!marker.exists());
}
/// editor.ts `whenEditorClosed`: a file still dirty when its editor closes keeps the marker until it is saved
/// (`onDidChangeDirty`), with no timer in between.
#[gpui::test]
fn the_wait_marker_waits_for_the_dirty_file_to_be_saved(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("wait-dirty", cx);
let marker = dir.join("marker");
std::fs::write(&marker, "").unwrap();
open(&workbench, cx, command_line(&["--wait", "--waitMarkerFilePath", &marker.to_string_lossy(), "a.txt"], &dir));
run(&workbench, cx, "workbench.action.splitEditor");
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("type", vec![serde_json::json!({ "text": "x" })], window, cx));
run(&workbench, cx, "workbench.action.closeActiveEditor");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.groups_in_grid_order().len()), 1, "a tab open in another group closes without asking");
assert!(workbench.read_with(cx, |workbench, _| workbench.editors.model("file", &dir.join("a.txt")).unwrap().is_dirty()));
assert!(marker.exists(), "the file is still dirty");
run(&workbench, cx, "workbench.action.files.save");
assert!(!marker.exists());
}
#[gpui::test]
fn the_wait_marker_is_deleted_when_the_window_closes(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("wait-window", cx);
let marker = dir.join("marker");
std::fs::write(&marker, "").unwrap();
// windowsMainService.ts: a `--wait` with nothing to open waits for the window.
open(&workbench, cx, command_line(&["--wait", "--waitMarkerFilePath", &marker.to_string_lossy()], &dir));
assert!(marker.exists());
run(&workbench, cx, "workbench.action.closeWindow");
assert!(!marker.exists());
}
#[gpui::test]
fn a_missing_file_opens_untitled_and_is_created_on_save(cx: &mut TestAppContext) {
let (dir, workbench, cx) = workbench_in("missing", cx);
let marker = dir.join("marker");
std::fs::write(&marker, "").unwrap();
open(&workbench, cx, command_line(&["--wait", "--waitMarkerFilePath", &marker.to_string_lossy(), "new.txt"], &dir));
let opened = workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
(editor.document().scheme.clone(), editor.document().path.clone(), editor.text_model().associated_file)
});
assert_eq!(opened, ("untitled".to_string(), dir.join("new.txt"), true));
run(&workbench, cx, "workbench.action.files.save");
assert!(dir.join("new.txt").exists());
assert!(marker.exists(), "the saved file is still open");
run(&workbench, cx, "workbench.action.closeActiveEditor");
assert!(!marker.exists());
}