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 ( | require ( | ||||||
| 	github.com/Masterminds/semver/v3 v3.2.1 | 	github.com/Masterminds/semver/v3 v3.2.1 | ||||||
| 	github.com/Masterminds/sprig/v3 v3.2.3 | 	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/davecgh/go-spew v1.1.1 | ||||||
| 	github.com/go-test/deep v1.1.0 | 	github.com/go-test/deep v1.1.0 | ||||||
| 	github.com/goccy/go-yaml v1.11.2 | 	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 h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA= | ||||||
| github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= | 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/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 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= | ||||||
| github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= | 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= | 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/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestApply_hooks(t *testing.T) { | func TestApply_hooks(t *testing.T) { | ||||||
|  | @ -133,10 +133,7 @@ func TestApply_hooks(t *testing.T) { | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | 			assert.Equal(t, tc.log, actual) | ||||||
| 			if exists { |  | ||||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 			} |  | ||||||
| 		} else { | 		} else { | ||||||
| 			assertLogEqualsToSnapshot(t, bs.String()) | 			assertLogEqualsToSnapshot(t, bs.String()) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -6,12 +6,12 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestApply_3(t *testing.T) { | func TestApply_3(t *testing.T) { | ||||||
|  | @ -134,10 +134,7 @@ func TestApply_3(t *testing.T) { | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | 			assert.Equal(t, tc.log, actual) | ||||||
| 			if exists { |  | ||||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 			} |  | ||||||
| 		} else { | 		} else { | ||||||
| 			assertLogEqualsToSnapshot(t, bs.String()) | 			assertLogEqualsToSnapshot(t, bs.String()) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -6,12 +6,12 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestApply_2(t *testing.T) { | func TestApply_2(t *testing.T) { | ||||||
|  | @ -134,10 +134,7 @@ func TestApply_2(t *testing.T) { | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | 			assert.Equal(t, tc.log, actual) | ||||||
| 			if exists { |  | ||||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 			} |  | ||||||
| 		} else { | 		} else { | ||||||
| 			assertLogEqualsToSnapshot(t, bs.String()) | 			assertLogEqualsToSnapshot(t, bs.String()) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -9,13 +9,13 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"github.com/stretchr/testify/require" | 	"github.com/stretchr/testify/require" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestLint(t *testing.T) { | func TestLint(t *testing.T) { | ||||||
|  | @ -167,10 +167,7 @@ releases: | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		diff, exists := testhelper.Diff(wantLog, gotLog, 3) | 		assert.Equal(t, wantLog, gotLog) | ||||||
| 		if exists { |  | ||||||
| 			t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	t.Run("fail on unselected need by default", func(t *testing.T) { | 	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/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestSync(t *testing.T) { | func TestSync(t *testing.T) { | ||||||
|  | @ -132,10 +132,7 @@ func TestSync(t *testing.T) { | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | 			assert.Equal(t, tc.log, actual) | ||||||
| 			if exists { |  | ||||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -9,6 +9,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"github.com/stretchr/testify/require" | 	"github.com/stretchr/testify/require" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
|  | @ -16,7 +17,6 @@ import ( | ||||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/runtime" | 	"github.com/helmfile/helmfile/pkg/runtime" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestTemplate(t *testing.T) { | func TestTemplate(t *testing.T) { | ||||||
|  | @ -168,10 +168,7 @@ releases: | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		diff, exists := testhelper.Diff(wantLog, gotLog, 3) | 		assert.Equal(t, wantLog, gotLog) | ||||||
| 		if exists { |  | ||||||
| 			t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	t.Run("fail on unselected need by default", func(t *testing.T) { | 	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 != "" { | 			if tc.log != "" { | ||||||
| 				actual := bs.String() | 				actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | 				assert.Equal(t, tc.log, actual) | ||||||
| 				if exists { |  | ||||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) |  | ||||||
| 				} |  | ||||||
| 			} else { | 			} else { | ||||||
| 				assertLogEqualsToSnapshot(t, bs.String()) | 				assertLogEqualsToSnapshot(t, bs.String()) | ||||||
| 			} | 			} | ||||||
|  | @ -4031,10 +4028,7 @@ changing working directory back to "/path/to" | ||||||
| 			if tc.log != "" { | 			if tc.log != "" { | ||||||
| 				actual := bs.String() | 				actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | 				assert.Equal(t, tc.log, actual) | ||||||
| 				if exists { |  | ||||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) |  | ||||||
| 				} |  | ||||||
| 			} | 			} | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -5,12 +5,12 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestDestroy_2(t *testing.T) { | func TestDestroy_2(t *testing.T) { | ||||||
|  | @ -126,10 +126,7 @@ func TestDestroy_2(t *testing.T) { | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | 			assert.Equal(t, tc.log, actual) | ||||||
| 			if exists { |  | ||||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,12 +6,12 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | 	ffs "github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| 	"github.com/helmfile/helmfile/pkg/testhelper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
|  | @ -195,10 +195,7 @@ func TestDestroy(t *testing.T) { | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 			diff, exists := testhelper.Diff(tc.log, actual, 3) | 			assert.Equal(t, tc.log, actual) | ||||||
| 			if exists { |  | ||||||
| 				t.Errorf("unexpected log:\nDIFF\n%s\nEOD", diff) |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
|  | @ -1049,10 +1050,7 @@ changing working directory back to "/path/to" | ||||||
| 			if tc.log != "" { | 			if tc.log != "" { | ||||||
| 				actual := bs.String() | 				actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | 				assert.Equal(t, tc.log, actual) | ||||||
| 				if exists { |  | ||||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) |  | ||||||
| 				} |  | ||||||
| 			} else { | 			} else { | ||||||
| 				testhelper.RequireLog(t, "app_diff_test_2", bs) | 				testhelper.RequireLog(t, "app_diff_test_2", bs) | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/helmfile/vals" | 	"github.com/helmfile/vals" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
|  | @ -1399,10 +1400,7 @@ changing working directory back to "/path/to" | ||||||
| 			if tc.log != "" { | 			if tc.log != "" { | ||||||
| 				actual := bs.String() | 				actual := bs.String() | ||||||
| 
 | 
 | ||||||
| 				diff, exists := testhelper.Diff(tc.log, actual, 3) | 				assert.Equal(t, tc.log, actual, 3) | ||||||
| 				if exists { |  | ||||||
| 					t.Errorf("unexpected log for data defined %s:\nDIFF\n%s\nEOD", tc.loc, diff) |  | ||||||
| 				} |  | ||||||
| 			} else { | 			} else { | ||||||
| 				testhelper.RequireLog(t, "app_diff_test_1", bs) | 				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" | 	"runtime" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func RequireLog(t *testing.T, dir string, bs *bytes.Buffer) { | 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) | 	wantLog := string(wantLogData) | ||||||
| 	gotLog := bs.String() | 	gotLog := bs.String() | ||||||
| 
 | 
 | ||||||
| 	diff, exists := Diff(wantLog, gotLog, 3) | 	assert.Equalf( | ||||||
| 	if exists { | 		t, | ||||||
| 		t.Errorf("unexpected %s: want (-), got (+): %s", testBaseName, diff) | 		wantLog, | ||||||
| 		t.Errorf( | 		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", | 		"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(), | 		t.Name(), | ||||||
| 		callerPkg, | 		callerPkg, | ||||||
| 	) | 	) | ||||||
| } | } | ||||||
| } |  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue