return error on empty path

This commit is contained in:
itchyny 2020-01-08 00:12:14 +09:00
parent 8d8905510f
commit 0e578e6b83
2 changed files with 33 additions and 0 deletions

9
mmv.go
View file

@ -25,6 +25,12 @@ type rename struct {
src, dst string
}
type emptyPathError struct{}
func (err *emptyPathError) Error() string {
return "empty path error"
}
type sameDestinationError struct {
path string
}
@ -38,6 +44,9 @@ func buildRenames(files map[string]string) ([]rename, error) {
vs := make(map[string]int, len(files))
revs := make(map[string]string, len(files))
for src, dst := range files {
if src == "" || dst == "" {
return nil, &emptyPathError{}
}
if _, ok := revs[dst]; ok {
return nil, &sameDestinationError{dst}
}

View file

@ -132,6 +132,30 @@ func TestMove(t *testing.T) {
"foo": "2",
},
},
{
name: "empty source path error",
files: map[string]string{
"foo": "baz",
"": "baz",
},
contents: map[string]string{
"foo": "0",
"bar": "1",
},
err: &emptyPathError{},
},
{
name: "empty destination path error",
files: map[string]string{
"foo": "baz",
"bar": "",
},
contents: map[string]string{
"foo": "0",
"bar": "1",
},
err: &emptyPathError{},
},
{
name: "same destination error",
files: map[string]string{