mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Add module display names, reject duplicate pack classes, log pack modules
ModuleInfo gains a required, non-blank display name next to id and version: ModuleInfo.of(id, name, version). PackModules now fails when a class is in two jars of one pack, naming the class and both jars. ModuleJar indexes class names while it reads the entry point, so the check runs before the pack class loader exists and nothing is half-loaded. Jars are read with the runtime's multi-release version, like the loader reads them: a versioned class counts once under its base name; module-info.class and anything under META-INF/ are ignored. A successful load logs one INFO summary through System.Logger with the display name, id, version and jar of each module, or a line saying the pack has no module jars. PackModulesSummary builds the text and is tested without a logging backend.
This commit is contained in:
1 parent
4fa6ddcd7a
commit
b137bc6d92
19 files changed
+328
-53
No files matched your search
@@ -31,6 +31,16 @@ class ApiValuesTest {
|
||||
assertThrows(IllegalArgumentException.class, () -> new ResourceKey<>("test:value", int.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void moduleInfoRequiresANameAndVersion() {
|
||||
var info = ModuleInfo.of("example:moss", "Moss", "1.0.0");
|
||||
assertEquals(ResourceId.parse("example:moss"), info.id());
|
||||
assertEquals("Moss", info.name());
|
||||
assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("example:moss", " ", "1.0.0"));
|
||||
assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("example:moss", "Moss", ""));
|
||||
assertThrows(NullPointerException.class, () -> ModuleInfo.of("example:moss", null, "1.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sparseObjectsCopySortAndRejectDuplicateOrOutOfBoundsBlocks() {
|
||||
var a = new ObjectBlock(new BlockPosition(-1, 0, 0), new BlockState("minecraft:stone"));
|
||||
|
||||
@@ -177,7 +177,7 @@ class PlacementLifecyclePortTest {
|
||||
void inactiveRequirementDeactivatesThirdPartyDependents() {
|
||||
var capModule = new IrisModule() {
|
||||
static final PlacementStage<Void> CAP = Stage.placement("example:cap/top");
|
||||
public ModuleInfo info() { return ModuleInfo.of("example:cap", "1"); }
|
||||
public ModuleInfo info() { return ModuleInfo.of("example:cap", "Cap", "1"); }
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(CAP).requires(StiltModule.FILL).run(context -> context.access().set(
|
||||
new PlacedBlock(new BlockPosition(5, 20, 5), PLANKS, Optional.empty())));
|
||||
|
||||
@@ -14,7 +14,7 @@ public final class GrassModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("example:grass", "1.0.0");
|
||||
return ModuleInfo.of("example:grass", "Grass", "1.0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,7 @@ public final class TerrainModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:terrain", "test");
|
||||
return ModuleInfo.of("iris:terrain", "Terrain", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,7 +15,7 @@ public final class AnchorModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:anchor", "test");
|
||||
return ModuleInfo.of("iris:anchor", "Anchor", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,7 @@ public final class BlocksModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:blocks", "test");
|
||||
return ModuleInfo.of("iris:blocks", "Blocks", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,7 +21,7 @@ public final class MossModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("example:moss", "1.0.0");
|
||||
return ModuleInfo.of("example:moss", "Moss", "1.0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,7 @@ public final class PrepareModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:prepare", "test");
|
||||
return ModuleInfo.of("iris:prepare", "Prepare", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,7 @@ public final class SnowModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:snow", "test");
|
||||
return ModuleInfo.of("iris:snow", "Snow", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,7 +19,7 @@ public final class StiltModule implements IrisModule {
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:stilt", "test");
|
||||
return ModuleInfo.of("iris:stilt", "Stilts", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -69,7 +69,7 @@ class ModuleRegistryTest {
|
||||
void loadingModulesCatalogsStageSettingsAndRegistrationsTogether() {
|
||||
var conditioned = new IrisModule() {
|
||||
static final PlacementStage<Void> WORK = Stage.placement("test:conditioned/work");
|
||||
public ModuleInfo info() { return ModuleInfo.of("test:conditioned", "1"); }
|
||||
public ModuleInfo info() { return ModuleInfo.of("test:conditioned", "Conditioned", "1"); }
|
||||
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
|
||||
public void register(FeatureRegistry registry) { registry.registerObject(Two.class, ignored -> context -> null); }
|
||||
};
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.example.placement.MossModule;
|
||||
import com.volmit.example.placement.SnowModule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PackModulesSummaryTest {
|
||||
@Test
|
||||
void listsNameIdVersionAndJarOfEveryModuleInLoadOrder() {
|
||||
var modules = List.of(
|
||||
new DiscoveredModule(new MossModule(), "modules/moss.jar"),
|
||||
new DiscoveredModule(new SnowModule(), "modules/snow.jar"));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 2 modules:
|
||||
Moss (example:moss 1.0.0) from modules/moss.jar
|
||||
Snow (iris:snow test) from modules/snow.jar""",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleModuleReadsAsOneModule() {
|
||||
var modules = List.of(new DiscoveredModule(new MossModule(), "modules/moss.jar"));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 1 module:
|
||||
Moss (example:moss 1.0.0) from modules/moss.jar""",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void packWithoutModulesSaysSo() {
|
||||
assertEquals("Pack overworld uses no modules: no jars in its modules directory",
|
||||
PackModulesSummary.describe("overworld", List.of()));
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,10 @@ class PackModulesTest {
|
||||
"public static final PlacementStage<Void> MARK = Stage.placement(\"alpha:core/mark\");";
|
||||
private static final String NO_MEMBERS = "";
|
||||
private static final String NO_STAGES = "";
|
||||
private static final String SHARED_CLASS = "pack.shared.Util";
|
||||
private static final String SHARED_SOURCE = "package pack.shared; public final class Util {}";
|
||||
private static final byte[] UNUSED_CLASS_BYTES = {(byte) 0xCA, (byte) 0xFE};
|
||||
private static final String DEFINED_PROPERTY = "irisapi.test.packModuleConstructed";
|
||||
|
||||
@TempDir
|
||||
Path pack;
|
||||
@@ -133,6 +137,76 @@ class PackModulesTest {
|
||||
"not from this jar");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameClassInTwoJarsFailsBeforeAnyModuleIsConstructed() {
|
||||
var constructorMarker = "public %s() { System.setProperty(\"" + DEFINED_PROPERTY + "\", \"true\"); }";
|
||||
module("pack.alpha.Alpha", "alpha:core", constructorMarker.formatted("Alpha"), NO_STAGES)
|
||||
.source(SHARED_CLASS, SHARED_SOURCE).writeInto(pack, "alpha.jar");
|
||||
module("pack.beta.Beta", "beta:core", constructorMarker.formatted("Beta"), NO_STAGES)
|
||||
.source(SHARED_CLASS, SHARED_SOURCE).writeInto(pack, "beta.jar");
|
||||
|
||||
try {
|
||||
assertMentions(failure(() -> PackModules.load(pack)),
|
||||
"Class " + SHARED_CLASS + " is in both module jars " + origin("alpha.jar") + " and " + origin("beta.jar"));
|
||||
assertNull(System.getProperty(DEFINED_PROPERTY), "a module was constructed before the duplicate was found");
|
||||
} finally {
|
||||
System.clearProperty(DEFINED_PROPERTY);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void moduleDescriptorsMayRepeatAcrossJars() {
|
||||
module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
|
||||
.resource("module-info.class", UNUSED_CLASS_BYTES).writeInto(pack, "alpha.jar");
|
||||
module("pack.beta.Beta", "beta:core", NO_MEMBERS, NO_STAGES)
|
||||
.resource("module-info.class", UNUSED_CLASS_BYTES).writeInto(pack, "beta.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(2, packModules.modules().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionedClassOfAMultiReleaseJarCountsUnderItsBaseName() {
|
||||
module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
|
||||
.source(SHARED_CLASS, SHARED_SOURCE).writeInto(pack, "alpha.jar");
|
||||
module("pack.beta.Beta", "beta:core", NO_MEMBERS, NO_STAGES)
|
||||
.multiRelease()
|
||||
.resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
|
||||
.writeInto(pack, "beta.jar");
|
||||
|
||||
assertMentions(failure(() -> PackModules.load(pack)),
|
||||
"Class " + SHARED_CLASS + " is in both module jars " + origin("alpha.jar") + " and " + origin("beta.jar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionedEntriesOutsideAMultiReleaseJarAreNotClasses() {
|
||||
module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
|
||||
.source(SHARED_CLASS, SHARED_SOURCE)
|
||||
.resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
|
||||
.writeInto(pack, "alpha.jar");
|
||||
module("pack.beta.Beta", "beta:core", NO_MEMBERS, NO_STAGES)
|
||||
.resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
|
||||
.writeInto(pack, "beta.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(2, packModules.modules().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiReleaseJarMayHoldAVersionedCopyOfItsOwnClass() {
|
||||
module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
|
||||
.source(SHARED_CLASS, SHARED_SOURCE)
|
||||
.multiRelease()
|
||||
.resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
|
||||
.writeInto(pack, "alpha.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(1, packModules.modules().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateModuleIdsAcrossJarsFailNamingBothJars() {
|
||||
module("pack.one.Duplicate", "dup:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "one.jar");
|
||||
|
||||
@@ -41,7 +41,7 @@ class StageGraphTest {
|
||||
|
||||
private static IrisModule module(String id, Consumer<StageGraph> install) {
|
||||
return new IrisModule() {
|
||||
public ModuleInfo info() { return ModuleInfo.of(id, "1"); }
|
||||
public ModuleInfo info() { return ModuleInfo.of(id, "Test module " + id, "1"); }
|
||||
public void install(StageGraph graph) { install.accept(graph); }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.Manifest;
|
||||
@@ -30,6 +31,8 @@ public final class TestModuleJar {
|
||||
private static final String SERVICES_FILE = "META-INF/services/" + IrisModule.class.getName();
|
||||
|
||||
private final Map<String, String> sources = new LinkedHashMap<>();
|
||||
private final Map<String, byte[]> resources = new LinkedHashMap<>();
|
||||
private boolean multiRelease;
|
||||
private final List<String> entryPoints = new ArrayList<>();
|
||||
private final List<Path> classpath = new ArrayList<>(List.of(classesOf(IrisModule.class), classesOf(AnchorModule.class)));
|
||||
|
||||
@@ -49,6 +52,18 @@ public final class TestModuleJar {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Adds a raw jar entry, such as a versioned class file, without compiling anything. */
|
||||
public TestModuleJar resource(String entryName, byte[] content) {
|
||||
resources.put(entryName, content.clone());
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Marks the jar {@code Multi-Release: true}, so its {@code META-INF/versions} entries count. */
|
||||
public TestModuleJar multiRelease() {
|
||||
multiRelease = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TestModuleJar compileAgainst(Path jar) {
|
||||
classpath.add(jar);
|
||||
return this;
|
||||
@@ -59,8 +74,9 @@ public final class TestModuleJar {
|
||||
try {
|
||||
var jar = Files.createDirectories(packRoot.resolve("modules")).resolve(jarName);
|
||||
var classes = compile(Files.createTempDirectory(packRoot, "classes"));
|
||||
try (var output = new JarOutputStream(Files.newOutputStream(jar), new Manifest())) {
|
||||
try (var output = new JarOutputStream(Files.newOutputStream(jar), manifest())) {
|
||||
writeClasses(output, classes);
|
||||
writeResources(output);
|
||||
writeEntryPoints(output);
|
||||
}
|
||||
return jar;
|
||||
@@ -92,6 +108,21 @@ public final class TestModuleJar {
|
||||
}
|
||||
}
|
||||
|
||||
private Manifest manifest() {
|
||||
var manifest = new Manifest();
|
||||
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
|
||||
if (multiRelease) manifest.getMainAttributes().put(Attributes.Name.MULTI_RELEASE, "true");
|
||||
return manifest;
|
||||
}
|
||||
|
||||
private void writeResources(JarOutputStream output) throws IOException {
|
||||
for (var resource : resources.entrySet()) {
|
||||
output.putNextEntry(new JarEntry(resource.getKey()));
|
||||
output.write(resource.getValue());
|
||||
output.closeEntry();
|
||||
}
|
||||
}
|
||||
|
||||
private void writeEntryPoints(JarOutputStream output) throws IOException {
|
||||
if (entryPoints.isEmpty()) return;
|
||||
output.putNextEntry(new JarEntry(SERVICES_FILE));
|
||||
@@ -111,11 +142,16 @@ public final class TestModuleJar {
|
||||
public final class %s implements IrisModule {
|
||||
%s
|
||||
|
||||
public ModuleInfo info() { return ModuleInfo.of("%s", "1.0.0"); }
|
||||
public ModuleInfo info() { return ModuleInfo.of("%s", "%s", "1.0.0"); }
|
||||
|
||||
public void install(StageGraph graph) { %s }
|
||||
}
|
||||
""".formatted(className.substring(0, dot), className.substring(dot + 1), members, moduleId, install);
|
||||
""".formatted(className.substring(0, dot), className.substring(dot + 1), members, moduleId, displayName(moduleId), install);
|
||||
}
|
||||
|
||||
/** The display name test modules get, so assertions can predict it from the id. */
|
||||
public static String displayName(String moduleId) {
|
||||
return "Test " + moduleId;
|
||||
}
|
||||
|
||||
private static Path classesOf(Class<?> type) {
|
||||
|
||||
Reference in new issue
Block a user