MCPcopy Create free account
hub / github.com/dropbox/dbxcli / TestHelpOutputRemainsTextWithJSONFlag

Function TestHelpOutputRemainsTextWithJSONFlag

cmd/root_test.go:386–438  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

384}
385
386func TestHelpOutputRemainsTextWithJSONFlag(t *testing.T) {
387 tests := []struct {
388 name string
389 args []string
390 }{
391 {
392 name: "root help",
393 args: []string{"--help", "--output=json"},
394 },
395 {
396 name: "root no command",
397 args: []string{"--output=json"},
398 },
399 {
400 name: "command help",
401 args: []string{"version", "--help", "--output=json"},
402 },
403 }
404
405 for _, tt := range tests {
406 t.Run(tt.name, func(t *testing.T) {
407 var stdout bytes.Buffer
408 var stderr bytes.Buffer
409 root := &cobra.Command{
410 Use: "dbxcli",
411 SilenceUsage: true,
412 SilenceErrors: true,
413 PersistentPreRunE: initDbx,
414 }
415 root.SetOut(&stdout)
416 root.SetErr(&stderr)
417 root.SetArgs(tt.args)
418 root.PersistentFlags().BoolP("verbose", "v", false, "")
419 root.PersistentFlags().String(outputFlag, "text", "")
420 root.PersistentFlags().String("as-member", "", "")
421 root.PersistentFlags().String("domain", "", "")
422 root.AddCommand(NewVersionCommand("test-version"))
423
424 if err := root.Execute(); err != nil {
425 t.Fatalf("Execute returned error: %v", err)
426 }
427 if got := stdout.String(); !strings.Contains(got, "Usage:") {
428 t.Fatalf("stdout = %q, want text help", got)
429 }
430 if strings.Contains(stdout.String(), `"ok"`) {
431 t.Fatalf("stdout = %q, want text help without JSON envelope", stdout.String())
432 }
433 if got := stderr.String(); got != "" {
434 t.Fatalf("stderr = %q, want empty", got)
435 }
436 })
437 }
438}
439
440func TestInitDbxStillRequiresAuthForDropboxCommands(t *testing.T) {
441 t.Setenv(envAccessToken, "")

Callers

nothing calls this directly

Calls 1

NewVersionCommandFunction · 0.85

Tested by

no test coverage detected