|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestStripGlobalFlags(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + args []string |
| 12 | + wantArgs []string |
| 13 | + wantNonInteract bool |
| 14 | + wantConfigPath string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "no global flags", |
| 18 | + args: []string{"s3", "ls"}, |
| 19 | + wantArgs: []string{"s3", "ls"}, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "bare non-interactive is stripped", |
| 23 | + args: []string{"--non-interactive", "s3", "ls"}, |
| 24 | + wantArgs: []string{"s3", "ls"}, |
| 25 | + wantNonInteract: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "non-interactive among aws args is stripped", |
| 29 | + args: []string{"s3", "ls", "--non-interactive", "--recursive"}, |
| 30 | + wantArgs: []string{"s3", "ls", "--recursive"}, |
| 31 | + wantNonInteract: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "non-interactive with explicit true value", |
| 35 | + args: []string{"--non-interactive=true", "s3", "ls"}, |
| 36 | + wantArgs: []string{"s3", "ls"}, |
| 37 | + wantNonInteract: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "non-interactive with explicit false value", |
| 41 | + args: []string{"--non-interactive=false", "s3", "ls"}, |
| 42 | + wantArgs: []string{"s3", "ls"}, |
| 43 | + wantNonInteract: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "config with separate value", |
| 47 | + args: []string{"--config", "/tmp/c.toml", "s3", "ls"}, |
| 48 | + wantArgs: []string{"s3", "ls"}, |
| 49 | + wantConfigPath: "/tmp/c.toml", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "config with equals value", |
| 53 | + args: []string{"--config=/tmp/c.toml", "s3", "ls"}, |
| 54 | + wantArgs: []string{"s3", "ls"}, |
| 55 | + wantConfigPath: "/tmp/c.toml", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "config among aws args", |
| 59 | + args: []string{"s3", "ls", "--config", "/tmp/c.toml"}, |
| 60 | + wantArgs: []string{"s3", "ls"}, |
| 61 | + wantConfigPath: "/tmp/c.toml", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "both flags together", |
| 65 | + args: []string{"--non-interactive", "--config=/tmp/c.toml", "s3", "ls"}, |
| 66 | + wantArgs: []string{"s3", "ls"}, |
| 67 | + wantNonInteract: true, |
| 68 | + wantConfigPath: "/tmp/c.toml", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "trailing config without value is dropped", |
| 72 | + args: []string{"s3", "ls", "--config"}, |
| 73 | + wantArgs: []string{"s3", "ls"}, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "similarly named flags are left untouched", |
| 77 | + args: []string{"s3", "ls", "--non-interactive-mode", "--config-file", "x"}, |
| 78 | + wantArgs: []string{"s3", "ls", "--non-interactive-mode", "--config-file", "x"}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + gotArgs, gf := stripGlobalFlags(tt.args) |
| 85 | + if !reflect.DeepEqual(gotArgs, tt.wantArgs) { |
| 86 | + t.Errorf("args = %v, want %v", gotArgs, tt.wantArgs) |
| 87 | + } |
| 88 | + if gf.nonInteractive != tt.wantNonInteract { |
| 89 | + t.Errorf("nonInteractive = %v, want %v", gf.nonInteractive, tt.wantNonInteract) |
| 90 | + } |
| 91 | + if gf.configPath != tt.wantConfigPath { |
| 92 | + t.Errorf("configPath = %q, want %q", gf.configPath, tt.wantConfigPath) |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments