check outdated generated code for rewriters in a test

This commit is contained in:
Lulu13022002 2024-03-14 20:41:34 +01:00
parent 70bcf0dd5c
commit c1f113dd23
No known key found for this signature in database
GPG Key ID: 491C8F0B8ACDEB01
3 changed files with 88 additions and 3 deletions

View File

@ -24,6 +24,12 @@ dependencies {
implementation("com.squareup:javapoet:1.13.0")
implementation(project(":paper-api"))
implementation("io.github.classgraph:classgraph:4.8.47")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}
tasks.test {
useJUnitPlatform()
systemProperty("paper.generator.rewriter.container", file("generated").toString()) // todo move to the sourceset
}
group = "io.papermc.paper"

View File

@ -11,6 +11,7 @@ import io.papermc.paper.generated.GeneratedFrom;
import net.minecraft.SharedConstants;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.VisibleForTesting;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@ -24,10 +25,13 @@ import java.util.Set;
public class SearchReplaceRewriter implements SourceRewriter {
protected static final String INDENT_UNIT = " ";
private static final String PAPER_START_FORMAT = "Paper start";
@VisibleForTesting
public static final String INDENT_UNIT = " ";
@VisibleForTesting
public static final String PAPER_START_FORMAT = "Paper start";
private static final String PAPER_END_FORMAT = "Paper end";
private static final String GENERATED_COMMENT_FORMAT = "// %s - Generated/%s"; // {0} = PAPER_START_FORMAT|PAPER_END_FORMAT {1} = pattern
@VisibleForTesting
public static final String GENERATED_COMMENT_FORMAT = "// %s - Generated/%s"; // {0} = PAPER_START_FORMAT|PAPER_END_FORMAT {1} = pattern
protected final Class<?> rewriteClass;
protected final String pattern;

View File

@ -0,0 +1,75 @@
package io.papermc.generator.rewriter;
import io.papermc.generator.Generators;
import io.papermc.generator.rewriter.utils.Annotations;
import io.papermc.generator.utils.ClassHelper;
import io.papermc.paper.generated.GeneratedFrom;
import net.minecraft.SharedConstants;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class OldGeneratedCodeTest {
private static final String CONTAINER = System.getProperty("paper.generator.rewriter.container");
private static String CURRENT_VERSION;
@BeforeAll
public static void initializeVersion() {
SharedConstants.tryDetectVersion();
CURRENT_VERSION = SharedConstants.getCurrentVersion().getName();
}
@Test
public void testOutdatedCode() throws IOException {
for (SourceRewriter rewriter : Generators.API_REWRITE) {
if (!(rewriter instanceof SearchReplaceRewriter srt)) {
continue;
}
String filePath = "%s/%s.java".formatted(
srt.rewriteClass.getPackageName().replace('.', '/'),
ClassHelper.getRootClass(srt.rewriteClass).getSimpleName()
);
Path path = Path.of(CONTAINER, filePath);
if (false && !Files.exists(path)) {
continue;
}
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
int lineCount = 0;
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
lineCount++;
int startPatternIndex = line.indexOf(SearchReplaceRewriter.GENERATED_COMMENT_FORMAT.formatted(SearchReplaceRewriter.PAPER_START_FORMAT, ""));
if (startPatternIndex != -1 && (startPatternIndex % SearchReplaceRewriter.INDENT_UNIT.length()) == 0 && line.stripTrailing().equals(line)) {
String nextLine = reader.readLine();
if (nextLine == null) {
break;
}
lineCount++;
String generatedComment = "// %s ".formatted(Annotations.annotationStyle(GeneratedFrom.class));
int generatedIndex = nextLine.indexOf(generatedComment);
if (generatedIndex != -1 && (generatedIndex % SearchReplaceRewriter.INDENT_UNIT.length()) == 0 && nextLine.stripTrailing().equals(nextLine)) {
String generatedVersion = nextLine.substring(generatedIndex + generatedComment.length());
Assertions.assertEquals(CURRENT_VERSION, generatedVersion,
"Code at line %s in %s is marked as being generated in version %s when the current version is %s".formatted(
lineCount, srt.rewriteClass.getCanonicalName(),
generatedVersion, CURRENT_VERSION));
}
}
}
}
}
}
}