Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions rules/aep0191/proto_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ import (
"github.com/aep-dev/api-linter/lint"
"github.com/aep-dev/api-linter/locations"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/types/descriptorpb"
)

// APIs must use proto3.
// APIs must use proto3 or newer edition.
var syntax = &lint.FileRule{
Name: lint.NewRuleName(191, "proto-version"),
RuleType: lint.NewRuleType(lint.MustRule),
LintFile: func(f *desc.FileDescriptor) []lint.Problem {
if !f.IsProto3() {
if !f.IsProto3() && f.Edition() < descriptorpb.Edition_EDITION_PROTO3 {
return []lint.Problem{{
Message: "All API proto files must use proto3 syntax.",
Suggestion: "syntax = \"proto3\";",
Message: "All API proto files must use proto3 or newer edition.",
Suggestion: "edition = \"2023\";",
Descriptor: f,
Location: locations.FileSyntax(f),
}}
Expand Down
12 changes: 7 additions & 5 deletions rules/aep0191/proto_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ import (

"github.com/aep-dev/api-linter/rules/internal/testutils"
"github.com/jhump/protoreflect/desc/builder"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestSyntax(t *testing.T) {
// Set up the two permutations.
tests := []struct {
testName string
isProto3 bool
edition descriptorpb.Edition
problems testutils.Problems
}{
{"Valid", true, testutils.Problems{}},
{"Invalid", false, testutils.Problems{{Suggestion: `syntax = "proto3";`}}},
{"Valid (proto3)", descriptorpb.Edition_EDITION_PROTO3, testutils.Problems{}},
{"Valid (2023)", descriptorpb.Edition_EDITION_2023, testutils.Problems{}},
{"Valid (2024)", descriptorpb.Edition_EDITION_2024, testutils.Problems{}},
{"Invalid (proto2)", descriptorpb.Edition_EDITION_PROTO2, testutils.Problems{{Suggestion: `edition = "2023";`}}},
}

// Run each permutation as an individual test.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Build an appropriate file descriptor.
f, err := builder.NewFile("library.proto").SetProto3(test.isProto3).Build()
f, err := builder.NewFile("library.proto").SetEdition(test.edition).Build()
if err != nil {
t.Fatalf("Could not build file descriptor.")
}

// Lint the file, and ensure we got the expected problems.
if diff := test.problems.SetDescriptor(f).Diff(syntax.Lint(f)); diff != "" {
t.Error(diff)
Expand Down