(t *testing.T)
| 197 | } |
| 198 | |
| 199 | func TestSetupTrace(t *testing.T) { |
| 200 | cases := []struct { |
| 201 | args []string |
| 202 | env map[string]string |
| 203 | long bool |
| 204 | expected string |
| 205 | }{ |
| 206 | {nil, nil, false, "trace flag = false"}, |
| 207 | {[]string{"--trace"}, nil, true, "trace flag = true"}, |
| 208 | {[]string{"--no-such-flag"}, nil, false, "unknown flag: --no-such-flag"}, |
| 209 | {nil, map[string]string{"DBG_TRACE": "true"}, true, "trace flag = true"}, |
| 210 | } |
| 211 | |
| 212 | for idx, tc := range cases { |
| 213 | i := strconv.Itoa(idx) |
| 214 | // test command that store value of foobar in local variable |
| 215 | trace := &cobra.Command{ |
| 216 | Use: "trace", |
| 217 | RunE: func(cmd *cobra.Command, args []string) error { |
| 218 | return fmt.Errorf("trace flag = %t", viper.GetBool(TraceFlag)) |
| 219 | }, |
| 220 | } |
| 221 | cmd := PrepareBaseCmd(trace, "DBG", "/qwerty/asdfgh") // some missing dir.. |
| 222 | cmd.Exit = func(int) {} |
| 223 | |
| 224 | viper.Reset() |
| 225 | args := append([]string{cmd.Use}, tc.args...) |
| 226 | stdout, stderr, err := RunCaptureWithArgs(cmd, args, tc.env) |
| 227 | require.NotNil(t, err, i) |
| 228 | require.Equal(t, "", stdout, i) |
| 229 | require.NotEqual(t, "", stderr, i) |
| 230 | msg := strings.Split(stderr, "\n") |
| 231 | desired := fmt.Sprintf("ERROR: %s", tc.expected) |
| 232 | assert.Equal(t, desired, msg[0], i) |
| 233 | t.Log(msg) |
| 234 | if tc.long && assert.True(t, len(msg) > 2, i) { |
| 235 | // the next line starts the stack trace... |
| 236 | assert.Contains(t, stderr, "TestSetupTrace", i) |
| 237 | assert.Contains(t, stderr, "setup_test.go", i) |
| 238 | } |
| 239 | } |
| 240 | } |
nothing calls this directly
no test coverage detected