From 92e8e14c08cb85a7e338b51939fc5c040017c498 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 6 Nov 2023 12:44:31 -0700 Subject: [PATCH] bubble root.yaml to first --- cmd/config.go | 10 ++++++++++ cmd/config_test.go | 36 ++++++++++++++++++++---------------- cmd/x | 0 3 files changed, 30 insertions(+), 16 deletions(-) create mode 100755 cmd/x diff --git a/cmd/config.go b/cmd/config.go index 3666ef9..b2f382a 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 } diff --git a/cmd/config_test.go b/cmd/config_test.go index aa3e4e9..5c43d78 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -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]) } } }) diff --git a/cmd/x b/cmd/x new file mode 100755 index 0000000..e69de29