Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fix two `FakeFs` inaccuracies (#64569)

Finn Evers2a4157111cbb
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index 167e63c98e7..7d37384cd2a 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -2845,8 +2845,13 @@ impl FakeFs {
None
}
- btree_map::Entry::Occupied(mut entry) => {
- entry.get_mut().file_content(&path)?;
+ btree_map::Entry::Occupied(entry) => {
+ // Like `unlink`, removing a symlink removes the link itself.
+ if let entry = entry.get()
+ && !entry.is_symlink()
+ {
+ entry.file_content(&path)?;
+ }
Some(entry.remove())
}
};
@@ -3128,33 +3133,42 @@ impl Fs for FakeFs {
let target = normalize_path(target);
let mut state = self.state.lock();
let mtime = state.get_and_increment_mtime();
- let inode = state.get_and_increment_inode();
+ let new_inode = state.get_and_increment_inode();
let source_entry = state.entry(&source)?;
let content = source_entry.file_content(&source)?.clone();
- let mut kind = Some(PathEventKind::Created);
- state.write_path(&target, |e| match e {
- btree_map::Entry::Occupied(e) => {
- if options.overwrite {
- kind = Some(PathEventKind::Changed);
- Ok(Some(e.get().clone()))
- } else if !options.ignore_if_exists {
+ let new_entry = move |inode| FakeFsEntry::File {
+ inode,
+ mtime,
+ len: content.len() as u64,
+ content,
+ git_dir_path: None,
+ };
+
+ let kind = state.write_path(&target, |e| match e {
+ btree_map::Entry::Occupied(mut e) => {
+ if !options.overwrite {
+ if options.ignore_if_exists {
+ return Ok(None);
+ }
anyhow::bail!("{target:?} already exists");
- } else {
- Ok(None)
}
+ let inode = match e.get() {
+ FakeFsEntry::File { inode, .. } => *inode,
+ FakeFsEntry::Dir { .. } => anyhow::bail!("{target:?} is a directory"),
+ FakeFsEntry::Symlink { .. } => new_inode,
+ };
+ e.insert(new_entry(inode));
+ Ok(Some(PathEventKind::Changed))
+ }
+ btree_map::Entry::Vacant(e) => {
+ e.insert(new_entry(new_inode));
+ Ok(Some(PathEventKind::Created))
}
- btree_map::Entry::Vacant(e) => Ok(Some(
- e.insert(FakeFsEntry::File {
- inode,
- mtime,
- len: content.len() as u64,
- content,
- git_dir_path: None,
- })
- .clone(),
- )),
})?;
- state.emit_event([(target, kind)]);
+
+ if let Some(kind) = kind {
+ state.emit_event([(target, Some(kind))]);
+ }
Ok(())
}
diff --git a/crates/fs/tests/integration/fs_tests.rs b/crates/fs/tests/integration/fs_tests.rs
index 5bd132d4e38..8a7ab4f4873 100644
--- a/crates/fs/tests/integration/fs_tests.rs
+++ b/crates/fs/tests/integration/fs_tests.rs
@@ -689,6 +689,103 @@ async fn test_fake_fs_rename_ignore_if_exists_leaves_source_and_target_unchanged
);
}
+async fn assert_copy_and_remove_semantics(root: &Path, fs: &dyn Fs) {
+ let source = root.join("source.txt");
+ let target = root.join("target.txt");
+ let overwrite = CopyOptions {
+ overwrite: true,
+ ignore_if_exists: false,
+ };
+
+ let target_inode_before = fs.metadata(&target).await.unwrap().unwrap().inode;
+ fs.copy_file(&source, &target, overwrite).await.unwrap();
+ assert_eq!(fs.load(&target).await.unwrap(), "from source");
+
+ cfg_select! {
+ unix => assert_eq!(
+ fs.metadata(&target).await.unwrap().unwrap().inode,
+ target_inode_before
+ ),
+ _ => { let _ = target_inode_before; }
+ }
+
+ fs.copy_file(&source, &target, CopyOptions::default())
+ .await
+ .unwrap_err();
+ fs.copy_file(
+ &source,
+ &target,
+ CopyOptions {
+ overwrite: false,
+ ignore_if_exists: true,
+ },
+ )
+ .await
+ .unwrap();
+
+ fs.copy_file(&source, &root.join("dir"), overwrite)
+ .await
+ .unwrap_err();
+ assert!(fs.is_dir(&root.join("dir")).await);
+}
+
+/// Removing a symlink to a directory removes the link, not the directory.
+async fn assert_remove_file_unlinks_symlink(root: &Path, fs: &dyn Fs) {
+ let link = root.join("link");
+ fs.remove_file(&link, RemoveOptions::default())
+ .await
+ .unwrap();
+ assert!(fs.metadata(&link).await.unwrap().is_none());
+ assert!(fs.is_dir(&root.join("dir")).await);
+ assert_eq!(
+ fs.load(&root.join("dir").join("inner.txt")).await.unwrap(),
+ "inner"
+ );
+}
+
+#[gpui::test]
+#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
+async fn test_realfs_copy_and_remove_semantics(cx: &mut TestAppContext) {
+ cx.executor().allow_parking();
+ let executor = cx.executor();
+ let temp_dir = TempDir::new().unwrap();
+ let root = temp_dir.path();
+ std::fs::write(root.join("source.txt"), "from source").unwrap();
+ std::fs::write(root.join("target.txt"), "from target").unwrap();
+ std::fs::create_dir(root.join("dir")).unwrap();
+ std::fs::write(root.join("dir").join("inner.txt"), "inner").unwrap();
+
+ let fs = RealFs::new(None, executor);
+ assert_copy_and_remove_semantics(root, fs.as_ref()).await;
+
+ // Creating symlinks requires elevated privileges on Windows, so like the
+ // watcher tests above, skip that part when it is not possible.
+ match make_dir_symlink(&root.join("dir"), &root.join("link")) {
+ Ok(()) => assert_remove_file_unlinks_symlink(root, fs.as_ref()).await,
+ Err(error) => eprintln!("skipping symlink removal check (cannot symlink: {error})"),
+ }
+}
+
+#[gpui::test]
+async fn test_fake_fs_copy_and_remove_semantics(executor: BackgroundExecutor) {
+ let fs = FakeFs::new(executor);
+ fs.insert_tree(
+ path!("/root"),
+ json!({
+ "source.txt": "from source",
+ "target.txt": "from target",
+ "dir": { "inner.txt": "inner" },
+ }),
+ )
+ .await;
+ fs.insert_symlink(path!("/root/link"), PathBuf::from(path!("/root/dir")))
+ .await;
+
+ let root = Path::new(path!("/root"));
+ assert_copy_and_remove_semantics(root, fs.as_ref()).await;
+ assert_remove_file_unlinks_symlink(root, fs.as_ref()).await;
+}
+
#[gpui::test]
async fn test_fake_fs_rename_onto_itself_keeps_the_file(executor: BackgroundExecutor) {
let fs = FakeFs::new(executor);