mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-30 20:40:04 +01:00
5504e87bfd
# Objective This is another fix related to the path handling issue when connecting from Unix to Windows, similar to #62038 and #63075, but confined to the path handling in the security modal. The root cause is the same: when using the standard library `Path` API across platforms — especially connecting from Unix to Windows — these `Path` methods give wrong results. The bugs I have observed include: 1. When opening a single file, its parent folder is not shown properly in the project list. 2. When opening a folder, no parent folder is auto-filled into the input field after "Trust all projects in". 3. When Zed runs in debug mode, opening two untrusted folders, checking "Trust parents", and then trusting would result in a panic. ## Solution Use the corresponding methods in `PathStyle`, which is designed for handling paths across platforms, instead of the `Path` methods in the relevant code. ## Testing Built and tested locally, with comparison screenshots attached. ## Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards ([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) and [icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md) guidelines) - [ ] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable ## Showcase || Before | After | |:--:|:--:|:--:| | Auto fill |<img width="657" height="343" alt="auto_fill_before" src="https://github.com/user-attachments/assets/e36ca3d5-998c-4bed-94e9-f97dd0e7ebf4" />| <img width="654" height="333" alt="auto_fill_after" src="https://github.com/user-attachments/assets/c8413fb5-d2dd-427a-afb0-9cbebc033742" /> | | Single file | <img width="654" height="322" alt="single_file_before" src="https://github.com/user-attachments/assets/bfe87ab0-bf87-491a-8f85-b5a2681763a9" />| <img width="657" height="325" alt="single_file_after" src="https://github.com/user-attachments/assets/0437cbde-42c3-44f6-847b-abd33e54cd2c" /> | --- Release Notes: - Fixed trusting projects on remote Windows machines from Unix systems
1076 lines
35 KiB
Rust
1076 lines
35 KiB
Rust
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
|
|
|
use collections::HashSet;
|
|
use gpui::{Entity, TestAppContext};
|
|
use serde_json::json;
|
|
use settings::SettingsStore;
|
|
use util::path;
|
|
|
|
use crate::{FakeFs, Project};
|
|
|
|
use project::{trusted_worktrees::*, worktree_store::WorktreeStore};
|
|
|
|
fn init_test(cx: &mut TestAppContext) {
|
|
cx.update(|cx| {
|
|
if cx.try_global::<SettingsStore>().is_none() {
|
|
let settings_store = SettingsStore::test(cx);
|
|
cx.set_global(settings_store);
|
|
}
|
|
if cx.try_global::<TrustedWorktrees>().is_some() {
|
|
cx.remove_global::<TrustedWorktrees>();
|
|
}
|
|
});
|
|
}
|
|
|
|
fn init_trust_global(
|
|
worktree_store: Entity<WorktreeStore>,
|
|
cx: &mut TestAppContext,
|
|
) -> Entity<TrustedWorktreesStore> {
|
|
cx.update(|cx| {
|
|
init(DbTrustedPaths::default(), cx);
|
|
track_worktree_trust(worktree_store, None, None, None, cx);
|
|
TrustedWorktrees::try_get_global(cx).expect("global should be set")
|
|
})
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_single_worktree_trust(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(path!("/root"), json!({ "main.rs": "fn main() {}" }))
|
|
.await;
|
|
|
|
let project = Project::test(fs, [path!("/root").as_ref()], cx).await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_id = worktree_store.read_with(cx, |store, cx| {
|
|
store.worktrees().next().unwrap().read(cx).id()
|
|
});
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let events: Rc<RefCell<Vec<TrustedWorktreesEvent>>> = Rc::default();
|
|
cx.update({
|
|
let events = events.clone();
|
|
|cx| {
|
|
cx.subscribe(&trusted_worktrees, move |_, event, _| {
|
|
events.borrow_mut().push(match event {
|
|
TrustedWorktreesEvent::Trusted(host, paths) => {
|
|
TrustedWorktreesEvent::Trusted(host.clone(), paths.clone())
|
|
}
|
|
TrustedWorktreesEvent::Restricted(host, paths) => {
|
|
TrustedWorktreesEvent::Restricted(host.clone(), paths.clone())
|
|
}
|
|
});
|
|
})
|
|
}
|
|
})
|
|
.detach();
|
|
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(!can_trust, "worktree should be restricted by default");
|
|
|
|
{
|
|
let events = events.borrow();
|
|
assert_eq!(events.len(), 1);
|
|
match &events[0] {
|
|
TrustedWorktreesEvent::Restricted(event_worktree_store, paths) => {
|
|
assert_eq!(event_worktree_store, &worktree_store.downgrade());
|
|
assert!(paths.contains(&PathTrust::Worktree(worktree_id)));
|
|
}
|
|
_ => panic!("expected Restricted event"),
|
|
}
|
|
}
|
|
|
|
let has_restricted = trusted_worktrees.read_with(cx, |store, cx| {
|
|
store.has_restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(has_restricted, "should have restricted worktrees");
|
|
|
|
let restricted = trusted_worktrees.read_with(cx, |trusted_worktrees, cx| {
|
|
trusted_worktrees.restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(restricted.iter().any(|(id, _)| *id == worktree_id));
|
|
|
|
events.borrow_mut().clear();
|
|
|
|
let can_trust_again = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(!can_trust_again, "worktree should still be restricted");
|
|
assert!(
|
|
events.borrow().is_empty(),
|
|
"no duplicate Restricted event on repeated can_trust"
|
|
);
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_id)]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
{
|
|
let events = events.borrow();
|
|
assert_eq!(events.len(), 1);
|
|
match &events[0] {
|
|
TrustedWorktreesEvent::Trusted(event_worktree_store, paths) => {
|
|
assert_eq!(event_worktree_store, &worktree_store.downgrade());
|
|
assert!(paths.contains(&PathTrust::Worktree(worktree_id)));
|
|
}
|
|
_ => panic!("expected Trusted event"),
|
|
}
|
|
}
|
|
|
|
let can_trust_after = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(can_trust_after, "worktree should be trusted after trust()");
|
|
|
|
let has_restricted_after = trusted_worktrees.read_with(cx, |store, cx| {
|
|
store.has_restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(
|
|
!has_restricted_after,
|
|
"should have no restricted worktrees after trust"
|
|
);
|
|
|
|
let restricted_after = trusted_worktrees.read_with(cx, |trusted_worktrees, cx| {
|
|
trusted_worktrees.restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(
|
|
restricted_after.is_empty(),
|
|
"restricted set should be empty"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_single_file_worktree_trust(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(path!("/root"), json!({ "foo.rs": "fn foo() {}" }))
|
|
.await;
|
|
|
|
let project = Project::test(fs, [path!("/root/foo.rs").as_ref()], cx).await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_id = worktree_store.read_with(cx, |store, cx| {
|
|
let worktree = store.worktrees().next().unwrap();
|
|
let worktree = worktree.read(cx);
|
|
assert!(worktree.is_single_file(), "expected single-file worktree");
|
|
worktree.id()
|
|
});
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let events: Rc<RefCell<Vec<TrustedWorktreesEvent>>> = Rc::default();
|
|
cx.update({
|
|
let events = events.clone();
|
|
|cx| {
|
|
cx.subscribe(&trusted_worktrees, move |_, event, _| {
|
|
events.borrow_mut().push(match event {
|
|
TrustedWorktreesEvent::Trusted(host, paths) => {
|
|
TrustedWorktreesEvent::Trusted(host.clone(), paths.clone())
|
|
}
|
|
TrustedWorktreesEvent::Restricted(host, paths) => {
|
|
TrustedWorktreesEvent::Restricted(host.clone(), paths.clone())
|
|
}
|
|
});
|
|
})
|
|
}
|
|
})
|
|
.detach();
|
|
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(
|
|
!can_trust,
|
|
"single-file worktree should be restricted by default"
|
|
);
|
|
|
|
{
|
|
let events = events.borrow();
|
|
assert_eq!(events.len(), 1);
|
|
match &events[0] {
|
|
TrustedWorktreesEvent::Restricted(event_worktree_store, paths) => {
|
|
assert_eq!(event_worktree_store, &worktree_store.downgrade());
|
|
assert!(paths.contains(&PathTrust::Worktree(worktree_id)));
|
|
}
|
|
_ => panic!("expected Restricted event"),
|
|
}
|
|
}
|
|
|
|
events.borrow_mut().clear();
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_id)]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
{
|
|
let events = events.borrow();
|
|
assert_eq!(events.len(), 1);
|
|
match &events[0] {
|
|
TrustedWorktreesEvent::Trusted(event_worktree_store, paths) => {
|
|
assert_eq!(event_worktree_store, &worktree_store.downgrade());
|
|
assert!(paths.contains(&PathTrust::Worktree(worktree_id)));
|
|
}
|
|
_ => panic!("expected Trusted event"),
|
|
}
|
|
}
|
|
|
|
let can_trust_after = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(
|
|
can_trust_after,
|
|
"single-file worktree should be trusted after trust()"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_multiple_single_file_worktrees_trust_one(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/root"),
|
|
json!({
|
|
"a.rs": "fn a() {}",
|
|
"b.rs": "fn b() {}",
|
|
"c.rs": "fn c() {}"
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[
|
|
path!("/root/a.rs").as_ref(),
|
|
path!("/root/b.rs").as_ref(),
|
|
path!("/root/c.rs").as_ref(),
|
|
],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_ids: Vec<_> = worktree_store.read_with(cx, |store, cx| {
|
|
store
|
|
.worktrees()
|
|
.map(|worktree| {
|
|
let worktree = worktree.read(cx);
|
|
assert!(worktree.is_single_file());
|
|
worktree.id()
|
|
})
|
|
.collect()
|
|
});
|
|
assert_eq!(worktree_ids.len(), 3);
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
for &worktree_id in &worktree_ids {
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(
|
|
!can_trust,
|
|
"worktree {worktree_id:?} should be restricted initially"
|
|
);
|
|
}
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_ids[1])]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
let can_trust_0 = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[0], cx)
|
|
});
|
|
let can_trust_1 = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[1], cx)
|
|
});
|
|
let can_trust_2 = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[2], cx)
|
|
});
|
|
|
|
assert!(!can_trust_0, "worktree 0 should still be restricted");
|
|
assert!(can_trust_1, "worktree 1 should be trusted");
|
|
assert!(!can_trust_2, "worktree 2 should still be restricted");
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_two_directory_worktrees_separate_trust(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/projects"),
|
|
json!({
|
|
"project_a": { "main.rs": "fn main() {}" },
|
|
"project_b": { "lib.rs": "pub fn lib() {}" }
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[
|
|
path!("/projects/project_a").as_ref(),
|
|
path!("/projects/project_b").as_ref(),
|
|
],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_ids: Vec<_> = worktree_store.read_with(cx, |store, cx| {
|
|
store
|
|
.worktrees()
|
|
.map(|worktree| {
|
|
let worktree = worktree.read(cx);
|
|
assert!(!worktree.is_single_file());
|
|
worktree.id()
|
|
})
|
|
.collect()
|
|
});
|
|
assert_eq!(worktree_ids.len(), 2);
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let can_trust_a = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[0], cx)
|
|
});
|
|
let can_trust_b = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[1], cx)
|
|
});
|
|
assert!(!can_trust_a, "project_a should be restricted initially");
|
|
assert!(!can_trust_b, "project_b should be restricted initially");
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_ids[0])]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
let can_trust_a = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[0], cx)
|
|
});
|
|
let can_trust_b = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[1], cx)
|
|
});
|
|
assert!(can_trust_a, "project_a should be trusted after trust()");
|
|
assert!(!can_trust_b, "project_b should still be restricted");
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_ids[1])]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
let can_trust_a = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[0], cx)
|
|
});
|
|
let can_trust_b = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_ids[1], cx)
|
|
});
|
|
assert!(can_trust_a, "project_a should remain trusted");
|
|
assert!(can_trust_b, "project_b should now be trusted");
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_directory_worktree_trust_enables_single_file(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/"),
|
|
json!({
|
|
"project": { "main.rs": "fn main() {}" },
|
|
"standalone.rs": "fn standalone() {}"
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[path!("/project").as_ref(), path!("/standalone.rs").as_ref()],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let (dir_worktree_id, file_worktree_id) = worktree_store.read_with(cx, |store, cx| {
|
|
let worktrees: Vec<_> = store.worktrees().collect();
|
|
assert_eq!(worktrees.len(), 2);
|
|
let (dir_worktree, file_worktree) = if worktrees[0].read(cx).is_single_file() {
|
|
(&worktrees[1], &worktrees[0])
|
|
} else {
|
|
(&worktrees[0], &worktrees[1])
|
|
};
|
|
assert!(!dir_worktree.read(cx).is_single_file());
|
|
assert!(file_worktree.read(cx).is_single_file());
|
|
(dir_worktree.read(cx).id(), file_worktree.read(cx).id())
|
|
});
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let can_trust_file = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, file_worktree_id, cx)
|
|
});
|
|
assert!(
|
|
!can_trust_file,
|
|
"single-file worktree should be restricted initially"
|
|
);
|
|
|
|
let can_trust_directory = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, dir_worktree_id, cx)
|
|
});
|
|
assert!(
|
|
!can_trust_directory,
|
|
"directory worktree should be restricted initially"
|
|
);
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(dir_worktree_id)]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
let can_trust_dir = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, dir_worktree_id, cx)
|
|
});
|
|
let can_trust_file_after = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, file_worktree_id, cx)
|
|
});
|
|
assert!(can_trust_dir, "directory worktree should be trusted");
|
|
assert!(
|
|
can_trust_file_after,
|
|
"single-file worktree should be trusted after directory worktree trust"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_parent_path_trust_enables_single_file(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/"),
|
|
json!({
|
|
"project": { "main.rs": "fn main() {}" },
|
|
"standalone.rs": "fn standalone() {}"
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[path!("/project").as_ref(), path!("/standalone.rs").as_ref()],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let (dir_worktree_id, file_worktree_id) = worktree_store.read_with(cx, |store, cx| {
|
|
let worktrees: Vec<_> = store.worktrees().collect();
|
|
assert_eq!(worktrees.len(), 2);
|
|
let (dir_worktree, file_worktree) = if worktrees[0].read(cx).is_single_file() {
|
|
(&worktrees[1], &worktrees[0])
|
|
} else {
|
|
(&worktrees[0], &worktrees[1])
|
|
};
|
|
assert!(!dir_worktree.read(cx).is_single_file());
|
|
assert!(file_worktree.read(cx).is_single_file());
|
|
(dir_worktree.read(cx).id(), file_worktree.read(cx).id())
|
|
});
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let can_trust_file = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, file_worktree_id, cx)
|
|
});
|
|
assert!(
|
|
!can_trust_file,
|
|
"single-file worktree should be restricted initially"
|
|
);
|
|
|
|
let can_trust_directory = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, dir_worktree_id, cx)
|
|
});
|
|
assert!(
|
|
!can_trust_directory,
|
|
"directory worktree should be restricted initially"
|
|
);
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::AbsPath(PathBuf::from(path!("/project")))]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
let can_trust_dir = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, dir_worktree_id, cx)
|
|
});
|
|
let can_trust_file_after = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, file_worktree_id, cx)
|
|
});
|
|
assert!(
|
|
can_trust_dir,
|
|
"directory worktree should be trusted after its parent is trusted"
|
|
);
|
|
assert!(
|
|
can_trust_file_after,
|
|
"single-file worktree should be trusted after directory worktree trust via its parent directory trust"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_abs_path_trust_covers_multiple_worktrees(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/root"),
|
|
json!({
|
|
"project_a": { "main.rs": "fn main() {}" },
|
|
"project_b": { "lib.rs": "pub fn lib() {}" }
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[
|
|
path!("/root/project_a").as_ref(),
|
|
path!("/root/project_b").as_ref(),
|
|
],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_ids: Vec<_> = worktree_store.read_with(cx, |store, cx| {
|
|
store
|
|
.worktrees()
|
|
.map(|worktree| worktree.read(cx).id())
|
|
.collect()
|
|
});
|
|
assert_eq!(worktree_ids.len(), 2);
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
for &worktree_id in &worktree_ids {
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(!can_trust, "worktree should be restricted initially");
|
|
}
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::AbsPath(PathBuf::from(path!("/root")))]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
for &worktree_id in &worktree_ids {
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(
|
|
can_trust,
|
|
"worktree should be trusted after parent path trust"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_auto_trust_all(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/"),
|
|
json!({
|
|
"project_a": { "main.rs": "fn main() {}" },
|
|
"project_b": { "lib.rs": "pub fn lib() {}" },
|
|
"single.rs": "fn single() {}"
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[
|
|
path!("/project_a").as_ref(),
|
|
path!("/project_b").as_ref(),
|
|
path!("/single.rs").as_ref(),
|
|
],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_ids: Vec<_> = worktree_store.read_with(cx, |store, cx| {
|
|
store
|
|
.worktrees()
|
|
.map(|worktree| worktree.read(cx).id())
|
|
.collect()
|
|
});
|
|
assert_eq!(worktree_ids.len(), 3);
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let events: Rc<RefCell<Vec<TrustedWorktreesEvent>>> = Rc::default();
|
|
cx.update({
|
|
let events = events.clone();
|
|
|cx| {
|
|
cx.subscribe(&trusted_worktrees, move |_, event, _| {
|
|
events.borrow_mut().push(match event {
|
|
TrustedWorktreesEvent::Trusted(host, paths) => {
|
|
TrustedWorktreesEvent::Trusted(host.clone(), paths.clone())
|
|
}
|
|
TrustedWorktreesEvent::Restricted(host, paths) => {
|
|
TrustedWorktreesEvent::Restricted(host.clone(), paths.clone())
|
|
}
|
|
});
|
|
})
|
|
}
|
|
})
|
|
.detach();
|
|
|
|
for &worktree_id in &worktree_ids {
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(!can_trust, "worktree should be restricted initially");
|
|
}
|
|
|
|
let has_restricted = trusted_worktrees.read_with(cx, |store, cx| {
|
|
store.has_restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(has_restricted, "should have restricted worktrees");
|
|
|
|
events.borrow_mut().clear();
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.auto_trust_all(cx);
|
|
});
|
|
|
|
for &worktree_id in &worktree_ids {
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(
|
|
can_trust,
|
|
"worktree {worktree_id:?} should be trusted after auto_trust_all"
|
|
);
|
|
}
|
|
|
|
let has_restricted_after = trusted_worktrees.read_with(cx, |store, cx| {
|
|
store.has_restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(
|
|
!has_restricted_after,
|
|
"should have no restricted worktrees after auto_trust_all"
|
|
);
|
|
|
|
let trusted_event_count = events
|
|
.borrow()
|
|
.iter()
|
|
.filter(|e| matches!(e, TrustedWorktreesEvent::Trusted(..)))
|
|
.count();
|
|
assert!(
|
|
trusted_event_count > 0,
|
|
"should have emitted Trusted events"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_trust_restrict_trust_cycle(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(path!("/root"), json!({ "main.rs": "fn main() {}" }))
|
|
.await;
|
|
|
|
let project = Project::test(fs, [path!("/root").as_ref()], cx).await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_id = worktree_store.read_with(cx, |store, cx| {
|
|
store.worktrees().next().unwrap().read(cx).id()
|
|
});
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let events: Rc<RefCell<Vec<TrustedWorktreesEvent>>> = Rc::default();
|
|
cx.update({
|
|
let events = events.clone();
|
|
|cx| {
|
|
cx.subscribe(&trusted_worktrees, move |_, event, _| {
|
|
events.borrow_mut().push(match event {
|
|
TrustedWorktreesEvent::Trusted(host, paths) => {
|
|
TrustedWorktreesEvent::Trusted(host.clone(), paths.clone())
|
|
}
|
|
TrustedWorktreesEvent::Restricted(host, paths) => {
|
|
TrustedWorktreesEvent::Restricted(host.clone(), paths.clone())
|
|
}
|
|
});
|
|
})
|
|
}
|
|
})
|
|
.detach();
|
|
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(!can_trust, "should be restricted initially");
|
|
assert_eq!(events.borrow().len(), 1);
|
|
events.borrow_mut().clear();
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_id)]),
|
|
cx,
|
|
);
|
|
});
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(can_trust, "should be trusted after trust()");
|
|
assert_eq!(events.borrow().len(), 1);
|
|
assert!(matches!(
|
|
&events.borrow()[0],
|
|
TrustedWorktreesEvent::Trusted(..)
|
|
));
|
|
events.borrow_mut().clear();
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.restrict(
|
|
worktree_store.downgrade(),
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_id)]),
|
|
cx,
|
|
);
|
|
});
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(!can_trust, "should be restricted after restrict()");
|
|
assert_eq!(events.borrow().len(), 1);
|
|
assert!(matches!(
|
|
&events.borrow()[0],
|
|
TrustedWorktreesEvent::Restricted(..)
|
|
));
|
|
|
|
let has_restricted = trusted_worktrees.read_with(cx, |store, cx| {
|
|
store.has_restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(has_restricted);
|
|
events.borrow_mut().clear();
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(worktree_id)]),
|
|
cx,
|
|
);
|
|
});
|
|
let can_trust = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, worktree_id, cx)
|
|
});
|
|
assert!(can_trust, "should be trusted again after second trust()");
|
|
assert_eq!(events.borrow().len(), 1);
|
|
assert!(matches!(
|
|
&events.borrow()[0],
|
|
TrustedWorktreesEvent::Trusted(..)
|
|
));
|
|
|
|
let has_restricted = trusted_worktrees.read_with(cx, |store, cx| {
|
|
store.has_restricted_worktrees(&worktree_store, cx)
|
|
});
|
|
assert!(!has_restricted);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_multi_host_trust_isolation(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/"),
|
|
json!({
|
|
"local_project": { "main.rs": "fn main() {}" },
|
|
"remote_project": { "lib.rs": "pub fn lib() {}" }
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(
|
|
fs,
|
|
[
|
|
path!("/local_project").as_ref(),
|
|
path!("/remote_project").as_ref(),
|
|
],
|
|
cx,
|
|
)
|
|
.await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let worktree_ids: Vec<_> = worktree_store.read_with(cx, |store, cx| {
|
|
store
|
|
.worktrees()
|
|
.map(|worktree| worktree.read(cx).id())
|
|
.collect()
|
|
});
|
|
assert_eq!(worktree_ids.len(), 2);
|
|
let local_worktree = worktree_ids[0];
|
|
let _remote_worktree = worktree_ids[1];
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let can_trust_local = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, local_worktree, cx)
|
|
});
|
|
assert!(!can_trust_local, "local worktree restricted on host_a");
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([PathTrust::Worktree(local_worktree)]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
let can_trust_local_after = trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, local_worktree, cx)
|
|
});
|
|
assert!(
|
|
can_trust_local_after,
|
|
"local worktree should be trusted on local host"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_invisible_worktree_stores_do_not_affect_trust(cx: &mut TestAppContext) {
|
|
init_test(cx);
|
|
|
|
let fs = FakeFs::new(cx.executor());
|
|
fs.insert_tree(
|
|
path!("/"),
|
|
json!({
|
|
"visible": { "main.rs": "fn main() {}" },
|
|
"other": { "a.rs": "fn other() {}" },
|
|
"invisible": { "b.rs": "fn invisible() {}" }
|
|
}),
|
|
)
|
|
.await;
|
|
|
|
let project = Project::test(fs, [path!("/visible").as_ref()], cx).await;
|
|
let worktree_store = project.read_with(cx, |project, _| project.worktree_store());
|
|
let visible_worktree_id = worktree_store.read_with(cx, |store, cx| {
|
|
store
|
|
.worktrees()
|
|
.find(|worktree| worktree.read(cx).root_dir().unwrap().ends_with("visible"))
|
|
.expect("visible worktree")
|
|
.read(cx)
|
|
.id()
|
|
});
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
let events: Rc<RefCell<Vec<TrustedWorktreesEvent>>> = Rc::default();
|
|
cx.update({
|
|
let events = events.clone();
|
|
|cx| {
|
|
cx.subscribe(&trusted_worktrees, move |_, event, _| {
|
|
events.borrow_mut().push(match event {
|
|
TrustedWorktreesEvent::Trusted(host, paths) => {
|
|
TrustedWorktreesEvent::Trusted(host.clone(), paths.clone())
|
|
}
|
|
TrustedWorktreesEvent::Restricted(host, paths) => {
|
|
TrustedWorktreesEvent::Restricted(host.clone(), paths.clone())
|
|
}
|
|
});
|
|
})
|
|
}
|
|
})
|
|
.detach();
|
|
|
|
assert!(
|
|
!trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, visible_worktree_id, cx)
|
|
}),
|
|
"visible worktree should be restricted initially"
|
|
);
|
|
assert_eq!(
|
|
HashSet::from_iter([(visible_worktree_id)]),
|
|
trusted_worktrees.read_with(cx, |store, _| {
|
|
store.restricted_worktrees_for_store(&worktree_store)
|
|
}),
|
|
"only visible worktree should be restricted",
|
|
);
|
|
|
|
let (new_visible_worktree, new_invisible_worktree) =
|
|
worktree_store.update(cx, |worktree_store, cx| {
|
|
let new_visible_worktree = worktree_store.create_worktree("/other", true, cx);
|
|
let new_invisible_worktree = worktree_store.create_worktree("/invisible", false, cx);
|
|
(new_visible_worktree, new_invisible_worktree)
|
|
});
|
|
let (new_visible_worktree, new_invisible_worktree) = (
|
|
new_visible_worktree.await.unwrap(),
|
|
new_invisible_worktree.await.unwrap(),
|
|
);
|
|
|
|
let new_visible_worktree_id =
|
|
new_visible_worktree.read_with(cx, |new_visible_worktree, _| new_visible_worktree.id());
|
|
assert!(
|
|
!trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, new_visible_worktree_id, cx)
|
|
}),
|
|
"new visible worktree should be restricted initially",
|
|
);
|
|
assert!(
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.can_trust(&worktree_store, new_invisible_worktree.read(cx).id(), cx)
|
|
}),
|
|
"invisible worktree should be skipped",
|
|
);
|
|
assert_eq!(
|
|
HashSet::from_iter([visible_worktree_id, new_visible_worktree_id]),
|
|
trusted_worktrees.read_with(cx, |store, _| {
|
|
store.restricted_worktrees_for_store(&worktree_store)
|
|
}),
|
|
"only visible worktrees should be restricted"
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_windows_style_abs_path_trust_from_unix_client(cx: &mut TestAppContext) {
|
|
use gpui::AppContext as _;
|
|
use project::worktree_store::WorktreeIdCounter;
|
|
use rpc::{AnyProtoClient, NoopProtoClient, proto};
|
|
use util::paths::PathStyle;
|
|
use worktree::Worktree;
|
|
|
|
init_test(cx);
|
|
|
|
let client = AnyProtoClient::new(NoopProtoClient::new());
|
|
let worktree_store = cx.update(|cx| {
|
|
cx.new(|_| {
|
|
WorktreeStore::remote(
|
|
true,
|
|
client.clone(),
|
|
1,
|
|
PathStyle::Windows,
|
|
WorktreeIdCounter::default(),
|
|
)
|
|
})
|
|
});
|
|
|
|
let make_worktree = |id: u64, name: &str, abs_path: &str, cx: &mut TestAppContext| {
|
|
let metadata = proto::WorktreeMetadata {
|
|
id,
|
|
root_name: name.to_string(),
|
|
visible: true,
|
|
abs_path: abs_path.to_string(),
|
|
root_repo_common_dir: None,
|
|
root_repo_is_linked_worktree: false,
|
|
};
|
|
let worktree = cx.update(|cx| {
|
|
Worktree::remote(
|
|
1,
|
|
clock::ReplicaId::new(1),
|
|
metadata,
|
|
client.clone(),
|
|
PathStyle::Windows,
|
|
cx,
|
|
)
|
|
});
|
|
worktree.update(cx, |worktree, _| {
|
|
worktree
|
|
.as_remote()
|
|
.unwrap()
|
|
.update_from_remote(proto::UpdateWorktree {
|
|
project_id: 1,
|
|
worktree_id: id,
|
|
abs_path: abs_path.to_string(),
|
|
root_name: name.to_string(),
|
|
updated_entries: vec![proto::Entry {
|
|
id: 1,
|
|
is_dir: true,
|
|
path: "".to_string(),
|
|
inode: 1,
|
|
mtime: Some(proto::Timestamp {
|
|
seconds: 0,
|
|
nanos: 0,
|
|
}),
|
|
is_ignored: false,
|
|
is_hidden: false,
|
|
is_external: false,
|
|
is_fifo: false,
|
|
size: None,
|
|
canonical_path: None,
|
|
is_unloaded: false,
|
|
}],
|
|
removed_entries: vec![],
|
|
scan_id: 1,
|
|
is_last_update: true,
|
|
updated_repositories: vec![],
|
|
removed_repositories: vec![],
|
|
root_repo_common_dir: None,
|
|
root_repo_is_linked_worktree: false,
|
|
});
|
|
});
|
|
cx.run_until_parked();
|
|
worktree
|
|
};
|
|
|
|
let worktree_t1 = make_worktree(1, "t1", r"C:\Users\me\dev\wt\t1", cx);
|
|
let worktree_t2 = make_worktree(2, "t2", r"C:\Users\me\dev\wt\t2", cx);
|
|
let t1_id = worktree_t1.read_with(cx, |worktree, _| worktree.id());
|
|
let t2_id = worktree_t2.read_with(cx, |worktree, _| worktree.id());
|
|
worktree_store.update(cx, |store, cx| {
|
|
store.add(&worktree_t1, cx);
|
|
store.add(&worktree_t2, cx);
|
|
});
|
|
assert!(
|
|
!worktree_t1.read_with(cx, |worktree, _| worktree.is_single_file()),
|
|
"t1 must be a folder worktree for this test to be meaningful"
|
|
);
|
|
|
|
let trusted_worktrees = init_trust_global(worktree_store.clone(), cx);
|
|
|
|
assert!(
|
|
!trusted_worktrees.update(cx, |store, cx| store.can_trust(&worktree_store, t1_id, cx)),
|
|
"t1 should be restricted by default"
|
|
);
|
|
|
|
trusted_worktrees.update(cx, |store, cx| {
|
|
store.trust(
|
|
&worktree_store,
|
|
HashSet::from_iter([
|
|
PathTrust::Worktree(t1_id),
|
|
PathTrust::AbsPath(PathBuf::from(r"C:\Users\me\dev\wt")),
|
|
]),
|
|
cx,
|
|
);
|
|
});
|
|
|
|
assert!(
|
|
trusted_worktrees.update(cx, |store, cx| store.can_trust(&worktree_store, t2_id, cx)),
|
|
"t2 should be auto-trusted by the trusted parent scope C:\\Users\\me\\dev\\wt"
|
|
);
|
|
}
|