From bdaaa25de1b5d9a8aac9276529e6e025bf3d0fc9 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 10 Nov 2023 15:59:57 +0100 Subject: [PATCH] Do not skip proto3 files, as it warns about out of date LSTs (#3681) * Do not skip proto3 files, as it warns about out of date LSTs * Update the _one_ test that we have --- .../org/openrewrite/protobuf/ProtoParser.java | 26 ++++++++++++++----- .../openrewrite/protobuf/ProtoParserTest.java | 10 +++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/ProtoParser.java b/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/ProtoParser.java index f2a7aae4bb4..bbd419a439c 100644 --- a/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/ProtoParser.java +++ b/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/ProtoParser.java @@ -17,22 +17,27 @@ import org.antlr.v4.runtime.*; import org.intellij.lang.annotations.Language; +import org.openrewrite.ExecutionContext; +import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Parser; -import org.openrewrite.*; +import org.openrewrite.SourceFile; import org.openrewrite.internal.EncodingDetectingInputStream; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.marker.Markers; import org.openrewrite.protobuf.internal.ProtoParserVisitor; import org.openrewrite.protobuf.internal.grammar.Protobuf2Lexer; import org.openrewrite.protobuf.internal.grammar.Protobuf2Parser; import org.openrewrite.protobuf.tree.Proto; +import org.openrewrite.text.PlainText; import org.openrewrite.tree.ParseError; import org.openrewrite.tree.ParsingEventListener; import org.openrewrite.tree.ParsingExecutionContextView; import java.nio.file.Path; -import java.util.Objects; import java.util.stream.Stream; +import static org.openrewrite.Tree.randomId; + public class ProtoParser implements Parser { @Override @@ -51,7 +56,18 @@ public Stream parseInputs(Iterable sourceFiles, @Nullable Pat parser.addErrorListener(new ForwardingErrorListener(input.getPath(), ctx)); if (sourceStr.contains("proto3")) { - return null; + // Pending Proto3 support, the best we can do is plain text & not skip files + return new PlainText( + randomId(), + path, + Markers.EMPTY, + is.getCharset().name(), + is.isCharsetBomMarked(), + input.getFileAttributes(), + null, + sourceStr, + null + ); } Proto.Document document = new ProtoParserVisitor( @@ -67,9 +83,7 @@ public Stream parseInputs(Iterable sourceFiles, @Nullable Pat ctx.getOnError().accept(t); return ParseError.build(this, input, relativeTo, ctx, t); } - }) - // filter out the nulls produced for `proto3` sources - .filter(Objects::nonNull); + }); } @Override diff --git a/rewrite-protobuf/src/test/java/org/openrewrite/protobuf/ProtoParserTest.java b/rewrite-protobuf/src/test/java/org/openrewrite/protobuf/ProtoParserTest.java index 2c2c8cc867c..0a078ee4f3f 100644 --- a/rewrite-protobuf/src/test/java/org/openrewrite/protobuf/ProtoParserTest.java +++ b/rewrite-protobuf/src/test/java/org/openrewrite/protobuf/ProtoParserTest.java @@ -18,18 +18,16 @@ import org.junit.jupiter.api.Test; import org.openrewrite.SourceFile; import org.openrewrite.test.RewriteTest; +import org.openrewrite.text.PlainText; import java.util.List; -import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; -public class ProtoParserTest implements RewriteTest { - +class ProtoParserTest implements RewriteTest { @Test void noNullsForProto3Files() { - List sources = ProtoParser.builder().build().parse("syntax = \"proto3\";") - .collect(Collectors.toList()); - assertThat(sources).isEmpty(); + List sources = ProtoParser.builder().build().parse("syntax = \"proto3\";").toList(); + assertThat(sources).singleElement().isInstanceOf(PlainText.class); } }