MovingBlocks / Terasology

Terasology - open source voxel world
http://terasology.org
Apache License 2.0
3.69k stars 1.34k forks source link

load game from save file in unit test #5050

Open keturn opened 2 years ago

keturn commented 2 years ago

In order to do full round-trip tests of saving and loading Terasology worlds, MTE needs a way to load from a saved game.

keturn commented 2 years ago

I was going to say "this is probably not too hard," but I didn't expect this: https://github.com/MovingBlocks/Terasology/blob/fbc40c3fe2cf0c605f950c19e6c021f43aa43c05/engine/src/main/java/org/terasology/engine/core/subsystem/headless/mode/StateHeadlessSetup.java#L55-L60

Headless mode always loads a save if it can find any! That's useful, but also a little scary: it means MTE could end up loading a game that doesn't match its supplied configuration without even realizing it.

keturn commented 2 years ago

Here's where MTE sets its home path: https://github.com/MovingBlocks/Terasology/blob/fbc40c3fe2cf0c605f950c19e6c021f43aa43c05/engine-tests/src/main/java/org/terasology/engine/integrationenvironment/Engines.java#L228-L230

so if we want to load things, we either need to put the things in that place, or point it to the place where things are, before it goes in to the StateHeadlessSetup state.

It seems like we can hack this together with the custom subsystem support we already have.

Is there any way for us to tell, once the world is up and running, whether it's from a saved game or newly-generated in this session?

keturn commented 2 years ago

This works, more or less: https://github.com/Terasology/WildAnimals/blob/158a008ec595daacf61fe8696bae66031859743a/src/test/java/org/terasology/wildAnimals/system/WildAnimalsSpawnSystemTest.java#L148

Leaving the test lifecycle on PER_METHOD, then using a static field in the subsystem to keep the home directory the same between tests.

Having more explicit support for this would still be nice, as that implementation relies on some unstable implementation details, such as the way the first test's home directory isn't cleaned up right away as soon as that one test finishes.