restic-scheduler/utils_test.go

102 lines
1.7 KiB
Go
Raw Permalink Normal View History

2022-02-23 22:13:00 +00:00
package main_test
import (
"testing"
main "git.iamthefij.com/iamthefij/restic-scheduler"
"github.com/go-test/deep"
)
2022-02-24 05:53:48 +00:00
func AssertEqual(t *testing.T, message string, expected, actual interface{}) bool {
t.Helper()
2022-03-24 17:09:33 +00:00
if diff := deep.Equal(expected, actual); diff != nil {
t.Errorf("%s: %v", message, diff)
return false
2022-02-24 05:53:48 +00:00
}
return true
}
func AssertEqualFail(t *testing.T, message string, expected, actual interface{}) {
t.Helper()
if !AssertEqual(t, message, expected, actual) {
t.FailNow()
}
}
2022-02-23 22:13:00 +00:00
func TestMergeEnvMap(t *testing.T) {
t.Parallel()
cases := []struct {
name string
parent map[string]string
child map[string]string
expected map[string]string
}{
{
name: "No child",
parent: map[string]string{
"key": "value",
},
child: nil,
expected: map[string]string{
"key": "value",
},
},
{
name: "No parent",
parent: nil,
child: map[string]string{
"key": "value",
},
expected: map[string]string{
"key": "value",
},
},
{
name: "Overwrite value",
parent: map[string]string{
"key": "old",
"other": "other",
},
child: map[string]string{
"key": "new",
},
expected: map[string]string{
"key": "new",
"other": "other",
},
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
actual := main.MergeEnvMap(c.parent, c.child)
if diff := deep.Equal(c.expected, actual); diff != nil {
t.Error(diff)
}
})
}
}
func TestEnvMapToList(t *testing.T) {
t.Parallel()
env := map[string]string{
"key": "value",
}
expected := []string{
"key=value",
}
actual := main.EnvMapToList(env)
if diff := deep.Equal(expected, actual); diff != nil {
t.Error(diff)
}
}