add test cases

This commit is contained in:
itchyny 2020-01-07 21:32:59 +09:00
parent dc16bb0051
commit c7ce5a25a1
3 changed files with 107 additions and 0 deletions

2
go.mod
View file

@ -1,3 +1,5 @@
module github.com/itchyny/mmv
go 1.13
require github.com/stretchr/testify v1.4.0

10
go.sum Normal file
View file

@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

95
mmv_test.go Normal file
View file

@ -0,0 +1,95 @@
package mmv
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMove(t *testing.T) {
testCases := []struct {
name string
files map[string]string
contents map[string]string
expected map[string]string
err error
}{
{
name: "nothing",
files: nil,
},
{
name: "one file",
files: map[string]string{
"foo": "bar",
},
contents: map[string]string{
"foo": "0",
},
expected: map[string]string{
"bar": "0",
},
},
{
name: "two files",
files: map[string]string{
"foo": "qux",
"bar": "quxx",
},
contents: map[string]string{
"foo": "0",
"bar": "1",
"baz": "2",
},
expected: map[string]string{
"qux": "0",
"quxx": "1",
"baz": "2",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "mvv-"+tc.name+"-")
defer os.RemoveAll(dir)
require.NoError(t, os.Chdir(dir))
require.NoError(t, err)
require.NoError(t, setupFiles(tc.contents))
require.NoError(t, Move(tc.files))
assert.Equal(t, tc.expected, fileContents("."))
})
}
}
func setupFiles(contents map[string]string) error {
for f, cnt := range contents {
if err := ioutil.WriteFile(f, []byte(cnt), 0600); err != nil {
return err
}
}
return nil
}
func fileContents(dir string) map[string]string {
m := make(map[string]string)
fis, _ := ioutil.ReadDir(dir)
for _, fi := range fis {
if fi.IsDir() {
for k, v := range fileContents(filepath.Join(dir, fi.Name())) {
m[k] = v
}
} else {
path := filepath.Join(dir, fi.Name())
cnt, _ := ioutil.ReadFile(path)
m[path] = string(cnt)
}
}
if len(m) == 0 {
return nil
}
return m
}