@@ -9,17 +9,19 @@ import (
99 "runtime"
1010 "slices"
1111 "strings"
12+ "testing"
1213
1314 "github.com/databricks/cli/internal/testutil"
15+ "github.com/databricks/cli/libs/golden"
1416 "github.com/databricks/cli/libs/iamutil"
1517 "github.com/databricks/databricks-sdk-go"
1618 "github.com/databricks/databricks-sdk-go/service/iam"
1719 "github.com/elliotchance/orderedmap/v3"
18- "github.com/stretchr/testify/assert"
1920 "github.com/stretchr/testify/require"
20- "github.com/wI2L/jsondiff"
2121)
2222
23+ var OverwriteMode = os .Getenv ("TESTS_OUTPUT" ) == "OVERWRITE"
24+
2325func ReadFile (t testutil.TestingT , ctx context.Context , filename string ) string {
2426 data , err := os .ReadFile (filename )
2527 if os .IsNotExist (err ) {
@@ -30,12 +32,6 @@ func ReadFile(t testutil.TestingT, ctx context.Context, filename string) string
3032 return NormalizeNewlines (string (data ))
3133}
3234
33- func WriteFile (t testutil.TestingT , ctx context.Context , filename , data string ) {
34- t .Logf ("Overwriting %s" , filename )
35- err := os .WriteFile (filename , []byte (data ), 0o644 )
36- require .NoError (t , err )
37- }
38-
3935func captureOutput (t testutil.TestingT , ctx context.Context , args []string ) string {
4036 t .Logf ("run args: [%s]" , strings .Join (args , ", " ))
4137 r := NewRunner (t , ctx , args ... )
@@ -45,20 +41,10 @@ func captureOutput(t testutil.TestingT, ctx context.Context, args []string) stri
4541 return ReplaceOutput (t , ctx , out )
4642}
4743
48- func assertEqualTexts (t testutil.TestingT , filename1 , filename2 , expected , out string ) {
49- if len (out ) < 1000 && len (expected ) < 1000 {
50- // This shows full strings + diff which could be useful when debugging newlines
51- assert .Equal (t , expected , out )
52- } else {
53- // only show diff for large texts
54- diff := testutil .Diff (filename1 , filename2 , expected , out )
55- t .Errorf ("Diff:\n " + diff )
56- }
57- }
58-
59- func logDiff (t testutil.TestingT , filename1 , filename2 , expected , out string ) {
60- diff := testutil .Diff (filename1 , filename2 , expected , out )
61- t .Logf ("Diff:\n " + diff )
44+ func WriteFile (t testutil.TestingT , filename , data string ) {
45+ t .Logf ("Overwriting %s" , filename )
46+ err := os .WriteFile (filename , []byte (data ), 0o644 )
47+ require .NoError (t , err )
6248}
6349
6450func RequireOutput (t testutil.TestingT , ctx context.Context , args []string , expectedFilename string ) {
@@ -71,10 +57,10 @@ func RequireOutput(t testutil.TestingT, ctx context.Context, args []string, expe
7157
7258 if out != expected {
7359 actual := fmt .Sprintf ("Output from %v" , args )
74- assertEqualTexts (t , expectedFilename , actual , expected , out )
60+ golden . AssertEqualTexts (t , expectedFilename , actual , expected , out )
7561
76- if os . Getenv ( "TESTS_OUTPUT" ) == "OVERWRITE" {
77- WriteFile (t , ctx , expectedPath , out )
62+ if OverwriteMode {
63+ WriteFile (t , expectedPath , out )
7864 }
7965 }
8066}
@@ -88,46 +74,13 @@ func RequireOutputJQ(t testutil.TestingT, ctx context.Context, args []string, ex
8874 out := captureOutput (t , ctx , args )
8975
9076 if out != expected {
91- patch , err := jsondiff .CompareJSON ([]byte (expected ), []byte (out ))
9277 actual := fmt .Sprintf ("Output from %v" , args )
93- if err != nil {
94- t .Logf ("CompareJSON error for %s vs %s: %s (fallback to textual comparison)" , args , expectedFilename , err )
95- assertEqualTexts (t , expectedFilename , actual , expected , out )
96- } else {
97- logDiff (t , expectedFilename , actual , expected , out )
98- ignoredDiffs := []string {}
99- erroredDiffs := []string {}
100- for _ , op := range patch {
101- if matchesPrefixes (ignorePaths , op .Path ) {
102- ignoredDiffs = append (ignoredDiffs , fmt .Sprintf ("%7s %s %v" , op .Type , op .Path , op .Value ))
103- } else {
104- erroredDiffs = append (erroredDiffs , fmt .Sprintf ("%7s %s %v" , op .Type , op .Path , op .Value ))
105- }
106- }
107- if len (ignoredDiffs ) > 0 {
108- t .Logf ("Ignored differences between %s and %s:\n ==> %s" , expectedFilename , args , strings .Join (ignoredDiffs , "\n ==> " ))
109- }
110- if len (erroredDiffs ) > 0 {
111- t .Errorf ("Unexpected differences between %s and %s:\n ==> %s" , expectedFilename , args , strings .Join (erroredDiffs , "\n ==> " ))
112- }
113- }
78+ golden .AssertEqualJSONs (t .(* testing.T ), expectedFilename , actual , expected , out , ignorePaths )
11479
115- if os .Getenv ("TESTS_OUTPUT" ) == "OVERWRITE" {
116- WriteFile (t , ctx , filepath .Join (dir , expectedFilename ), out )
117- }
118- }
119- }
120-
121- func matchesPrefixes (prefixes []string , path string ) bool {
122- for _ , p := range prefixes {
123- if p == path {
124- return true
125- }
126- if strings .HasPrefix (path , p + "/" ) {
127- return true
80+ if OverwriteMode {
81+ WriteFile (t , expectedPath , out )
12882 }
12983 }
130- return false
13184}
13285
13386var (
0 commit comments