-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature(WorkspaceValidationTests): add possible to create workspace-wide tests #4949
base: develop
Are you sure you want to change the base?
Changes from 4 commits
873d9df
3a8c58e
d81d35b
907ea8f
4c635b1
34ef3e7
fb1b63f
55eca21
b71438e
eaee6ba
11a655a
dc4bdb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.workspace.validation; | ||
|
||
import com.google.common.collect.Streams; | ||
import com.google.common.io.ByteStreams; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DynamicContainer; | ||
import org.junit.jupiter.api.DynamicNode; | ||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.TestFactory; | ||
import org.terasology.engine.core.TerasologyConstants; | ||
import org.terasology.engine.core.module.ModuleManager; | ||
import org.terasology.engine.entitySystem.prefab.Prefab; | ||
import org.terasology.engine.logic.behavior.BehaviorComponent; | ||
import org.terasology.engine.logic.behavior.asset.BehaviorTree; | ||
import org.terasology.gestalt.assets.Asset; | ||
import org.terasology.gestalt.assets.AssetData; | ||
import org.terasology.gestalt.assets.ResourceUrn; | ||
import org.terasology.gestalt.assets.management.AssetManager; | ||
import org.terasology.gestalt.module.Module; | ||
import org.terasology.gestalt.module.resources.ModuleFileSource; | ||
import org.terasology.moduletestingenvironment.Engines; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
public class WorkspaceBehaviorsTests { | ||
|
||
@TestFactory | ||
Stream<DynamicNode> behaviours() { | ||
System.setProperty(ModuleManager.LOAD_CLASSPATH_MODULES_PROPERTY, "true"); | ||
ModuleManager temporary = new ModuleManager(""); | ||
return temporary.getRegistry() | ||
.getModuleIds() | ||
.stream() | ||
.filter(name -> { | ||
Module module = temporary.getRegistry().getLatestModuleVersion(name); | ||
ModuleFileSource resources = module.getResources(); | ||
|
||
boolean haveBehaviors = !resources.getFilesInPath(true, "assets/behaviors").isEmpty(); | ||
boolean haveBehaviorOverride = | ||
resources.getFilesInPath(false, "overrides") | ||
.stream() | ||
.anyMatch(fr -> fr.getPath().contains("behaviors")); | ||
boolean haveBehaviorDelta = | ||
resources.getFilesInPath(false, "deltas") | ||
.stream() | ||
.anyMatch(fr -> fr.getPath().contains("behaviors")); | ||
|
||
boolean havePrefab = resources.getFiles().stream() | ||
.filter(fr -> fr.toString().contains("behaviours") && fr.toString().contains(".prefab")) | ||
.anyMatch(fr -> { | ||
try (InputStream inputStream = fr.open()) { | ||
byte[] bytes = ByteStreams.toByteArray(inputStream); | ||
String content = new String(bytes, TerasologyConstants.CHARSET); | ||
return content.contains(BehaviorComponent.class.getSimpleName()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
}); | ||
|
||
return haveBehaviors || haveBehaviorOverride || haveBehaviorDelta || havePrefab; | ||
}) | ||
.map(moduleName -> { | ||
EnginesAccessor engine = new EnginesAccessor(Set.of(moduleName.toString()), null); | ||
AtomicReference<AssetManager> assetManagerRef = new AtomicReference<>(); | ||
return DynamicContainer.dynamicContainer(String.format("Module - %s", moduleName), Streams.concat( | ||
Stream.of(engine).map((e) -> | ||
DynamicTest.dynamicTest("setup", () -> { | ||
e.setup(); | ||
assetManagerRef.set(e.getHostContext().get(AssetManager.class)); | ||
}) | ||
), | ||
Stream.of(DynamicContainer.dynamicContainer("Assets tests", Stream.of( | ||
asset( | ||
assetManagerRef, | ||
BehaviorTree.class, | ||
(b) -> Assertions.assertNotNull(b.getData()) | ||
), | ||
asset( | ||
assetManagerRef, | ||
Prefab.class, | ||
prefab -> prefab.hasComponent(BehaviorComponent.class), | ||
prefab -> Assertions.assertNotNull(prefab.getComponent(BehaviorComponent.class).tree) | ||
|
||
)))), | ||
Stream.of(DynamicTest.dynamicTest("tearDown", engine::tearDown)))); | ||
}); | ||
} | ||
|
||
private <T extends AssetData, A extends Asset<T>> DynamicContainer asset(AtomicReference<AssetManager> assetManager, | ||
Class<A> assetClazz, | ||
Consumer<A> validator) { | ||
return asset(assetManager, assetClazz, (a) -> true, validator); | ||
|
||
} | ||
|
||
private <T extends AssetData, A extends Asset<T>> DynamicContainer asset(AtomicReference<AssetManager> assetManager, | ||
Class<A> assetClazz, | ||
Predicate<A> filter, | ||
Consumer<A> validator) { | ||
|
||
return DynamicContainer.dynamicContainer(String.format("AssetType: %s", assetClazz.getSimpleName()), | ||
DynamicTest.stream( | ||
Stream.generate(() -> assetManager.get().getAvailableAssets(assetClazz).stream()) | ||
.limit(1) | ||
.flatMap(s -> s) | ||
.filter(urn -> filter.test(assetManager.get().getAsset(urn, assetClazz).get())), | ||
ResourceUrn::toString, | ||
urn -> { | ||
A asset = (A) assetManager.get().getAsset(urn, assetClazz).get(); | ||
validator.accept(asset); | ||
} | ||
)); | ||
|
||
} | ||
|
||
public static class EnginesAccessor extends Engines { | ||
|
||
public EnginesAccessor(Set<String> dependencies, String worldGeneratorUri) { | ||
super(dependencies, worldGeneratorUri); | ||
} | ||
|
||
@Override | ||
protected void setup() { | ||
super.setup(); | ||
} | ||
|
||
@Override | ||
protected void tearDown() { | ||
super.tearDown(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.workspace.validation; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.terasology.engine.core.module.ModuleManager; | ||
import org.terasology.gestalt.naming.Name; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Example test for check modules dependency validations. | ||
*/ | ||
public class WorkspaceModulesResolvingTests { | ||
|
||
public static Stream<Arguments> modulesAndModuleManager() { | ||
System.setProperty(ModuleManager.LOAD_CLASSPATH_MODULES_PROPERTY, "true"); | ||
ModuleManager temporary = new ModuleManager(""); | ||
return temporary.getRegistry() | ||
.getModuleIds() | ||
.stream() | ||
.map((name) -> Arguments.of(temporary, name)); | ||
} | ||
|
||
public static Stream<Arguments> modulePairsAndModuleManager() { | ||
System.setProperty(ModuleManager.LOAD_CLASSPATH_MODULES_PROPERTY, "true"); | ||
ModuleManager temporary = new ModuleManager(""); | ||
Set<Name> moduleIds = temporary.getRegistry() | ||
.getModuleIds(); | ||
Comment on lines
+34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I have been trying to deprecate ModuleManager#getRegistry, because for the most part, other things being able to mess with the registry that ModuleManager is responsible for managing is bad for business. Here you are using it to get all the modules in the current workspace. We could add a method to return this sort of read-only collection of module IDs, that would be pretty safe. I'm guessing the other use case we have for wanting to see all the workspace's modules is the Advanced Game Setup screen where you pick which modules to activate. That uses a lot more than just the ID... I haven't looked at it to find out what interface it really wants. |
||
return moduleIds | ||
.stream() | ||
.flatMap((name) -> moduleIds.stream().map((name2) -> Arguments.of(temporary, name, name2))); | ||
} | ||
|
||
@DisplayName("Try to resolve and load module") | ||
@ParameterizedTest(name = "{displayName} - {1}") | ||
@MethodSource("modulesAndModuleManager") | ||
void resolveAndLoadModule(ModuleManager moduleManager, Name moduleName) { | ||
moduleManager.resolveAndLoadEnvironment(moduleName); | ||
Assertions.assertNotNull(moduleManager.getEnvironment()); | ||
} | ||
|
||
@DisplayName("Try to resolve and load pair modules") | ||
@ParameterizedTest(name = "{displayName} - [{1}, {2}]") | ||
@MethodSource("modulePairsAndModuleManager") | ||
void resolveAndLoadPairModules(ModuleManager moduleManager, Name moduleName1, Name moduleName2) { | ||
moduleManager.resolveAndLoadEnvironment(moduleName1, moduleName2); | ||
Assertions.assertNotNull(moduleManager.getEnvironment()); | ||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you confirmed this can actually fail? I'm wondering because of Terasology/ModuleTestingEnvironment#69 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, i cannot. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's going on here? Why do we have a subclass that doesn't actually have any custom behavior? 😰
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is special temporary magic for accessing protected methods. ;)
Engines is nice enough, but i cannot access to EngineCleaner