Skip to content

Visual Studio Code, rebuilt in Rust

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

use gpui::VisualTestContext;
use serde_json::{Value, json};

use crate::workbench::Workbench;

fn uri(path: &Path) -> Value {
    json!({ "scheme": "file", "path": path.to_string_lossy() })
}

fn type_text(workbench: &gpui::Entity<Workbench>, text: &str, cx: &mut VisualTestContext) {
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("type", vec![json!({ "text": text })], window, cx)));
    cx.run_until_parked();
}

/// The (path, text, dirty) of the active group's editors.
fn tabs(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext) -> Vec<(String, String, bool)> {
    workbench.read_with(cx, |workbench, _| {
        let tab = |editor: &crate::editor::OpenEditor| (editor.document().path.file_name().unwrap().to_string_lossy().into_owned(), editor.document().lines.join("\n"), editor.is_dirty());
        workbench.editors.editors.iter().map(tab).collect()
    })
}

/// workingCopyFileService.ts `delete`: the dirty models of a deleted folder's files are soft-reverted, their text kept.
#[gpui::test]
fn a_delete_soft_reverts_the_dirty_models_inside(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "bulk-delete-soft-revert", "alpha");
    type_text(&workbench, "1", cx);
    assert_eq!(tabs(&workbench, cx), [("a.txt".into(), "1alpha".into(), true)]);
    workbench.update(cx, |workbench, cx| workbench.models_after_file_operation(1, &json!([{ "target": uri(&dir) }]), cx));
    assert_eq!(tabs(&workbench, cx), [("a.txt".into(), "1alpha".into(), false)]);
}

/// workingCopyFileService.ts `doMoveOrCopy`: a copy soft-reverts the target's dirty model, which then shows the copied file.
#[gpui::test]
fn a_copy_onto_a_dirty_model_reverts_it_to_the_copied_file(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "bulk-copy-revert", "alpha");
    let (a, b) = (dir.join("a.txt"), dir.join("b.txt"));
    type_text(&workbench, "1", cx);
    std::fs::write(&b, "beta").unwrap();
    std::fs::copy(&b, &a).unwrap();
    workbench.update(cx, |workbench, cx| workbench.models_after_file_operation(3, &json!([{ "source": uri(&b), "target": uri(&a), "bytes": b"beta".to_vec() }]), cx));
    cx.run_until_parked();
    assert_eq!(tabs(&workbench, cx), [("a.txt".into(), "beta".into(), false)]);
}

/// textFileEditorModelManager.ts `onDidRunWorkingCopyFileOperation` for a move onto an open model: the target model
/// takes the dirty source's text and stays dirty, and the source's editor goes where the group shows the target already.
#[gpui::test]
fn a_dirty_move_onto_an_open_model_gives_it_the_text(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "bulk-move-onto-open", "alpha");
    let (a, b) = (dir.join("a.txt"), dir.join("b.txt"));
    std::fs::write(&b, "beta").unwrap();
    workbench.update_in(cx, |workbench, window, cx| {
        workbench.open_file(b.clone(), true, window, cx);
        workbench.open_file(a.clone(), true, window, cx);
    });
    cx.run_until_parked();
    type_text(&workbench, "1", cx);
    std::fs::rename(&a, &b).unwrap();
    workbench.update(cx, |workbench, cx| workbench.models_after_file_operation(2, &json!([{ "source": uri(&a), "target": uri(&b) }]), cx));
    cx.run_until_parked();
    assert_eq!(tabs(&workbench, cx), [("b.txt".into(), "1alpha".into(), true)]);
    let active = workbench.read_with(cx, |workbench, _| workbench.editors.active().map(|editor| editor.document().path.clone()));
    assert_eq!(active, Some(b));
}

/// editorService.ts `handleMovedFile` replaces the source's editors in every group, not only the active one.
#[gpui::test]
fn a_move_onto_an_open_model_replaces_the_source_in_every_group(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "bulk-move-every-group", "alpha");
    let (a, b) = (dir.join("a.txt"), dir.join("b.txt"));
    std::fs::write(&b, "beta").unwrap();
    workbench.update_in(cx, |workbench, window, cx| {
        workbench.open_file(b.clone(), true, window, cx);
        workbench.open_file(a.clone(), true, window, cx);
    });
    cx.run_until_parked();
    type_text(&workbench, "1", cx);
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.splitEditor", vec![], window, cx)));
    cx.run_until_parked();
    std::fs::rename(&a, &b).unwrap();
    workbench.update(cx, |workbench, cx| workbench.models_after_file_operation(2, &json!([{ "source": uri(&a), "target": uri(&b) }]), cx));
    cx.run_until_parked();
    assert_eq!(tabs(&workbench, cx), [("b.txt".into(), "1alpha".into(), true)], "the split group's editor shows the target");
    let first_group = workbench.read_with(cx, |workbench, _| workbench.editors.other_groups[0].editors.iter().map(|editor| editor.document().path.clone()).collect::<Vec<_>>());
    assert_eq!(first_group, [b], "the first group's editor of the source closes where it shows the target");
}

/// The same move from a clean source: the target model reads the moved file, clean; a source with no editor of the
/// target in its group shows the target model in its place.
#[gpui::test]
fn a_clean_move_onto_an_open_model_reloads_it(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "bulk-move-onto-model", "alpha");
    let (a, b) = (dir.join("a.txt"), dir.join("b.txt"));
    std::fs::write(&b, "beta").unwrap();
    workbench.update(cx, |workbench, _| {
        let document = workbench.resolve_document("file", &b, &Value::Null).unwrap();
        workbench.editors.open_model(document);
    });
    std::fs::rename(&a, &b).unwrap();
    workbench.update(cx, |workbench, cx| workbench.models_after_file_operation(2, &json!([{ "source": uri(&a), "target": uri(&b) }]), cx));
    cx.run_until_parked();
    assert_eq!(tabs(&workbench, cx), [("b.txt".into(), "alpha".into(), false)]);
    let models = workbench.read_with(cx, |workbench, _| workbench.editors.all_editors().filter(|editor| editor.document().is("file", &b)).count());
    let shared = workbench.read_with(cx, |workbench, _| std::rc::Rc::ptr_eq(&workbench.editors.editors[0].model, &workbench.editors.models.iter().find(|model| model.document().is("file", &b)).unwrap().model));
    assert_eq!((models, shared), (2, true), "the tab and the model without an editor share the target's text model");
}