@@ -2,7 +2,6 @@ package tests
22
33import (
44 "bufio"
5- "bytes"
65 "os"
76 "os/exec"
87 "path/filepath"
@@ -52,6 +51,71 @@ func readCommandConfigs() (map[string]string, error) {
5251 return goldenToCommand , nil
5352}
5453
54+ // matchWithPlaceholders checks if the output matches the expected text with placeholders
55+ func matchWithPlaceholders (actual , expected string ) bool {
56+ // First try exact match
57+ if actual == expected {
58+ return true
59+ }
60+
61+ // Handle login.golden placeholders
62+ if strings .Contains (expected , "<STATE_PLACEHOLDER>" ) {
63+ // Extract parts before and after the placeholder
64+ beforeState := expected [:strings .Index (expected , "<STATE_PLACEHOLDER>" )]
65+ afterState := expected [strings .Index (expected , "<STATE_PLACEHOLDER>" )+ len ("<STATE_PLACEHOLDER>" ):]
66+
67+ // Check if the output contains the parts before and after
68+ if ! strings .HasPrefix (actual , beforeState ) {
69+ return false
70+ }
71+
72+ // Handle the error placeholder if it exists
73+ if strings .Contains (afterState , "<ERROR_PLACEHOLDER>" ) {
74+ beforeError := afterState [:strings .Index (afterState , "<ERROR_PLACEHOLDER>" )]
75+ afterError := afterState [strings .Index (afterState , "<ERROR_PLACEHOLDER>" )+ len ("<ERROR_PLACEHOLDER>" ):]
76+
77+ // The middle part should be the UUID - find where the beforeError starts
78+ stateEnd := strings .Index (actual [len (beforeState ):], beforeError ) + len (beforeState )
79+ if stateEnd < len (beforeState ) {
80+ return false
81+ }
82+
83+ // Extract the UUID
84+ stateValue := actual [len (beforeState ):stateEnd ]
85+
86+ // Validate UUID format (simple check)
87+ if ! strings .Contains (stateValue , "-" ) || len (stateValue ) < 30 {
88+ return false
89+ }
90+
91+ // Remaining text after error message
92+ remainingText := actual [stateEnd + len (beforeError ):]
93+
94+ // Check if error message is valid (either "unexpected newline" or "EOF")
95+ if ! strings .HasPrefix (remainingText , "unexpected newline" ) &&
96+ ! strings .HasPrefix (remainingText , "EOF" ) {
97+ return false
98+ }
99+
100+ // Check the final part after the error message
101+ errorMsgEnd := 0
102+ if strings .HasPrefix (remainingText , "unexpected newline" ) {
103+ errorMsgEnd = len ("unexpected newline" )
104+ } else if strings .HasPrefix (remainingText , "EOF" ) {
105+ errorMsgEnd = len ("EOF" )
106+ }
107+
108+ return strings .HasPrefix (remainingText [errorMsgEnd :], afterError )
109+ } else {
110+ // Just check if the remaining text appears after the state
111+ stateEnd := strings .Index (actual [len (beforeState ):], afterState )
112+ return stateEnd >= 0
113+ }
114+ }
115+
116+ return false
117+ }
118+
55119func TestGoldenFiles (t * testing.T ) {
56120 // Read command configurations
57121 goldenToCommand , err := readCommandConfigs ()
@@ -106,15 +170,30 @@ func TestGoldenFiles(t *testing.T) {
106170 // because some golden files might be testing error cases
107171
108172 // Read expected output
109- expected , err := os .ReadFile (goldenFile )
173+ expectedBytes , err := os .ReadFile (goldenFile )
110174 if err != nil {
111175 t .Fatalf ("Failed to read golden file: %v" , err )
112176 }
113177
114- // Compare outputs
115- if ! bytes .Equal (output , expected ) {
116- t .Errorf ("Output does not match golden file.\n Command: %s\n Expected:\n %s\n \n Got:\n %s" ,
117- cmd , string (expected ), string (output ))
178+ expected := string (expectedBytes )
179+ actual := string (output )
180+
181+ // Normalize line endings for cross-platform compatibility
182+ expected = strings .ReplaceAll (expected , "\r \n " , "\n " )
183+ actual = strings .ReplaceAll (actual , "\r \n " , "\n " )
184+
185+ // Special case for login test which has placeholders
186+ if strings .Contains (expected , "<STATE_PLACEHOLDER>" ) {
187+ if ! matchWithPlaceholders (actual , expected ) {
188+ t .Errorf ("Output does not match golden file with placeholders.\n Command: %s\n Expected:\n %s\n \n Got:\n %s" ,
189+ cmd , expected , actual )
190+ }
191+ } else {
192+ // Standard equality check for other files
193+ if actual != expected {
194+ t .Errorf ("Output does not match golden file.\n Command: %s\n Expected:\n %s\n \n Got:\n %s" ,
195+ cmd , expected , actual )
196+ }
118197 }
119198 })
120199 }
0 commit comments