MCPcopy
hub / github.com/grafana/dskit / TestParseFlagsAndArgumentsWithEnv

Function TestParseFlagsAndArgumentsWithEnv

flagext/parse_test.go:14–32  ·  view source on GitHub ↗

Tests in this file mutate os.Args and must NOT use t.Parallel().

(t *testing.T)

Source from the content-addressed store, hash-verified

12// Tests in this file mutate os.Args and must NOT use t.Parallel().
13
14func TestParseFlagsAndArgumentsWithEnv(t *testing.T) {
15 fs := flag.NewFlagSet("test", flag.ContinueOnError)
16 var host string
17 var port int
18 fs.StringVar(&host, "server.host", "localhost", "host")
19 fs.IntVar(&port, "server.port", 80, "port")
20
21 origArgs := os.Args
22 t.Cleanup(func() { os.Args = origArgs })
23 os.Args = []string{"cmd", "-server.host", "clihost"}
24
25 t.Setenv("APP_SERVER_PORT", "9090")
26
27 args, err := ParseFlagsAndArgumentsWithEnv(fs, "APP")
28 require.NoError(t, err)
29 assert.Empty(t, args)
30 assert.Equal(t, "clihost", host, "CLI flag should be used")
31 assert.Equal(t, 9090, port, "env var should set unset flag")
32}
33
34func TestParseFlagsWithoutArgumentsWithEnv(t *testing.T) {
35 fs := flag.NewFlagSet("test", flag.ContinueOnError)

Callers

nothing calls this directly

Calls 2

EqualMethod · 0.45

Tested by

no test coverage detected