check same source path after clean

This commit is contained in:
itchyny 2020-01-08 11:10:27 +09:00
parent 7ab2aee878
commit ac672113f2
2 changed files with 23 additions and 0 deletions

11
mmv.go
View file

@ -31,6 +31,14 @@ func (err *emptyPathError) Error() string {
return "empty path error"
}
type sameSourceError struct {
path string
}
func (err *sameSourceError) Error() string {
return fmt.Sprintf("duplicate source: %s", err.path)
}
type sameDestinationError struct {
path string
}
@ -57,6 +65,9 @@ func buildRenames(files map[string]string) ([]rename, error) {
if d := filepath.Clean(src); d != src {
delete(files, src)
src = d
if _, ok := files[src]; ok {
return nil, &sameSourceError{src}
}
files[src] = dst
}
if d := filepath.Clean(dst); d != dst {

View file

@ -186,6 +186,18 @@ func TestRename(t *testing.T) {
"foo": "1",
},
},
{
name: "cleaned path same source error",
files: map[string]string{
"foo": "baz",
"bar/../foo": "bar",
},
contents: map[string]string{
"foo": "0",
"bar": "1",
},
err: &sameSourceError{"foo"},
},
{
name: "cleaned path same destination error",
files: map[string]string{