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

Add a subproject with a JUnit session listener #54

Merged
merged 21 commits into from
May 26, 2024
Merged
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
9 changes: 9 additions & 0 deletions junit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'java-library'
}

dependencies {
implementation('org.junit.platform:junit-platform-launcher:1.9.3')
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
// BSL should not be exposed and the actual version should be provided by the neo dep
compileOnly("cpw.mods:bootstraplauncher:${project.bootstraplauncher_version}")
}
13 changes: 13 additions & 0 deletions junit/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

import net.neoforged.fml.junit.JUnitService;
import org.junit.platform.launcher.LauncherSessionListener;

module net.neoforged.fancymodlauncher.junit {
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
requires org.junit.platform.launcher;
requires cpw.mods.bootstraplauncher;
provides LauncherSessionListener with JUnitService;
}
27 changes: 27 additions & 0 deletions junit/src/main/java/net/neoforged/fml/junit/JUnitService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.junit;

import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.LauncherSessionListener;

public class JUnitService implements LauncherSessionListener {
private ClassLoader oldLoader;
public JUnitService() {

}

@Override
public void launcherSessionOpened(LauncherSession session) {
oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(LaunchWrapper.getTransformingLoader());
}

@Override
public void launcherSessionClosed(LauncherSession session) {
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
34 changes: 34 additions & 0 deletions junit/src/main/java/net/neoforged/fml/junit/LaunchWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.junit;

import cpw.mods.bootstraplauncher.BootstrapLauncher;

import java.nio.file.Files;
import java.nio.file.Path;

public class LaunchWrapper {
private static ClassLoader transformingCL;

public static synchronized ClassLoader getTransformingLoader() {
if (transformingCL != null) return transformingCL;
final var oldLoader = Thread.currentThread().getContextClassLoader();

try {
final String[] args = Files.readAllLines(Path.of(System.getProperty("fml.junit.argsfile", "mainargs.txt"))).toArray(String[]::new);
BootstrapLauncher.main(args);

transformingCL = Thread.currentThread().getContextClassLoader();
} catch (Exception exception) {
System.err.println("Failed to start Minecraft: " + exception);
throw new RuntimeException(exception);
} finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}

return transformingCL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
net.neoforged.fml.junit.JUnitService
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.loading.targets;

import net.neoforged.api.distmarker.Dist;

public class JUnitDevLaunchTarget extends CommonDevLaunchHandler {
@Override
public Dist getDist() {
return Dist.DEDICATED_SERVER;
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected void runService(String[] arguments, ModuleLayer gameLayer) throws Throwable {
Class.forName(gameLayer.findModule("neoforge").orElseThrow(), "net.neoforged.neoforge.junit.JUnitMain").getMethod("main", String[].class).invoke(null, (Object)arguments);
}

@Override
public String name() {
return "junitfmldev";
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.loading.targets;

import net.neoforged.api.distmarker.Dist;

public class JUnitUserDevLaunchTarget extends ForgeUserdevLaunchHandler {
@Override
public Dist getDist() {
return Dist.DEDICATED_SERVER;
}

@Override
protected void runService(String[] arguments, ModuleLayer gameLayer) throws Throwable {
Class.forName(gameLayer.findModule("neoforge").orElseThrow(), "net.neoforged.neoforge.junit.JUnitMain").getMethod("main", String[].class).invoke(null, (Object)arguments);
}

@Override
public String name() {
return "junitfmluserdev";
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ net.neoforged.fml.loading.targets.ForgeServerDevLaunchHandler
net.neoforged.fml.loading.targets.ForgeServerUserdevLaunchHandler
net.neoforged.fml.loading.targets.ForgeDataDevLaunchHandler
net.neoforged.fml.loading.targets.ForgeDataUserdevLaunchHandler
net.neoforged.fml.loading.targets.JUnitDevLaunchTarget
net.neoforged.fml.loading.targets.JUnitUserDevLaunchTarget
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ include 'loader'
include 'core'
include 'events'
include 'earlydisplay'
include 'junit'

includeLanguage 'minecraft'
includeLanguage 'java'
Expand Down
Loading