Skip to content

Commit 3d05325

Browse files
committed
Ignoring paths using glob patterns
1 parent ad539a3 commit 3d05325

14 files changed

+557
-151
lines changed

config.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type config struct {
1212
diff.Output
13+
ignore patterns
1314
lhsFile string
1415
rhsFile string
1516
}
@@ -19,6 +20,7 @@ func readConfig() config {
1920

2021
flag.StringVar(&c.Output.Indent, "indent", " ", "indent string")
2122
flag.BoolVar(&c.Output.ShowTypes, "show-types", false, "show types")
23+
flag.Var(&c.ignore, "ignore", "paths to ignore (glob)")
2224
flag.Parse()
2325

2426
if len(flag.Args()) < 2 {

diff/diff.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
TypesDiffer Type = iota
1111
ContentDiffer
1212
Identical
13+
Invalid
1314
)
1415

1516
type Differ interface {

diff/diff_test.go

+96-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package diff
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
67
)
@@ -28,14 +29,15 @@ func TestDiff(t *testing.T) {
2829
{LHS: []int{1, 2, 3}, RHS: []int{4, 5}, Want: ContentDiffer},
2930
{LHS: []int{1, 2, 3}, RHS: []float64{4, 5}, Want: TypesDiffer},
3031
{LHS: []int{1, 2, 3}, RHS: []float64{4, 5}, Want: TypesDiffer},
31-
{LHS: []func(){func() {}}, RHS: []func(){func() {}}, Want: TypesDiffer, Error: true},
32+
{LHS: []func(){func() {}}, RHS: []func(){func() {}}, Want: ContentDiffer, Error: true},
3233
{LHS: map[int]int{2: 4, 6: 12}, RHS: map[int]int{2: 4, 6: 12}, Want: Identical},
3334
{LHS: map[int]int{2: 4, 6: 12, 8: 16}, RHS: map[int]int{2: 4, 6: 12}, Want: ContentDiffer},
3435
{LHS: map[int]int{2: 4, 6: 12}, RHS: map[int]int{2: 4, 6: 12, 1: 2}, Want: ContentDiffer},
3536
{LHS: map[int]int{2: 4, 6: 12}, RHS: map[float64]int{2: 4, 6: 12}, Want: TypesDiffer},
3637
{
3738
LHS: map[int]func(){0: func() {}},
3839
RHS: map[int]func(){0: func() {}},
40+
Want: ContentDiffer,
3941
Error: true,
4042
},
4143
{LHS: map[int]int{2: 4, 6: 12}, RHS: map[int]int{1: 2, 3: 6}, Want: ContentDiffer},
@@ -53,7 +55,7 @@ func TestDiff(t *testing.T) {
5355
{LHS: []interface{}{1, 2, 3}, RHS: []interface{}{1, 2, 3}, Want: Identical},
5456
{LHS: []interface{}{1, 2, 3}, RHS: []interface{}{1, 2, 3.3}, Want: ContentDiffer},
5557
{LHS: []interface{}(nil), RHS: []interface{}{1, 2, 3.3}, Want: ContentDiffer},
56-
{LHS: []int(nil), RHS: []int{}, Want: ContentDiffer},
58+
{LHS: []int(nil), RHS: []int{}, Want: Identical},
5759
{LHS: func() {}, RHS: func() {}, Want: TypesDiffer, Error: true},
5860
} {
5961
diff, err := Diff(test.LHS, test.RHS)
@@ -68,7 +70,7 @@ func TestDiff(t *testing.T) {
6870
if diff.Diff() != test.Want {
6971
t.Logf("LHS: %+#v\n", test.LHS)
7072
t.Logf("LHS: %+#v\n", test.RHS)
71-
t.Errorf("Diff(%v, %v) = %q, expected %q", test.LHS, test.RHS, diff.Diff(), test.Want)
73+
t.Errorf("Diff(%#v, %#v) = %q, expected %q", test.LHS, test.RHS, diff.Diff(), test.Want)
7274
}
7375
}
7476
}
@@ -124,7 +126,7 @@ func TestTypes(t *testing.T) {
124126

125127
ss := typ.Strings()
126128
indented := typ.StringIndent(testKey, testPrefix, testOutput)
127-
testStrings(t, test, ss, indented)
129+
testStrings("TestTypes", t, test, ss, indented)
128130
}
129131
}
130132

@@ -165,7 +167,7 @@ func TestScalar(t *testing.T) {
165167

166168
ss := typ.Strings()
167169
indented := typ.StringIndent(testKey, testPrefix, testOutput)
168-
testStrings(t, test, ss, indented)
170+
testStrings("TestScalar", t, test, ss, indented)
169171
}
170172
}
171173

@@ -233,11 +235,16 @@ func TestSlice(t *testing.T) {
233235

234236
ss := typ.Strings()
235237
indented := typ.StringIndent(testKey, testPrefix, testOutput)
236-
testStrings(t, test, ss, indented)
238+
testStrings("TestSlice", t, test, ss, indented)
237239
}
238240

239-
invalid := &Slice{
240-
Type: Type(-1),
241+
invalid, err := NewSlice(nil, nil)
242+
if invalidErr, ok := err.(InvalidType); ok {
243+
if !strings.Contains(invalidErr.Error(), "nil") {
244+
t.Errorf("NewSlice(nil, nil): unexpected format for InvalidType error: got %s", err)
245+
}
246+
} else {
247+
t.Errorf("NewSlice(nil, nil): expected InvalidType error, got %s", err)
241248
}
242249
ss := invalid.Strings()
243250
if len(ss) != 0 {
@@ -248,10 +255,28 @@ func TestSlice(t *testing.T) {
248255
if indented != "" {
249256
t.Errorf("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q", testKey, testPrefix, testOutput, indented, "")
250257
}
258+
259+
invalid, err = NewSlice([]int{}, nil)
260+
if invalidErr, ok := err.(InvalidType); ok {
261+
if !strings.Contains(invalidErr.Error(), "nil") {
262+
t.Errorf("NewSlice([]int{}, nil): unexpected format for InvalidType error: got %s", err)
263+
}
264+
} else {
265+
t.Errorf("NewSlice([]int{}, nil): expected InvalidType error, got %s", err)
266+
}
267+
ss = invalid.Strings()
268+
if len(ss) != 0 {
269+
t.Errorf("len(invalidSlice.Strings()) = %d, expected 0", len(ss))
270+
}
271+
272+
indented = invalid.StringIndent(testKey, testPrefix, testOutput)
273+
if indented != "" {
274+
t.Errorf("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q", testKey, testPrefix, testOutput, indented, "")
275+
}
251276
}
252277

253278
func TestMap(t *testing.T) {
254-
for _, test := range []stringTest{
279+
for i, test := range []stringTest{
255280
{
256281
LHS: map[int]int{1: 2, 3: 4},
257282
RHS: map[int]int{1: 2, 3: 4},
@@ -280,6 +305,18 @@ func TestMap(t *testing.T) {
280305
},
281306
Type: ContentDiffer,
282307
},
308+
{
309+
LHS: map[int]int{1: 2, 2: 3},
310+
RHS: map[int]int{1: 3, 2: 3},
311+
Want: [][]string{
312+
[]string{},
313+
[]string{"-", "int", "1", "2"},
314+
[]string{"+", "int", "1", "3"},
315+
[]string{"int", "2", "3"},
316+
[]string{},
317+
},
318+
Type: ContentDiffer,
319+
},
283320
{
284321
LHS: map[int]int{1: 2},
285322
RHS: map[int]int{},
@@ -301,23 +338,28 @@ func TestMap(t *testing.T) {
301338
Type: ContentDiffer,
302339
},
303340
} {
304-
typ, err := NewMap(test.LHS, test.RHS)
341+
m, err := NewMap(test.LHS, test.RHS)
305342

306343
if err != nil {
307344
t.Errorf("NewMap(%+v, %+v): unexpected error: %q", test.LHS, test.RHS, err)
308345
continue
309346
}
310-
if typ.Diff() != test.Type {
311-
t.Errorf("Types.Diff() = %q, expected %q", typ.Diff(), test.Type)
347+
if m.Diff() != test.Type {
348+
t.Errorf("Types.Diff() = %q, expected %q", m.Diff(), test.Type)
312349
}
313350

314-
ss := typ.Strings()
315-
indented := typ.StringIndent(testKey, testPrefix, testOutput)
316-
testStrings(t, test, ss, indented)
351+
ss := m.Strings()
352+
indented := m.StringIndent(testKey, testPrefix, testOutput)
353+
testStrings(fmt.Sprintf("TestMap[%d]", i), t, test, ss, indented)
317354
}
318355

319-
invalid := &Map{
320-
Type: Type(-1),
356+
invalid, err := NewMap(nil, nil)
357+
if invalidErr, ok := err.(InvalidType); ok {
358+
if !strings.Contains(invalidErr.Error(), "nil") {
359+
t.Errorf("NewMap(nil, nil): unexpected format for InvalidType error: got %s", err)
360+
}
361+
} else {
362+
t.Errorf("NewMap(nil, nil): expected InvalidType error, got %s", err)
321363
}
322364
ss := invalid.Strings()
323365
if len(ss) != 0 {
@@ -328,20 +370,52 @@ func TestMap(t *testing.T) {
328370
if indented != "" {
329371
t.Errorf("invalidMap.StringIndent(%q, %q, %+v) = %q, expected %q", testKey, testPrefix, testOutput, indented, "")
330372
}
373+
374+
invalid, err = NewMap(map[int]int{}, nil)
375+
if invalidErr, ok := err.(InvalidType); ok {
376+
if !strings.Contains(invalidErr.Error(), "nil") {
377+
t.Errorf("NewMap(map[int]int{}, nil): unexpected format for InvalidType error: got %s", err)
378+
}
379+
} else {
380+
t.Errorf("NewMap(map[int]int{}, nil): expected InvalidType error, got %s", err)
381+
}
382+
ss = invalid.Strings()
383+
if len(ss) != 0 {
384+
t.Errorf("len(invalidMap.Strings()) = %d, expected 0", len(ss))
385+
}
386+
387+
indented = invalid.StringIndent(testKey, testPrefix, testOutput)
388+
if indented != "" {
389+
t.Errorf("invalidMap.StringIndent(%q, %q, %+v) = %q, expected %q", testKey, testPrefix, testOutput, indented, "")
390+
}
391+
}
392+
393+
func TestIgnore(t *testing.T) {
394+
ignore := Ignore{}
395+
396+
if ignore.Diff() != Identical {
397+
t.Errorf("Ignore{}.Diff() = %q, expected %q", ignore.Diff(), Identical)
398+
}
399+
if len(ignore.Strings()) != 0 {
400+
t.Errorf("len(Ignore{}.Strings()) = %d, expected 0", len(ignore.Strings()))
401+
}
402+
if indented := ignore.StringIndent(testKey, testPrefix, testOutput); indented != "" {
403+
t.Errorf("Ignore{}.StringIndent(...) = %q, expected %q", indented, "")
404+
}
331405
}
332406

333-
func testStrings(t *testing.T, test stringTest, ss []string, indented string) {
407+
func testStrings(context string, t *testing.T, test stringTest, ss []string, indented string) {
334408
for i, want := range test.Want {
335409
s := ss[i]
336410

337-
for _, needle := range want {
411+
for i, needle := range want {
338412
if !strings.Contains(s, needle) {
339-
t.Errorf("typ.Strings() = %#v, expected it to contain %q", ss, needle)
413+
t.Errorf("%s: typ.Strings()[%d] = %q, expected it to contain %q", context, i, ss[i], needle)
340414
}
341415
if !strings.Contains(indented, needle) {
342416
t.Errorf(
343-
"typ.StringIndent(%q, %q, %+v) = %q, expected it to contain %q",
344-
testKey, testPrefix, testOutput, indented, needle,
417+
"%s: typ.StringIndent(%q, %q, %+v) = %q, expected it to contain %q",
418+
context, testKey, testPrefix, testOutput, indented, needle,
345419
)
346420
}
347421
}

diff/errors.go

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package diff
22

33
import (
4+
"fmt"
45
"reflect"
56
)
67

@@ -12,3 +13,12 @@ type Unsupported struct {
1213
func (e Unsupported) Error() string {
1314
return "unsupported types: " + e.LHS.String() + ", " + e.RHS.String()
1415
}
16+
17+
type InvalidType struct {
18+
Value interface{}
19+
For string
20+
}
21+
22+
func (e InvalidType) Error() string {
23+
return fmt.Sprintf("%T is not a valid type for %s", e.Value, e.For)
24+
}

diff/ignore.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package diff
2+
3+
type Ignore struct{}
4+
5+
func (t Ignore) Diff() Type {
6+
return Identical
7+
}
8+
9+
func (t Ignore) Strings() []string {
10+
return []string{}
11+
}
12+
13+
func (t Ignore) StringIndent(key, prefix string, conf Output) string {
14+
return ""
15+
}

0 commit comments

Comments
 (0)