| 1 | |
| 2 | |
| 3 | |
| 4 | package copytool |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "slices" |
| 11 | "testing" |
| 12 | |
| 13 | "gotest.tools/v3/assert" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | func TestCommandDoesNotMutateOptions(t *testing.T) { |
| 19 | initial := &Options{Verbose: false, Recursive: false} |
| 20 | tool, err := newRsyncTool(initial) |
| 21 | if err != nil { |
| 22 | t.Skip("rsync not found:", err) |
| 23 | } |
| 24 | |
| 25 | override := &Options{Verbose: true, Recursive: true} |
| 26 | |
| 27 | _, _ = tool.Command(t.Context(), []string{"/tmp/src", "/tmp/dst"}, override) |
| 28 | |
| 29 | assert.Equal(t, tool.Options.Verbose, false, "Command() must not mutate stored Options.Verbose") |
| 30 | assert.Equal(t, tool.Options.Recursive, false, "Command() must not mutate stored Options.Recursive") |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | func TestRsyncCommandEndsOptionParsing(t *testing.T) { |
| 36 | tool, err := newRsyncTool(&Options{}) |
| 37 | if err != nil { |
| 38 | t.Skip("rsync not found:", err) |
| 39 | } |
| 40 | |
| 41 | const dashPath = "--rsh=touch pwned" |
| 42 | |
| 43 | cmd, err := tool.Command(t.Context(), []string{dashPath, "/tmp/dst"}, nil) |
| 44 | assert.NilError(t, err) |
| 45 | |
| 46 | sep := slices.Index(cmd.Args, "--") |
| 47 | assert.Assert(t, sep != -1, "rsync args must contain the %#q separator: %v", "--", cmd.Args) |
| 48 | assert.Assert(t, slices.Index(cmd.Args, dashPath) > sep, "path %#q must come after %#q: %v", dashPath, "--", cmd.Args) |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | func TestRsyncCommandPaths(t *testing.T) { |
| 57 | if _, err := newRsyncTool(&Options{}); err != nil { |
| 58 | t.Skip("rsync not found:", err) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | tmpDir := t.TempDir() |
| 63 | dir := filepath.Join(tmpDir, "dir") |
| 64 | assert.NilError(t, os.Mkdir(dir, 0o755)) |
| 65 | dir2 := filepath.Join(tmpDir, "dir2") |
| 66 | assert.NilError(t, os.Mkdir(dir2, 0o755)) |
| 67 | file := filepath.Join(tmpDir, "file.txt") |
| 68 | assert.NilError(t, os.WriteFile(file, []byte("hello"), 0o644)) |
| 69 | existing := filepath.Join(tmpDir, "existing") |
| 70 | assert.NilError(t, os.Mkdir(existing, 0o755)) |
| 71 | missing := filepath.Join(tmpDir, "missing") |
| 72 | |
| 73 | tests := []struct { |
| 74 | name string |
| 75 | opts Options |
| 76 | args []string |
| 77 | expected []string |
| 78 | }{ |
| 79 | { |
| 80 | |
| 81 | name: "dir into missing dst", opts: Options{Recursive: true}, |
| 82 | args: []string{dir, missing}, expected: []string{dir + "/", missing}, |
| 83 | }, |
| 84 | { |
| 85 | |
| 86 | name: "dir into existing dst", opts: Options{Recursive: true}, |
| 87 | args: []string{dir, existing}, expected: []string{dir, existing}, |
| 88 | }, |
| 89 | { |
| 90 | |
| 91 | name: "dir with slash into existing dst", opts: Options{Recursive: true}, |
| 92 | args: []string{dir + "/", existing}, expected: []string{dir, existing}, |
| 93 | }, |
| 94 | { |
| 95 | name: "dir with slash into missing dst", opts: Options{Recursive: true}, |
| 96 | args: []string{dir + "/", missing}, expected: []string{dir + "/", missing}, |
| 97 | }, |
| 98 | { |
| 99 | |
| 100 | name: "file into missing dst", opts: Options{Recursive: true}, |
| 101 | args: []string{file, missing}, expected: []string{file, missing}, |
| 102 | }, |
| 103 | { |
| 104 | |
| 105 | name: "multiple sources", opts: Options{Recursive: true}, |
| 106 | args: []string{dir + "/", dir2, missing}, expected: []string{dir, dir2, missing}, |
| 107 | }, |
| 108 | { |
| 109 | |
| 110 | |
| 111 | name: "non-recursive", opts: Options{}, |
| 112 | args: []string{dir + "/", existing}, expected: []string{dir + "/", existing}, |
| 113 | }, |
| 114 | } |
| 115 | for _, tt := range tests { |
| 116 | t.Run(tt.name, func(t *testing.T) { |
| 117 | tool, err := newRsyncTool(&tt.opts) |
| 118 | assert.NilError(t, err) |
| 119 | cmd, err := tool.Command(t.Context(), tt.args, nil) |
| 120 | assert.NilError(t, err) |
| 121 | |
| 122 | |
| 123 | sep := slices.Index(cmd.Args, "--") |
| 124 | assert.Assert(t, sep != -1, "rsync args must contain the %#q separator: %v", "--", cmd.Args) |
| 125 | assert.DeepEqual(t, cmd.Args[sep+1:], tt.expected) |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | func TestRsyncCommandUndeterminedPath(t *testing.T) { |
| 134 | if runtime.GOOS == "windows" { |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | t.Skip("ENOTDIR for a file used as a directory is POSIX-specific") |
| 140 | } |
| 141 | tool, err := newRsyncTool(&Options{Recursive: true}) |
| 142 | if err != nil { |
| 143 | t.Skip("rsync not found:", err) |
| 144 | } |
| 145 | |
| 146 | tmpDir := t.TempDir() |
| 147 | dir := filepath.Join(tmpDir, "dir") |
| 148 | assert.NilError(t, os.Mkdir(dir, 0o755)) |
| 149 | file := filepath.Join(tmpDir, "file.txt") |
| 150 | assert.NilError(t, os.WriteFile(file, []byte("hello"), 0o644)) |
| 151 | |
| 152 | |
| 153 | _, err = tool.Command(t.Context(), []string{dir, filepath.Join(file, "dst")}, nil) |
| 154 | assert.ErrorContains(t, err, "not a directory") |
| 155 | } |
| 156 | |
| 157 | func TestTrimTrailingSlashes(t *testing.T) { |
| 158 | tests := []struct{ path, expected string }{ |
| 159 | {"/tmp/foo", "/tmp/foo"}, |
| 160 | {"/tmp/foo/", "/tmp/foo"}, |
| 161 | {"/tmp/foo//", "/tmp/foo"}, |
| 162 | {"/", "/"}, |
| 163 | {"", ""}, |
| 164 | } |
| 165 | for _, tt := range tests { |
| 166 | assert.Equal(t, trimTrailingSlashes(tt.path), tt.expected, "path=%q", tt.path) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func TestQuoteRemotePath(t *testing.T) { |
| 171 | tests := []struct{ path, expected string }{ |
| 172 | {"/tmp/foo", "/tmp/foo"}, |
| 173 | {"foo bar", "'foo bar'"}, |
| 174 | {"/tmp/$(touch pwned)", `'/tmp/$(touch pwned)'`}, |
| 175 | |
| 176 | {"~", "~"}, |
| 177 | {"~/foo bar", "~/'foo bar'"}, |
| 178 | {"~foo", "~foo"}, |
| 179 | {"~foo/bar", "~foo/bar"}, |
| 180 | {"~foo/bar baz", "~foo/'bar baz'"}, |
| 181 | {"~$(touch pwned)/foo", `'~$(touch pwned)/foo'`}, |
| 182 | } |
| 183 | for _, tt := range tests { |
| 184 | assert.Equal(t, quoteRemotePath(tt.path), tt.expected, "path=%q", tt.path) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | func TestNewAutoSurfacesPathError(t *testing.T) { |
| 191 | t.Setenv("LIMA_HOME", t.TempDir()) |
| 192 | paths := []string{"nonexistent-instance-for-test:/tmp/x", "/tmp/y"} |
| 193 | |
| 194 | _, err := New(t.Context(), string(BackendAuto), paths, &Options{}) |
| 195 | assert.ErrorContains(t, err, "instance `nonexistent-instance-for-test` does not exist") |
| 196 | } |