dnd: Skip local moves into the source directory

This commit is contained in:
Zhizhen He 2026-10-28 20:36:04 +08:00
parent c514311380
commit ed4d37a9fa
2 changed files with 181 additions and 0 deletions

View file

@ -652,6 +652,29 @@ func (dnd *dnd) all_mime_data_dropped() (err error) {
dnd.end_drop(true)
return dnd.render_screen()
}
if !drop_status.is_remote_client && drop_status.action == move_on_drop {
dest_info, err := dnd.drop_output_dir.Stat()
if err != nil {
return err
}
for i, u := range drop_status.uri_list {
if u.kind != parsed_uri_file || u.path == "" {
continue
}
parent_info, err := os.Stat(filepath.Dir(u.path))
if err != nil {
return err
}
if os.SameFile(parent_info, dest_info) {
if _, err := os.Lstat(u.path); err != nil {
return err
}
// Moving into the source directory is a no-op. Exclude it
// from both staging and end_drop's source cleanup.
drop_status.uri_list[i].path = ""
}
}
}
f, err := dnd.new_tdir()
if err != nil {
return err

View file

@ -9,6 +9,9 @@ import (
"path/filepath"
"sort"
"testing"
"time"
"github.com/kovidgoyal/kitty/tools/tui/loop"
)
// openDir opens a directory for use as an *os.File, closing it on test cleanup.
@ -103,6 +106,161 @@ func TestLocalCopyHardlinkPolicy(t *testing.T) {
}
}
func runLocalDrop(t *testing.T, dest string, uris []parsed_uri, action int, copy_mode string) (*dnd, error) {
t.Helper()
lp, err := loop.New()
if err != nil {
t.Fatal(err)
}
d := &dnd{lp: lp, opts: &Options{CopyMode: copy_mode}, in_test_mode: true}
if err := d.setup_base_dir(dest); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
d.reset_drop()
d.remove_tdir()
d.drop_output_dir.Close()
})
d.drop_status = drop_status{action: action, reading_data: true, uri_list: uris}
if err := d.all_mime_data_dropped(); err != nil {
return d, err
}
deadline := time.Now().Add(5 * time.Second)
for d.drop_status.local_copy.ctx != nil {
if time.Now().After(deadline) {
t.Fatal("local drop did not complete")
}
if err := d.drop_on_wakeup(); err != nil {
return d, err
}
time.Sleep(time.Millisecond)
}
return d, nil
}
func TestLocalMoveToSourceDirectory(t *testing.T) {
for _, copy_mode := range []string{"auto", "independent"} {
for _, alias := range []string{"none", "source", "destination"} {
for _, name := range []string{"file.txt", "folder", "link", "dangling"} {
t.Run(copy_mode+"/"+alias+"/"+name, func(t *testing.T) {
dest := t.TempDir()
files := map[string]string{"file.txt": "original contents", "folder/nested/file.txt": "nested contents"}
links := map[string]string{"link": "file.txt", "dangling": "missing"}
buildTree(t, dest, files, links)
before, err := os.Lstat(filepath.Join(dest, name))
if err != nil {
t.Fatal(err)
}
source_dir, dest_dir := dest, dest
if alias != "none" {
path := filepath.Join(t.TempDir(), "alias")
if err := os.Symlink(dest, path); err != nil {
t.Fatal(err)
}
if alias == "source" {
source_dir = path
} else {
dest_dir = path
}
}
d, err := runLocalDrop(t, dest_dir, []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(source_dir, name)}}, move_on_drop, copy_mode)
if err != nil {
t.Fatal(err)
}
for path, want := range files {
data, err := os.ReadFile(filepath.Join(dest, path))
if err != nil {
t.Errorf("source %s disappeared after moving to its own directory: %v", path, err)
} else if string(data) != want {
t.Errorf("source %s: got %q, want %q", path, data, want)
}
}
for path, want := range links {
if got, err := os.Readlink(filepath.Join(dest, path)); err != nil || got != want {
t.Errorf("symlink %s: got %q, %v, want %q", path, got, err, want)
}
}
if after, err := os.Lstat(filepath.Join(dest, name)); err != nil || !os.SameFile(before, after) {
t.Errorf("moving %s into its own directory changed its identity: %v", name, err)
}
if d.num_dropped_files != 0 {
t.Errorf("counted %d dropped files for a no-op move", d.num_dropped_files)
}
})
}
}
}
}
func TestLocalDropMixedSourceDirectories(t *testing.T) {
for _, tc := range []struct {
name string
action int
copy_mode string
}{
{"copy-auto", copy_on_drop, "auto"},
{"copy-independent", copy_on_drop, "independent"},
{"move-auto", move_on_drop, "auto"},
{"move-independent", move_on_drop, "independent"},
} {
t.Run(tc.name, func(t *testing.T) {
src, dest := t.TempDir(), t.TempDir()
local := map[string]string{"keep.txt": "keep file", "folder/keep.txt": "keep directory"}
incoming := map[string]string{"moved.txt": "incoming file", "incoming/nested.txt": "incoming directory", "linked.txt": "hardlinked file"}
buildTree(t, dest, local, nil)
buildTree(t, src, incoming, nil)
if err := os.Link(filepath.Join(src, "linked.txt"), filepath.Join(dest, "linked.txt")); err != nil {
t.Fatal(err)
}
uris := []parsed_uri{
{kind: parsed_uri_file, path: filepath.Join(dest, "keep.txt")},
{kind: parsed_uri_file, path: filepath.Join(dest, "folder")},
{kind: parsed_uri_file},
{kind: parsed_uri_data, mime: "text/plain", data: []byte("data URI contents")},
}
for _, name := range []string{"moved.txt", "incoming", "linked.txt"} {
uris = append(uris, parsed_uri{kind: parsed_uri_file, path: filepath.Join(src, name)})
}
d, err := runLocalDrop(t, dest, uris, tc.action, tc.copy_mode)
if err != nil {
t.Fatal(err)
}
for _, files := range []map[string]string{local, incoming, {"data-uri-1.txt": "data URI contents"}} {
for path, want := range files {
if data, err := os.ReadFile(filepath.Join(dest, path)); err != nil || string(data) != want {
t.Errorf("dropped file %s: got %q, %v, want %q", path, data, err, want)
}
}
}
for path, want := range incoming {
data, err := os.ReadFile(filepath.Join(src, path))
if tc.action == move_on_drop {
if !os.IsNotExist(err) {
t.Errorf("source %s still exists after moving: %v", path, err)
}
} else if err != nil || string(data) != want {
t.Errorf("copy changed source %s: got %q, %v, want %q", path, data, err, want)
}
}
want_count := 6
if tc.action == move_on_drop {
want_count = 4
}
if d.num_dropped_files != want_count {
t.Errorf("got %d dropped files, want %d", d.num_dropped_files, want_count)
}
})
}
}
func TestLocalMoveMissingSource(t *testing.T) {
dest := t.TempDir()
uris := []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(dest, "missing")}}
if _, err := runLocalDrop(t, dest, uris, move_on_drop, "auto"); !os.IsNotExist(err) {
t.Fatalf("got %v, want a missing source error", err)
}
}
func TestLocalCopyDuplicateBasenames(t *testing.T) {
for _, tc := range []struct {
name string