replace custom diff in tests using testify (#1215)
This commit is contained in:
		
							parent
							
								
									473820cd6f
								
							
						
					
					
						commit
						eb21377f39
					
				
							
								
								
									
										1
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										1
									
								
								go.mod
								
								
								
								
							|  | @ -7,7 +7,6 @@ toolchain go1.21.0 | |||
| require ( | ||||
| 	github.com/Masterminds/semver/v3 v3.2.1 | ||||
| 	github.com/Masterminds/sprig/v3 v3.2.3 | ||||
| 	github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a | ||||
| 	github.com/davecgh/go-spew v1.1.1 | ||||
| 	github.com/go-test/deep v1.1.0 | ||||
| 	github.com/goccy/go-yaml v1.11.2 | ||||
|  |  | |||
							
								
								
									
										2
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										2
									
								
								go.sum
								
								
								
								
							|  | @ -272,8 +272,6 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd | |||
| github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA= | ||||
| github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= | ||||
| github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= | ||||
| github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a h1:pv34s756C4pEXnjgPfGYgdhg/ZdajGhyOvzx8k+23nw= | ||||
| github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A= | ||||
| github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= | ||||
| github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= | ||||
| github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= | ||||
|  |  | |||
|  | @ -6,11 +6,11 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestApply_hooks(t *testing.T) { | ||||
|  | @ -133,10 +133,7 @@ func TestApply_hooks(t *testing.T) { | |||
| 		if tc.log != "" { | ||||
| 			actual := bs.String() | ||||
| 
 | ||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 			if exists { | ||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 			} | ||||
| 			assert.Equal(t, tc.log, actual) | ||||
| 		} else { | ||||
| 			assertLogEqualsToSnapshot(t, bs.String()) | ||||
| 		} | ||||
|  |  | |||
|  | @ -6,12 +6,12 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestApply_3(t *testing.T) { | ||||
|  | @ -134,10 +134,7 @@ func TestApply_3(t *testing.T) { | |||
| 		if tc.log != "" { | ||||
| 			actual := bs.String() | ||||
| 
 | ||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 			if exists { | ||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 			} | ||||
| 			assert.Equal(t, tc.log, actual) | ||||
| 		} else { | ||||
| 			assertLogEqualsToSnapshot(t, bs.String()) | ||||
| 		} | ||||
|  |  | |||
|  | @ -6,12 +6,12 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestApply_2(t *testing.T) { | ||||
|  | @ -134,10 +134,7 @@ func TestApply_2(t *testing.T) { | |||
| 		if tc.log != "" { | ||||
| 			actual := bs.String() | ||||
| 
 | ||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 			if exists { | ||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 			} | ||||
| 			assert.Equal(t, tc.log, actual) | ||||
| 		} else { | ||||
| 			assertLogEqualsToSnapshot(t, bs.String()) | ||||
| 		} | ||||
|  |  | |||
|  | @ -9,13 +9,13 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"github.com/stretchr/testify/require" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestLint(t *testing.T) { | ||||
|  | @ -167,10 +167,7 @@ releases: | |||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		diff, exists := testhelper.Diff(wantLog, gotLog, 3) | ||||
| 		if exists { | ||||
| 			t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 		} | ||||
| 		assert.Equal(t, wantLog, gotLog) | ||||
| 	} | ||||
| 
 | ||||
| 	t.Run("fail on unselected need by default", func(t *testing.T) { | ||||
|  |  | |||
|  | @ -6,12 +6,12 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestSync(t *testing.T) { | ||||
|  | @ -132,10 +132,7 @@ func TestSync(t *testing.T) { | |||
| 		if tc.log != "" { | ||||
| 			actual := bs.String() | ||||
| 
 | ||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 			if exists { | ||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 			} | ||||
| 			assert.Equal(t, tc.log, actual) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -9,6 +9,7 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"github.com/stretchr/testify/require" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
|  | @ -16,7 +17,6 @@ import ( | |||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/runtime" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestTemplate(t *testing.T) { | ||||
|  | @ -168,10 +168,7 @@ releases: | |||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		diff, exists := testhelper.Diff(wantLog, gotLog, 3) | ||||
| 		if exists { | ||||
| 			t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 		} | ||||
| 		assert.Equal(t, wantLog, gotLog) | ||||
| 	} | ||||
| 
 | ||||
| 	t.Run("fail on unselected need by default", func(t *testing.T) { | ||||
|  |  | |||
|  | @ -3911,10 +3911,7 @@ changing working directory back to "/path/to" | |||
| 			if tc.log != "" { | ||||
| 				actual := bs.String() | ||||
| 
 | ||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 				if exists { | ||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) | ||||
| 				} | ||||
| 				assert.Equal(t, tc.log, actual) | ||||
| 			} else { | ||||
| 				assertLogEqualsToSnapshot(t, bs.String()) | ||||
| 			} | ||||
|  | @ -4031,10 +4028,7 @@ changing working directory back to "/path/to" | |||
| 			if tc.log != "" { | ||||
| 				actual := bs.String() | ||||
| 
 | ||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 				if exists { | ||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) | ||||
| 				} | ||||
| 				assert.Equal(t, tc.log, actual) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
|  |  | |||
|  | @ -5,12 +5,12 @@ import ( | |||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| func TestDestroy_2(t *testing.T) { | ||||
|  | @ -126,10 +126,7 @@ func TestDestroy_2(t *testing.T) { | |||
| 		if tc.log != "" { | ||||
| 			actual := bs.String() | ||||
| 
 | ||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 			if exists { | ||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 			} | ||||
| 			assert.Equal(t, tc.log, actual) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -6,12 +6,12 @@ import ( | |||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
|  | @ -195,10 +195,7 @@ func TestDestroy(t *testing.T) { | |||
| 		if tc.log != "" { | ||||
| 			actual := bs.String() | ||||
| 
 | ||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 			if exists { | ||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) | ||||
| 			} | ||||
| 			assert.Equal(t, tc.log, actual) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -6,6 +6,7 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
|  | @ -1049,10 +1050,7 @@ changing working directory back to "/path/to" | |||
| 			if tc.log != "" { | ||||
| 				actual := bs.String() | ||||
| 
 | ||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 				if exists { | ||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) | ||||
| 				} | ||||
| 				assert.Equal(t, tc.log, actual) | ||||
| 			} else { | ||||
| 				testhelper.RequireLog(t, "app_diff_test_2", bs) | ||||
| 			} | ||||
|  |  | |||
|  | @ -6,6 +6,7 @@ import ( | |||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 	"github.com/helmfile/vals" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"go.uber.org/zap" | ||||
| 
 | ||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | ||||
|  | @ -1399,10 +1400,7 @@ changing working directory back to "/path/to" | |||
| 			if tc.log != "" { | ||||
| 				actual := bs.String() | ||||
| 
 | ||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | ||||
| 				if exists { | ||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) | ||||
| 				} | ||||
| 				assert.Equal(t, tc.log, actual, 3) | ||||
| 			} else { | ||||
| 				testhelper.RequireLog(t, "app_diff_test_1", bs) | ||||
| 			} | ||||
|  |  | |||
|  | @ -1,103 +0,0 @@ | |||
| package testhelper | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"math" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"github.com/aryann/difflib" | ||||
| ) | ||||
| 
 | ||||
| func Diff(want, got string, context int) (string, bool) { | ||||
| 	records := difflib.Diff( | ||||
| 		strings.Split(want, "\n"), | ||||
| 		strings.Split(got, "\n"), | ||||
| 	) | ||||
| 
 | ||||
| 	w := &bytes.Buffer{} | ||||
| 
 | ||||
| 	changed := checkAndPrintRecords(w, records, context) | ||||
| 
 | ||||
| 	return w.String(), changed | ||||
| } | ||||
| 
 | ||||
| func checkAndPrintRecords(w io.Writer, records []difflib.DiffRecord, context int) bool { | ||||
| 	var changed bool | ||||
| 	if context >= 0 { | ||||
| 		distances := calculateDistances(records) | ||||
| 		omitting := false | ||||
| 		for i, diff := range records { | ||||
| 			if diff.Delta != difflib.Common { | ||||
| 				changed = true | ||||
| 			} | ||||
| 			if distances[i] > context { | ||||
| 				if !omitting { | ||||
| 					fmt.Fprintln(w, "...") | ||||
| 					omitting = true | ||||
| 				} | ||||
| 			} else { | ||||
| 				omitting = false | ||||
| 				fmt.Fprintln(w, formatRecord(diff)) | ||||
| 			} | ||||
| 		} | ||||
| 	} else { | ||||
| 		for _, diff := range records { | ||||
| 			if diff.Delta != difflib.Common { | ||||
| 				changed = true | ||||
| 			} | ||||
| 			fmt.Fprintln(w, formatRecord(diff)) | ||||
| 		} | ||||
| 	} | ||||
| 	return changed | ||||
| } | ||||
| 
 | ||||
| func formatRecord(diff difflib.DiffRecord) string { | ||||
| 	var prefix string | ||||
| 	switch diff.Delta { | ||||
| 	case difflib.RightOnly: | ||||
| 		prefix = "+ " | ||||
| 	case difflib.LeftOnly: | ||||
| 		prefix = "- " | ||||
| 	case difflib.Common: | ||||
| 		prefix = "  " | ||||
| 	} | ||||
| 
 | ||||
| 	return prefix + diff.Payload | ||||
| } | ||||
| 
 | ||||
| // Shamelessly and thankfully copied from https://github.com/databus23/helm-diff/blob/99b8474af7726ca6f57b37b0b8b8f3cd36c991e8/diff/diff.go#L116
 | ||||
| func calculateDistances(diffs []difflib.DiffRecord) map[int]int { | ||||
| 	distances := map[int]int{} | ||||
| 
 | ||||
| 	// Iterate forwards through diffs, set 'distance' based on closest 'change' before this line
 | ||||
| 	change := -1 | ||||
| 	for i, diff := range diffs { | ||||
| 		if diff.Delta != difflib.Common { | ||||
| 			change = i | ||||
| 		} | ||||
| 		distance := math.MaxInt32 | ||||
| 		if change != -1 { | ||||
| 			distance = i - change | ||||
| 		} | ||||
| 		distances[i] = distance | ||||
| 	} | ||||
| 
 | ||||
| 	// Iterate backwards through diffs, reduce 'distance' based on closest 'change' after this line
 | ||||
| 	change = -1 | ||||
| 	for i := len(diffs) - 1; i >= 0; i-- { | ||||
| 		diff := diffs[i] | ||||
| 		if diff.Delta != difflib.Common { | ||||
| 			change = i | ||||
| 		} | ||||
| 		if change != -1 { | ||||
| 			distance := change - i | ||||
| 			if distance < distances[i] { | ||||
| 				distances[i] = distance | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return distances | ||||
| } | ||||
|  | @ -7,6 +7,8 @@ import ( | |||
| 	"runtime" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
| 
 | ||||
| func RequireLog(t *testing.T, dir string, bs *bytes.Buffer) { | ||||
|  | @ -56,13 +58,12 @@ func RequireLog(t *testing.T, dir string, bs *bytes.Buffer) { | |||
| 	wantLog := string(wantLogData) | ||||
| 	gotLog := bs.String() | ||||
| 
 | ||||
| 	diff, exists := Diff(wantLog, gotLog, 3) | ||||
| 	if exists { | ||||
| 		t.Errorf("unexpected %s: want (-), got (+): %s", testBaseName, diff) | ||||
| 		t.Errorf( | ||||
| 	assert.Equalf( | ||||
| 		t, | ||||
| 		wantLog, | ||||
| 		gotLog, | ||||
| 		"If you think this is due to the snapshot file being outdated, rerun this test with `HELMFILE_UPDATE_SNAPSHOT=1 go test -v -run %s %s` to update the snapshot", | ||||
| 		t.Name(), | ||||
| 		callerPkg, | ||||
| 	) | ||||
| 	} | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue