Skip to content

Commit

Permalink
feat!: load only modules listed as dependencies, not everything in `m…
Browse files Browse the repository at this point in the history
…odules/`

Should make things run smoother, or at least easier to troubleshoot, in workspaces with many module sources present.
  • Loading branch information
keturn committed Jul 24, 2021
1 parent 7e90c2d commit 54c5776
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.opentest4j.MultipleFailuresError;
import org.slf4j.LoggerFactory;
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.registry.In;
import org.terasology.moduletestingenvironment.extension.Dependencies;
import org.terasology.moduletestingenvironment.extension.UseWorldGenerator;
Expand Down Expand Up @@ -70,8 +69,6 @@ public void afterAll(ExtensionContext context) throws Exception {

@Override
public void beforeAll(ExtensionContext context) throws Exception {
System.setProperty(ModuleManager.LOAD_CLASSPATH_MODULES_PROPERTY, "true");

if (context.getRequiredTestClass().isAnnotationPresent(Nested.class)) {
// nested classes get set up in the parent
ModuleTestingHelper parentHelper = helperContexts.get(context.getRequiredTestClass().getEnclosingClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.terasology.engine.context.Context;
import org.terasology.engine.core.GameEngine;
import org.terasology.engine.core.PathManager;
import org.terasology.engine.core.PathManagerProvider;
import org.terasology.engine.core.TerasologyConstants;
import org.terasology.engine.core.TerasologyEngine;
import org.terasology.engine.core.TerasologyEngineBuilder;
Expand Down Expand Up @@ -55,11 +56,14 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

/**
* Base class for tests involving full {@link TerasologyEngine} instances. View the tests included in this module for
Expand Down Expand Up @@ -155,6 +159,9 @@ public class ModuleTestingEnvironment {
private final List<TerasologyEngine> engines = Lists.newArrayList();
private long safetyTimeoutMs = DEFAULT_SAFETY_TIMEOUT;

PathManager pathManager;
PathManagerProvider.Cleaner pathManagerCleaner;

/**
* Set up and start the engine as configured via this environment.
* <p>
Expand All @@ -164,6 +171,7 @@ public class ModuleTestingEnvironment {
*/
@BeforeEach
public void setup() throws Exception {
mockPathManager();
host = createHost();
ScreenGrabber grabber = mock(ScreenGrabber.class);
hostContext.put(ScreenGrabber.class, grabber);
Expand All @@ -180,6 +188,13 @@ public void tearDown() {
engines.forEach(TerasologyEngine::shutdown);
engines.forEach(TerasologyEngine::cleanup);
engines.clear();
try {
pathManagerCleaner.close();
} catch (RuntimeException e) {
throw e;
} catch (Throwable e) {
throw new RuntimeException(e);
}
host = null;
hostContext = null;
}
Expand Down Expand Up @@ -398,6 +413,8 @@ private TerasologyEngine createHeadedEngine() throws IOException {
}

private TerasologyEngine createEngine(TerasologyEngineBuilder terasologyEngineBuilder) throws IOException {
System.setProperty(ModuleManager.LOAD_CLASSPATH_MODULES_PROPERTY, "true");

// create temporary home paths so the MTE engines don't overwrite config/save files in your real home path
Path path = Files.createTempDirectory("terasology-mte-engine");
PathManager pathManager = PathManager.getInstance();
Expand Down Expand Up @@ -449,6 +466,14 @@ private void registerCurrentDirectoryIfModule(TerasologyEngine terasologyEngine)
}
}

protected void mockPathManager() {
PathManager originalPathManager = PathManager.getInstance();
pathManager = spy(originalPathManager);
when(pathManager.getModulePaths()).thenReturn(Collections.emptyList());
pathManagerCleaner = new PathManagerProvider.Cleaner(originalPathManager, pathManager);
PathManagerProvider.setPathManager(pathManager);
};

private TerasologyEngine createHost() throws IOException {
TerasologyEngine terasologyEngine = createHeadlessEngine();
terasologyEngine.getFromEngineContext(SystemConfig.class).writeSaveGamesEnabled.set(false);
Expand Down

0 comments on commit 54c5776

Please sign in to comment.