mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
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:
1 parent
6263075ecd
commit
4cb718f706
13 files changed
+413
-129
No files matched your search
@@ -7,7 +7,8 @@ import com.volmit.object.ObjectGeneratorFactory;
|
||||
|
||||
/** Host implementation collects non-stage registrations before loading pack configuration.
|
||||
* Feature IDs must be unique. JSON fields must be unique per attachment target, including the
|
||||
* settings types stages name in {@code when(...)}.
|
||||
* settings types stages name in {@code when(...)}. Feature IDs and mantle keys a module registers
|
||||
* use the namespace of its module id.
|
||||
*/
|
||||
public interface FeatureRegistry {
|
||||
/** Registers a layer before pack loading. Null arguments throw NullPointerException;
|
||||
|
||||
@@ -5,7 +5,10 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Module identity. Stages a module defines must use the namespace of its id. The name is for
|
||||
/** Module identity. Every stage a module defines has an id that starts with the module id and
|
||||
* {@code /}, such as {@code iris:stilt/fill} for module {@code iris:stilt}, so a module id has no
|
||||
* {@code /} of its own: {@code iris:stilt/extra} would own stages under {@code iris:stilt}'s path.
|
||||
* Settings ids and mantle keys the module registers use its namespace. The name is for
|
||||
* people, for example in load logs; the id is what the host and other modules match on. Name and
|
||||
* version must fit on one log line, so they reject control characters such as line breaks.
|
||||
*
|
||||
@@ -15,10 +18,13 @@ import java.util.Objects;
|
||||
* hides from modules or one missing from a package Iris exposes; those still fail the load.
|
||||
*/
|
||||
public record ModuleInfo(ResourceId id, String name, String version, List<PackageName> optionalPackages) {
|
||||
private static final String STAGE_SEPARATOR = "/";
|
||||
|
||||
public ModuleInfo {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(version, "version");
|
||||
requireSingleSegment(id);
|
||||
requireOneLineText(id, "name", name);
|
||||
requireOneLineText(id, "version", version);
|
||||
optionalPackages = List.copyOf(optionalPackages);
|
||||
@@ -43,6 +49,27 @@ public record ModuleInfo(ResourceId id, String name, String version, List<Packag
|
||||
+ " which covers that package and its subpackages"));
|
||||
}
|
||||
|
||||
/** Whether {@code stageId} may name a stage of this module: it starts with this module's id and '/'. */
|
||||
public boolean ownsStage(ResourceId stageId) {
|
||||
return stageId.namespace().equals(id.namespace()) && stageId.path().startsWith(stagePrefix());
|
||||
}
|
||||
|
||||
/** What every stage id of this module starts with, such as {@code iris:stilt/}. */
|
||||
public String stageIdPrefix() {
|
||||
return id.namespace() + ":" + stagePrefix();
|
||||
}
|
||||
|
||||
private String stagePrefix() {
|
||||
return id.path() + STAGE_SEPARATOR;
|
||||
}
|
||||
|
||||
private static void requireSingleSegment(ResourceId id) {
|
||||
if (!id.path().contains(STAGE_SEPARATOR)) return;
|
||||
throw new IllegalArgumentException("Module id " + id + " must not contain '" + STAGE_SEPARATOR
|
||||
+ "': stage ids are a module id, '" + STAGE_SEPARATOR + "' and a name, so " + id
|
||||
+ " would name stages under another module's id");
|
||||
}
|
||||
|
||||
private static void requireOneLineText(ResourceId id, String field, String value) {
|
||||
if (value.isBlank()) {
|
||||
throw new IllegalArgumentException("Blank module " + field + ": " + id);
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.stage.Pipeline;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -17,10 +18,18 @@ public record LoadedModules(FeatureCatalog catalog, ResolvedGraph placement, Res
|
||||
Objects.requireNonNull(chunk, "chunk");
|
||||
}
|
||||
|
||||
/** Pass the built-in modules together with {@link PackModules#modules()} of the pack. */
|
||||
/** Pass the built-in modules together with {@link PackModules#modules()} of the pack.
|
||||
*
|
||||
* @param reservedHostFields JSON fields the host itself uses, per attachment target
|
||||
* @param reservedNamespaces namespaces only built-in modules may use, such as {@code iris}; a
|
||||
* pack module whose id uses one fails the load
|
||||
*/
|
||||
public static LoadedModules load(
|
||||
List<DiscoveredModule> modules,
|
||||
Map<FeatureAttachment.Target, Set<String>> reservedHostFields) {
|
||||
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
|
||||
Collection<String> reservedNamespaces) {
|
||||
var reserved = ReservedNamespaces.of(reservedNamespaces);
|
||||
modules.forEach(reserved::requireAllowed);
|
||||
var registry = new ModuleRegistry(reservedHostFields);
|
||||
var stages = new StageGraphBuilder();
|
||||
for (var discovered : modules) {
|
||||
@@ -36,7 +45,7 @@ public record LoadedModules(FeatureCatalog catalog, ResolvedGraph placement, Res
|
||||
private static void installModule(DiscoveredModule discovered, StageGraphBuilder stages, ModuleRegistry registry) {
|
||||
try {
|
||||
stages.install(discovered);
|
||||
discovered.module().register(registry);
|
||||
registry.register(discovered);
|
||||
} catch (LinkageError error) {
|
||||
throw new IllegalArgumentException("Module " + discovered.info().id() + " from "
|
||||
+ discovered.origin() + " failed to link or initialize a class while installing: " + error
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.volmit.host;
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.mantle.MantleCodec;
|
||||
import com.volmit.mantle.MantleKey;
|
||||
import com.volmit.mantle.MantleLifetime;
|
||||
@@ -16,9 +17,11 @@ import java.util.Set;
|
||||
|
||||
/** Reference host registry. Single-threaded registration ends with freeze(); the catalog is
|
||||
* immutable. IDs are unique across feature kinds; JSON fields are unique per attachment target.
|
||||
* Pass existing host JSON fields to reserve them before modules register. A failed registration
|
||||
* leaves the registry unchanged. A failed module callback invalidates the host's whole build.
|
||||
* This does not decode JSON, discover modules or instantiate settings.
|
||||
* Pass existing host JSON fields to reserve them before modules register. Modules register through
|
||||
* {@link #register(DiscoveredModule)}; object feature ids and mantle keys must use the registering
|
||||
* module's namespace. A failed registration leaves the registry unchanged. A failed module callback
|
||||
* invalidates the host's whole build. This does not decode JSON, discover modules or instantiate
|
||||
* settings.
|
||||
*/
|
||||
public final class ModuleRegistry implements FeatureRegistry {
|
||||
private final Map<Class<?>, FeatureDescriptor<?>> settings = new HashMap<>();
|
||||
@@ -28,6 +31,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
private final Set<Class<?>> featureTypes = new HashSet<>();
|
||||
private final Map<Field, String> fields = new HashMap<>();
|
||||
private FeatureCatalog catalog;
|
||||
private DiscoveredModule registering;
|
||||
|
||||
public ModuleRegistry() {
|
||||
this(Map.of());
|
||||
@@ -37,12 +41,28 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
reserveHostFields(reservedFields);
|
||||
}
|
||||
|
||||
/** Lets the module register its contributions through {@link IrisModule#register}. */
|
||||
public void register(DiscoveredModule discovered) {
|
||||
Objects.requireNonNull(discovered, "discovered");
|
||||
checkOpen();
|
||||
if (registering != null) {
|
||||
throw new IllegalStateException("Module " + discovered.info().id() + " registered during " + registering.info().id());
|
||||
}
|
||||
registering = discovered;
|
||||
try {
|
||||
discovered.module().register(this);
|
||||
} finally {
|
||||
registering = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <F extends IrisFeature> void registerObject(
|
||||
Class<F> featureType,
|
||||
ObjectGeneratorFactory<F> factory) {
|
||||
checkOpen();
|
||||
var descriptor = FeatureDescriptor.of(featureType);
|
||||
requireModuleNamespace("Object generator settings " + featureType.getName() + " with feature id", descriptor.id());
|
||||
var registration = new ObjectRegistration<>(descriptor, factory);
|
||||
claim(descriptor);
|
||||
objects.put(featureType, registration);
|
||||
@@ -53,6 +73,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
checkOpen();
|
||||
Objects.requireNonNull(key, "key");
|
||||
Objects.requireNonNull(codec, "codec");
|
||||
requireModuleNamespace("Mantle layer", ResourceId.parse(key.id()));
|
||||
|
||||
var registration = new MantleRegistration<>(key, lifetime, codec, codec.version());
|
||||
if (mantle.putIfAbsent(key.id(), registration) != null) {
|
||||
@@ -78,6 +99,16 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
return catalog;
|
||||
}
|
||||
|
||||
private void requireModuleNamespace(String registration, ResourceId id) {
|
||||
if (registering == null) {
|
||||
throw new IllegalStateException(registration + " " + id + " registered outside IrisModule.register");
|
||||
}
|
||||
var namespace = registering.info().id().namespace();
|
||||
if (id.namespace().equals(namespace)) return;
|
||||
throw new IllegalArgumentException(registration + " " + id + " must use namespace '" + namespace
|
||||
+ "' of its registering module " + registering);
|
||||
}
|
||||
|
||||
private void reserveHostFields(Map<FeatureAttachment.Target, Set<String>> reservedFields) {
|
||||
for (var entry : reservedFields.entrySet()) {
|
||||
for (var name : entry.getValue()) {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/** Namespaces that only modules from the host's classpath may use, such as {@code iris}. A pack
|
||||
* module's id must use another namespace. Its stages, settings ids and mantle keys follow its
|
||||
* namespace, so none of them can be in a reserved namespace either.
|
||||
*/
|
||||
final class ReservedNamespaces {
|
||||
private final Set<String> namespaces;
|
||||
|
||||
private ReservedNamespaces(Set<String> namespaces) {
|
||||
this.namespaces = namespaces;
|
||||
}
|
||||
|
||||
/** Fails on anything that is not a namespace, such as a blank string or a full id. */
|
||||
static ReservedNamespaces of(Collection<String> namespaces) {
|
||||
Objects.requireNonNull(namespaces, "reservedNamespaces");
|
||||
var valid = new TreeSet<String>();
|
||||
for (var namespace : namespaces) {
|
||||
valid.add(requireNamespace(namespace));
|
||||
}
|
||||
return new ReservedNamespaces(Set.copyOf(valid));
|
||||
}
|
||||
|
||||
void requireAllowed(DiscoveredModule discovered) {
|
||||
if (discovered.source() == DiscoveredModule.Source.HOST) return;
|
||||
var namespace = discovered.info().id().namespace();
|
||||
if (!namespaces.contains(namespace)) return;
|
||||
throw new IllegalArgumentException("Module " + discovered.info().id() + " from " + discovered.origin()
|
||||
+ " uses reserved namespace '" + namespace + "', 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");
|
||||
}
|
||||
|
||||
private static String requireNamespace(String namespace) {
|
||||
Objects.requireNonNull(namespace, "reserved namespace");
|
||||
if (!ResourceId.isNamespace(namespace)) {
|
||||
throw new IllegalArgumentException("Reserved namespace '" + namespace
|
||||
+ "' is not a namespace; name one such as iris, without ':' or a path");
|
||||
}
|
||||
return namespace;
|
||||
}
|
||||
}
|
||||
@@ -17,27 +17,32 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Reference host collection of module stages. Install every module, then resolve each pipeline
|
||||
* once per pack load. Install order never affects the result. If a module's install throws,
|
||||
* discard the whole builder: it may hold a partial module. Errors about one module name its origin.
|
||||
*
|
||||
* <p>A module defines only stages whose ids start with its own id and '/', and a settings type
|
||||
* named in {@code when(...)} must have a feature id in the namespace of a module that names it.
|
||||
*/
|
||||
public final class StageGraphBuilder implements StageGraph {
|
||||
private final Map<ResourceId, StageDeclaration> declarations = new HashMap<>();
|
||||
private final Map<ResourceId, String> moduleOrigins = new HashMap<>();
|
||||
private ResourceId installingModule;
|
||||
private final Map<ResourceId, DiscoveredModule> modules = new HashMap<>();
|
||||
private DiscoveredModule installing;
|
||||
|
||||
public void install(DiscoveredModule discovered) {
|
||||
Objects.requireNonNull(discovered, "discovered");
|
||||
var moduleId = discovered.info().id();
|
||||
claimModule(moduleId, discovered.origin());
|
||||
installingModule = moduleId;
|
||||
claimModule(discovered);
|
||||
installing = discovered;
|
||||
try {
|
||||
discovered.module().install(this);
|
||||
} finally {
|
||||
installingModule = null;
|
||||
installing = null;
|
||||
}
|
||||
requireCompleteDefinitions(moduleId);
|
||||
requireCompleteDefinitions(discovered.info().id());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,32 +65,66 @@ public final class StageGraphBuilder implements StageGraph {
|
||||
return GraphResolver.resolve(pipeline, declarations.values());
|
||||
}
|
||||
|
||||
/** Settings types named by when(...), ordered by feature id. */
|
||||
/** Settings types named by when(...), ordered by feature id. Fails if a type's feature id is not
|
||||
* in the namespace of any module that names it: some module of that namespace must own it.
|
||||
*/
|
||||
public List<Class<? extends IrisFeature>> settingsTypes() {
|
||||
return declarations.values().stream()
|
||||
.flatMap(declaration -> declaration.settings().stream())
|
||||
.distinct()
|
||||
.sorted(Comparator.comparing(FeatureDescriptor::id))
|
||||
.<Class<? extends IrisFeature>>map(FeatureDescriptor::type)
|
||||
var uses = settingsUses();
|
||||
uses.forEach(this::requireSettingsOwner);
|
||||
return uses.stream()
|
||||
.<Class<? extends IrisFeature>>map(use -> use.settings().type())
|
||||
.toList();
|
||||
}
|
||||
|
||||
private void claimModule(ResourceId moduleId, String origin) {
|
||||
if (installingModule != null) {
|
||||
throw new IllegalStateException("Module " + moduleId + " installed during " + installingModule);
|
||||
private void claimModule(DiscoveredModule discovered) {
|
||||
var moduleId = discovered.info().id();
|
||||
if (installing != null) {
|
||||
throw new IllegalStateException("Module " + moduleId + " installed during " + installing.info().id());
|
||||
}
|
||||
var previous = moduleOrigins.putIfAbsent(moduleId, origin);
|
||||
var previous = modules.putIfAbsent(moduleId, discovered);
|
||||
if (previous != null) {
|
||||
throw new IllegalArgumentException("Duplicate module " + moduleId + " from " + origin
|
||||
+ "; already loaded from " + previous);
|
||||
throw new IllegalArgumentException("Duplicate module " + moduleId + " from " + discovered.origin()
|
||||
+ "; already loaded from " + previous.origin());
|
||||
}
|
||||
}
|
||||
|
||||
/** Each settings type named by when(...), ordered by feature id, with the modules naming it. */
|
||||
private List<SettingsUse> settingsUses() {
|
||||
var uses = new HashMap<Class<?>, SettingsUse>();
|
||||
for (var declaration : declarations.values()) {
|
||||
declaration.settings().ifPresent(settings -> uses
|
||||
.computeIfAbsent(settings.type(), ignored -> new SettingsUse(settings, new TreeSet<>()))
|
||||
.namers().add(declaration.module()));
|
||||
}
|
||||
return uses.values().stream()
|
||||
.sorted(Comparator.comparing((SettingsUse use) -> use.settings().id())
|
||||
.thenComparing(use -> use.settings().type().getName()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private void requireSettingsOwner(SettingsUse use) {
|
||||
var settings = use.settings();
|
||||
var namers = use.namers();
|
||||
var namespace = settings.id().namespace();
|
||||
if (namers.stream().anyMatch(module -> module.namespace().equals(namespace))) return;
|
||||
throw new IllegalArgumentException("Settings type " + settings.type().getName() + " has feature id "
|
||||
+ settings.id() + ", but no module of namespace '" + namespace + "' names it in when(...); it is named by "
|
||||
+ describeModules(namers) + ". A feature id uses the namespace of the module that owns the settings");
|
||||
}
|
||||
|
||||
private String describeModules(SortedSet<ResourceId> moduleIds) {
|
||||
return moduleIds.stream()
|
||||
.map(modules::get)
|
||||
.map(DiscoveredModule::toString)
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
|
||||
private StageDeclaration declare(Stage<?> stage) {
|
||||
Objects.requireNonNull(stage, "stage");
|
||||
var module = requireInstallingModule(stage);
|
||||
requireModuleNamespace(stage, module);
|
||||
var discovered = requireInstallingModule(stage);
|
||||
requireOwnedStageId(stage, discovered);
|
||||
|
||||
var module = discovered.info().id();
|
||||
var declaration = new StageDeclaration(stage, module);
|
||||
var previous = declarations.putIfAbsent(stage.id(), declaration);
|
||||
if (previous != null) {
|
||||
@@ -95,18 +134,18 @@ public final class StageGraphBuilder implements StageGraph {
|
||||
return declaration;
|
||||
}
|
||||
|
||||
private ResourceId requireInstallingModule(Stage<?> stage) {
|
||||
if (installingModule == null) {
|
||||
private DiscoveredModule requireInstallingModule(Stage<?> stage) {
|
||||
if (installing == null) {
|
||||
throw new IllegalStateException("Stage " + stage + " defined outside IrisModule.install");
|
||||
}
|
||||
return installingModule;
|
||||
return installing;
|
||||
}
|
||||
|
||||
private void requireModuleNamespace(Stage<?> stage, ResourceId module) {
|
||||
if (!stage.id().namespace().equals(module.namespace())) {
|
||||
throw new IllegalArgumentException("Stage " + stage + " must use namespace '"
|
||||
+ module.namespace() + "' of its defining module " + module + " from " + moduleOrigins.get(module));
|
||||
}
|
||||
private static void requireOwnedStageId(Stage<?> stage, DiscoveredModule discovered) {
|
||||
var info = discovered.info();
|
||||
if (info.ownsStage(stage.id())) return;
|
||||
throw new IllegalArgumentException("Stage " + stage + " must start with " + info.stageIdPrefix()
|
||||
+ ", the id of its defining module " + discovered);
|
||||
}
|
||||
|
||||
private void requireCompleteDefinitions(ResourceId moduleId) {
|
||||
@@ -121,4 +160,6 @@ public final class StageGraphBuilder implements StageGraph {
|
||||
+ " has no body; end its definition with run, produce or accumulate");
|
||||
});
|
||||
}
|
||||
|
||||
private record SettingsUse(FeatureDescriptor<?> settings, SortedSet<ResourceId> namers) {}
|
||||
}
|
||||
@@ -1,17 +1,26 @@
|
||||
package com.volmit.resource;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Case-sensitive namespaced identity. Parsing never supplies an implicit namespace. */
|
||||
public record ResourceId(String namespace, String path) implements Comparable<ResourceId> {
|
||||
private static final Pattern NAMESPACE = Pattern.compile("[a-z0-9_.-]+");
|
||||
private static final Pattern PATH = Pattern.compile("[a-z0-9_./-]+");
|
||||
|
||||
public ResourceId {
|
||||
Objects.requireNonNull(namespace, "namespace");
|
||||
Objects.requireNonNull(path, "path");
|
||||
if (!namespace.matches("[a-z0-9_.-]+") || !path.matches("[a-z0-9_./-]+")) {
|
||||
if (!isNamespace(namespace) || !PATH.matcher(path).matches()) {
|
||||
throw new IllegalArgumentException("Invalid resource ID: " + namespace + ":" + path);
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether {@code value} is a valid namespace on its own, such as {@code iris}. */
|
||||
public static boolean isNamespace(String value) {
|
||||
return NAMESPACE.matcher(value).matches();
|
||||
}
|
||||
|
||||
public static ResourceId parse(String value) {
|
||||
Objects.requireNonNull(value, "value");
|
||||
int separator = value.indexOf(':');
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in new issue
Block a user