From 6343e83d56f5c3e84f3a128661104635937d8092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Durgun?= Date: Tue, 26 Sep 2023 11:04:03 +0300 Subject: [PATCH] Change request directory and add test.PathToDir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oğuzhan Durgun --- go.mod | 5 ++- go.sum | 3 ++ internal/module/module_test.go | 30 ++++++++++++++++++ internal/test/buf.gen.yaml | 2 +- internal/test/test.go | 22 +++++++++++++ .../code_generator_request.pb.bin | Bin 236048 -> 236055 bytes 6 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 internal/module/module_test.go create mode 100644 internal/test/test.go rename internal/test/{ => testdata}/code_generator_request.pb.bin (99%) diff --git a/go.mod b/go.mod index 63a978d..48ac875 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,15 @@ go 1.21 require ( buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.31.0-20230824200731-b9b8148056b9.1 github.com/lyft/protoc-gen-star/v2 v2.0.3 + github.com/stretchr/testify v1.8.4 google.golang.org/protobuf v1.31.0 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/afero v1.9.5 // indirect - github.com/stretchr/testify v1.8.4 // indirect golang.org/x/text v0.10.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 37ca387..e3117c5 100644 --- a/go.sum +++ b/go.sum @@ -126,8 +126,10 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lyft/protoc-gen-star/v2 v2.0.3 h1:/3+/2sWyXeMLzKd1bX+ixWKgEMsULrIivpDsuaF441o= github.com/lyft/protoc-gen-star/v2 v2.0.3/go.mod h1:amey7yeodaJhXSbf/TlLvWiqQfLOSpEk//mLlc+axEk= @@ -438,6 +440,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/module/module_test.go b/internal/module/module_test.go new file mode 100644 index 0000000..28136bf --- /dev/null +++ b/internal/module/module_test.go @@ -0,0 +1,30 @@ +// Copyright 2021-2023 Zenauth Ltd. +// SPDX-License-Identifier: Apache-2.0 + +package module_test + +import ( + "bytes" + "os" + "testing" + + pgs "github.com/lyft/protoc-gen-star/v2" + "github.com/stretchr/testify/require" + + "github.com/cerbos/protoc-gen-jsonschema/internal/common" + "github.com/cerbos/protoc-gen-jsonschema/internal/module" + "github.com/cerbos/protoc-gen-jsonschema/internal/test" +) + +const requestName = "code_generator_request.pb.bin" + +func TestModule(t *testing.T) { + reqFile, err := os.Open(test.PathToDir(t, requestName)) + require.NoError(t, err) + resBytes := &bytes.Buffer{} + pgs.Init( + pgs.DebugEnv(common.DebugEnv), + pgs.ProtocInput(reqFile), + pgs.ProtocOutput(resBytes), + ).RegisterModule(module.New()).Render() +} diff --git a/internal/test/buf.gen.yaml b/internal/test/buf.gen.yaml index 53765a1..3e002d3 100644 --- a/internal/test/buf.gen.yaml +++ b/internal/test/buf.gen.yaml @@ -3,5 +3,5 @@ version: v1 plugins: - name: debug out: . - opt: . + opt: testdata path: ["go", "run", "github.com/lyft/protoc-gen-star/protoc-gen-debug@latest"] diff --git a/internal/test/test.go b/internal/test/test.go new file mode 100644 index 0000000..81fe6f4 --- /dev/null +++ b/internal/test/test.go @@ -0,0 +1,22 @@ +// Copyright 2021-2023 Zenauth Ltd. +// SPDX-License-Identifier: Apache-2.0 + +package test + +import ( + "path/filepath" + "runtime" + "testing" +) + +func PathToDir(tb testing.TB, dir string) string { + tb.Helper() + + _, currFile, _, ok := runtime.Caller(0) + if !ok { + tb.Error("Failed to detect testdata directory") + return "" + } + + return filepath.Join(filepath.Dir(currFile), "testdata", dir) +} diff --git a/internal/test/code_generator_request.pb.bin b/internal/test/testdata/code_generator_request.pb.bin similarity index 99% rename from internal/test/code_generator_request.pb.bin rename to internal/test/testdata/code_generator_request.pb.bin index 3853a6dd2dff8e164561765c9291aa01e0c1b09f..d32f2789a6030025af9ea0dbefa6872dd8f23a19 100644 GIT binary patch delta 85 zcmbQRg>U*6z6mm%93`p6B`Jv|i5ulsmM27$uKeMZB$)hBEZFBWME`0#Niqd;N$7+8M67V$rVO%A$}iMKlc!i_<$f+ bH_r%H7l^Xv6K3rv%ou@~Y5NH?X8&XWhTRvk