Skip to content

Commit

Permalink
fix: add tests (#16)
Browse files Browse the repository at this point in the history
Adding some tests to ensure:

- The generated output matches what is currently expected.
- Output is consistent with the AEP linter (this is disabled until aep-dev/api-linter#86 is merged).
  • Loading branch information
toumorokoshi authored Sep 6, 2024
1 parent 15b5501 commit 80bb3d9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
./scripts/verify-goldens.sh
# - name: Run script
# run: |
# ./scripts/run-api-linter.sh
16 changes: 16 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion schema/resourcedefinition.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion scripts/run-api-linter.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,4 +11,5 @@ fi

api-linter \
./example/bookstore/v1/bookstore.proto \
-I /tmp/googleapis
-I /tmp/googleapis \
--set-exit-status
9 changes: 9 additions & 0 deletions scripts/verify-goldens.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions writer/proto/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
))
}
Expand Down

0 comments on commit 80bb3d9

Please sign in to comment.