-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
265 lines (240 loc) · 6.88 KB
/
Copy pathcommand_test.go
File metadata and controls
265 lines (240 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package command_test
import (
"errors"
"slices"
"strings"
"testing"
gloo "github.com/gloo-foo/framework"
"github.com/gloo-foo/testable"
"github.com/spf13/afero"
command "github.com/gloo-foo/cmd-comm"
)
// comm produces three columns from two SORTED inputs:
// - column 1 (no tab): lines only in input1
// - column 2 (one tab): lines only in input2
// - column 3 (two tabs): lines in both
//
// The -1/-2/-3 flags suppress a column and collapse the leading tab of every
// later column. These tests assert the exact column/tab layout, not just counts.
func input2(ss ...string) command.CommInput {
in := make(command.CommInput, len(ss))
for i, s := range ss {
in[i] = []byte(s)
}
return in
}
func assertLines(t *testing.T, got, want []string) {
t.Helper()
if !slices.Equal(got, want) {
t.Fatalf("got %q, want %q", got, want)
}
}
func TestComm_ThreeColumnLayout(t *testing.T) {
// input1: apple banana cherry grape ; input2: banana cherry kiwi.
lines, err := testable.TestLines(
command.Comm(input2("banana", "cherry", "kiwi")),
"apple\nbanana\ncherry\ngrape\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{
"apple", // only in input1, column 1
"\t\tbanana", // in both, column 3
"\t\tcherry", // in both, column 3
"grape", // only in input1, column 1
"\tkiwi", // only in input2, column 2
})
}
func TestComm_SuppressColumn1(t *testing.T) {
// -1: column 1 gone; columns 2 and 3 lose their leading tab.
lines, err := testable.TestLines(
command.Comm(input2("banana", "cherry", "kiwi"), command.CommSuppressColumn1),
"apple\nbanana\ncherry\ngrape\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{
"\tbanana", // column 3 keeps one tab (column 2's slot)
"\tcherry",
"kiwi", // column 2 now at the left margin
})
}
func TestComm_SuppressColumn2(t *testing.T) {
// -2: column 2 gone; column 3 loses the second tab.
lines, err := testable.TestLines(
command.Comm(input2("banana", "cherry", "kiwi"), command.CommSuppressColumn2),
"apple\nbanana\ncherry\ngrape\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{
"apple",
"\tbanana", // column 3 keeps column 1's tab only
"\tcherry",
"grape",
})
}
func TestComm_SuppressColumn3(t *testing.T) {
// -3: common lines gone; only the unique columns remain.
lines, err := testable.TestLines(
command.Comm(input2("banana", "cherry", "kiwi"), command.CommSuppressColumn3),
"apple\nbanana\ncherry\ngrape\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{
"apple",
"grape",
"\tkiwi",
})
}
func TestComm_CommonOnly(t *testing.T) {
// -1 -2: only the common column, with no indentation at all.
lines, err := testable.TestLines(
command.Comm(input2("banana", "cherry", "kiwi"),
command.CommSuppressColumn1, command.CommSuppressColumn2),
"apple\nbanana\ncherry\ngrape\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"banana", "cherry"})
}
func TestComm_NoSuppressConstantsMatchDefault(t *testing.T) {
// The No* constants are the disabled forms and must behave like no flag.
lines, err := testable.TestLines(
command.Comm(input2("banana"),
command.CommNoSuppressColumn1,
command.CommNoSuppressColumn2,
command.CommNoSuppressColumn3),
"apple\nbanana\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"apple", "\t\tbanana"})
}
func TestComm_Input1TailDrains(t *testing.T) {
// input1 longer than input2: the leftover column-1 lines must all emit.
lines, err := testable.TestLines(
command.Comm(input2("a")),
"a\nb\nc\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"\t\ta", "b", "c"})
}
func TestComm_Input2TailDrains(t *testing.T) {
// input2 longer than input1: the leftover column-2 lines must all emit.
lines, err := testable.TestLines(
command.Comm(input2("a", "b", "c")),
"a\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"\t\ta", "\tb", "\tc"})
}
func TestComm_EmptyInputs(t *testing.T) {
lines, err := testable.TestLines(command.Comm(command.CommInput{}), "")
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{})
}
func TestComm_NoSecondInput(t *testing.T) {
// With neither a CommInput nor a second positional, input2 is empty: every
// input1 line is unique and lands in column 1.
lines, err := testable.TestLines(
command.Comm(strings.NewReader("apple\nbanana\n")),
"",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"apple", "banana"})
}
func TestComm_PositionalFilesViaMemFs(t *testing.T) {
// Both inputs as File positionals, read through an injected filesystem.
fs := afero.NewMemMapFs()
if err := afero.WriteFile(fs, "a.txt", []byte("apple\nbanana\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := afero.WriteFile(fs, "b.txt", []byte("banana\nkiwi\n"), 0o644); err != nil {
t.Fatal(err)
}
lines, err := testable.TestLines(
command.Comm(gloo.File("a.txt"), gloo.File("b.txt"), command.CommFs(fs)),
"ignored upstream\n",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"apple", "\t\tbanana", "\tkiwi"})
}
func TestComm_ReaderPositionals(t *testing.T) {
// Both inputs as io.Reader positionals.
lines, err := testable.TestLines(
command.Comm(strings.NewReader("apple\nbanana\n"), strings.NewReader("banana\nkiwi\n")),
"",
)
if err != nil {
t.Fatal(err)
}
assertLines(t, lines, []string{"apple", "\t\tbanana", "\tkiwi"})
}
func TestComm_FileNotFoundPropagates(t *testing.T) {
fs := afero.NewMemMapFs()
_, err := testable.TestLines(
command.Comm(gloo.File("missing.txt"), command.CommFs(fs)),
"",
)
if err == nil {
t.Fatal("expected an error opening a missing file, got nil")
}
}
func TestComm_Input2FileNotFoundPropagates(t *testing.T) {
fs := afero.NewMemMapFs()
if err := afero.WriteFile(fs, "a.txt", []byte("apple\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := testable.TestLines(
command.Comm(gloo.File("a.txt"), gloo.File("missing.txt"), command.CommFs(fs)),
"",
)
if err == nil {
t.Fatal("expected an error opening the missing second file, got nil")
}
}
func TestComm_ScannerErrorPropagates(t *testing.T) {
// A reader that fails mid-scan must surface its error, not be swallowed.
_, err := testable.TestLines(
command.Comm(errReader{}),
"",
)
if !errors.Is(err, errBoom) {
t.Fatalf("got %v, want %v", err, errBoom)
}
}
func TestComm_DefaultFsResolves(t *testing.T) {
// With no CommFs option, comm opens File positionals on the OS filesystem.
lines, err := testable.TestLines(
command.Comm(gloo.File("testdata/file1.txt"), gloo.File("testdata/file2.txt")),
"",
)
if err != nil {
t.Fatal(err)
}
if len(lines) == 0 {
t.Fatal("expected output from on-disk testdata files")
}
}
var errBoom = errors.New("boom")
// errReader is an io.Reader that always fails, exercising the scanner error path.
type errReader struct{}
func (errReader) Read([]byte) (int, error) { return 0, errBoom }