From eddc32434d3dcfeaf49225012ff2200ea6e02857 Mon Sep 17 00:00:00 2001 From: ix0rai Date: Wed, 4 Dec 2024 19:46:12 -0600 Subject: [PATCH] logic cleanup and new test --- .../command/DropInvalidMappingsTest.java | 22 ++++++++++++++++--- .../expected/EmptyMappings.mapping | 4 ++++ .../expected/MappingSave.mapping | 4 ++++ .../input/EmptyMappings.mapping | 3 --- .../input/MappingSave.mapping | 8 +++++++ .../translation/mapping/MappingsChecker.java | 13 ++++++----- 6 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 enigma-cli/src/test/resources/drop_invalid_mappings/expected/MappingSave.mapping create mode 100644 enigma-cli/src/test/resources/drop_invalid_mappings/input/MappingSave.mapping diff --git a/enigma-cli/src/test/java/org/quiltmc/enigma/command/DropInvalidMappingsTest.java b/enigma-cli/src/test/java/org/quiltmc/enigma/command/DropInvalidMappingsTest.java index cc2dbc129..7d1a492e4 100644 --- a/enigma-cli/src/test/java/org/quiltmc/enigma/command/DropInvalidMappingsTest.java +++ b/enigma-cli/src/test/java/org/quiltmc/enigma/command/DropInvalidMappingsTest.java @@ -8,19 +8,23 @@ import java.nio.file.Path; public class DropInvalidMappingsTest extends CommandTest { - private static final Path JAR = TestUtil.obfJar("lone_class"); + private static final Path LONE_JAR = TestUtil.obfJar("lone_class"); + private static final Path INNER_JAR = TestUtil.obfJar("inner_classes"); private static final Path INPUT_DIR = getResource("/drop_invalid_mappings/input/"); private static final Path EXPECTED_DIR = getResource("/drop_invalid_mappings/expected/"); + private static final Path INVALID_MAPPINGS_INPUT = INPUT_DIR.resolve("InvalidMappings.mapping"); private static final Path INVALID_MAPPINGS_EXPECTED = EXPECTED_DIR.resolve("InvalidMappings.mapping"); private static final Path EMPTY_MAPPINGS_INPUT = INPUT_DIR.resolve("EmptyMappings.mapping"); private static final Path EMPTY_MAPPINGS_EXPECTED = EXPECTED_DIR.resolve("EmptyMappings.mapping"); + private static final Path MAPPING_SAVE_INPUT = INPUT_DIR.resolve("MappingSave.mapping"); + private static final Path MAPPING_SAVE_EXPECTED = EXPECTED_DIR.resolve("MappingSave.mapping"); @Test public void testInvalidMappings() throws Exception { Path resultFile = Files.createTempFile("invalidMappingsResult", ".mapping"); - DropInvalidMappingsCommand.run(JAR, INVALID_MAPPINGS_INPUT, resultFile); + DropInvalidMappingsCommand.run(LONE_JAR, INVALID_MAPPINGS_INPUT, resultFile); String expectedLines = Files.readString(INVALID_MAPPINGS_EXPECTED); String actualLines = Files.readString(resultFile); @@ -32,11 +36,23 @@ public void testInvalidMappings() throws Exception { public void testEmptyMappings() throws Exception { Path resultFile = Files.createTempFile("emptyMappingsResult", ".mapping"); - DropInvalidMappingsCommand.run(JAR, EMPTY_MAPPINGS_INPUT, resultFile); + DropInvalidMappingsCommand.run(LONE_JAR, EMPTY_MAPPINGS_INPUT, resultFile); String expectedLines = Files.readString(EMPTY_MAPPINGS_EXPECTED); String actualLines = Files.readString(resultFile); Assertions.assertEquals(expectedLines, actualLines); } + + @Test + public void testMappingSave() throws Exception { + Path resultFile = Files.createTempFile("mappingSaveResult", ".mapping"); + + DropInvalidMappingsCommand.run(INNER_JAR, MAPPING_SAVE_INPUT, resultFile); + + String expectedLines = Files.readString(MAPPING_SAVE_EXPECTED); + String actualLines = Files.readString(resultFile); + + Assertions.assertEquals(expectedLines, actualLines); + } } diff --git a/enigma-cli/src/test/resources/drop_invalid_mappings/expected/EmptyMappings.mapping b/enigma-cli/src/test/resources/drop_invalid_mappings/expected/EmptyMappings.mapping index e69de29bb..7b003e3fe 100644 --- a/enigma-cli/src/test/resources/drop_invalid_mappings/expected/EmptyMappings.mapping +++ b/enigma-cli/src/test/resources/drop_invalid_mappings/expected/EmptyMappings.mapping @@ -0,0 +1,4 @@ +CLASS a + CLASS b + METHOD b (Ljava/lang/String;)V + ARG 2 nonEmpty diff --git a/enigma-cli/src/test/resources/drop_invalid_mappings/expected/MappingSave.mapping b/enigma-cli/src/test/resources/drop_invalid_mappings/expected/MappingSave.mapping new file mode 100644 index 000000000..7284de4d4 --- /dev/null +++ b/enigma-cli/src/test/resources/drop_invalid_mappings/expected/MappingSave.mapping @@ -0,0 +1,4 @@ +CLASS c + CLASS a + METHOD (Lc;I)V + ARG 2 x diff --git a/enigma-cli/src/test/resources/drop_invalid_mappings/input/EmptyMappings.mapping b/enigma-cli/src/test/resources/drop_invalid_mappings/input/EmptyMappings.mapping index 4ad142540..91bc828b5 100644 --- a/enigma-cli/src/test/resources/drop_invalid_mappings/input/EmptyMappings.mapping +++ b/enigma-cli/src/test/resources/drop_invalid_mappings/input/EmptyMappings.mapping @@ -3,6 +3,3 @@ CLASS a METHOD (Ljava/lang/String;)V ARG 1 METHOD a ()Ljava/lang/String; - CLASS b - METHOD b (Ljava/lang/String;)V - ARG 2 diff --git a/enigma-cli/src/test/resources/drop_invalid_mappings/input/MappingSave.mapping b/enigma-cli/src/test/resources/drop_invalid_mappings/input/MappingSave.mapping new file mode 100644 index 000000000..398aa3c1e --- /dev/null +++ b/enigma-cli/src/test/resources/drop_invalid_mappings/input/MappingSave.mapping @@ -0,0 +1,8 @@ +CLASS c + METHOD a ()V + ARG 34 invalid + ARG 23423 invalid + CLASS a + FIELD a I + METHOD (Lc;I)V + ARG 2 x diff --git a/enigma/src/main/java/org/quiltmc/enigma/impl/translation/mapping/MappingsChecker.java b/enigma/src/main/java/org/quiltmc/enigma/impl/translation/mapping/MappingsChecker.java index 30c9bfa2a..71fe89bfb 100644 --- a/enigma/src/main/java/org/quiltmc/enigma/impl/translation/mapping/MappingsChecker.java +++ b/enigma/src/main/java/org/quiltmc/enigma/impl/translation/mapping/MappingsChecker.java @@ -4,6 +4,7 @@ import org.quiltmc.enigma.api.ProgressListener; import org.quiltmc.enigma.api.analysis.index.jar.EntryIndex; import org.quiltmc.enigma.api.analysis.index.jar.JarIndex; +import org.quiltmc.enigma.api.source.TokenType; import org.quiltmc.enigma.api.translation.mapping.EntryMapping; import org.quiltmc.enigma.api.translation.mapping.ResolutionStrategy; import org.quiltmc.enigma.api.translation.mapping.tree.EntryTree; @@ -79,7 +80,7 @@ private boolean shouldDropBrokenEntry(Dropped dropped, Entry entry) { } // Method entry has parameter names, keep it even though it's not the root. - return !(entry instanceof MethodEntry) || this.hasNoChildren(entry, dropped); + return !(entry instanceof MethodEntry) || this.hasNoMappedChildren(entry, dropped); // Entry is not the root, and is not a method with params } @@ -103,24 +104,24 @@ private boolean shouldDropEmptyMapping(Dropped dropped, Entry entry) { boolean isEmpty = (mapping.targetName() == null && mapping.javadoc() == null) || !this.project.isRenamable(entry); if (isEmpty) { - return this.hasNoChildren(entry, dropped); + return this.hasNoMappedChildren(entry, dropped); } } return false; } - private boolean hasNoChildren(Entry entry, Dropped dropped) { + private boolean hasNoMappedChildren(Entry entry, Dropped dropped) { var children = this.mappings.getChildren(entry); // account for child mappings that have been dropped already if (!children.isEmpty()) { for (Entry child : children) { var mapping = this.mappings.get(child); - if (mapping != null && !(mapping.targetName() == null && mapping.javadoc() == null)) { + if (!dropped.getDroppedMappings().containsKey(child) + && mapping != null && mapping.tokenType() != TokenType.OBFUSCATED + || !this.hasNoMappedChildren(child, dropped)) { return false; - } else if (!dropped.getDroppedMappings().containsKey(child) && this.hasNoChildren(child, dropped)) { - return true; } } }