send_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package transfer
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/google/go-cmp/cmp"
  10. )
  11. var _ = fmt.Print
  12. func TestPathMappingSend(t *testing.T) {
  13. opts := &Options{}
  14. tdir := t.TempDir()
  15. b := filepath.Join(tdir, "b")
  16. os.Mkdir(b, 0o700)
  17. os.WriteFile(filepath.Join(b, "r"), nil, 0600)
  18. os.Mkdir(filepath.Join(b, "d"), 0o700)
  19. os.WriteFile(filepath.Join(b, "d", "r"), nil, 0600)
  20. gm := func(args ...string) ([]*File, error) {
  21. return files_for_send(opts, args)
  22. }
  23. mp := func(path string, is_remote bool) string {
  24. path = strings.TrimSpace(path)
  25. if strings.HasPrefix(path, "~") || filepath.IsAbs(path) {
  26. return path
  27. }
  28. return filepath.Join(tdir, path)
  29. }
  30. tf := func(expected string, args ...string) {
  31. files, err := gm(args...)
  32. if err != nil {
  33. t.Fatalf("Failed with mode: %s cwd: %s home: %s and args: %#v\n%s", opts.Mode, cwd_path(), home_path(), args, err)
  34. }
  35. actual := make(map[string]string)
  36. for _, f := range files {
  37. actual[f.expanded_local_path] = f.remote_path
  38. }
  39. e := make(map[string]string, len(actual))
  40. for _, rec := range strings.Split(expected, " ") {
  41. k, v, _ := strings.Cut(rec, ":")
  42. e[mp(k, false)] = mp(v, true)
  43. }
  44. if diff := cmp.Diff(e, actual); diff != "" {
  45. t.Fatalf("Failed with mode: %s cwd: %s home: %s and args: %#v\n%s", opts.Mode, cwd_path(), home_path(), args, diff)
  46. }
  47. }
  48. opts.Mode = "mirror"
  49. run_with_paths(b, "/foo/bar", func() {
  50. tf("b/r:b/r b/d:b/d b/d/r:b/d/r", "r", "d")
  51. tf("b/r:b/r b/d/r:b/d/r", "r", "d/r")
  52. })
  53. run_with_paths(b, tdir, func() {
  54. tf("b/r:~/b/r b/d:~/b/d b/d/r:~/b/d/r", "r", "d")
  55. })
  56. opts.Mode = "normal"
  57. run_with_paths("/some/else", "/foo/bar", func() {
  58. tf("b/r:/dest/r b/d:/dest/d b/d/r:/dest/d/r", filepath.Join(b, "r"), filepath.Join(b, "d"), "/dest")
  59. tf("b/r:~/dest/r b/d:~/dest/d b/d/r:~/dest/d/r", filepath.Join(b, "r"), filepath.Join(b, "d"), "~/dest")
  60. })
  61. run_with_paths(b, "/foo/bar", func() {
  62. tf("b/r:/dest/r b/d:/dest/d b/d/r:/dest/d/r", "r", "d", "/dest")
  63. })
  64. os.Symlink("/foo/b", filepath.Join(b, "e"))
  65. os.Symlink("r", filepath.Join(b, "s"))
  66. os.Link(filepath.Join(b, "r"), filepath.Join(b, "h"))
  67. file_idx := 0
  68. first_file := func(args ...string) *File {
  69. files, err := gm(args...)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. return files[file_idx]
  74. }
  75. ae := func(a any, b any) {
  76. if diff := cmp.Diff(a, b); diff != "" {
  77. t.Fatalf("%s", diff)
  78. }
  79. }
  80. run_with_paths("/some/else", "/foo/bar", func() {
  81. f := first_file(filepath.Join(b, "e"), "dest")
  82. ae(f.symbolic_link_target, "path:/foo/b")
  83. f = first_file(filepath.Join(b, "s"), filepath.Join(b, "r"), "dest")
  84. ae(f.symbolic_link_target, "fid:2")
  85. f = first_file(filepath.Join(b, "h"), "dest")
  86. ae(f.file_type, FileType_regular)
  87. file_idx = 1
  88. f = first_file(filepath.Join(b, "h"), filepath.Join(b, "r"), "dest")
  89. ae(f.hard_link_target, "fid:1")
  90. ae(f.file_type, FileType_link)
  91. })
  92. }