test config resolving targets
parent
d36f9e74b3
commit
84aa7cb319
|
|
@ -4,6 +4,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -54,7 +55,11 @@ func (config config) Targets() []string {
|
|||
|
||||
func (config config) targets() ([]string, error) {
|
||||
patterns := []string{config.target, fmt.Sprintf("%s.*", config.target)}
|
||||
if stat, err := os.Stat(config.target); err == nil && stat.IsDir() {
|
||||
isDir := false
|
||||
if stat, err := os.Stat(config.target); err == nil {
|
||||
isDir = stat.IsDir()
|
||||
}
|
||||
if isDir {
|
||||
patterns = []string{fmt.Sprintf("%s/*", config.target)}
|
||||
}
|
||||
|
||||
|
|
@ -68,6 +73,9 @@ func (config config) targets() ([]string, error) {
|
|||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
if isDir {
|
||||
return []string{path.Join(config.target, "root.yaml")}, nil
|
||||
}
|
||||
return []string{config.target}, nil
|
||||
}
|
||||
return result, nil
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
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])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue