дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-25 02:44:37 +02:00
`HostConfig` (9 fields) was written out as a full struct literal in about 100 tests of `ide-host`, almost all of them the same boilerplate, so a new field meant editing ~100 places. This adds one base constructor for tests and migrates every literal that is a plain variant of it. Tests that differ keep struct-update overrides (`HostConfig { sink_for: .., ..HostConfig::for_test(&base) }`). No builder.
- Unit tests (`src/`): `#[cfg(test)] pub(crate) fn HostConfig::for_test(base: &Path)` in `lib.rs`.
- Integration tests (`tests/`): `tests/common/mod.rs` with `host_config(base: &Path)`, pulled in with `mod common;`. It builds the same value as `for_test`.
Both build: `config`, `data`, and `data` again as the machine-local dir (the layout most tests used), `LanguageRegistry::load(None)`, empty syntax, a sink that drops events, identity owner, `Handle::current()`, `project_environment: false`.
Why not one constructor for both: integration tests compile against the crate without `cfg(test)`, so a shared one needs a new `testing` cargo feature plus a dev-dependency on the crate itself (nothing in `ide-host` does that today). Two small definitions need no feature, no `Cargo.toml` change and no public item.
Co-authored-by: Mr0Bread <[email protected]>
142 рядки
6.4 KiB
Rust
142 рядки
6.4 KiB
Rust
mod common;
|
|
|
|
use ide_core::{ChangeSet, TextChange};
|
|
use ide_host::{Host, ops::vcs};
|
|
use std::{path::Path, process::Command, time::Duration};
|
|
|
|
fn git(root: &Path, args: &[&str]) {
|
|
let out = Command::new("git").current_dir(root).args(args).output().unwrap();
|
|
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn gbk_document_gutter_diff_stage_and_rollback_share_encoding() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let base = dunce::canonicalize(temp.path()).unwrap();
|
|
let root = base.join("project");
|
|
std::fs::create_dir(&root).unwrap();
|
|
git(&root, &["init", "-b", "main"]);
|
|
git(&root, &["config", "user.name", "Test"]);
|
|
git(&root, &["config", "user.email", "[email protected]"]);
|
|
git(&root, &["config", "commit.gpgsign", "false"]);
|
|
git(&root, &["config", "core.autocrlf", "false"]);
|
|
let path = root.join("Example.java");
|
|
let old = "// 中文注释\nclass Example {\n // 保持不变\n int value = 1;\n}\n";
|
|
let original = ide_vfs::encode(old, "gbk", false).unwrap();
|
|
std::fs::write(&path, &original).unwrap();
|
|
std::fs::write(root.join("Utf8.java"), old).unwrap();
|
|
git(&root, &["add", "."]);
|
|
git(&root, &["commit", "-m", "GBK Java"]);
|
|
let host = Host::new(common::host_config(&base));
|
|
host.trust.decide(&root, true, false).unwrap();
|
|
let project = host.open_project("main", &root).unwrap();
|
|
for _ in 0..500 {
|
|
if !project.repos().is_empty() {
|
|
break;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
}
|
|
let mut defaults = project.documents.file_defaults();
|
|
defaults.project_fallback = "gbk".into();
|
|
project.documents.set_file_defaults(defaults);
|
|
assert_eq!(project.text_of(&path).unwrap(), old);
|
|
assert_eq!(vcs::vcs_file_at(&host, "main", path.clone(), None).await.unwrap().as_deref(), Some(old));
|
|
let doc = project.documents.open(&path).unwrap();
|
|
assert_eq!(doc.encoding, "gbk");
|
|
// A live document's explicit selection wins over later project defaults.
|
|
let mut defaults = project.documents.file_defaults();
|
|
defaults.project_fallback = "windows-1252".into();
|
|
project.documents.set_file_defaults(defaults);
|
|
assert!(vcs::vcs_line_hunks(&host, "main", path.clone()).await.unwrap().is_empty());
|
|
let from = old.find("value = 1").unwrap() + "value = ".len();
|
|
let offset = old[..from].encode_utf16().count() as u32;
|
|
project
|
|
.documents
|
|
.edit(
|
|
doc.id,
|
|
doc.version,
|
|
ChangeSet { changes: vec![TextChange { from: offset, to: offset + 1, insert: "2".into() }] },
|
|
)
|
|
.unwrap();
|
|
let hunks = vcs::vcs_line_hunks(&host, "main", path.clone()).await.unwrap();
|
|
assert_eq!(hunks.len(), 1);
|
|
assert_eq!((hunks[0].start, hunks[0].lines), (3, 1));
|
|
assert_eq!(hunks[0].old_text, " int value = 1;\n");
|
|
project.documents.save(doc.id).unwrap();
|
|
vcs::vcs_stage_hunks(&host, "main", path.clone(), vec![0]).await.unwrap();
|
|
let repo = ide_vcs::Repo::discover(&root).unwrap().unwrap();
|
|
assert_eq!(
|
|
repo.file_bytes_at(&path, Some(":0")).unwrap().unwrap(),
|
|
ide_vfs::encode(&old.replace("value = 1", "value = 2"), "gbk", false).unwrap()
|
|
);
|
|
repo.unstage(std::slice::from_ref(&path)).unwrap();
|
|
let snapshot = project.documents.snapshot(doc.id).unwrap();
|
|
let line_start =
|
|
snapshot.text.split_inclusive('\n').take(3).map(|s| s.encode_utf16().count()).sum::<usize>() as u32;
|
|
project
|
|
.documents
|
|
.edit(
|
|
doc.id,
|
|
snapshot.version,
|
|
ChangeSet {
|
|
changes: vec![TextChange {
|
|
from: line_start,
|
|
to: line_start + " int value = 2;\n".encode_utf16().count() as u32,
|
|
insert: hunks[0].old_text.clone(),
|
|
}],
|
|
},
|
|
)
|
|
.unwrap();
|
|
project.documents.save(doc.id).unwrap();
|
|
assert_eq!(std::fs::read(&path).unwrap(), original);
|
|
assert!(vcs::vcs_line_hunks(&host, "main", path.clone()).await.unwrap().is_empty());
|
|
assert!(vcs::vcs_blame(&host, "main", path.clone(), None, None).await.unwrap().iter().all(|l| l.author == "Test"));
|
|
// Missing working files still use configured encoding for historical content.
|
|
project.documents.close(doc.id);
|
|
let mut defaults = project.documents.file_defaults();
|
|
defaults.project_fallback = "gbk".into();
|
|
project.documents.set_file_defaults(defaults);
|
|
std::fs::remove_file(&path).unwrap();
|
|
assert_eq!(vcs::vcs_file_at(&host, "main", path, None).await.unwrap().as_deref(), Some(old));
|
|
let utf8 = root.join("Utf8.java");
|
|
std::fs::remove_file(&utf8).unwrap();
|
|
assert_eq!(vcs::vcs_file_at(&host, "main", utf8, None).await.unwrap().as_deref(), Some(old));
|
|
host.close_project("main");
|
|
}
|
|
|
|
/// A committed symlink to a folder (#3528): its revision is the link target, even though the
|
|
/// working path is a directory.
|
|
#[cfg(unix)]
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn a_directory_symlink_revision_is_its_target() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let base = dunce::canonicalize(temp.path()).unwrap();
|
|
let root = base.join("project");
|
|
std::fs::create_dir_all(root.join("target")).unwrap();
|
|
git(&root, &["init", "-b", "main"]);
|
|
git(&root, &["config", "user.name", "Test"]);
|
|
git(&root, &["config", "user.email", "[email protected]"]);
|
|
git(&root, &["config", "commit.gpgsign", "false"]);
|
|
std::fs::write(root.join("target/file.txt"), "example\n").unwrap();
|
|
std::os::unix::fs::symlink("target", root.join("dir-link")).unwrap();
|
|
git(&root, &["add", "."]);
|
|
git(&root, &["commit", "-m", "Add a directory symlink"]);
|
|
let host = Host::new(common::host_config(&base));
|
|
host.trust.decide(&root, true, false).unwrap();
|
|
let project = host.open_project("main", &root).unwrap();
|
|
for _ in 0..500 {
|
|
if !project.repos().is_empty() {
|
|
break;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
}
|
|
let link = root.join("dir-link");
|
|
assert!(link.is_dir());
|
|
assert_eq!(
|
|
vcs::vcs_file_at(&host, "main", link.clone(), Some("HEAD".into())).await.unwrap().as_deref(),
|
|
Some("target")
|
|
);
|
|
assert_eq!(vcs::vcs_file_mode(&host, "main", link, Some("HEAD".into())).await.unwrap().as_deref(), Some("120000"));
|
|
host.close_project("main");
|
|
}
|