summaryrefslogtreecommitdiff
path: root/cmd/args_test.go
blob: 3153fd862515fdf55add6835c3e9823f706916d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package cmd

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestParseConfigUpdates(t *testing.T) {
	tests := []struct {
		flags   string
		err     bool
		updates map[string]any
	}{
		{
			flags:   `--foo bar`,
			updates: map[string]any{"foo": "bar"},
		},
		{
			flags:   `--foo=bar`,
			updates: map[string]any{"foo": "bar"},
		},
		{
			flags:   `--foo`,
			updates: map[string]any{"foo": true},
		},
		{
			flags:   `--foo false`,
			updates: map[string]any{"foo": false},
		},
		{
			flags:   `--foo a --foo b`,
			updates: map[string]any{"foo": []any{"a", "b"}},
		},
		{
			flags:   `--foo a --foo=b`,
			updates: map[string]any{"foo": []any{"a", "b"}},
		},
		{
			flags:   `--foo 69`,
			updates: map[string]any{"foo": 69},
		},
		{
			flags:   `--foo.bar a`,
			updates: map[string]any{"foo.bar": "a"},
		},
		{
			flags: `foo`,
			err:   true,
		},
		{
			flags: `--foo a b`,
			err:   true,
		},
	}
	for _, test := range tests {
		t.Run(test.flags, func(t *testing.T) {
			args, err := parseConfigArgs(strings.Fields(test.flags))

			if test.err {
				require.Error(t, err)
				require.Empty(t, args)
				return
			}

			require.NoError(t, err)
			require.Equal(t, test.updates, args)
		})
	}
}