add pttodo.NewRootFromFiles
This commit is contained in:
@@ -13,6 +13,19 @@ type Root struct {
|
|||||||
Done []Todo
|
Done []Todo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRootFromFiles(p ...string) (Root, error) {
|
||||||
|
var result Root
|
||||||
|
for _, p := range p {
|
||||||
|
subroot, err := NewRootFromFile(p)
|
||||||
|
if err != nil {
|
||||||
|
return Root{}, err
|
||||||
|
}
|
||||||
|
result.MergeIn(subroot)
|
||||||
|
}
|
||||||
|
result.AutoMove()
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewRootFromFile(p string) (Root, error) {
|
func NewRootFromFile(p string) (Root, error) {
|
||||||
if b, err := os.ReadFile(p); err == nil && len(bytes.TrimSpace(b)) == 0 {
|
if b, err := os.ReadFile(p); err == nil && len(bytes.TrimSpace(b)) == 0 {
|
||||||
return Root{}, nil
|
return Root{}, nil
|
||||||
|
|||||||
@@ -233,3 +233,27 @@ func TestRootFromFile(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRootFromFiles(t *testing.T) {
|
||||||
|
d := t.TempDir()
|
||||||
|
ps := []string{
|
||||||
|
path.Join(d, "a"),
|
||||||
|
path.Join(d, "b"),
|
||||||
|
}
|
||||||
|
os.WriteFile(ps[0], []byte(`["a"]`), os.ModePerm)
|
||||||
|
os.WriteFile(ps[1], []byte(`["b"]`), os.ModePerm)
|
||||||
|
|
||||||
|
got, err := NewRootFromFiles(ps...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := Root{
|
||||||
|
Todo: []Todo{
|
||||||
|
{Todo: "a"},
|
||||||
|
{Todo: "b"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
|
||||||
|
t.Errorf("want\n\t%+v, got \n\t%+v", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user