Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit a2f2b95

Browse files
Merge pull request #2195 from tiborvass/19.03-fix-cp
[19.03 backport] cp: allow trailing slash in non-existant destination Upstream-commit: ab4a5cc0f77b19f8fc960709d66f24a433e0169c Component: cli
2 parents 982bbfb + 17870bc commit a2f2b95

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

components/cli/cli/command/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func AddPlatformFlag(flags *pflag.FlagSet, target *string) {
130130

131131
// ValidateOutputPath validates the output paths of the `export` and `save` commands.
132132
func ValidateOutputPath(path string) error {
133-
dir := filepath.Dir(path)
133+
dir := filepath.Dir(filepath.Clean(path))
134134
if dir != "" && dir != "." {
135135
if _, err := os.Stat(dir); os.IsNotExist(err) {
136136
return errors.Errorf("invalid output path: directory %q does not exist", dir)

components/cli/cli/command/utils_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package command
22

33
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
47
"testing"
58

9+
"github.com/pkg/errors"
610
"gotest.tools/assert"
711
)
812

@@ -31,3 +35,39 @@ func TestStringSliceReplaceAt(t *testing.T) {
3135
assert.Assert(t, !ok)
3236
assert.DeepEqual(t, []string{"foo"}, out)
3337
}
38+
39+
func TestValidateOutputPath(t *testing.T) {
40+
basedir, err := ioutil.TempDir("", "TestValidateOutputPath")
41+
assert.NilError(t, err)
42+
defer os.RemoveAll(basedir)
43+
dir := filepath.Join(basedir, "dir")
44+
notexist := filepath.Join(basedir, "notexist")
45+
err = os.MkdirAll(dir, 0755)
46+
assert.NilError(t, err)
47+
file := filepath.Join(dir, "file")
48+
err = ioutil.WriteFile(file, []byte("hi"), 0644)
49+
assert.NilError(t, err)
50+
var testcases = []struct {
51+
path string
52+
err error
53+
}{
54+
{basedir, nil},
55+
{file, nil},
56+
{dir, nil},
57+
{dir + string(os.PathSeparator), nil},
58+
{notexist, nil},
59+
{notexist + string(os.PathSeparator), nil},
60+
{filepath.Join(notexist, "file"), errors.New("does not exist")},
61+
}
62+
63+
for _, testcase := range testcases {
64+
t.Run(testcase.path, func(t *testing.T) {
65+
err := ValidateOutputPath(testcase.path)
66+
if testcase.err == nil {
67+
assert.NilError(t, err)
68+
} else {
69+
assert.ErrorContains(t, err, testcase.err.Error())
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)