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"
"path"
"path/filepath"
"sort"
"strings"
)
@ -50,6 +51,15 @@ func (config config) Targets() []string {
if err != nil {
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
}

View File

@ -3,11 +3,16 @@ package main
import (
"os"
"path"
"sort"
"testing"
)
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 {
setup func(*testing.T, string)
given string
@ -19,18 +24,19 @@ func TestConfigTargets(t *testing.T) {
},
"one file": {
setup: func(t *testing.T, d string) {
os.WriteFile(path.Join(d, "x"), nil, os.ModePerm)
touch(t, "x")
},
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)
touch(t, path.Join(d, "a"))
touch(t, path.Join(d, "root.yaml"))
touch(t, path.Join(d, "z"))
},
given: "x",
want: []string{"x", "x.todo.xyz"},
given: "",
want: []string{"root.yaml", "a", "z"},
},
"empty dir": {
setup: func(t *testing.T, d string) {
@ -42,9 +48,9 @@ func TestConfigTargets(t *testing.T) {
"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)
touch(t, path.Join(d, "x", "a"))
touch(t, path.Join(d, "x", "a.todo.xyz"))
touch(t, path.Join(d, "x", "b"))
},
given: "x",
want: []string{"a", "a.todo.xyz", "b"},
@ -60,22 +66,20 @@ func TestConfigTargets(t *testing.T) {
}
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)
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 {
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