@@ -17,6 +17,7 @@ import (
1717 "github.com/elliotchance/orderedmap/v3"
1818 "github.com/stretchr/testify/assert"
1919 "github.com/stretchr/testify/require"
20+ "github.com/wI2L/jsondiff"
2021)
2122
2223func ReadFile (t testutil.TestingT , ctx context.Context , filename string ) string {
@@ -34,28 +35,62 @@ func WriteFile(t testutil.TestingT, ctx context.Context, filename, data string)
3435 require .NoError (t , err )
3536}
3637
37- func RequireOutput (t testutil.TestingT , ctx context.Context , args []string , expectedFilename string ) {
38- _ , filename , _ , _ := runtime .Caller (1 )
39- dir := filepath .Dir (filename )
40- expected := ReadFile (t , ctx , filepath .Join (dir , expectedFilename ))
41-
38+ func captureOutput (t testutil.TestingT , ctx context.Context , args []string ) string {
4239 t .Logf ("run args: [%s]" , strings .Join (args , ", " ))
4340 r := NewRunner (t , ctx , args ... )
4441 stdout , stderr , err := r .Run ()
4542 require .NoError (t , err )
4643 out := stderr .String () + stdout .String ()
44+ return ReplaceOutput (t , ctx , out )
45+ }
4746
48- out = ReplaceOutput (t , ctx , out )
47+ func assertEqualTexts (t testutil.TestingT , filename1 , filename2 , expected , out string ) {
48+ if len (out ) < 1000 && len (expected ) < 1000 {
49+ // This shows full strings + diff which could be useful when debugging newlines
50+ assert .Equal (t , expected , out )
51+ } else {
52+ // only show diff for large texts
53+ diff := testutil .Diff (filename1 , filename2 , expected , out )
54+ t .Errorf ("Diff:\n " + diff )
55+ }
56+ }
57+
58+ func RequireOutput (t testutil.TestingT , ctx context.Context , args []string , expectedFilename string ) {
59+ _ , filename , _ , _ := runtime .Caller (1 )
60+ dir := filepath .Dir (filename )
61+ expectedPath := filepath .Join (dir , expectedFilename )
62+ expected := ReadFile (t , ctx , expectedPath )
63+
64+ out := captureOutput (t , ctx , args )
4965
5066 if out != expected {
51- if len (out ) < 1000 && len (expected ) < 1000 {
52- // This shows full strings + diff which could be useful when debugging newlines
53- assert .Equal (t , expected , out )
54- } else {
55- // only show diff for large texts
67+ actual := fmt .Sprintf ("Output from %v" , args )
68+ assertEqualTexts (t , expectedFilename , actual , expected , out )
69+
70+ if os .Getenv ("TESTS_OUTPUT" ) == "OVERWRITE" {
71+ WriteFile (t , ctx , expectedPath , out )
72+ }
73+ }
74+ }
75+
76+ func RequireOutputJQ (t testutil.TestingT , ctx context.Context , args []string , expectedFilename string , ignorePaths []string ) {
77+ _ , filename , _ , _ := runtime .Caller (1 )
78+ dir := filepath .Dir (filename )
79+ expectedPath := filepath .Join (dir , expectedFilename )
80+ expected := ReadFile (t , ctx , expectedPath )
81+
82+ out := captureOutput (t , ctx , args )
83+
84+ if out != expected {
85+ patch , err := jsondiff .CompareJSON ([]byte (expected ), []byte (out ))
86+ if err != nil {
87+ t .Logf ("CompareJSON error for %s vs %s: %s (fallback to textual comparison)" , args , expectedFilename , err )
5688 actual := fmt .Sprintf ("Output from %v" , args )
57- diff := testutil .Diff (expectedFilename , actual , expected , out )
58- t .Errorf ("Diff:\n " + diff )
89+ assertEqualTexts (t , expectedFilename , actual , expected , out )
90+ } else {
91+ for _ , op := range patch {
92+ t .Errorf ("PATCH %s %s: %s" , args , expectedFilename , op )
93+ }
5994 }
6095
6196 if os .Getenv ("TESTS_OUTPUT" ) == "OVERWRITE" {
0 commit comments