diff --git a/core/src/main/java/io/anemos/metastore/core/proto/validate/ProtoLint.java b/core/src/main/java/io/anemos/metastore/core/proto/validate/ProtoLint.java index c732d1c..c74be22 100644 --- a/core/src/main/java/io/anemos/metastore/core/proto/validate/ProtoLint.java +++ b/core/src/main/java/io/anemos/metastore/core/proto/validate/ProtoLint.java @@ -5,6 +5,7 @@ import io.anemos.metastore.v1alpha1.LintRule; import io.anemos.metastore.v1alpha1.RuleInfo; +import java.io.IOException; import java.util.List; public class ProtoLint { @@ -166,4 +167,19 @@ private boolean isPascalCase(String fieldName) { return true; } + public void lintOnPackage(Descriptors.Descriptor ref) { + Descriptors.FileDescriptor fileDescriptor = proto.getFileDescriptorByFileName(ref.getFile().getName()); + String protoPackageName = fileDescriptor.getPackage(); + String fileName = ref.getFile().getFullName(); + String dirName = fileName.substring(0, fileName.lastIndexOf("/")).replace("/", "."); + if (!dirName.equals(protoPackageName)) { + results.addResult(fileDescriptor, RuleInfo.newBuilder() + .setLintRule(LintRule.LINT_PACKAGE_NO_DIR_ALIGNMENT) + .setCode(String.format("L%d/00", LintRule.LINT_PACKAGE_NO_DIR_ALIGNMENT_VALUE)) + .build() + ); + } + } + + } diff --git a/core/src/main/java/io/anemos/metastore/core/proto/validate/ValidationResults.java b/core/src/main/java/io/anemos/metastore/core/proto/validate/ValidationResults.java index 0daf04f..4226bcb 100644 --- a/core/src/main/java/io/anemos/metastore/core/proto/validate/ValidationResults.java +++ b/core/src/main/java/io/anemos/metastore/core/proto/validate/ValidationResults.java @@ -76,6 +76,11 @@ void addResult(Descriptors.ServiceDescriptor descriptor, RuleInfo ruleInfo) { serviceResult.addResult(ruleInfo); } + void addResult(Descriptors.FileDescriptor descriptor, RuleInfo ruleInfo) { + FileResultContainer fileResult = getOrCreateFile(descriptor.getFullName()); + fileResult.addResult(ruleInfo); + } + void setPatch(Descriptors.FieldDescriptor fd, FieldChangeInfo patch) { MessageResultContainer messageResult = getOrCreateMessage(fd.getContainingType().getFullName()); messageResult.addPatch(fd, patch); @@ -192,10 +197,18 @@ public void setPatch(ChangeInfo patch) { } public FileResult getResult() { - return FileResult.newBuilder() + + FileResult.Builder builder = FileResult.newBuilder() .setFileName(fullName) - .setChange(patch) - .build(); + .addAllInfo(info); + if (patch != null){ + builder.setChange(patch); + } + return builder.build(); + } + + public void addResult(RuleInfo ruleInfo) { + info.add(ruleInfo); } } diff --git a/core/src/test/java/io/anemos/metastore/core/proto/validation/LintTest.java b/core/src/test/java/io/anemos/metastore/core/proto/validation/LintTest.java index b5d4d21..e176da8 100644 --- a/core/src/test/java/io/anemos/metastore/core/proto/validation/LintTest.java +++ b/core/src/test/java/io/anemos/metastore/core/proto/validation/LintTest.java @@ -1,12 +1,16 @@ package io.anemos.metastore.core.proto.validation; +import com.google.protobuf.DescriptorProtos; import com.google.protobuf.Descriptors; +import com.google.protobuf.Service; import io.anemos.metastore.core.Lint; -import io.anemos.metastore.core.proto.ProtoDescriptor; +import io.anemos.metastore.core.proto.*; import io.anemos.metastore.core.proto.validate.ProtoLint; import io.anemos.metastore.core.proto.validate.ValidationResults; +import io.anemos.metastore.invalid.Invalid; import io.anemos.metastore.v1alpha1.*; import org.junit.Assert; +import org.junit.ComparisonFailure; import org.junit.Test; import java.io.IOException; @@ -85,6 +89,7 @@ public void methodOk() throws IOException { Assert.assertNull(getInfoForMethod(mr, "MethodOk")); } + private Report lintMessage(Descriptors.Descriptor d) throws IOException { ProtoDescriptor pd = new ProtoDescriptor(d); String message = d.getFullName(); @@ -124,6 +129,7 @@ private List getInfoForMethod(ServiceResult mr, String methodName) { return null; } + private void assertOnMethod(ServiceResult mr, String methodName, LintRule expecredRule, String expectedCode) { List infoForField = getInfoForMethod(mr, methodName); String code = null; @@ -156,20 +162,62 @@ private void assertOnField(MessageResult mr, int fieldNumber, LintRule expecredR Assert.assertEquals(expecredRule, rule); } - private void assertOnMessage(MessageResult mr, LintRule expecredRule, String expectedCode) { + private void assertOnMessage(MessageResult mr, LintRule expectedRule, String expectedCode) { String code = null; LintRule rule = null; for (RuleInfo ruleInfo : mr.getInfoList()) { if (ruleInfo.getCode().equals(expectedCode) - && ruleInfo.getLintRule().equals(expecredRule)) { + && ruleInfo.getLintRule().equals(expectedRule)) { return; } code = ruleInfo.getCode(); rule = ruleInfo.getLintRule(); } Assert.assertEquals(expectedCode, code); - Assert.assertEquals(expecredRule, rule); + Assert.assertEquals(expectedRule, rule); + } + + @Test + public void packageScopeVersionValid() throws IOException { + Descriptors.Descriptor descriptor = Lint.LintFieldNamesBad.getDescriptor(); + Report result = lintPackage(descriptor); + Assert.assertEquals(0, result.getFileResultsCount()); + } + + @Test + public void packageScopeVersionInvalid() throws IOException { + Descriptors.Descriptor descriptor = Invalid.InvalidMessage.getDescriptor(); + Report result = lintPackage(descriptor); + FileResult fr = result.getFileResultsOrThrow("anemos/metastore/invalid/invalid.proto"); + + Assert.assertEquals(1, result.getFileResultsCount()); + assertOnFile(fr, LintRule.LINT_PACKAGE_NO_DIR_ALIGNMENT, "L70001/00"); + } + + private void assertOnFile(FileResult fr, LintRule expectedRule, String expectedCode){ + String code = null; + LintRule rule = null; + for (RuleInfo ruleInfo : fr.getInfoList()) { + if (ruleInfo.getCode().equals(expectedCode) + && ruleInfo.getLintRule().equals(expectedRule)) { + return; + } + code = ruleInfo.getCode(); + rule = ruleInfo.getLintRule(); + } + Assert.assertEquals(expectedCode, code); + Assert.assertEquals(expectedRule, rule); } + private Report lintPackage(Descriptors.Descriptor ref) throws IOException { + ProtoDescriptor pd = new ProtoDescriptor(ref); + ValidationResults results = new ValidationResults(); + + ProtoLint lint = new ProtoLint(pd, results); + lint.lintOnPackage(ref); + return results.getReport(); + } + + } diff --git a/core/src/test/proto/anemos/metastore/invalid/invalid.proto b/core/src/test/proto/anemos/metastore/invalid/invalid.proto new file mode 100644 index 0000000..ad1bac6 --- /dev/null +++ b/core/src/test/proto/anemos/metastore/invalid/invalid.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package anemos.metastore.invalid.test; + +option java_package = "io.anemos.metastore.invalid"; + +message InvalidMessage {} \ No newline at end of file diff --git a/proto/src/main/proto/anemos/metastore/v1alpha1/report.proto b/proto/src/main/proto/anemos/metastore/v1alpha1/report.proto index 3e4e07e..959c073 100644 --- a/proto/src/main/proto/anemos/metastore/v1alpha1/report.proto +++ b/proto/src/main/proto/anemos/metastore/v1alpha1/report.proto @@ -49,10 +49,11 @@ message FileResult { string file_name = 1; repeated string message_result_ref = 2; + repeated RuleInfo info = 3; ChangeInfo change = 4; - //ResultCount result_count = 3; + //ResultCount result_count = 6; } message MessageResult { diff --git a/proto/src/main/proto/anemos/metastore/v1alpha1/rules.proto b/proto/src/main/proto/anemos/metastore/v1alpha1/rules.proto index 14692e0..c83526e 100644 --- a/proto/src/main/proto/anemos/metastore/v1alpha1/rules.proto +++ b/proto/src/main/proto/anemos/metastore/v1alpha1/rules.proto @@ -27,4 +27,7 @@ enum LintRule { LINT_METHOD_RTYPE_END_WITH_RESPONSE = 50003; LINT_GENERAL_CUSTOM = 60000; + + LINT_PACKAGE_CUSTOM = 70000; + LINT_PACKAGE_NO_DIR_ALIGNMENT = 70001; }