|
| 1 | +package cmdutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIsStdin(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + path string |
| 15 | + expected bool |
| 16 | + }{ |
| 17 | + {name: "dash is stdin", path: "-", expected: true}, |
| 18 | + {name: "empty is not stdin", path: "", expected: false}, |
| 19 | + {name: "file path is not stdin", path: "spec.yaml", expected: false}, |
| 20 | + {name: "dash prefix is not stdin", path: "-file", expected: false}, |
| 21 | + {name: "double dash is not stdin", path: "--", expected: false}, |
| 22 | + } |
| 23 | + |
| 24 | + for _, tt := range tests { |
| 25 | + t.Run(tt.name, func(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + assert.Equal(t, tt.expected, IsStdin(tt.path)) |
| 28 | + }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestInputFileFromArgs(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + args []string |
| 38 | + expected string |
| 39 | + }{ |
| 40 | + {name: "no args returns stdin indicator", args: []string{}, expected: StdinIndicator}, |
| 41 | + {name: "nil args returns stdin indicator", args: nil, expected: StdinIndicator}, |
| 42 | + {name: "single file arg", args: []string{"spec.yaml"}, expected: "spec.yaml"}, |
| 43 | + {name: "explicit dash", args: []string{"-"}, expected: "-"}, |
| 44 | + {name: "multiple args returns first", args: []string{"spec.yaml", "out.yaml"}, expected: "spec.yaml"}, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + assert.Equal(t, tt.expected, InputFileFromArgs(tt.args)) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestArgAt(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + args []string |
| 61 | + index int |
| 62 | + defaultVal string |
| 63 | + expected string |
| 64 | + }{ |
| 65 | + {name: "returns value at index", args: []string{"a", "b", "c"}, index: 1, defaultVal: "", expected: "b"}, |
| 66 | + {name: "returns first element", args: []string{"a"}, index: 0, defaultVal: "x", expected: "a"}, |
| 67 | + {name: "returns default when out of range", args: []string{"a"}, index: 1, defaultVal: "default", expected: "default"}, |
| 68 | + {name: "returns default for empty args", args: []string{}, index: 0, defaultVal: "default", expected: "default"}, |
| 69 | + {name: "returns default for nil args", args: nil, index: 0, defaultVal: "default", expected: "default"}, |
| 70 | + {name: "returns empty default", args: []string{}, index: 0, defaultVal: "", expected: ""}, |
| 71 | + {name: "returns default for negative index", args: []string{"a", "b"}, index: -1, defaultVal: "default", expected: "default"}, |
| 72 | + {name: "returns default for large negative index", args: []string{"a"}, index: -100, defaultVal: "fallback", expected: "fallback"}, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + assert.Equal(t, tt.expected, ArgAt(tt.args, tt.index, tt.defaultVal)) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestStdinOrFileArgs(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + validator := StdinOrFileArgs(1, 2) |
| 87 | + |
| 88 | + t.Run("accepts one arg", func(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + err := validator(nil, []string{"spec.yaml"}) |
| 91 | + assert.NoError(t, err) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("accepts two args", func(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + err := validator(nil, []string{"spec.yaml", "out.yaml"}) |
| 97 | + assert.NoError(t, err) |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("rejects three args with max 2", func(t *testing.T) { |
| 101 | + t.Parallel() |
| 102 | + err := validator(nil, []string{"a", "b", "c"}) |
| 103 | + assert.Error(t, err) |
| 104 | + assert.Contains(t, err.Error(), "accepts at most 2 arg(s)") |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("unbounded max accepts many args", func(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + unbounded := StdinOrFileArgs(1, -1) |
| 110 | + err := unbounded(nil, []string{"a", "b", "c", "d", "e"}) |
| 111 | + assert.NoError(t, err, "negative maxArgs should allow unlimited args") |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("zero minArgs accepts any arg count", func(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + noMin := StdinOrFileArgs(0, 2) |
| 117 | + err := noMin(nil, []string{}) |
| 118 | + assert.NoError(t, err, "zero minArgs should accept empty args") |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("error message includes min arg count", func(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + min3 := StdinOrFileArgs(3, 5) |
| 124 | + err := min3(nil, []string{"a", "b"}) |
| 125 | + if err != nil { |
| 126 | + assert.Contains(t, err.Error(), "requires at least 3 arg(s)") |
| 127 | + } |
| 128 | + }) |
| 129 | +} |
0 commit comments