From 0e578e6b834be1f9c2ff122ef9f215cd2180f729 Mon Sep 17 00:00:00 2001 From: itchyny Date: Wed, 8 Jan 2020 00:12:14 +0900 Subject: [PATCH] return error on empty path --- mmv.go | 9 +++++++++ mmv_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/mmv.go b/mmv.go index bd1da4e..eaa2b31 100644 --- a/mmv.go +++ b/mmv.go @@ -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} } diff --git a/mmv_test.go b/mmv_test.go index 761976c..05e7724 100644 --- a/mmv_test.go +++ b/mmv_test.go @@ -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{