Skip to content

Commit

Permalink
Unit test for MustFindString (k3s-io#8013)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <derek.nola@suse.com>
  • Loading branch information
dereknola committed Aug 3, 2023
1 parent 124c22b commit 0142978
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/configfilearg/defaultparser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configfilearg

import (
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 0142978

Please sign in to comment.