Unit test for MustFindString (#8013)

Signed-off-by: Derek Nola <derek.nola@suse.com>
This commit is contained in:
Derek Nola 2023-07-31 10:22:34 -07:00 committed by GitHub
parent 946c8ea842
commit 6d360e6473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package configfilearg
import (
"os"
"reflect"
"testing"
)
@ -72,3 +73,57 @@ func Test_UnitMustParse(t *testing.T) {
})
}
}
func Test_UnitMustFindString(t *testing.T) {
tests := []struct {
name string
args []string
target string
setup func() error // Optional, delete if unused
teardown func() error // Optional, delete if unused
want string
}{
{
name: "Target not found in config file",
args: []string{"--foo", "bar"},
target: "token",
want: "",
setup: func() error { return os.Setenv("K3S_CONFIG_FILE", "./testdata/data.yaml") },
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
{
name: "Target found in config file",
args: []string{"--foo", "bar"},
target: "token",
want: "12345",
setup: func() error { return os.Setenv("K3S_CONFIG_FILE", "./testdata/defaultdata.yaml") },
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
{
name: "Override flag found, function is short-circuited",
args: []string{"--foo", "bar", "-h"},
target: "token",
want: "-h",
setup: func() error { return os.Setenv("K3S_CONFIG_FILE", "./testdata/defaultdata.yaml") },
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer tt.teardown()
if err := tt.setup(); err != nil {
t.Errorf("Setup for MustFindString() failed = %v", err)
return
}
if got := MustFindString(tt.args, tt.target); got != tt.want {
t.Errorf("MustFindString() = %+v\nWant = %+v", got, tt.want)
}
})
}
}