|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package command |
| 16 | + |
| 17 | +// Usage text rules: |
| 18 | +// - <arg> = required argument |
| 19 | +// - [arg] = optional argument |
| 20 | +// - <arg...> = one or more required arguments |
| 21 | +// - Flags are included in the usage line if they are required |
| 22 | +// - Use --all for all libraries |
| 23 | +// |
| 24 | +// Note: We do not test the 'migrate' tool here because it is not built with |
| 25 | +// the urfave/cli library and does not follow these usage patterns. |
| 26 | + |
| 27 | +import ( |
| 28 | + "bytes" |
| 29 | + "os/exec" |
| 30 | + "strings" |
| 31 | + "testing" |
| 32 | + |
| 33 | + "github.com/google/go-cmp/cmp" |
| 34 | +) |
| 35 | + |
| 36 | +func TestLibrarianUsage(t *testing.T) { |
| 37 | + const bin = "github.com/googleapis/librarian/cmd/librarian" |
| 38 | + for _, test := range []struct { |
| 39 | + desc string |
| 40 | + args []string |
| 41 | + want string |
| 42 | + }{ |
| 43 | + {"root", nil, "librarian [command]"}, |
| 44 | + {"add", []string{"add"}, "librarian add <apis...>"}, |
| 45 | + {"generate", []string{"generate"}, "librarian generate <library>"}, |
| 46 | + {"bump", []string{"bump"}, "librarian bump <library>"}, |
| 47 | + {"tidy", []string{"tidy"}, "librarian tidy"}, |
| 48 | + {"update", []string{"update"}, "librarian update <sources...>"}, |
| 49 | + {"version", []string{"version"}, "librarian version"}, |
| 50 | + {"publish", []string{"publish"}, "librarian publish"}, |
| 51 | + {"tag", []string{"tag"}, "librarian tag"}, |
| 52 | + } { |
| 53 | + t.Run(test.desc, func(t *testing.T) { |
| 54 | + got := runUsage(t, bin, test.args) |
| 55 | + if diff := cmp.Diff(test.want, got); diff != "" { |
| 56 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestLibrarianopsUsage(t *testing.T) { |
| 63 | + const bin = "github.com/googleapis/librarian/cmd/librarianops" |
| 64 | + for _, test := range []struct { |
| 65 | + desc string |
| 66 | + args []string |
| 67 | + want string |
| 68 | + }{ |
| 69 | + {"root", nil, "librarianops [command]"}, |
| 70 | + {"generate", []string{"generate"}, "librarianops generate [<repo> | -C <dir>]"}, |
| 71 | + } { |
| 72 | + t.Run(test.desc, func(t *testing.T) { |
| 73 | + got := runUsage(t, bin, test.args) |
| 74 | + if diff := cmp.Diff(test.want, got); diff != "" { |
| 75 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestSurferUsage(t *testing.T) { |
| 82 | + const bin = "github.com/googleapis/librarian/cmd/surfer" |
| 83 | + for _, test := range []struct { |
| 84 | + desc string |
| 85 | + args []string |
| 86 | + want string |
| 87 | + }{ |
| 88 | + {"root", nil, "surfer [command]"}, |
| 89 | + {"generate", []string{"generate"}, "surfer generate <path to gcloud.yaml> --googleapis <path>"}, |
| 90 | + } { |
| 91 | + t.Run(test.desc, func(t *testing.T) { |
| 92 | + got := runUsage(t, bin, test.args) |
| 93 | + if diff := cmp.Diff(test.want, got); diff != "" { |
| 94 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestToolUsage(t *testing.T) { |
| 101 | + for _, test := range []struct { |
| 102 | + desc string |
| 103 | + bin string |
| 104 | + args []string |
| 105 | + want string |
| 106 | + }{ |
| 107 | + {"import-configs root", "github.com/googleapis/librarian/tool/cmd/importconfigs", nil, "import-configs [command]"}, |
| 108 | + {"import-configs update-transports", "github.com/googleapis/librarian/tool/cmd/importconfigs", []string{"update-transports"}, "import-configs update-transports --googleapis <path>"}, |
| 109 | + {"import-metadata", "github.com/googleapis/librarian/tool/cmd/importmetadata", nil, "import-metadata --python-repo <path> --librarian-repo <path>"}, |
| 110 | + } { |
| 111 | + t.Run(test.desc, func(t *testing.T) { |
| 112 | + got := runUsage(t, test.bin, test.args) |
| 113 | + if diff := cmp.Diff(test.want, got); diff != "" { |
| 114 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// runUsage executes the binary with the given args and the appropriate help flag, |
| 121 | +// returning the captured usage string. |
| 122 | +func runUsage(t *testing.T, bin string, args []string) string { |
| 123 | + t.Helper() |
| 124 | + var stdout bytes.Buffer |
| 125 | + fullArgs := append([]string{"run", bin}, args...) |
| 126 | + fullArgs = append(fullArgs, "--help") |
| 127 | + cmd := exec.Command("go", fullArgs...) |
| 128 | + cmd.Stdout = &stdout |
| 129 | + cmd.Stderr = &stdout // Some help might go to stderr |
| 130 | + if err := cmd.Run(); err != nil { |
| 131 | + t.Fatalf("go %v failed: %v\nOutput: %s", fullArgs, err, stdout.String()) |
| 132 | + } |
| 133 | + return captureUsage(t, stdout.String()) |
| 134 | +} |
| 135 | + |
| 136 | +// captureUsage extracts the usage line from the command output. It looks for |
| 137 | +// a line containing "USAGE:" and returns the following line. |
| 138 | +func captureUsage(t *testing.T, output string) string { |
| 139 | + t.Helper() |
| 140 | + lines := strings.Split(output, "\n") |
| 141 | + for i, line := range lines { |
| 142 | + if strings.Contains(strings.ToUpper(line), "USAGE:") { |
| 143 | + if i+1 < len(lines) { |
| 144 | + return strings.TrimSpace(lines[i+1]) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return "" |
| 149 | +} |
0 commit comments