Skip to content

Visual Studio Code, rebuilt in Rust

Rust
6.9 KB
Raw Blame
use serde_json::json;

use super::*;

/// port/fixtures/settings-import: a macOS home folder with VS Code, Cursor and VSCodium user data.
fn fixture_home() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../../port/fixtures/settings-import")
}

fn fixture_sources() -> Vec<Source> {
    sources_in(&fixture_home(), &fixture_home().join("Library/Application Support"))
}

fn source(name: &str) -> Source {
    fixture_sources().into_iter().find(|source| source.name == name).unwrap()
}

#[test]
fn finds_the_three_editors() {
    let names: Vec<&str> = fixture_sources().iter().map(|source| source.name).collect();
    assert_eq!(names, ["VS Code", "Cursor", "VSCodium"]);
    assert!(sources(Path::new("/nonexistent")).is_empty());
}

#[test]
fn reads_vs_code_without_the_removed_features() {
    let import = read(&source("VS Code"));
    let keys: Vec<&str> = import.settings.keys().map(String::as_str).collect();
    assert_eq!(keys, ["editor.fontSize", "workbench.colorTheme", "workbench.iconTheme", "[markdown]"]);
    assert_eq!(import.settings["[markdown]"], json!({ "editor.wordWrap": "on" }));
    assert_eq!(import.keybindings, [json!({ "key": "cmd+k cmd+t", "command": "workbench.action.selectTheme" })]);
    assert_eq!(import.snippets.iter().map(|(name, _)| name.as_str()).collect::<Vec<_>>(), ["rust.json"]);
    assert_eq!(import.extensions, ["rust-lang.rust-analyzer"]);
}

#[test]
fn reads_cursor_and_vscodium() {
    let cursor = read(&source("Cursor"));
    assert_eq!(cursor.settings.keys().collect::<Vec<_>>(), ["editor.tabSize", "workbench.colorTheme"]);
    assert_eq!(cursor.extensions, ["esbenp.prettier-vscode"]);
    let vscodium = read(&source("VSCodium"));
    assert_eq!(vscodium.settings["workbench.colorTheme"], "Quiet Light");
    assert_eq!(vscodium.snippets.iter().map(|(name, _)| name.as_str()).collect::<Vec<_>>(), ["global.code-snippets"]);
    // No extensions.json: the folder names give the ids.
    assert_eq!(vscodium.extensions, ["vscodevim.vim"]);
}

#[test]
fn writes_into_the_profile_keeping_what_it_has() {
    let root = std::env::temp_dir().join(format!("app-settings-import-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(root.join("snippets")).unwrap();
    std::fs::write(root.join("settings.json"), "{\n\t// mine\n\t\"editor.fontSize\": 12,\n\t\"files.trimTrailingWhitespace\": true\n}").unwrap();
    std::fs::write(root.join("keybindings.json"), "[\n\t{ \"key\": \"cmd+k cmd+t\", \"command\": \"workbench.action.selectTheme\" }\n]").unwrap();
    let configuration = config::service::Settings::load(config::registry::Registry::core(), root.join("User"), None).configuration;
    let target = Target { settings: &root.join("settings.json"), keybindings: &root.join("keybindings.json"), snippets_home: &root.join("snippets") };

    let written = write(&read(&source("VS Code")), &target, &configuration).unwrap();
    assert_eq!(written, Written { settings: 4, keybindings: 0, snippets: 1 });
    let written = write(&read(&source("Cursor")), &target, &configuration).unwrap();
    assert_eq!(written, Written { settings: 2, keybindings: 1, snippets: 0 });

    let settings = std::fs::read_to_string(root.join("settings.json")).unwrap();
    assert!(settings.contains("// mine"));
    let settings = json::parse(&settings, &mut Vec::new(), parse_options()).unwrap();
    assert_eq!(settings["editor.fontSize"], 15);
    assert_eq!(settings["files.trimTrailingWhitespace"], true);
    assert_eq!(settings["workbench.colorTheme"], "Monokai");
    assert_eq!(settings["editor.tabSize"], 2);
    let keybindings = json::parse(&std::fs::read_to_string(root.join("keybindings.json")).unwrap(), &mut Vec::new(), parse_options()).unwrap();
    assert_eq!(keybindings.as_array().unwrap().len(), 2);
    assert!(root.join("snippets/rust.json").is_file());
    std::fs::remove_dir_all(&root).unwrap();
}

/// A profile without keybindings gets `keybindings.json` as the user wrote it: its comments stay.
#[test]
fn an_empty_profile_takes_keybindings_json_with_its_comments() {
    let root = std::env::temp_dir().join(format!("app-settings-import-comments-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    let configuration = config::service::Settings::load(config::registry::Registry::core(), root.join("User"), None).configuration;
    let target = Target { settings: &root.join("settings.json"), keybindings: &root.join("keybindings.json"), snippets_home: &root.join("snippets") };

    let written = write(&Import { snippets: Vec::new(), ..read(&source("VS Code")) }, &target, &configuration).unwrap();
    assert_eq!(written.keybindings, 1);
    let keybindings = std::fs::read_to_string(root.join("keybindings.json")).unwrap();
    // jsonEdit.ts `removeProperty` formats the lines it touches: the entry before the removed one.
    let expected = "// Place your key bindings in this file to override the defaults\n[\n\t{\n\t\t\"key\": \"cmd+k cmd+t\",\n\t\t\"command\": \"workbench.action.selectTheme\"\n\t}\n]";
    assert_eq!(keybindings.trim_end(), expected);
    std::fs::remove_dir_all(&root).unwrap();
}

#[test]
fn finds_an_extensions_folder_by_extensions_json_else_by_its_manifest() {
    let vscode = source("VS Code").extensions_dir;
    assert_eq!(extension_folder(&vscode, "rust-lang.rust-analyzer"), Some(vscode.join("rust-lang.rust-analyzer-0.3.2000")));
    assert_eq!(extension_folder(&vscode, "vscodevim.vim"), None);
    let vscodium = source("VSCodium").extensions_dir;
    assert_eq!(extension_folder(&vscodium, "vscodevim.vim"), Some(vscodium.join("vscodevim.vim-1.29.0")));
}

/// The extensions VS Code has disabled (`extensionsIdentifiers/disabled` in its global storage) are read, and the
/// database is opened read-only.
#[test]
fn reads_the_disabled_extensions() {
    let home = std::env::temp_dir().join(format!("app-settings-import-disabled-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&home);
    let source = Source { name: "VS Code", user_dir: home.join("Code/User"), extensions_dir: home.join(".vscode/extensions") };
    std::fs::create_dir_all(source.user_dir.join("globalStorage")).unwrap();
    std::fs::create_dir_all(&source.extensions_dir).unwrap();
    std::fs::write(source.extensions_dir.join("extensions.json"), r#"[{ "identifier": { "id": "alpha.one" } }, { "identifier": { "id": "Beta.Two" } }]"#).unwrap();
    let database = source.user_dir.join("globalStorage/state.vscdb");
    storage::Storage::open(&database).set("extensionsIdentifiers/disabled", r#"[{ "id": "beta.two" }, { "id": "github.copilot-chat" }]"#.into());
    let modified = std::fs::metadata(&database).unwrap().modified().unwrap();

    let import = read(&source);
    assert_eq!(import.extensions, ["alpha.one", "beta.two"]);
    assert_eq!(import.disabled_extensions, ["beta.two"]);
    assert_eq!(std::fs::metadata(&database).unwrap().modified().unwrap(), modified);
    std::fs::remove_dir_all(&home).unwrap();
}