Skip to content

chore: color value_source for deployment values #9922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Sep 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit test for missing opts case
  • Loading branch information
Emyrk committed Sep 29, 2023
commit 03a78834e463eb7e49213cbaf177bb5f001fed32
40 changes: 35 additions & 5 deletions cli/clibase/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,36 @@ func TestOptionSet_ParseEnv(t *testing.T) {
func TestOptionSet_JsonMarshal(t *testing.T) {
t.Parallel()

// This unit test ensures if the source optionset is missing the option
// and cannot determine the type, it will not panic. The unmarshal will
// succeed with a best effort.
t.Run("MissingSrcOption", func(t *testing.T) {
t.Parallel()

var str clibase.String = "something"
var arr clibase.StringArray = []string{"foo", "bar"}
opts := clibase.OptionSet{
clibase.Option{
Name: "StringOpt",
Value: &str,
},
clibase.Option{
Name: "ArrayOpt",
Value: &arr,
},
}
data, err := json.Marshal(opts)
require.NoError(t, err, "marshal option set")

tgt := clibase.OptionSet{}
err = json.Unmarshal(data, &tgt)
require.NoError(t, err, "unmarshal option set")
for i := range opts {
compareOptionsExceptValues(t, opts[i], tgt[i])
require.Empty(t, tgt[i].Value.String(), "unknown value types are empty")
}
})

t.Run("RegexCase", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -265,7 +295,8 @@ func TestOptionSet_JsonMarshal(t *testing.T) {
exp := opts[i]
found := newOpts[i]

compareOptions(t, exp, found)
compareOptionsExceptValues(t, exp, found)
compareValues(t, exp, found)
}

thirdOpts := (&codersdk.DeploymentValues{}).Options()
Expand All @@ -279,12 +310,13 @@ func TestOptionSet_JsonMarshal(t *testing.T) {
exp := opts[i]
found := thirdOpts[i]

compareOptions(t, exp, found)
compareOptionsExceptValues(t, exp, found)
compareValues(t, exp, found)
}
})
}

func compareOptions(t *testing.T, exp, found clibase.Option) {
func compareOptionsExceptValues(t *testing.T, exp, found clibase.Option) {
t.Helper()

require.Equalf(t, exp.Name, found.Name, "option name %q", exp.Name)
Expand All @@ -301,8 +333,6 @@ func compareOptions(t *testing.T, exp, found clibase.Option) {
require.Equalf(t, exp.Group, found.Group, "option group %q", exp.Name)
// UseInstead is the same comparison problem, just check the length
require.Equalf(t, len(exp.UseInstead), len(found.UseInstead), "option use instead %q", exp.Name)

compareValues(t, exp, found)
}

func compareValues(t *testing.T, exp, found clibase.Option) {
Expand Down