forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags_test.go
55 lines (44 loc) · 1.43 KB
/
flags_test.go
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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"flag"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFlagsFromEnv(t *testing.T) {
expected := "my value"
os.Setenv("EPR_TEST_DUMMY", expected)
var dummyFlag string
flagSet := flag.NewFlagSet("", flag.PanicOnError)
flagSet.StringVar(&dummyFlag, "test-dummy", "default", "Dummy flag used for testing.")
require.Equal(t, "default", dummyFlag)
flagsFromEnv(flagSet)
require.Equal(t, expected, dummyFlag)
}
func TestFlagsPrecedence(t *testing.T) {
expected := "flag value"
os.Setenv("EPR_TEST_PRECEDENCE_DUMMY", "other value")
var dummyFlag string
flagSet := flag.NewFlagSet("", flag.PanicOnError)
flagSet.StringVar(&dummyFlag, "test-precedence-dummy", "default", "Dummy flag used for testing.")
require.Equal(t, "default", dummyFlag)
args := []string{"test", "-test-precedence-dummy=" + expected}
parseFlagSetWithArgs(flagSet, args)
require.Equal(t, expected, dummyFlag)
}
func TestFlagEnvName(t *testing.T) {
cases := []struct {
flagName string
expected string
}{
{"dry-run", "EPR_DRY_RUN"},
{"test-dummy", "EPR_TEST_DUMMY"},
}
for _, c := range cases {
assert.Equal(t, c.expected, flagEnvName(c.flagName))
}
}