Файли
openidea/crates/ide-host/tests/settings_sync.rs
T
LU XIANGRUIтаwildcatDownstairs 3811dfb512 feat(settings): sync the list of installed plugins (#6756)
Adds a Plugins category to Settings Sync. plugins.json maps each computer's random id to the plugins installed in its data directory; every computer writes only its own list, so lists never conflict and an uninstall only shortens one of them. After a sync, plugins other computers have and this one lacks are looked up in the marketplace and offered in a notification under the marketplace's names; ids that are not marketplace ids are dropped. Install... lets the user pick (each goes through the usual install and consent page) and Ignore These Plugins declines them; nothing is installed or uninstalled on its own. An unreadable plugins.json is left untouched.

The hidden plugins.installFromMarket action now accepts a single id (Import Settings) or a list of ids (Settings Sync).

Closes #6750

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 14:37:35 +08:00

163 рядки
8.2 KiB
Rust

//! Settings Sync between two machines through a local bare repository.
mod common;
use ide_host::Host;
use ide_host::ops::settings_sync::{SettingsSyncMode, SettingsSyncResult, has_local_changes, sync_now};
use ide_ssh::secrets::testing::use_memory_store;
use std::collections::BTreeMap;
use std::path::Path;
type Storage = BTreeMap<String, BTreeMap<String, String>>;
fn machine(dir: &Path, remote: &Path, settings: &str) -> Host {
std::fs::create_dir_all(dir.join("config")).unwrap();
let repository = format!("[settingsSync]\nrepository = {:?}\n", remote.to_str().unwrap());
std::fs::write(dir.join("config/settings.toml"), format!("{repository}{settings}")).unwrap();
Host::new(common::host_config(dir))
}
fn storage(entries: &[(&str, &str, &str)]) -> Storage {
let mut out = Storage::new();
for (category, key, value) in entries {
out.entry((*category).into()).or_default().insert((*key).into(), (*value).into());
}
out
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn two_machines_sync_through_a_bare_repository() {
use_memory_store();
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("settings.git");
assert!(std::process::Command::new("git").args(["init", "--bare", "-q"]).arg(&remote).status().unwrap().success());
let a = machine(
&tmp.path().join("a"),
&remote,
"[general]\nconfirmExit = false\nlastRunVersion = \"a\"\n[editor]\nfontSize = 15\n\
[languages.python]\ninterpreter = \"/a/bin/python\"\n[proxy]\npassword = \"p\"\n[terminal.env]\nAWS_ACCESS_KEY_ID = \"AKIA-SECRET\"\n",
);
let b = machine(
&tmp.path().join("b"),
&remote,
"[general]\nlastRunVersion = \"b\"\n[editor]\ntabSize = 2\n[languages.python]\ninterpreter = \"/b/bin/python\"\n",
);
let macros = storage(&[("macros", "openidea.macros.v1", "[\"a\"]")]);
let a_storage = storage(&[("macros", "openidea.macros.v1", "[\"a\"]"), ("plugins", "acme.minimap", "Minimap")]);
// A pushes first; B finds settings it never synced with and is asked.
assert_eq!(sync_now(&a, &a_storage, None, false).unwrap(), SettingsSyncResult::default());
assert!(sync_now(&b, &Storage::new(), None, false).unwrap().needs_choice);
let merged = sync_now(&b, &Storage::new(), Some(SettingsSyncMode::Merge), false).unwrap();
assert!(merged.settings_changed);
assert_eq!(merged.storage, macros);
let minimap = BTreeMap::from([("acme.minimap".to_owned(), "Minimap".to_owned())]);
assert_eq!(merged.plugins_elsewhere, minimap, "offered, not installed");
let settings = b.global_settings.effective();
assert_eq!((settings.editor.font_size, settings.editor.tab_size), (15.0, 2));
assert_eq!(settings.languages.python.interpreter, "/b/bin/python", "machine paths stay");
assert_eq!((settings.general.confirm_exit, settings.general.last_run_version.as_str()), (false, "b"));
// A picks up B's change; nothing secret or machine-specific reached the repository.
assert!(sync_now(&a, &macros, None, false).unwrap().settings_changed);
assert_eq!(a.global_settings.effective().editor.tab_size, 2);
assert_eq!(a.global_settings.effective().languages.python.interpreter, "/a/bin/python");
let log = std::process::Command::new("git").current_dir(&remote).args(["log", "-p", "main"]).output().unwrap();
let log = String::from_utf8(log.stdout).unwrap();
assert!(
log.contains("\"fontSize\": 15")
&& !log.contains("python")
&& !log.contains("password")
&& !log.contains("lastRunVersion")
&& !log.contains("AKIA-SECRET"),
"{log}"
);
// Nothing changed since: the check after a settings change stays off the network.
assert!(!has_local_changes(&a, &macros).unwrap());
assert!(has_local_changes(&a, &a_storage).unwrap(), "a newly installed plugin counts");
a.global_settings
.set(ide_workspace::settings::SettingsScope::Global, "languages.python.interpreter", "/a/py2".into())
.unwrap();
assert!(!has_local_changes(&a, &macros).unwrap(), "machine paths do not count");
// Both change the font size: each machine's own value wins where it syncs, and is reported.
a.global_settings.set(ide_workspace::settings::SettingsScope::Global, "editor.fontSize", 16.into()).unwrap();
assert!(has_local_changes(&a, &macros).unwrap());
b.global_settings.set(ide_workspace::settings::SettingsScope::Global, "editor.fontSize", 17.into()).unwrap();
sync_now(&a, &macros, None, false).unwrap();
let late = sync_now(&b, &macros, None, false).unwrap();
assert_eq!(late.overwritten, ["editor.fontSize"]);
sync_now(&a, &macros, None, false).unwrap();
assert_eq!(a.global_settings.effective().editor.font_size, 17.0);
}
/// Commits `contents` over `name` in the repository (`None` deletes it), as another tool would.
fn edit_remote(remote: &Path, work: &Path, name: &str, contents: Option<&str>) {
let git = |dir: &Path, args: &[&str]| {
let mut command = std::process::Command::new("git");
command.current_dir(dir).args(["-c", "user.name=t", "-c", "[email protected]"]).args(args);
assert!(command.status().unwrap().success(), "{args:?}");
};
let _ = std::fs::remove_dir_all(work);
git(remote.parent().unwrap(), &["clone", "-q", remote.to_str().unwrap(), work.to_str().unwrap()]);
match contents {
Some(text) => std::fs::write(work.join(name), text).unwrap(),
None => std::fs::remove_file(work.join(name)).unwrap(),
}
git(work, &["commit", "-qam", "edit"]);
git(work, &["push", "-q", "origin", "HEAD:main"]);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_broken_or_missing_file_never_clears_this_computers_settings() {
use_memory_store();
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("settings.git");
assert!(
std::process::Command::new("git")
.args(["init", "--bare", "-q", "-b", "main"])
.arg(&remote)
.status()
.unwrap()
.success()
);
let a = machine(&tmp.path().join("a"), &remote, "[editor]\nfontSize = 15\n");
let macros = storage(&[("macros", "openidea.macros.v1", "[\"a\"]")]);
sync_now(&a, &macros, None, false).unwrap();
let show = |name: &str| {
let spec = format!("main:{name}");
let out = std::process::Command::new("git").current_dir(&remote).args(["show", &spec]).output().unwrap();
String::from_utf8(out.stdout).unwrap()
};
// Conflict markers or another shape: the sync stops, naming the file, and changes nothing.
for (name, broken) in [("editor.json", "<<<<<<< theirs\n{}\n"), ("macros.json", "[]\n")] {
let good = show(name);
edit_remote(&remote, &tmp.path().join("work"), name, Some(broken));
let error = sync_now(&a, &macros, None, false).unwrap_err();
assert!(error.message.contains(name), "{}", error.message);
assert_eq!(a.global_settings.effective().editor.font_size, 15.0, "{name}");
assert_eq!(show(name), broken, "{name} is left for the user to repair");
edit_remote(&remote, &tmp.path().join("work"), name, Some(&good));
let repaired = sync_now(&a, &macros, None, false).unwrap();
assert!(!repaired.settings_changed && repaired.storage.is_empty(), "{repaired:?}");
}
// An unreadable plugin list is left alone; the rest still syncs.
let good = show("plugins.json");
edit_remote(&remote, &tmp.path().join("work"), "plugins.json", Some("<<<<<<< theirs\n"));
let with_plugin = storage(&[("macros", "openidea.macros.v1", "[\"a\"]"), ("plugins", "acme.minimap", "Minimap")]);
assert!(sync_now(&a, &with_plugin, None, false).unwrap().plugins_elsewhere.is_empty());
assert_eq!(show("plugins.json"), "<<<<<<< theirs\n");
edit_remote(&remote, &tmp.path().join("work"), "plugins.json", Some(&good));
// A deleted file is written again from this computer's settings.
edit_remote(&remote, &tmp.path().join("work"), "editor.json", None);
let result = sync_now(&a, &macros, None, false).unwrap();
assert!(!result.settings_changed, "{result:?}");
assert_eq!(a.global_settings.effective().editor.font_size, 15.0);
assert!(show("editor.json").contains("\"fontSize\": 15"));
}