@@ -18,6 +18,7 @@ import (
18
18
//"bytes"
19
19
"context"
20
20
"os"
21
+ "os/exec"
21
22
22
23
//"os/exec"
23
24
"testing"
@@ -126,6 +127,90 @@ func Test_goToGo(t *testing.T) {
126
127
t .Errorf ("goToGo() = \n ===\n %v+++, want \n ===\n %v+++" , got , string (f ))
127
128
}
128
129
})
130
+
131
+ t .Run ("gertrude" , func (t * testing.T ) {
132
+ ctx := context .Background ()
133
+ f , err := os .ReadFile ("./testdata/gertrude.go" )
134
+ if err != nil {
135
+ panic (err )
136
+ }
137
+ if got := goToGo (ctx , f ); got != string (f ) {
138
+ t .Errorf ("goToGo() = \n ===\n %v+++, want \n ===\n %v+++" , got , string (f ))
139
+ }
140
+ })
141
+ }
142
+
143
+ func DiffStyleOutput (a , b string ) (string , error ) {
144
+ // Create temporary files to hold the input strings
145
+ fileA , err := os .CreateTemp ("" , "fileA" )
146
+ if err != nil {
147
+ return "" , err
148
+ }
149
+ defer os .Remove (fileA .Name ())
150
+
151
+ fileB , err := os .CreateTemp ("" , "fileB" )
152
+ if err != nil {
153
+ return "" , err
154
+ }
155
+ defer os .Remove (fileB .Name ())
156
+
157
+ // Write strings to temporary files
158
+ if _ , err := fileA .WriteString (a ); err != nil {
159
+ return "" , err
160
+ }
161
+ if _ , err := fileB .WriteString (b ); err != nil {
162
+ return "" , err
163
+ }
164
+
165
+ // Call the diff command
166
+ cmd := exec .Command ("diff" , "-u" , fileA .Name (), fileB .Name ()) //#nosec G204
167
+ output , err := cmd .CombinedOutput ()
168
+ if err != nil {
169
+ // diff command returns non-zero exit code when files differ, ignore the error
170
+ if exitErr , ok := err .(* exec.ExitError ); ok {
171
+ if exitErr .ExitCode () != 1 {
172
+ return "" , err
173
+ }
174
+ } else {
175
+ return "" , err
176
+ }
177
+ }
178
+
179
+ return string (output ), nil
180
+ }
181
+
182
+ func TestDefault (t * testing.T ) {
183
+ t .Run ("default" , func (t * testing.T ) {
184
+ ctx := context .Background ()
185
+ f , err := os .ReadFile ("./testdata/default.go" )
186
+ if err != nil {
187
+ panic (err )
188
+ }
189
+ if got := goToGo (ctx , f ); got != string (f ) {
190
+ diff , err := DiffStyleOutput (string (f ), got )
191
+ if err != nil {
192
+ panic (err )
193
+ }
194
+ t .Errorf ("Difference Found: %s\n " , diff )
195
+ }
196
+ })
197
+ }
198
+
199
+ func TestDuration (t * testing.T ) {
200
+ t .Run ("default" , func (t * testing.T ) {
201
+ ctx := context .Background ()
202
+ f , err := os .ReadFile ("./testdata/duration.go" )
203
+ if err != nil {
204
+ panic (err )
205
+ }
206
+ if got := goToGo (ctx , f ); got != string (f ) {
207
+ diff , err := DiffStyleOutput (string (f ), got )
208
+ if err != nil {
209
+ panic (err )
210
+ }
211
+ t .Errorf ("Difference Found: %s\n " , diff )
212
+ }
213
+ })
129
214
}
130
215
131
216
/*
0 commit comments