Commit 134c1a8
Fixed Trailing Slashes in cp command + added unit tests (#3921)
Fixes: #2835
## Changes
Fixed handling of trailing directory separators in the `cp` command.
When a target path ends with `/` or `\`, the command now validates that
the directory exists before copying. If the directory doesn't exist, it
returns a clear error message instead of failing later.
Added a helper function `hasTrailingDirSeparator` to detect trailing
directory separators for both Unix-style (`/`) and Windows-style (`\`)
paths.
## Why
Previously, the `cp` command didn't properly handle trailing directory
separators. When users specified a target path like `target/dir/`
(expecting it to be treated as a directory), the command would not
validate that the directory exists first, leading to confusing error
messages later in the copy process. This change ensures that trailing
directory separators are explicitly treated as indicating a directory
target, and validates that the directory exists upfront.
## Tests
Added unit tests in `integration/cmd/fs/cp_test.go`:
- `TestFsCpFileToNonExistentDir`: Tests copying files to both existing
and non-existent directories with trailing slashes
- `TestFsCpFileToNonExistentDirWindowsPaths`: Windows-specific tests for
trailing backslashes (`\`) and forward slashes (`/`) on Windows
The tests verify that:
- Copying to an existing directory with a trailing separator succeeds
- Copying to a non-existent directory with a trailing separator returns
an appropriate error message
- Both Unix-style (`/`) and Windows-style (`\`) separators are handled
correctly
---------
Co-authored-by: Andrew Nester <[email protected]>1 parent 05a5e2a commit 134c1a8
2 files changed
+82
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
126 | 128 | | |
127 | 129 | | |
128 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
129 | 141 | | |
130 | 142 | | |
131 | 143 | | |
| |||
190 | 202 | | |
191 | 203 | | |
192 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
193 | 210 | | |
194 | 211 | | |
195 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
0 commit comments