pttodo/cmd/config_test.go

84 lines
1.7 KiB
Go

package main
import (
"os"
"path"
"sort"
"testing"
)
func TestConfigTargets(t *testing.T) {
cases := map[string]struct {
setup func(*testing.T, string)
given string
want []string
}{
"does not exist": {
given: "x",
want: []string{"x"},
},
"one file": {
setup: func(t *testing.T, d string) {
os.WriteFile(path.Join(d, "x"), nil, os.ModePerm)
},
given: "x",
want: []string{"x"},
},
"two files": {
setup: func(t *testing.T, d string) {
os.WriteFile(path.Join(d, "x"), nil, os.ModePerm)
os.WriteFile(path.Join(d, "x.todo.xyz"), nil, os.ModePerm)
},
given: "x",
want: []string{"x", "x.todo.xyz"},
},
"empty dir": {
setup: func(t *testing.T, d string) {
os.Mkdir(path.Join(d, "x"), os.ModePerm)
},
given: "x",
want: []string{"root.yaml"},
},
"dir": {
setup: func(t *testing.T, d string) {
os.Mkdir(path.Join(d, "x"), os.ModePerm)
os.WriteFile(path.Join(d, "x", "a"), nil, os.ModePerm)
os.WriteFile(path.Join(d, "x", "a.todo.xyz"), nil, os.ModePerm)
os.WriteFile(path.Join(d, "x", "b"), nil, os.ModePerm)
},
given: "x",
want: []string{"a", "a.todo.xyz", "b"},
},
}
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
d := t.TempDir()
if c.setup != nil {
c.setup(t, d)
}
config := config{target: path.Join(d, c.given)}
got := config.Targets()
if len(got) != len(c.want) {
t.Error(c.want, got)
}
for i := range got {
got[i] = path.Base(got[i])
}
for i := range c.want {
c.want[i] = path.Base(c.want[i])
}
sort.Strings(c.want)
sort.Strings(got)
for i := range got {
if got[i] != c.want[i] {
t.Error(i, c.want[i], got[i])
}
}
})
}
}