Skip to content

chore: Test fixes and small tweaks #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ build:
mkdir -p bin/
go build -i -v -o bin/diff -ldflags="$(LDFLAGS)"

.PHONY: test
test:
go test -v ./...

.PHONY: bootstrap
bootstrap:
ifndef HAS_GLIDE
Expand Down
22 changes: 13 additions & 9 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,47 @@ func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppre
if oldContent.Content != newContent.Content {
// modified
fmt.Fprintf(to, ansi.Color("%s has changed:", "yellow")+"\n", key)
diffs := generateDiff(oldContent, newContent)
diffs := diffMappingResults(oldContent, newContent)
if len(diffs) > 0 {
seenAnyChanges = true
}
printDiff(suppressedKinds, oldContent.Kind, context, diffs, to)
printDiffRecords(suppressedKinds, oldContent.Kind, context, diffs, to)
}
} else {
// removed
fmt.Fprintf(to, ansi.Color("%s has been removed:", "yellow")+"\n", key)
diffs := generateDiff(oldContent, emptyMapping)
diffs := diffMappingResults(oldContent, emptyMapping)
if len(diffs) > 0 {
seenAnyChanges = true
}
printDiff(suppressedKinds, oldContent.Kind, context, diffs, to)
printDiffRecords(suppressedKinds, oldContent.Kind, context, diffs, to)
}
}

for key, newContent := range newIndex {
if _, ok := oldIndex[key]; !ok {
// added
fmt.Fprintf(to, ansi.Color("%s has been added:", "yellow")+"\n", key)
diffs := generateDiff(emptyMapping, newContent)
diffs := diffMappingResults(emptyMapping, newContent)
if len(diffs) > 0 {
seenAnyChanges = true
}
printDiff(suppressedKinds, newContent.Kind, context, diffs, to)
printDiffRecords(suppressedKinds, newContent.Kind, context, diffs, to)
}
}
return seenAnyChanges
}

func generateDiff(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
return diffStrings(oldContent.Content, newContent.Content)
}

func diffStrings(before, after string) []difflib.DiffRecord {
const sep = "\n"
return difflib.Diff(strings.Split(oldContent.Content, sep), strings.Split(newContent.Content, sep))
return difflib.Diff(strings.Split(before, sep), strings.Split(after, sep))
}

func printDiff(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {
func printDiffRecords(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {
for _, ckind := range suppressedKinds {
if ckind == kind {
str := fmt.Sprintf("+ Changes suppressed on sensitive content of type %s\n", kind)
Expand Down
27 changes: 22 additions & 5 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func TestPrintDiffWithContext(t *testing.T) {
func assertDiff(t *testing.T, before, after string, context int, expected string) {
ansi.DisableColors(true)
var output bytes.Buffer
printDiff([]string{}, "some-resource", context, before, after, &output)
diffs := diffStrings(before, after)
printDiffRecords([]string{}, "some-resource", context, diffs, &output)
actual := output.String()
if actual != expected {
t.Errorf("Unexpected diff output: \nExpected:\n#%v# \nActual:\n#%v#", expected, actual)
Expand Down Expand Up @@ -153,16 +154,32 @@ metadata:
`,
}}

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

require.Equal(t, `default, nginx, Deployment (apps) has changed:
var buf1 bytes.Buffer

if changesSeen := DiffManifests(specBeta, specRelease, []string{}, 10, &buf1); !changesSeen {
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`")
}

require.Equal(t, `default, nginx, Deployment (apps) has changed:

- apiVersion: apps/v1beta1
+ apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx

`, buf.String())
`, buf1.String())
})

t.Run("OnNoChange", func(t *testing.T) {
var buf2 bytes.Buffer

if changesSeen := DiffManifests(specRelease, specRelease, []string{}, 10, &buf2); changesSeen {
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`")
}

require.Equal(t, ``, buf2.String())
})
}