mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
Refuse to replace an existing directory, or a symlink to a directory, with a file, reporting EISDIR before any data is transferred instead of a raw OSError (or silently replacing the symlink). Report failures to create parent directories with the actual errno and directory name. When a send fails, the kitten now prints hints for the common causes: a missing trailing slash on a directory destination and a ~ expanded by the shell on the remote machine. Fixes #8118 Closes #10710
130 lines
4.1 KiB
Go
130 lines
4.1 KiB
Go
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
|
|
|
package transfer
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
var _ = fmt.Print
|
|
|
|
func TestPathMappingSend(t *testing.T) {
|
|
opts := &Options{}
|
|
tdir := t.TempDir()
|
|
b := filepath.Join(tdir, "b")
|
|
os.Mkdir(b, 0o700)
|
|
os.WriteFile(filepath.Join(b, "r"), nil, 0600)
|
|
os.Mkdir(filepath.Join(b, "d"), 0o700)
|
|
os.WriteFile(filepath.Join(b, "d", "r"), nil, 0600)
|
|
|
|
gm := func(args ...string) ([]*File, error) {
|
|
return files_for_send(opts, args)
|
|
}
|
|
|
|
mp := func(path string, is_remote bool) string {
|
|
path = strings.TrimSpace(path)
|
|
if strings.HasPrefix(path, "~") || filepath.IsAbs(path) {
|
|
return path
|
|
}
|
|
return filepath.Join(tdir, path)
|
|
}
|
|
|
|
tf := func(expected string, args ...string) {
|
|
files, err := gm(args...)
|
|
if err != nil {
|
|
t.Fatalf("Failed with mode: %s cwd: %s home: %s and args: %#v\n%s", opts.Mode, cwd_path(), home_path(), args, err)
|
|
}
|
|
actual := make(map[string]string)
|
|
for _, f := range files {
|
|
actual[f.expanded_local_path] = f.remote_path
|
|
}
|
|
e := make(map[string]string, len(actual))
|
|
for rec := range strings.SplitSeq(expected, " ") {
|
|
k, v, _ := strings.Cut(rec, ":")
|
|
e[mp(k, false)] = mp(v, true)
|
|
}
|
|
if diff := cmp.Diff(e, actual); diff != "" {
|
|
t.Fatalf("Failed with mode: %s cwd: %s home: %s and args: %#v\n%s", opts.Mode, cwd_path(), home_path(), args, diff)
|
|
}
|
|
}
|
|
|
|
opts.Mode = "mirror"
|
|
run_with_paths(b, "/foo/bar", func() {
|
|
tf("b/r:b/r b/d:b/d b/d/r:b/d/r", "r", "d")
|
|
tf("b/r:b/r b/d/r:b/d/r", "r", "d/r")
|
|
})
|
|
run_with_paths(b, tdir, func() {
|
|
tf("b/r:~/b/r b/d:~/b/d b/d/r:~/b/d/r", "r", "d")
|
|
})
|
|
opts.Mode = "normal"
|
|
run_with_paths("/some/else", "/foo/bar", func() {
|
|
tf("b/r:/dest/r b/d:/dest/d b/d/r:/dest/d/r", filepath.Join(b, "r"), filepath.Join(b, "d"), "/dest")
|
|
tf("b/r:~/dest/r b/d:~/dest/d b/d/r:~/dest/d/r", filepath.Join(b, "r"), filepath.Join(b, "d"), "~/dest")
|
|
})
|
|
run_with_paths(b, "/foo/bar", func() {
|
|
tf("b/r:/dest/r b/d:/dest/d b/d/r:/dest/d/r", "r", "d", "/dest")
|
|
})
|
|
os.Symlink("/foo/b", filepath.Join(b, "e"))
|
|
os.Symlink("r", filepath.Join(b, "s"))
|
|
os.Link(filepath.Join(b, "r"), filepath.Join(b, "h"))
|
|
|
|
file_idx := 0
|
|
first_file := func(args ...string) *File {
|
|
files, err := gm(args...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return files[file_idx]
|
|
}
|
|
ae := func(a any, b any) {
|
|
if diff := cmp.Diff(a, b); diff != "" {
|
|
t.Fatalf("%s", diff)
|
|
}
|
|
}
|
|
run_with_paths("/some/else", "/foo/bar", func() {
|
|
f := first_file(filepath.Join(b, "e"), "dest")
|
|
ae(f.symbolic_link_target, "path:/foo/b")
|
|
f = first_file(filepath.Join(b, "s"), filepath.Join(b, "r"), "dest")
|
|
ae(f.symbolic_link_target, "fid:2")
|
|
f = first_file(filepath.Join(b, "h"), "dest")
|
|
ae(f.file_type, FileType_regular)
|
|
file_idx = 1
|
|
f = first_file(filepath.Join(b, "h"), filepath.Join(b, "r"), "dest")
|
|
ae(f.hard_link_target, "fid:1")
|
|
ae(f.file_type, FileType_link)
|
|
})
|
|
}
|
|
|
|
func TestSendFailureHints(t *testing.T) {
|
|
opts := &Options{}
|
|
home := filepath.ToSlash(home_path())
|
|
eisdir := func(remote_path string) *File {
|
|
return &File{remote_path: remote_path, err_msg: "EISDIR:Cannot replace the existing directory x with a file"}
|
|
}
|
|
th := func(args []string, failed []*File, expected ...string) {
|
|
t.Helper()
|
|
actual := failure_hints(opts, args, failed)
|
|
if len(actual) != len(expected) {
|
|
t.Fatalf("Unexpected hints for %#v: %#v", args, actual)
|
|
}
|
|
for i, e := range expected {
|
|
if !strings.Contains(actual[i], e) {
|
|
t.Fatalf("Hint %#v does not contain %#v", actual[i], e)
|
|
}
|
|
}
|
|
}
|
|
th([]string{"a", "/x/d"}, nil)
|
|
th([]string{"a", "/x/d"}, []*File{eisdir("/x/d")}, "trailing slash, for example: /x/d/")
|
|
th([]string{"a", "/x/d/"}, []*File{eisdir("/x/d/a")})
|
|
th([]string{"a", "b", "/x/d"}, []*File{eisdir("/x/d/a")})
|
|
th([]string{"a", home + "/Downloads"}, []*File{{remote_path: home + "/Downloads", err_msg: "ENOTSUP:x"}}, `\~/Downloads`)
|
|
th([]string{"a", home + "/Downloads"}, []*File{eisdir(home + "/Downloads")}, "trailing slash", `\~/Downloads`)
|
|
opts.Mode = "mirror"
|
|
th([]string{"a", "/x/d"}, []*File{eisdir("/x/d")})
|
|
}
|