From f4fe017ede1e0ad1e5c5c120d8ae2f258de3829c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Sat, 10 Sep 2022 15:00:20 +0200 Subject: [PATCH] chore: add toggle package unit tests (#4577) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Signed-off-by: Charles-Edouard Brétéché --- pkg/toggle/toggle_test.go | 176 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 pkg/toggle/toggle_test.go diff --git a/pkg/toggle/toggle_test.go b/pkg/toggle/toggle_test.go new file mode 100644 index 0000000000..c79c96fc27 --- /dev/null +++ b/pkg/toggle/toggle_test.go @@ -0,0 +1,176 @@ +package toggle + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_newToggle(t *testing.T) { + type args struct { + defaultValue bool + envVar string + } + tests := []struct { + name string + args args + want *toggle + }{{ + name: "nothing set", + want: &toggle{}, + }, { + name: "default value", + args: args{ + defaultValue: true, + }, + want: &toggle{ + defaultValue: true, + }, + }, { + name: "env var", + args: args{ + envVar: "test", + }, + want: &toggle{ + envVar: "test", + }, + }, { + name: "all", + args: args{ + defaultValue: true, + envVar: "test", + }, + want: &toggle{ + defaultValue: true, + envVar: "test", + }, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := newToggle(tt.args.defaultValue, tt.args.envVar) + assert.Equal(t, tt.want, got) + }) + } +} + +func Test_toggle_Parse(t *testing.T) { + type args struct { + in string + } + tests := []struct { + name string + args args + wantErr bool + }{{ + name: "empty", + wantErr: false, + }, { + name: "true", + args: args{"true"}, + wantErr: false, + }, { + name: "false", + args: args{"false"}, + wantErr: false, + }, { + name: "not a bool", + args: args{"test"}, + wantErr: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := newToggle(false, "") + err := tr.Parse(tt.args.in) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + +func Test_toggle_Enabled(t *testing.T) { + type fields struct { + defaultValue bool + envVar string + } + tests := []struct { + name string + fields fields + value string + env map[string]string + want bool + }{{ + name: "empty", + want: false, + }, { + name: "default true", + fields: fields{ + defaultValue: true, + }, + want: true, + }, { + name: "default false", + fields: fields{ + defaultValue: false, + }, + want: false, + }, { + name: "parse true", + fields: fields{ + defaultValue: false, + }, + value: "true", + want: true, + }, { + name: "parse false", + fields: fields{ + defaultValue: true, + }, + value: "false", + want: false, + }, { + name: "env true", + fields: fields{ + defaultValue: false, + envVar: "TOGGLE_FLAG", + }, + env: map[string]string{ + "TOGGLE_FLAG": "true", + }, + want: true, + }, { + name: "env false", + fields: fields{ + defaultValue: true, + envVar: "TOGGLE_FLAG", + }, + env: map[string]string{ + "TOGGLE_FLAG": "false", + }, + want: false, + }, { + name: "value takes precedence on env var", + fields: fields{ + defaultValue: false, + envVar: "TOGGLE_FLAG", + }, + value: "true", + env: map[string]string{ + "TOGGLE_FLAG": "false", + }, + want: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.env { + t.Setenv(k, v) + } + tr := newToggle(tt.fields.defaultValue, tt.fields.envVar) + tr.Parse(tt.value) + got := tr.Enabled() + assert.Equal(t, tt.want, got) + }) + } +}