From 80bb3d96d214290fc15b700fc675d717b39467cd Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 5 Sep 2024 22:38:49 -0700 Subject: [PATCH] fix: add tests (#16) Adding some tests to ensure: - The generated output matches what is currently expected. - Output is consistent with the AEP linter (this is disabled until https://github.com/aep-dev/api-linter/pull/86 is merged). --- .github/workflows/test.yml | 25 +++++++++++++++++++++++++ parser/parser.go | 16 ++++++++++++++++ schema/resourcedefinition.proto | 2 +- scripts/run-api-linter.sh | 4 +++- scripts/verify-goldens.sh | 9 +++++++++ writer/proto/resource.go | 6 +++--- 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100755 scripts/verify-goldens.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a0cb8fd --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,25 @@ +name: Tests + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.22.3 + + - name: Check out code + uses: actions/checkout@v2 + + - name: Verify no change to generated files. + run: | + go install github.com/bufbuild/buf/cmd/buf@v1.38.0 + ./scripts/verify-goldens.sh + + # - name: Run script + # run: | + # ./scripts/run-api-linter.sh diff --git a/parser/parser.go b/parser/parser.go index 88f415c..d5e7592 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -30,6 +30,11 @@ type ParsedResource struct { Parents []*ParsedResource } +type ParsedProperty struct { + *schema.Property + Name string +} + func NewParsedService(s *schema.Service) (*ParsedService, error) { resourceByType, err := loadResourceByType(s) if err != nil { @@ -80,6 +85,17 @@ func addGetToResource(pr *ParsedResource) { } } +func (r *ParsedResource) GetFieldsSortedByNumber() []*ParsedProperty { + fields := []*ParsedProperty{} + for name, p := range r.Properties { + fields = append(fields, &ParsedProperty{ + Property: p, + Name: name, + }) + } + return fields +} + // add an id field to the resource. // TODO(yft): this has to be reconciled with the // existence of path. diff --git a/schema/resourcedefinition.proto b/schema/resourcedefinition.proto index 9aad5fa..042d364 100644 --- a/schema/resourcedefinition.proto +++ b/schema/resourcedefinition.proto @@ -51,7 +51,7 @@ enum Type { message Property { Type type = 1; - // field number used for protobuf or other systems where fields must + // field number used for protobuf or other systems where fields must // be explicitly enumerated. int32 number = 2; bool readOnly = 3; diff --git a/scripts/run-api-linter.sh b/scripts/run-api-linter.sh index 0e7c9bb..2741cf1 100755 --- a/scripts/run-api-linter.sh +++ b/scripts/run-api-linter.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -e if ! which api-linter ; then go install github.com/aep-dev/api-linter/cmd/api-linter@latest @@ -10,4 +11,5 @@ fi api-linter \ ./example/bookstore/v1/bookstore.proto \ - -I /tmp/googleapis \ No newline at end of file + -I /tmp/googleapis \ + --set-exit-status \ No newline at end of file diff --git a/scripts/verify-goldens.sh b/scripts/verify-goldens.sh new file mode 100755 index 0000000..0c4a974 --- /dev/null +++ b/scripts/verify-goldens.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -e +./scripts/regenerate-all.sh +if git diff --exit-code; then + echo "No differences found." +else + echo "Differences found."; + exit 1; +fi \ No newline at end of file diff --git a/writer/proto/resource.go b/writer/proto/resource.go index 31c351f..546969d 100644 --- a/writer/proto/resource.go +++ b/writer/proto/resource.go @@ -91,15 +91,15 @@ func AddResource(r *parser.ParsedResource, fb *builder.FileBuilder, sb *builder. // GenerateResourceMesssage adds the resource message. func GeneratedResourceMessage(r *parser.ParsedResource) (*builder.MessageBuilder, error) { mb := builder.NewMessage(r.Kind) - for n, p := range r.Properties { + for _, p := range r.GetFieldsSortedByNumber() { typ := builder.FieldTypeBool() switch p.Type { case schema.Type_STRING: typ = builder.FieldTypeString() } - mb.AddField(builder.NewField(n, typ).SetNumber(p.Number).SetComments( + mb.AddField(builder.NewField(p.Name, typ).SetNumber(p.Number).SetComments( builder.Comments{ - LeadingComment: fmt.Sprintf("Field for %v.", n), + LeadingComment: fmt.Sprintf("Field for %v.", p.Name), }, )) }