|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/dropbox/dbxcli/internal/output" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func TestStructuredOutputCommandAudit(t *testing.T) { |
| 16 | + got := structuredOutputCommandPaths(RootCmd) |
| 17 | + got = append(got, NewVersionCommand("test").Name()) |
| 18 | + sort.Strings(got) |
| 19 | + |
| 20 | + want := []string{ |
| 21 | + "account", |
| 22 | + "cp", |
| 23 | + "du", |
| 24 | + "get", |
| 25 | + "ls", |
| 26 | + "mkdir", |
| 27 | + "mv", |
| 28 | + "put", |
| 29 | + "restore", |
| 30 | + "revs", |
| 31 | + "rm", |
| 32 | + "search", |
| 33 | + "share list folder", |
| 34 | + "share list link", |
| 35 | + "share-link create", |
| 36 | + "share-link download", |
| 37 | + "share-link info", |
| 38 | + "share-link list", |
| 39 | + "share-link revoke", |
| 40 | + "share-link update", |
| 41 | + "team add-member", |
| 42 | + "team info", |
| 43 | + "team list-groups", |
| 44 | + "team list-members", |
| 45 | + "team remove-member", |
| 46 | + "version", |
| 47 | + } |
| 48 | + sort.Strings(want) |
| 49 | + |
| 50 | + if len(got) != len(want) { |
| 51 | + t.Fatalf("structured commands = %v, want %v", got, want) |
| 52 | + } |
| 53 | + for i := range want { |
| 54 | + if got[i] != want[i] { |
| 55 | + t.Fatalf("structured commands = %v, want %v", got, want) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func structuredOutputCommandPaths(root *cobra.Command) []string { |
| 61 | + var paths []string |
| 62 | + var walk func(*cobra.Command, []string) |
| 63 | + walk = func(cmd *cobra.Command, parents []string) { |
| 64 | + parts := parents |
| 65 | + if cmd != root { |
| 66 | + parts = append(append([]string{}, parents...), cmd.Name()) |
| 67 | + if commandSupportsStructuredOutput(cmd) { |
| 68 | + paths = append(paths, strings.Join(parts, " ")) |
| 69 | + } |
| 70 | + } |
| 71 | + for _, child := range cmd.Commands() { |
| 72 | + walk(child, parts) |
| 73 | + } |
| 74 | + } |
| 75 | + walk(root, nil) |
| 76 | + return paths |
| 77 | +} |
| 78 | + |
| 79 | +func TestJSONOperationOutputContractShape(t *testing.T) { |
| 80 | + encoded, err := json.Marshal(newJSONOperationOutput(nil, nil, nil)) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("marshal operation output: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + var raw map[string]json.RawMessage |
| 86 | + if err := json.Unmarshal(encoded, &raw); err != nil { |
| 87 | + t.Fatalf("decode operation output: %v", err) |
| 88 | + } |
| 89 | + for _, key := range []string{"input", "results", "warnings"} { |
| 90 | + if _, ok := raw[key]; !ok { |
| 91 | + t.Fatalf("operation output = %s, missing %q", encoded, key) |
| 92 | + } |
| 93 | + } |
| 94 | + if len(raw) != 3 { |
| 95 | + t.Fatalf("operation output = %s, want only input/results/warnings", encoded) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestUnsupportedCommandsReturnJSONErrorEnvelope(t *testing.T) { |
| 100 | + for _, name := range []string{"login", "logout", "completion"} { |
| 101 | + t.Run(name, func(t *testing.T) { |
| 102 | + var stdout, stderr bytes.Buffer |
| 103 | + cmd := &cobra.Command{Use: name} |
| 104 | + cmd.SetOut(&stdout) |
| 105 | + cmd.SetErr(&stderr) |
| 106 | + cmd.Flags().String(outputFlag, "json", "") |
| 107 | + |
| 108 | + err := validateOutputFormat(cmd) |
| 109 | + if !errors.Is(err, output.ErrStructuredOutputUnsupported) { |
| 110 | + t.Fatalf("validateOutputFormat error = %v, want structured output unsupported", err) |
| 111 | + } |
| 112 | + |
| 113 | + renderCommandError(cmd, err) |
| 114 | + if stderr.Len() != 0 { |
| 115 | + t.Fatalf("stderr = %q, want empty", stderr.String()) |
| 116 | + } |
| 117 | + |
| 118 | + got := decodeJSONErrorResponse(t, stdout.String()) |
| 119 | + if got.OK { |
| 120 | + t.Fatalf("ok = true, want false") |
| 121 | + } |
| 122 | + if got.Error.Code != jsonErrorCodeStructuredOutputUnsupported { |
| 123 | + t.Fatalf("code = %q, want %q", got.Error.Code, jsonErrorCodeStructuredOutputUnsupported) |
| 124 | + } |
| 125 | + if got.Warnings == nil || len(got.Warnings) != 0 { |
| 126 | + t.Fatalf("warnings = %+v, want empty array", got.Warnings) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments