Skip to content

Commit e0516f1

Browse files
authored
chore: Test fixes and small tweaks (#75)
As the follow-up for recent merges. `make test` should be passing now.
1 parent eb0e4e0 commit e0516f1

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ build:
2020
mkdir -p bin/
2121
go build -i -v -o bin/diff -ldflags="$(LDFLAGS)"
2222

23+
.PHONY: test
24+
test:
25+
go test -v ./...
26+
2327
.PHONY: bootstrap
2428
bootstrap:
2529
ifndef HAS_GLIDE

diff/diff.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,47 @@ func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppre
2020
if oldContent.Content != newContent.Content {
2121
// modified
2222
fmt.Fprintf(to, ansi.Color("%s has changed:", "yellow")+"\n", key)
23-
diffs := generateDiff(oldContent, newContent)
23+
diffs := diffMappingResults(oldContent, newContent)
2424
if len(diffs) > 0 {
2525
seenAnyChanges = true
2626
}
27-
printDiff(suppressedKinds, oldContent.Kind, context, diffs, to)
27+
printDiffRecords(suppressedKinds, oldContent.Kind, context, diffs, to)
2828
}
2929
} else {
3030
// removed
3131
fmt.Fprintf(to, ansi.Color("%s has been removed:", "yellow")+"\n", key)
32-
diffs := generateDiff(oldContent, emptyMapping)
32+
diffs := diffMappingResults(oldContent, emptyMapping)
3333
if len(diffs) > 0 {
3434
seenAnyChanges = true
3535
}
36-
printDiff(suppressedKinds, oldContent.Kind, context, diffs, to)
36+
printDiffRecords(suppressedKinds, oldContent.Kind, context, diffs, to)
3737
}
3838
}
3939

4040
for key, newContent := range newIndex {
4141
if _, ok := oldIndex[key]; !ok {
4242
// added
4343
fmt.Fprintf(to, ansi.Color("%s has been added:", "yellow")+"\n", key)
44-
diffs := generateDiff(emptyMapping, newContent)
44+
diffs := diffMappingResults(emptyMapping, newContent)
4545
if len(diffs) > 0 {
4646
seenAnyChanges = true
4747
}
48-
printDiff(suppressedKinds, newContent.Kind, context, diffs, to)
48+
printDiffRecords(suppressedKinds, newContent.Kind, context, diffs, to)
4949
}
5050
}
5151
return seenAnyChanges
5252
}
5353

54-
func generateDiff(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
54+
func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
55+
return diffStrings(oldContent.Content, newContent.Content)
56+
}
57+
58+
func diffStrings(before, after string) []difflib.DiffRecord {
5559
const sep = "\n"
56-
return difflib.Diff(strings.Split(oldContent.Content, sep), strings.Split(newContent.Content, sep))
60+
return difflib.Diff(strings.Split(before, sep), strings.Split(after, sep))
5761
}
5862

59-
func printDiff(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {
63+
func printDiffRecords(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {
6064
for _, ckind := range suppressedKinds {
6165
if ckind == kind {
6266
str := fmt.Sprintf("+ Changes suppressed on sensitive content of type %s\n", kind)

diff/diff_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ func TestPrintDiffWithContext(t *testing.T) {
119119
func assertDiff(t *testing.T, before, after string, context int, expected string) {
120120
ansi.DisableColors(true)
121121
var output bytes.Buffer
122-
printDiff([]string{}, "some-resource", context, before, after, &output)
122+
diffs := diffStrings(before, after)
123+
printDiffRecords([]string{}, "some-resource", context, diffs, &output)
123124
actual := output.String()
124125
if actual != expected {
125126
t.Errorf("Unexpected diff output: \nExpected:\n#%v# \nActual:\n#%v#", expected, actual)
@@ -153,16 +154,32 @@ metadata:
153154
`,
154155
}}
155156

156-
var buf bytes.Buffer
157-
DiffManifests(specBeta, specRelease, []string{}, 10, &buf)
157+
t.Run("OnChange", func(t *testing.T) {
158158

159-
require.Equal(t, `default, nginx, Deployment (apps) has changed:
159+
var buf1 bytes.Buffer
160+
161+
if changesSeen := DiffManifests(specBeta, specRelease, []string{}, 10, &buf1); !changesSeen {
162+
t.Error("Unexpected return value from DiffManifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
163+
}
164+
165+
require.Equal(t, `default, nginx, Deployment (apps) has changed:
160166
161167
- apiVersion: apps/v1beta1
162168
+ apiVersion: apps/v1
163169
kind: Deployment
164170
metadata:
165171
name: nginx
166172
167-
`, buf.String())
173+
`, buf1.String())
174+
})
175+
176+
t.Run("OnNoChange", func(t *testing.T) {
177+
var buf2 bytes.Buffer
178+
179+
if changesSeen := DiffManifests(specRelease, specRelease, []string{}, 10, &buf2); changesSeen {
180+
t.Error("Unexpected return value from DiffManifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
181+
}
182+
183+
require.Equal(t, ``, buf2.String())
184+
})
168185
}

0 commit comments

Comments
 (0)