MCPcopy Index your code
hub / github.com/jetify-com/devbox / TestConfigFlagDefaultsToEnv

Function TestConfigFlagDefaultsToEnv

internal/boxcli/config_test.go:17–67  ·  view source on GitHub ↗

TestConfigFlagDefaultsToEnv verifies that the --config flag defaults to the DEVBOX_CONFIG environment variable when the flag is not passed, and that an explicit flag value still takes precedence.

(t *testing.T)

Source from the content-addressed store, hash-verified

15// DEVBOX_CONFIG environment variable when the flag is not passed, and that an
16// explicit flag value still takes precedence.
17func TestConfigFlagDefaultsToEnv(t *testing.T) {
18 tests := []struct {
19 name string
20 envVal string // empty means unset
21 args []string
22 want string
23 }{
24 {
25 name: "unset env and no flag yields empty path",
26 want: "",
27 },
28 {
29 name: "env var sets the default config path",
30 envVal: "/path/from/env",
31 want: "/path/from/env",
32 },
33 {
34 name: "explicit flag overrides env var",
35 envVal: "/path/from/env",
36 args: []string{"--config", "/path/from/flag"},
37 want: "/path/from/flag",
38 },
39 {
40 name: "explicit flag without env var",
41 args: []string{"-c", "/path/from/flag"},
42 want: "/path/from/flag",
43 },
44 }
45
46 for _, testCase := range tests {
47 t.Run(testCase.name, func(t *testing.T) {
48 if testCase.envVal != "" {
49 t.Setenv(envir.DevboxConfig, testCase.envVal)
50 } else {
51 t.Setenv(envir.DevboxConfig, "")
52 }
53
54 flags := &pathFlag{}
55 cmd := &cobra.Command{Use: "test", RunE: func(*cobra.Command, []string) error { return nil }}
56 flags.register(cmd)
57 cmd.SetArgs(testCase.args)
58 if err := cmd.Execute(); err != nil {
59 t.Fatal(err)
60 }
61
62 if flags.path != testCase.want {
63 t.Errorf("flags.path = %q, want %q", flags.path, testCase.want)
64 }
65 })
66 }
67}

Callers

nothing calls this directly

Calls 3

registerMethod · 0.95
RunMethod · 0.65
ExecuteMethod · 0.65

Tested by

no test coverage detected