Skip to content

Commit cdcb8a0

Browse files
Gertwoed (#425)
* wtf, this mostly works! * woa * sorta close? * uh.. maybe stop there? * maybe works? * move back to global * add maps * comment for the future * more comments --------- Co-authored-by: lekko-app[bot] <108442683+lekko-app[bot]@users.noreply.github.com>
1 parent 41f27d8 commit cdcb8a0

File tree

10 files changed

+1065
-285
lines changed

10 files changed

+1065
-285
lines changed

cmd/lekko/sync.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"google.golang.org/protobuf/types/descriptorpb"
4040
"google.golang.org/protobuf/types/dynamicpb"
4141
"google.golang.org/protobuf/types/known/anypb"
42+
"google.golang.org/protobuf/types/known/durationpb"
4243

4344
bffv1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/bff/v1beta1"
4445
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
@@ -505,6 +506,10 @@ func goToGo(ctx context.Context, f []byte) string {
505506
if err != nil {
506507
panic(err)
507508
}
509+
err = registry.AddFileDescriptor(durationpb.File_google_protobuf_duration_proto, false)
510+
if err != nil {
511+
panic(err)
512+
}
508513
syncer := sync.NewGoSyncerLite("", "", registry.Types)
509514
namespace, err := syncer.SourceToNamespace(ctx, f)
510515
if err != nil {

cmd/lekko/sync_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
//"bytes"
1919
"context"
2020
"os"
21+
"os/exec"
2122

2223
//"os/exec"
2324
"testing"
@@ -126,6 +127,90 @@ func Test_goToGo(t *testing.T) {
126127
t.Errorf("goToGo() = \n===\n%v+++, want \n===\n%v+++", got, string(f))
127128
}
128129
})
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+
})
129214
}
130215

131216
/*

0 commit comments

Comments
 (0)