From 84aa7cb319ed1a82e81a84ce808d679168f4dd0c Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 6 Nov 2023 12:38:11 -0700 Subject: [PATCH] test config resolving targets --- cmd/config.go | 10 +++++- cmd/config_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 cmd/config_test.go diff --git a/cmd/config.go b/cmd/config.go index 9976dd2..3666ef9 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 diff --git a/cmd/config_test.go b/cmd/config_test.go new file mode 100644 index 0000000..aa3e4e9 --- /dev/null +++ b/cmd/config_test.go @@ -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]) + } + } + }) + } +}