Enforce namespace ownership: reserved namespaces and module-prefixed stage ids

- LoadedModules.load takes the host's reserved namespaces (such as iris);
  a pack module whose id uses one fails, naming its jar and the namespace.
- A module defines only stages whose ids start with its own id and '/'.
  Module ids may no longer contain '/', so ids cannot nest and every
  stage id has exactly one possible owner.
- Object feature ids and mantle keys must use the registering module's
  namespace; ModuleRegistry.register(DiscoveredModule) scopes registration.
- A settings type named in when(...) needs a feature id in the namespace
  of some module naming it, so modules can still condition on another
  module's settings.
This commit is contained in:
piexel committed 2026-10-05 21:31:55 +02:00
1 parent 6263075ecd
commit 4cb718f706
13 files changed
+413 -129

No files matched your search

@@ -68,6 +68,22 @@ class ApiValuesTest {
assertEquals("Mosß é", ModuleInfo.of("example:moss", "Mosß é", "1.0.0").name());
}
@Test
void moduleIdsAreOneSegmentAndOwnTheStageIdsBelowThem() {
var nested = assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("iris:stilt/extra", "Extra", "1"));
assertEquals("Module id iris:stilt/extra must not contain '/': stage ids are a module id, '/' and a name, "
+ "so iris:stilt/extra would name stages under another module's id", nested.getMessage());
var stilt = ModuleInfo.of("iris:stilt", "Stilt", "1");
assertEquals("iris:stilt/", stilt.stageIdPrefix());
assertTrue(stilt.ownsStage(ResourceId.parse("iris:stilt/fill")));
assertTrue(stilt.ownsStage(ResourceId.parse("iris:stilt/fill/deep")));
assertFalse(stilt.ownsStage(ResourceId.parse("iris:stilt")));
assertFalse(stilt.ownsStage(ResourceId.parse("iris:stilts/fill")));
assertFalse(stilt.ownsStage(ResourceId.parse("iris:anchor/height")));
assertFalse(stilt.ownsStage(ResourceId.parse("other:stilt/fill")));
}
@Test
void sparseObjectsCopySortAndRejectDuplicateOrOutOfBoundsBlocks() {
var a = new ObjectBlock(new BlockPosition(-1, 0, 0), new BlockState("minecraft:stone"));
@@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Optional;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
@@ -197,7 +198,7 @@ class PlacementLifecyclePortTest {
@Test
void loadingThePortCatalogsEveryConditioningSettingsType() {
var loaded = LoadedModules.load(PlacementModules.builtIns(), Map.of());
var loaded = LoadedModules.load(PlacementModules.builtIns(), Map.of(), Set.of());
assertEquals(List.of("example:moss", "iris:snow", "iris:stilt"),
loaded.catalog().settings().stream().map(settings -> settings.id().toString()).toList());
@@ -7,6 +7,7 @@ import com.volmit.stage.StageGraph;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class DiscoveredModuleTest {
@@ -29,7 +30,7 @@ class DiscoveredModuleTest {
var module = new ShiftingIdentity();
var discovered = DiscoveredModule.builtIn(module);
var loaded = LoadedModules.load(List.of(discovered), Map.of());
var loaded = LoadedModules.load(List.of(discovered), Map.of(), Set.of());
assertEquals("first:core", discovered.info().id().toString());
assertEquals(List.of("first:core/mark"),
@@ -12,6 +12,7 @@ import java.io.DataOutput;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.*;
class ModuleRegistryTest {
@@ -27,15 +28,26 @@ class ModuleRegistryTest {
@Feature(id = "test:abstract", name = "Abstract", field = "abstract", targets = FeatureAttachment.Target.BIOME)
abstract static class AbstractFeature implements IrisFeature {}
/** Runs {@code registrations} as the register(...) of a built-in module with the given id. */
private static void registerAs(ModuleRegistry registry, String moduleId, Consumer<FeatureRegistry> registrations) {
registry.register(DiscoveredModule.builtIn(new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of(moduleId, "Test module " + moduleId, "1"); }
public void install(StageGraph graph) {}
public void register(FeatureRegistry registry) { registrations.accept(registry); }
}));
}
@Test
void validatesIdsTypesAndFieldsAcrossFeatureKindsWithoutPartialRegistration() {
var registry = new ModuleRegistry();
registry.registerSettings(One.class);
registry.registerSettings(One.class);
assertThrows(IllegalArgumentException.class, () -> registry.registerObject(Collision.class, ignored -> context -> null));
registry.registerObject(Two.class, ignored -> context -> null);
assertThrows(IllegalArgumentException.class, () -> registry.registerObject(Duplicate.class, ignored -> context -> null));
assertThrows(IllegalArgumentException.class, () -> registry.registerObject(One.class, ignored -> context -> null));
registerAs(registry, "test:objects", objects -> {
assertThrows(IllegalArgumentException.class, () -> objects.registerObject(Collision.class, ignored -> context -> null));
objects.registerObject(Two.class, ignored -> context -> null);
assertThrows(IllegalArgumentException.class, () -> objects.registerObject(Duplicate.class, ignored -> context -> null));
assertThrows(IllegalArgumentException.class, () -> objects.registerObject(One.class, ignored -> context -> null));
});
var catalog = registry.freeze();
assertEquals(List.of("test:one"), catalog.settings().stream().map(value -> value.id().toString()).toList());
assertEquals(List.of("test:two"), catalog.objects().stream().map(value -> value.descriptor().id().toString()).toList());
@@ -47,8 +59,10 @@ class ModuleRegistryTest {
assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(One.class));
assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(MissingAnnotation.class));
assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(AbstractFeature.class));
assertThrows(NullPointerException.class, () -> registry.registerObject(Two.class, null));
registry.registerObject(Two.class, ignored -> context -> null);
registerAs(registry, "test:objects", objects -> {
assertThrows(NullPointerException.class, () -> objects.registerObject(Two.class, null));
objects.registerObject(Two.class, ignored -> context -> null);
});
assertSame(registry.freeze(), registry.freeze());
assertThrows(IllegalStateException.class, () -> registry.registerSettings(One.class));
}
@@ -56,15 +70,36 @@ class ModuleRegistryTest {
@Test
void mantleIdsAreUniqueAndCodecVersionIsSnapshotted() {
var registry = new ModuleRegistry();
var key = new MantleKey<>("test:owners", String.class);
var key = new MantleKey<>("test:layer", String.class);
var codec = codec(3);
registry.registerMantle(key, MantleLifetime.GENERATION, codec);
assertThrows(IllegalArgumentException.class, () -> registry.registerMantle(key, MantleLifetime.PERSISTENT, codec));
assertThrows(IllegalArgumentException.class, () -> registry.registerMantle(new MantleKey<>("test:bad", String.class), MantleLifetime.GENERATION, codec(-1)));
registerAs(registry, "test:layers", layers -> {
layers.registerMantle(key, MantleLifetime.GENERATION, codec);
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(key, MantleLifetime.PERSISTENT, codec));
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(new MantleKey<>("test:bad", String.class), MantleLifetime.GENERATION, codec(-1)));
});
assertEquals(3, registry.freeze().mantle().getFirst().version());
assertEquals(1, registry.freeze().mantle().size());
}
@Test
void objectFeatureIdsAndMantleKeysUseTheRegisteringModulesNamespace() {
var registry = new ModuleRegistry();
var object = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "other:objects",
objects -> objects.registerObject(Two.class, ignored -> context -> null)));
assertEquals("Object generator settings " + Two.class.getName() + " with feature id test:two must use namespace "
+ "'other' of its registering module other:objects from host classpath", object.getMessage());
var mantle = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "other:layers",
layers -> layers.registerMantle(new MantleKey<>("iris:depth", String.class), MantleLifetime.GENERATION, codec(1))));
assertEquals("Mantle layer iris:depth must use namespace 'other' of its registering module other:layers from host classpath",
mantle.getMessage());
assertThrows(IllegalStateException.class, () -> registry.registerMantle(new MantleKey<>("test:loose", String.class),
MantleLifetime.GENERATION, codec(1)));
assertTrue(registry.freeze().objects().isEmpty());
assertTrue(registry.freeze().mantle().isEmpty());
}
@Test
void loadingModulesCatalogsStageSettingsAndRegistrationsTogether() {
var conditioned = new IrisModule() {
@@ -73,14 +108,14 @@ class ModuleRegistryTest {
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); }
};
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of());
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of());
assertEquals(List.of("test:one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
assertEquals(1, loaded.catalog().objects().size());
assertEquals(1, loaded.placement().stages().size());
assertTrue(loaded.chunk().stages().isEmpty());
var reserved = Map.of(FeatureAttachment.Target.BIOME, Set.of("shared"));
assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), reserved));
assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), reserved, Set.of()));
}
private static MantleCodec<String> codec(int version) {
@@ -13,6 +13,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.volmit.support.TestModuleJar.module;
import static org.junit.jupiter.api.Assertions.*;
@@ -31,6 +32,8 @@ class PackModulesTest {
private static final String REQUIRES_BUILT_IN_STAGE =
"graph.define(Stage.placement(\"user:core/carved\")).requires(example.builtin.CarveModule.CLEAR).run(context -> {});";
private static final Set<String> RESERVED_NAMESPACES = Set.of("iris");
@TempDir
Path pack;
@@ -41,7 +44,7 @@ class PackModulesTest {
private static LoadedModules loadWithBuiltIns(PackModules packModules) {
var modules = new ArrayList<>(PlacementModules.builtIns());
modules.addAll(packModules.modules());
return LoadedModules.load(modules, Map.of());
return LoadedModules.load(modules, Map.of(), Set.of());
}
/** Versioned entries of a multi-release jar are read like any class, so they must be real class files. */
@@ -340,7 +343,7 @@ class PackModulesTest {
try (var packModules = PackModules.load(pack)) {
assertEquals(List.of(new MissingClasses(origin("user.jar"), List.of(OPTIONAL_CLASS))), packModules.missingClasses());
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of()));
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of()));
}
}
@@ -474,7 +477,7 @@ class PackModulesTest {
.writeInto(pack, "beta.jar");
try (var packModules = PackModules.load(pack)) {
var loaded = LoadedModules.load(packModules.modules(), Map.of());
var loaded = LoadedModules.load(packModules.modules(), Map.of(), Set.of());
assertTrue(loaded.placement().stages().stream().anyMatch(stage -> stage.stage().id().toString().equals("beta:core/size")));
var beta = packModules.modules().getLast().module().getClass();
assertEquals(0, beta.getDeclaredField("SHARED").getAnnotations().length,
@@ -491,7 +494,7 @@ class PackModulesTest {
try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
modules.addAll(packModules.modules());
var order = LoadedModules.load(modules, Map.of()).placement().stages().stream()
var order = LoadedModules.load(modules, Map.of(), Set.of()).placement().stages().stream()
.map(stage -> stage.stage().id().toString()).toList();
assertEquals(List.of("iris:carve/clear", "user:core/carved"), order);
}
@@ -569,7 +572,7 @@ class PackModulesTest {
.writeInto(pack, "once.jar");
try (var packModules = PackModules.load(pack)) {
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of()));
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of()));
assertEquals("once:core", packModules.modules().getFirst().info().id().toString());
assertEquals(1, loaded.placement().stages().size());
}
@@ -588,7 +591,7 @@ class PackModulesTest {
.writeInto(pack, "beta.jar");
try (var packModules = PackModules.load(pack)) {
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of())),
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of())),
"Module beta:core from " + origin("beta.jar") + " failed to link or initialize a class while installing",
"java.lang.NoSuchMethodError", "pack.lib.Lib.added()");
}
@@ -600,7 +603,7 @@ class PackModulesTest {
module("pack.two.Duplicate", "dup:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "two.jar");
try (var packModules = PackModules.load(pack)) {
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of())),
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of())),
"Duplicate module dup:core from " + origin("two.jar") + "; already loaded from " + origin("one.jar"));
}
}
@@ -616,14 +619,48 @@ class PackModulesTest {
}
@Test
void foreignStageNamespaceFailsNamingTheJar() {
void stageOutsideTheModuleIdFailsNamingTheJar() {
module("pack.Foreign", "mine:core", NO_MEMBERS,
"graph.define(Stage.placement(\"theirs:core/work\")).run(context -> {});")
"graph.define(Stage.placement(\"mine:other/work\")).run(context -> {});")
.writeInto(pack, "foreign.jar");
try (var packModules = PackModules.load(pack)) {
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of())),
"must use namespace 'mine' of its defining module mine:core from " + origin("foreign.jar"));
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of())),
"Stage mine:other/work must start with mine:core/, the id of its defining module mine:core from " + origin("foreign.jar"));
}
}
@Test
void packModuleInAReservedNamespaceFailsNamingTheJarAndTheNamespace() {
module("pack.Stilt", "iris:stilt", NO_MEMBERS,
"graph.define(Stage.placement(\"iris:stilt/fill\")).run(context -> {});")
.writeInto(pack, "stilt.jar");
try (var packModules = PackModules.load(pack)) {
assertEquals("Module iris:stilt from " + origin("stilt.jar") + " uses reserved namespace 'iris', which only "
+ "modules that ship with the host may use; give the module a namespace of its own, which its stages, "
+ "settings ids and mantle keys then use too",
failure(() -> LoadedModules.load(packModules.modules(), Map.of(), RESERVED_NAMESPACES)));
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of()));
}
}
@Test
void builtInModulesMayUseReservedNamespaces() {
module("pack.user.User", "user:core", NO_MEMBERS, REQUIRES_BUILT_IN_STAGE)
.compileAgainstHostOf(CarveModule.class)
.writeInto(pack, "user.jar");
try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
modules.addAll(packModules.modules());
assertDoesNotThrow(() -> LoadedModules.load(modules, Map.of(), RESERVED_NAMESPACES));
}
}
@Test
void invalidReservedNamespaceFails() {
assertEquals("Reserved namespace 'iris:core' is not a namespace; name one such as iris, without ':' or a path",
failure(() -> LoadedModules.load(List.of(), Map.of(), Set.of("iris:core"))));
}
}
@@ -52,6 +52,12 @@ class StageGraphTest {
return builder.resolve(pipeline);
}
private static List<Class<? extends IrisFeature>> resolveSettings(IrisModule... modules) {
var builder = new StageGraphBuilder();
for (var module : modules) builder.install(DiscoveredModule.builtIn(module));
return builder.settingsTypes();
}
private static List<String> order(ResolvedGraph graph) {
return graph.stages().stream().map(stage -> stage.stage().id().toString()).toList();
}
@@ -77,24 +83,24 @@ class StageGraphTest {
@Test
void adjacentBlockStagesShareOnePassAndRunPerBlockInOrder() {
var first = Stage.placementBlock("test:first");
var second = Stage.placementBlock("test:second");
var first = Stage.placementBlock("test:blocks/first");
var second = Stage.placementBlock("test:blocks/second");
var events = new ArrayList<String>();
var graph = resolve(Pipeline.PLACEMENT, module("test:blocks", stages -> {
stages.define(second).requires(first).run(context -> events.add("second@" + context.block().position().x()));
stages.define(first).run(context -> events.add("first@" + context.block().position().x()));
}));
assertEquals(List.of(List.of("test:first", "test:second")), passes(graph));
assertEquals(List.of(List.of("test:blocks/first", "test:blocks/second")), passes(graph));
assertTrue(place(graph, flatEdit(), ConfiguredSettings.none()).isPlaced());
assertEquals(List.of("first@0", "second@0", "first@1", "second@1"), events);
}
@Test
void requiredInvocationStageBetweenBlockStagesSplitsThePass() {
var collect = Stage.placementBlock("test:collect", Counter.class);
var middle = Stage.placement("test:middle", Integer.class);
var paint = Stage.placementBlock("test:paint");
var collect = Stage.placementBlock("test:split/collect", Counter.class);
var middle = Stage.placement("test:split/middle", Integer.class);
var paint = Stage.placementBlock("test:split/paint");
var events = new ArrayList<String>();
var graph = resolve(Pipeline.PLACEMENT, module("test:split", stages -> {
stages.define(collect).accumulate(Counter::new, (context, counter) -> {
@@ -108,7 +114,7 @@ class StageGraphTest {
stages.define(paint).requires(middle).run(context -> events.add("paint:" + context.output(middle)));
}));
assertEquals(List.of(List.of("test:collect"), List.of("test:paint")), passes(graph));
assertEquals(List.of(List.of("test:split/collect"), List.of("test:split/paint")), passes(graph));
assertTrue(place(graph, flatEdit(), ConfiguredSettings.none()).isPlaced());
assertEquals(List.of("collect", "collect", "middle", "paint:2", "paint:2"), events);
}
@@ -116,71 +122,71 @@ class StageGraphTest {
@Test
void unrelatedInvocationStagesRunOutsideBlockPassesSoTheyDoNotSplitThem() {
var graph = resolve(Pipeline.PLACEMENT, module("test:fusion", stages -> {
stages.define(Stage.placementBlock("test:a")).run(context -> {});
stages.define(Stage.placement("test:b")).run(context -> {});
stages.define(Stage.placementBlock("test:c")).run(context -> {});
stages.define(Stage.placementBlock("test:fusion/a")).run(context -> {});
stages.define(Stage.placement("test:fusion/b")).run(context -> {});
stages.define(Stage.placementBlock("test:fusion/c")).run(context -> {});
}));
assertEquals(List.of("test:b", "test:a", "test:c"), order(graph));
assertEquals(List.of(List.of("test:a", "test:c")), passes(graph));
assertEquals(List.of("test:fusion/b", "test:fusion/a", "test:fusion/c"), order(graph));
assertEquals(List.of(List.of("test:fusion/a", "test:fusion/c")), passes(graph));
}
@Test
void unorderedStagesFollowLexicalIdsRegardlessOfInstallOrder() {
var zebra = module("zeta:module", stages -> stages.define(Stage.placement("zeta:work")).run(context -> {}));
var alpha = module("alpha:module", stages -> stages.define(Stage.placement("alpha:work")).run(context -> {}));
var zebra = module("zeta:module", stages -> stages.define(Stage.placement("zeta:module/work")).run(context -> {}));
var alpha = module("alpha:module", stages -> stages.define(Stage.placement("alpha:module/work")).run(context -> {}));
var forward = resolve(Pipeline.PLACEMENT, zebra, alpha);
var reversed = resolve(Pipeline.PLACEMENT, alpha, zebra);
assertEquals(List.of("alpha:work", "zeta:work"), order(forward));
assertEquals(List.of("alpha:module/work", "zeta:module/work"), order(forward));
assertEquals(forward.describe(), reversed.describe());
assertEquals(List.of(new ResolvedGraph.UnorderedPair(ResourceId.parse("alpha:work"), ResourceId.parse("zeta:work"))),
assertEquals(List.of(new ResolvedGraph.UnorderedPair(ResourceId.parse("alpha:module/work"), ResourceId.parse("zeta:module/work"))),
forward.unorderedPairs());
}
@Test
void softEdgesOrderOnlyInstalledStagesAndReportAbsentTargets() {
var late = Stage.placement("test:late");
var early = Stage.placement("test:early");
var late = Stage.placement("test:soft/late");
var early = Stage.placement("test:soft/early");
var graph = resolve(Pipeline.PLACEMENT, module("test:soft", stages -> {
stages.define(early).softAfter(StageRef.of("test:late")).run(context -> {});
stages.define(late).softBefore(StageRef.of("other:missing")).run(context -> {});
stages.define(early).softAfter(StageRef.of("test:soft/late")).run(context -> {});
stages.define(late).softBefore(StageRef.of("other:gone/missing")).run(context -> {});
}));
assertEquals(List.of("test:late", "test:early"), order(graph));
assertEquals(List.of("test:soft/late", "test:soft/early"), order(graph));
var earlyStage = graph.stages().get(1);
assertEquals(List.of(new StageEdge(late.id(), early.id(), StageEdge.Kind.SOFT)), earlyStage.incoming());
assertEquals(List.of(StageRef.of("other:missing")), graph.stages().get(0).absentSoftTargets());
assertTrue(graph.describe().contains("absent soft targets other:missing"));
assertEquals(List.of(StageRef.of("other:gone/missing")), graph.stages().get(0).absentSoftTargets());
assertTrue(graph.describe().contains("absent soft targets other:gone/missing"));
assertTrue(graph.unorderedPairs().isEmpty());
}
@Test
void missingRequirementNamesBothStagesAndTheirModules() {
var absent = Stage.placement("other:absent");
var absent = Stage.placement("other:gone/absent");
var error = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:needy", stages -> stages.define(Stage.placement("test:needy/work")).requires(absent).run(context -> {}))));
assertEquals("Stage test:needy/work (module test:needy) requires other:absent, which no installed module "
assertEquals("Stage test:needy/work (module test:needy) requires other:gone/absent, which no installed module "
+ "defines (expected a module in namespace 'other')", error.getMessage());
}
@Test
void requirementMustBeTheOwnersDeclarationNotACopyWithTheSameId() {
var owned = Stage.placement("test:owned");
var copy = Stage.placement("test:owned");
var owned = Stage.placement("test:owner/owned");
var copy = Stage.placement("test:owner/owned");
var error = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:owner", stages -> stages.define(owned).run(context -> {})),
module("test:user", stages -> stages.define(Stage.placement("test:user/work")).requires(copy).run(context -> {}))));
assertTrue(error.getMessage().contains("different declaration of test:owned (module test:owner)"), error.getMessage());
assertTrue(error.getMessage().contains("different declaration of test:owner/owned (module test:owner)"), error.getMessage());
}
@Test
void cyclesListEveryStageWithItsModuleAndEdgeKind() {
var a = Stage.placement("test:a");
var b = Stage.placement("other:b");
var c = Stage.placement("test:c");
var a = Stage.placement("test:first/a");
var b = Stage.placement("other:second/b");
var c = Stage.placement("test:first/c");
var error = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:first", stages -> {
stages.define(a).requires(b).run(context -> {});
@@ -188,21 +194,30 @@ class StageGraphTest {
}),
module("other:second", stages -> stages.define(b).softAfter(c.ref()).run(context -> {}))));
assertEquals("Stage cycle in pipeline PLACEMENT: other:b (module other:second) -[required]-> "
+ "test:a (module test:first) -[required]-> test:c (module test:first) -[soft]-> "
+ "other:b (module other:second)", error.getMessage());
assertEquals("Stage cycle in pipeline PLACEMENT: other:second/b (module other:second) -[required]-> "
+ "test:first/a (module test:first) -[required]-> test:first/c (module test:first) -[soft]-> "
+ "other:second/b (module other:second)", error.getMessage());
}
@Test
void stageNamespaceMustMatchItsModuleAndDefinitionsAreUnique() {
void stageIdsMustStartWithTheirModuleIdAndDefinitionsAreUnique() {
var foreign = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:module", stages -> stages.define(Stage.placement("iris:stolen")).run(context -> {}))));
assertTrue(foreign.getMessage().contains("must use namespace 'test'"), foreign.getMessage());
module("test:module", stages -> stages.define(Stage.placement("iris:stolen/work")).run(context -> {}))));
assertEquals("Stage iris:stolen/work must start with test:module/, the id of its defining module test:module "
+ "from host classpath", foreign.getMessage());
var shared = Stage.placement("test:shared");
for (var outside : List.of("test:other/work", "test:module", "test:modules/work")) {
var error = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:module", stages -> stages.define(Stage.placement(outside)).run(context -> {}))));
assertTrue(error.getMessage().contains("must start with test:module/"), error.getMessage());
}
var shared = Stage.placement("test:one/shared");
var duplicate = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:one", stages -> stages.define(shared).run(context -> {})),
module("test:two", stages -> stages.define(shared).run(context -> {}))));
module("test:one", stages -> {
stages.define(shared).run(context -> {});
stages.define(shared).run(context -> {});
})));
assertTrue(duplicate.getMessage().contains("already defined by module test:one"), duplicate.getMessage());
var module = module("test:twice", stages -> {});
@@ -211,16 +226,30 @@ class StageGraphTest {
assertThrows(IllegalArgumentException.class, () -> builder.install(DiscoveredModule.builtIn(module)));
}
@Test
void settingsMustBeOwnedByAModuleOfTheirNamespace() {
var foreign = assertThrows(IllegalArgumentException.class, () -> resolveSettings(
module("other:user", stages -> stages.define(Stage.placement("other:user/work")).when(Toggle.class).run((context, toggle) -> {}))));
assertEquals("Settings type " + Toggle.class.getName() + " has feature id test:toggle, but no module of namespace 'test' "
+ "names it in when(...); it is named by other:user from host classpath. A feature id uses the namespace "
+ "of the module that owns the settings", foreign.getMessage());
var shared = resolveSettings(
module("test:owner", stages -> stages.define(Stage.placement("test:owner/work")).when(Toggle.class).run((context, toggle) -> {})),
module("other:user", stages -> stages.define(Stage.placement("other:user/work")).when(Toggle.class).run((context, toggle) -> {})));
assertEquals(List.of(Toggle.class), shared);
}
@Test
void dependenciesCannotCrossPipelines() {
var placementStage = Stage.placement("test:placed");
ChunkStage<Void> chunkStage = Stage.chunk("test:chunk");
var placementStage = Stage.placement("test:hard/placed");
ChunkStage<Void> chunkStage = Stage.chunk("test:hard/chunk");
var hard = module("test:hard", stages -> {
stages.define(placementStage).run(context -> {});
stages.define(chunkStage).requires(placementStage).run(context -> {});
});
var error = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.CHUNK, hard));
assertTrue(error.getMessage().contains("in pipeline CHUNK depends on test:placed (module test:hard) in pipeline PLACEMENT"),
assertTrue(error.getMessage().contains("in pipeline CHUNK depends on test:hard/placed (module test:hard) in pipeline PLACEMENT"),
error.getMessage());
var soft = module("test:soft", stages -> {
@@ -232,21 +261,21 @@ class StageGraphTest {
@Test
void definitionsNeedExactlyOneBodyOfTheRightKind() {
var withOutput = Stage.placement("test:output", Integer.class);
var withoutOutput = Stage.placement("test:plain");
var accumulating = Stage.placementBlock("test:acc", Counter.class);
var withOutput = Stage.placement("test:bodies/output", Integer.class);
var withoutOutput = Stage.placement("test:bodies/plain");
var accumulating = Stage.placementBlock("test:bodies/acc", Counter.class);
var run = assertThrows(IllegalStateException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:run", stages -> stages.define(withOutput).run(context -> {}))));
module("test:bodies", stages -> stages.define(withOutput).run(context -> {}))));
assertTrue(run.getMessage().contains("declares output Integer; define it with produce(...)"), run.getMessage());
assertThrows(IllegalStateException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:produce", stages -> stages.define(withoutOutput).produce(context -> null))));
module("test:bodies", stages -> stages.define(withoutOutput).produce(context -> null))));
assertThrows(IllegalStateException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:accrun", stages -> stages.define(accumulating).run(context -> {}))));
module("test:bodies", stages -> stages.define(accumulating).run(context -> {}))));
var missing = assertThrows(IllegalStateException.class, () -> resolve(Pipeline.PLACEMENT,
module("test:missing", stages -> stages.define(withoutOutput).requires())));
module("test:bodies", stages -> stages.define(withoutOutput).requires())));
assertTrue(missing.getMessage().contains("has no body"), missing.getMessage());
assertThrows(IllegalStateException.class, () -> resolve(Pipeline.PLACEMENT, module("test:again", stages -> {
assertThrows(IllegalStateException.class, () -> resolve(Pipeline.PLACEMENT, module("test:bodies", stages -> {
var definition = stages.define(withoutOutput);
definition.run(context -> {});
definition.requires(withOutput);
@@ -274,9 +303,9 @@ class StageGraphTest {
@Test
void outputsAreReadableOnlyThroughTransitiveRequirementsAndAfterTheirPass() {
var height = Stage.placement("test:height", Integer.class);
var offset = Stage.placement("test:offset", Integer.class);
var counted = Stage.placementBlock("test:counted", Counter.class);
var height = Stage.placement("test:outputs/height", Integer.class);
var offset = Stage.placement("test:outputs/offset", Integer.class);
var counted = Stage.placementBlock("test:outputs/counted", Counter.class);
var observed = new ArrayList<Object>();
var graph = resolve(Pipeline.PLACEMENT, module("test:outputs", stages -> {
stages.define(height).produce(context -> 10);
@@ -285,10 +314,10 @@ class StageGraphTest {
counter.count++;
observed.add(context.output(height));
});
stages.define(Stage.placementBlock("test:peek")).requires(counted).run(context -> observed.add(
stages.define(Stage.placementBlock("test:outputs/peek")).requires(counted).run(context -> observed.add(
assertThrows(IllegalStateException.class, () -> context.output(counted)).getMessage()));
stages.define(Stage.placement("test:reader")).requires(counted).run(context -> observed.add(context.output(counted).count));
stages.define(Stage.placement("test:snoop")).run(context -> observed.add(
stages.define(Stage.placement("test:outputs/reader")).requires(counted).run(context -> observed.add(context.output(counted).count));
stages.define(Stage.placement("test:outputs/snoop")).run(context -> observed.add(
assertThrows(IllegalStateException.class, () -> context.output(height)).getMessage()));
}));
@@ -296,8 +325,8 @@ class StageGraphTest {
assertEquals(10, observed.get(1));
assertTrue(observed.get(2).toString().contains("before it completed"), observed.get(2).toString());
assertTrue(observed.contains(2));
assertTrue(observed.contains("Stage test:snoop read the output of test:height without requiring it; "
+ "add requires(test:height) to its definition"));
assertTrue(observed.contains("Stage test:outputs/snoop read the output of test:outputs/height without requiring it; "
+ "add requires(test:outputs/height) to its definition"));
}
@Test
@@ -312,9 +341,9 @@ class StageGraphTest {
@Test
void conditionalStagesCascadeToDependentsWithoutAnyError() {
var toggled = Stage.placement("test:toggled", Integer.class);
var dependent = Stage.placement("test:dependent");
var unrelated = Stage.placement("test:unrelated");
var toggled = Stage.placement("test:cascade/toggled", Integer.class);
var dependent = Stage.placement("test:cascade/dependent");
var unrelated = Stage.placement("test:cascade/unrelated");
var events = new ArrayList<String>();
var graph = resolve(Pipeline.PLACEMENT, module("test:cascade", stages -> {
stages.define(toggled).when(Toggle.class).produce((context, toggle) -> {
@@ -336,11 +365,11 @@ class StageGraphTest {
void skippedBlocksAreHiddenFromLaterStagesOfThePass() {
var written = new ArrayList<Integer>();
var graph = resolve(Pipeline.PLACEMENT, module("test:skip", stages -> {
var filter = Stage.placementBlock("test:filter");
var filter = Stage.placementBlock("test:skip/filter");
stages.define(filter).run(context -> {
if (context.block().position().x() == 0) context.skip();
});
stages.define(Stage.placementBlock("test:write")).requires(filter).run(context -> written.add(context.block().position().x()));
stages.define(Stage.placementBlock("test:skip/write")).requires(filter).run(context -> written.add(context.block().position().x()));
}));
place(graph, flatEdit(), ConfiguredSettings.none());
@@ -351,14 +380,14 @@ class StageGraphTest {
void rejectionStopsTheAttemptAndCommitsNothing() {
var events = new ArrayList<String>();
var graph = resolve(Pipeline.PLACEMENT, module("test:reject", stages -> {
var write = Stage.placementBlock("test:write");
var write = Stage.placementBlock("test:reject/write");
stages.define(write).run(context -> {
events.add("write");
context.access().set(new PlacedBlock(new BlockPosition(0, 10, 0), STONE, Optional.empty()));
context.reject("blocked");
context.reject("ignored");
});
stages.define(Stage.placement("test:after")).requires(write).run(context -> events.add("after"));
stages.define(Stage.placement("test:reject/after")).requires(write).run(context -> events.add("after"));
}));
var edit = flatEdit();