Fix windows style file paths in fs cp command (#1118)

## Changes
Copying a local file in windows to remote directory in DBFS would fail
if the path was specified as a windows style path (compared to a UNIX
style path). This PR fixes that.

Note, UNIX style paths will continue to work because `filepath.Base`
respects both `/` and `\` as file separators. See: `IsPathSeparator` in
https://go.dev/src/os/path_windows.go.

Fixes issue: https://github.com/databricks/cli/issues/1109.

## Tests
Integration test and manually
```
C:\Users\shreyas.goenka>Desktop\cli.exe fs cp .\Desktop\foo.txt dbfs:/Users/shreyas.goenka@databricks.com
.\Desktop\foo.txt -> dbfs:/Users/shreyas.goenka@databricks.com/foo.txt

C:\Users\shreyas.goenka>Desktop\cli.exe fs cat  dbfs:/Users/shreyas.goenka@databricks.com/foo.txt
hello, world
````
This commit is contained in:
shreyas-goenka 2024-01-12 00:19:42 +05:30 committed by GitHub
parent 7dcdadde79
commit 2c0d06715c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -60,7 +60,7 @@ func (c *copy) cpDirToDir(sourceDir, targetDir string) error {
}
func (c *copy) cpFileToDir(sourcePath, targetDir string) error {
fileName := path.Base(sourcePath)
fileName := filepath.Base(sourcePath)
targetPath := path.Join(targetDir, fileName)
return c.cpFileToFile(sourcePath, targetPath)

View File

@ -6,6 +6,7 @@ import (
"io"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
@ -140,6 +141,22 @@ func TestAccFsCpFileToDir(t *testing.T) {
}
}
func TestAccFsCpFileToDirForWindowsPaths(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non-windows OS")
}
ctx := context.Background()
sourceFiler, sourceDir := setupLocalFiler(t)
targetFiler, targetDir := setupDbfsFiler(t)
setupSourceFile(t, ctx, sourceFiler)
windowsPath := filepath.Join(filepath.FromSlash(sourceDir), "foo.txt")
RequireSuccessfulRun(t, "fs", "cp", windowsPath, targetDir)
assertTargetFile(t, ctx, targetFiler, "foo.txt")
}
func TestAccFsCpDirToDirFileNotOverwritten(t *testing.T) {
ctx := context.Background()
table := setupTable()