|
| 1 | +/* |
| 2 | +Copyright © 2023 OpenFGA |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmdutils |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + "github.com/spf13/viper" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestBindViperToFlags(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + |
| 31 | + const flagName = "header" |
| 32 | + |
| 33 | + testcases := []struct { |
| 34 | + name string |
| 35 | + value any |
| 36 | + expected []string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "slice value produces one flag value per element", |
| 40 | + value: []any{ |
| 41 | + "X-Custom-Header: value1", |
| 42 | + "X-Request-ID: abc123", |
| 43 | + }, |
| 44 | + expected: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "single element slice", |
| 48 | + value: []any{"X-Custom-Header: value1"}, |
| 49 | + expected: []string{"X-Custom-Header: value1"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "empty slice leaves flag untouched", |
| 53 | + value: []any{}, |
| 54 | + expected: []string{}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "typed string slice produces one flag value per element", |
| 58 | + value: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, |
| 59 | + expected: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "typed int slice produces one flag value per element", |
| 63 | + value: []int{1, 2, 3}, |
| 64 | + expected: []string{"1", "2", "3"}, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "scalar string produces single flag value", |
| 68 | + value: "https://api.fga.example", |
| 69 | + expected: []string{"https://api.fga.example"}, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "space separated scalar string (env var style) splits into multiple flag values", |
| 73 | + value: "X-Custom-Header:value1 X-Request-ID:abc123", |
| 74 | + expected: []string{"X-Custom-Header:value1", "X-Request-ID:abc123"}, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "boolean value is stringified", |
| 78 | + value: true, |
| 79 | + expected: []string{"true"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "integer value is stringified", |
| 83 | + value: 42, |
| 84 | + expected: []string{"42"}, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + for _, test := range testcases { |
| 89 | + t.Run(test.name, func(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + cmd := &cobra.Command{Use: "root"} |
| 93 | + cmd.Flags().StringArray(flagName, nil, "") |
| 94 | + |
| 95 | + viperInstance := viper.New() |
| 96 | + viperInstance.Set(flagName, test.value) |
| 97 | + |
| 98 | + BindViperToFlags(cmd, viperInstance) |
| 99 | + |
| 100 | + got, err := cmd.Flags().GetStringArray(flagName) |
| 101 | + require.NoError(t, err) |
| 102 | + assert.Equal(t, test.expected, got) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments