bubble root.yaml to first

master
Bel LaPointe 2023-11-06 12:44:31 -07:00
parent 84aa7cb319
commit 92e8e14c08
3 changed files with 30 additions and 16 deletions

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
@ -50,6 +51,15 @@ func (config config) Targets() []string {
if err != nil { if err != nil {
panic(err) panic(err)
} }
sort.Strings(result)
for i := range result {
if path.Base(result[i]) == "root.yaml" {
newresult := append([]string{result[i]}, result[:i]...)
newresult = append(newresult, result[i+1:]...)
result = newresult
break
}
}
return result return result
} }

View File

@ -3,11 +3,16 @@ package main
import ( import (
"os" "os"
"path" "path"
"sort"
"testing" "testing"
) )
func TestConfigTargets(t *testing.T) { func TestConfigTargets(t *testing.T) {
touch := func(t *testing.T, p string) {
if err := os.WriteFile(p, nil, os.ModePerm); err != nil {
t.Fatal(err)
}
}
cases := map[string]struct { cases := map[string]struct {
setup func(*testing.T, string) setup func(*testing.T, string)
given string given string
@ -19,18 +24,19 @@ func TestConfigTargets(t *testing.T) {
}, },
"one file": { "one file": {
setup: func(t *testing.T, d string) { setup: func(t *testing.T, d string) {
os.WriteFile(path.Join(d, "x"), nil, os.ModePerm) touch(t, "x")
}, },
given: "x", given: "x",
want: []string{"x"}, want: []string{"x"},
}, },
"two files": { "two files": {
setup: func(t *testing.T, d string) { setup: func(t *testing.T, d string) {
os.WriteFile(path.Join(d, "x"), nil, os.ModePerm) touch(t, path.Join(d, "a"))
os.WriteFile(path.Join(d, "x.todo.xyz"), nil, os.ModePerm) touch(t, path.Join(d, "root.yaml"))
touch(t, path.Join(d, "z"))
}, },
given: "x", given: "",
want: []string{"x", "x.todo.xyz"}, want: []string{"root.yaml", "a", "z"},
}, },
"empty dir": { "empty dir": {
setup: func(t *testing.T, d string) { setup: func(t *testing.T, d string) {
@ -42,9 +48,9 @@ func TestConfigTargets(t *testing.T) {
"dir": { "dir": {
setup: func(t *testing.T, d string) { setup: func(t *testing.T, d string) {
os.Mkdir(path.Join(d, "x"), os.ModePerm) os.Mkdir(path.Join(d, "x"), os.ModePerm)
os.WriteFile(path.Join(d, "x", "a"), nil, os.ModePerm) touch(t, path.Join(d, "x", "a"))
os.WriteFile(path.Join(d, "x", "a.todo.xyz"), nil, os.ModePerm) touch(t, path.Join(d, "x", "a.todo.xyz"))
os.WriteFile(path.Join(d, "x", "b"), nil, os.ModePerm) touch(t, path.Join(d, "x", "b"))
}, },
given: "x", given: "x",
want: []string{"a", "a.todo.xyz", "b"}, want: []string{"a", "a.todo.xyz", "b"},
@ -60,22 +66,20 @@ func TestConfigTargets(t *testing.T) {
} }
config := config{target: path.Join(d, c.given)} config := config{target: path.Join(d, c.given)}
got := config.Targets() got := config.Targets()
if len(got) != len(c.want) {
t.Error(c.want, got)
}
for i := range got { for i := range got {
got[i] = path.Base(got[i]) got[i] = path.Base(got[i])
} }
for i := range c.want { for i := range c.want {
c.want[i] = path.Base(c.want[i]) c.want[i] = path.Base(c.want[i])
} }
sort.Strings(c.want)
sort.Strings(got)
t.Logf("want\n\t%+v, got \n\t%+v", c.want, got)
if len(got) != len(c.want) {
t.Error(c.want, got)
}
for i := range got { for i := range got {
if got[i] != c.want[i] { if got[i] != c.want[i] {
t.Error(i, c.want[i], got[i]) t.Errorf("[%d] wanted %s, got %s", i, c.want[i], got[i])
} }
} }
}) })

0
cmd/x Executable file
View File