|
19 | 19 | package utils
|
20 | 20 |
|
21 | 21 | import (
|
| 22 | + "bytes" |
22 | 23 | "crypto/x509"
|
23 | 24 | "fmt"
|
24 | 25 | "io"
|
25 | 26 | "log/slog"
|
26 | 27 | "testing"
|
27 | 28 |
|
| 29 | + "github.com/alecthomas/kingpin/v2" |
28 | 30 | "github.com/gravitational/trace"
|
29 | 31 | "github.com/stretchr/testify/require"
|
30 | 32 | )
|
@@ -161,3 +163,82 @@ func TestAllowWhitespace(t *testing.T) {
|
161 | 163 | require.Equal(t, tt.out, AllowWhitespace(tt.in), fmt.Sprintf("test case %v", i))
|
162 | 164 | }
|
163 | 165 | }
|
| 166 | + |
| 167 | +func TestUpdateAppUsageTemplate(t *testing.T) { |
| 168 | + makeApp := func(usageWriter io.Writer) *kingpin.Application { |
| 169 | + app := InitCLIParser("TestUpdateAppUsageTemplate", "some help message") |
| 170 | + app.UsageWriter(usageWriter) |
| 171 | + app.Terminate(func(int) {}) |
| 172 | + |
| 173 | + app.Command("hello", "Hello.") |
| 174 | + |
| 175 | + create := app.Command("create", "Create.") |
| 176 | + create.Command("box", "Box.") |
| 177 | + create.Command("rocket", "Rocket.") |
| 178 | + return app |
| 179 | + } |
| 180 | + |
| 181 | + tests := []struct { |
| 182 | + name string |
| 183 | + inputArgs []string |
| 184 | + outputContains string |
| 185 | + }{ |
| 186 | + { |
| 187 | + name: "command width aligned for app help", |
| 188 | + inputArgs: []string{}, |
| 189 | + outputContains: ` |
| 190 | +Commands: |
| 191 | + help Show help. |
| 192 | + hello Hello. |
| 193 | + create box Box. |
| 194 | + create rocket Rocket. |
| 195 | +`, |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "command width aligned for command help", |
| 199 | + inputArgs: []string{"create"}, |
| 200 | + outputContains: ` |
| 201 | +Commands: |
| 202 | + create box Box. |
| 203 | + create rocket Rocket. |
| 204 | +`, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "command width aligned for unknown command error", |
| 208 | + inputArgs: []string{"unknown"}, |
| 209 | + outputContains: ` |
| 210 | +Commands: |
| 211 | + help Show help. |
| 212 | + hello Hello. |
| 213 | + create box Box. |
| 214 | + create rocket Rocket. |
| 215 | +`, |
| 216 | + }, |
| 217 | + } |
| 218 | + for _, tt := range tests { |
| 219 | + t.Run(tt.name, func(t *testing.T) { |
| 220 | + t.Run("help flag", func(t *testing.T) { |
| 221 | + var buffer bytes.Buffer |
| 222 | + app := makeApp(&buffer) |
| 223 | + args := append(tt.inputArgs, "--help") |
| 224 | + UpdateAppUsageTemplate(app, args) |
| 225 | + |
| 226 | + app.Usage(args) |
| 227 | + require.Contains(t, buffer.String(), tt.outputContains) |
| 228 | + }) |
| 229 | + |
| 230 | + t.Run("help command", func(t *testing.T) { |
| 231 | + var buffer bytes.Buffer |
| 232 | + app := makeApp(&buffer) |
| 233 | + args := append([]string{"help"}, tt.inputArgs...) |
| 234 | + UpdateAppUsageTemplate(app, args) |
| 235 | + |
| 236 | + // HelpCommand is triggered on PreAction during Parse. |
| 237 | + // See kingpin.Application.init for more details. |
| 238 | + _, err := app.Parse(args) |
| 239 | + require.NoError(t, err) |
| 240 | + require.Contains(t, buffer.String(), tt.outputContains) |
| 241 | + }) |
| 242 | + }) |
| 243 | + } |
| 244 | +} |
0 commit comments