Skip to content
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

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extensions
/facades/*
!/facades/PC
!/facades/TeraEd
!/facades/WorkspaceValidation
!/facades/subprojects.gradle
/modules/*
!/modules/subprojects.gradle
Expand Down
11 changes: 11 additions & 0 deletions facades/WorkspaceValidation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Boo eclipse! - every eclipse project needs its own .gitignore file

# Ignore Eclipse
/.checkstyle
/.project
/.classpath
/.settings/
/bin/

# Ignore gradle
/build/
70 changes: 70 additions & 0 deletions facades/WorkspaceValidation/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

// The Editor facade is responsible for the (shader) editor - a plain Java application runnable on PCs

// Grab all the common stuff like plugins to use, artifact repositories, code analysis config
apply from: "$rootDir/config/gradle/publish.gradle"

// Base the engine tests on the same version number as the engine
version = project(':engine').version
println "TeraEd VERSION: $version"

// Jenkins-Artifactory integration catches on to this as part of the Maven-type descriptor
group = 'org.terasology.facades'

sourceSets {
// Adjust output path (changed with the Gradle 6 upgrade, this puts it back)
main.java.outputDir = new File("$buildDir/classes")
test.java.outputDir = new File("$buildDir/testClasses")
}

dependencies {
implementation project(':engine')
implementation "org.terasology:reflections:0.9.12-MB"

implementation(platform(project(":modules")))

implementation(project(":engine-tests"))

implementation("org.terasology.modules:ModuleTestingEnvironment:0.3.3-SNAPSHOT")

implementation(group: 'com.google.guava', name: 'guava', version: '30.1-jre')

implementation(project(":subsystems:DiscordRPC"))
implementation(project(":subsystems:TypeHandlerLibrary"))

implementation(group: 'org.lwjglx', name: 'lwjgl3-awt', version: '0.1.7') {
exclude group: 'org.lwjgl', module: ''
}

// Test lib dependencies
implementation(platform("org.junit:junit-bom:5.8.1")) {
// junit-bom will set version numbers for the other org.junit dependencies.
}
implementation("org.junit.jupiter:junit-jupiter-api")
implementation("org.junit.jupiter:junit-jupiter-params")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation('org.mockito:mockito-inline:3.12.4')
}

test {
// dependsOn copyResourcesToClasses
dependsOn rootProject.extractNatives

description("Runs all tests (slow)")

systemProperty("junit.jupiter.execution.timeout.default", "4m")
}



// Prep an IntelliJ module for the facade
idea {
module {
// Change around the output a bit
inheritOutputDirs = false
outputDir = file('build/classes')
testOutputDir = file('build/testClasses')
}
}
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 {
Copy link
Member

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? 😰

Copy link
Contributor Author

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


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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, i cannot.
I tried to break one dependency.. but then gradle fail to resolve dependencies...
I don't know how to break dependency without breaking gradle resolution...

}
}