Skip to content

Commit acf6adb

Browse files
CursedFlamesNotStirred
authored andcommitted
Don't attempt to load CC config from file in tests
1 parent bcc64a0 commit acf6adb

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/main/java/io/github/opencubicchunks/cubicchunks/CubicChunks.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
*/
1919
@Mod("cubicchunks")
2020
public class CubicChunks extends CubicChunksBase {
21+
/**
22+
* true when running in a junit test, false otherwise.
23+
*/
24+
public static boolean IS_IN_TEST = false;
2125
protected static CommonConfig config = null;
2226
// For hardcoding height in P1
2327
public static final int SUPERFLAT_HEIGHT = 5;

src/main/java/io/github/opencubicchunks/cubicchunks/config/CommonConfig.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
import com.electronwill.nightconfig.core.CommentedConfig;
66
import com.electronwill.nightconfig.core.Config;
7+
import io.github.opencubicchunks.cubicchunks.CubicChunks;
78
import net.neoforged.fml.loading.FMLPaths;
89

910
public class CommonConfig extends BaseConfig {
1011
private static final String FILE_NAME = "cubicchunks_common.toml";
1112
// TODO forge/fabric-agnostic method for getting config directory
12-
private static final File FILE_PATH = new File(FMLPaths.CONFIGDIR.get().toFile(), FILE_NAME);
13+
// Note that this relies on IS_IN_TEST being set before this class is classloaded
14+
private static final File FILE_PATH = CubicChunks.IS_IN_TEST ? null : new File(FMLPaths.CONFIGDIR.get().toFile(), FILE_NAME);
1315

1416
private static final String KEY_GENERAL = "general";
1517
private static final String KEY_VERTICAL_VIEW_DISTANCE = KEY_GENERAL + ".verticalViewDistance";
@@ -60,6 +62,10 @@ public void setGenerateNewWorldsAsCC(boolean generateNewWorldsAsCC) {
6062

6163
public static CommonConfig getConfig() {
6264
var config = createDefaultConfig();
65+
if (CubicChunks.IS_IN_TEST) {
66+
// Skip file access when running in a test environment; tests should manually update relevant config values before running game code.
67+
return new CommonConfig(config);
68+
}
6369
// Read existing values to the config
6470
if (FILE_PATH.exists()) {
6571
read(FILE_PATH, config);

src/test/java/io/github/opencubicchunks/cubicchunks/testutils/BaseTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.opencubicchunks.cubicchunks.testutils;
22

3+
import io.github.opencubicchunks.cubicchunks.CubicChunks;
34
import net.minecraft.SharedConstants;
45
import net.minecraft.server.Bootstrap;
56
import org.junit.jupiter.api.AfterEach;
@@ -12,6 +13,7 @@ public class BaseTest {
1213
@BeforeAll
1314
public static void setup() {
1415
SharedConstants.tryDetectVersion();
16+
CubicChunks.IS_IN_TEST = true;
1517
Bootstrap.bootStrap();
1618
SharedConstants.IS_RUNNING_IN_IDE = true;
1719
}

0 commit comments

Comments
 (0)