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 JUnit subproject #573

Merged
merged 25 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/build-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: build
arguments: assemble checkFormatting
cache-read-only: false

- name: Publish artifacts
Expand Down
17 changes: 16 additions & 1 deletion .github/workflows/test-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,23 @@ jobs:
arguments: setup
cache-read-only: false

- name: Build with Gradle
- name: Run game tests with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: :tests:runGameTestServer
cache-read-only: false

- name: Run JUnit tests with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: :tests:runUnitTests
cache-read-only: false

- name: Store reports
if: failure()
uses: actions/upload-artifact@v4
with:
name: reports
path: |
**/build/reports/
**/build/test-results/
10 changes: 2 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import java.util.regex.Pattern
plugins {
id 'net.neoforged.gradleutils' version '3.0.0-alpha.10' apply false
id 'com.diffplug.spotless' version '6.22.0' apply false
id 'net.neoforged.licenser' version '0.7.2' apply false
id 'net.neoforged.licenser' version '0.7.2'
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
id 'neoforge.formatting-conventions'
}

apply plugin: 'net.neoforged.gradleutils'
Expand Down Expand Up @@ -51,9 +52,6 @@ subprojects {
java.toolchain.languageVersion.set(JavaLanguageVersion.of(project.java_version))
}

apply plugin: 'com.diffplug.spotless'
apply plugin: 'net.neoforged.licenser'

repositories {
mavenCentral()
}
Expand Down Expand Up @@ -129,7 +127,3 @@ spotless {
bumpThisNumberIfACustomStepChanges(3)
}
}

apply from: rootProject.file('buildscript/spotless-rules.gradle')
apply from: rootProject.file('buildscript/generate-package-infos.gradle')
apply from: rootProject.file('buildscript/apply-all-formatting.gradle')
3 changes: 3 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'groovy-gradle-plugin'
}
81 changes: 81 additions & 0 deletions buildSrc/src/main/groovy/neoforge.formatting-conventions.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.regex.Matcher

project.plugins.apply('com.diffplug.spotless')

final generatePackageInfos = tasks.register('generatePackageInfos', Task) {
doLast {
fileTree('src/main/java').each { javaFile ->
def packageInfoFile = new File(javaFile.parent, 'package-info.java')
if (!packageInfoFile.exists()) {
def pkgName = javaFile.toString().replaceAll(Matcher.quoteReplacement(File.separator), '/')
pkgName = pkgName.substring(pkgName.indexOf('net/neoforged/'), pkgName.lastIndexOf('/'))
pkgName = pkgName.replaceAll('/', '.')

def pkgInfoText = """
|@FieldsAreNonnullByDefault
|@MethodsReturnNonnullByDefault
|@ParametersAreNonnullByDefault
|package $pkgName;
|
|import javax.annotation.ParametersAreNonnullByDefault;
|import net.minecraft.FieldsAreNonnullByDefault;
|import net.minecraft.MethodsReturnNonnullByDefault;
""".stripMargin().trim()

packageInfoFile.text = pkgInfoText
}
}
}
}

spotless {
java {
endWithNewline()
indentWithSpaces()
removeUnusedImports()
toggleOffOn()
eclipse().configFile rootProject.file('codeformat/formatter-config.xml')
importOrder()

// courtesy of diffplug/spotless#240
// https://github.com/diffplug/spotless/issues/240#issuecomment-385206606
custom 'noWildcardImports', { String fileContents ->
if (fileContents.contains('*;\n')) {
throw new GradleException('No wildcard imports are allowed!')
}
}

custom 'noNotNull', { String fileContents ->
if (fileContents.contains('@NotNull') || fileContents.contains('@Nonnull')) {
throw new GradleException('@NotNull and @Nonnull are disallowed.')
}
}
bumpThisNumberIfACustomStepChanges(2)
}
}

tasks.named('licenseFormat').configure {
mustRunAfter generatePackageInfos
}
tasks.named('spotlessApply').configure {
mustRunAfter generatePackageInfos
mustRunAfter tasks.named('licenseFormat')
}

tasks.register('applyAllFormatting', Task) {
dependsOn generatePackageInfos
dependsOn tasks.named('licenseFormat')
dependsOn tasks.named('spotlessApply')
group = 'verification'
}

tasks.register('checkFormatting', Task) {
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
dependsOn 'licenseCheck'
dependsOn 'spotlessCheck'
group = 'verification'
}

tasks.register('runUnitTests', Task) {
group = 'verification'
tasks.withType(Test).each { tsk -> it.dependsOn(tsk) }
}
9 changes: 0 additions & 9 deletions buildscript/apply-all-formatting.gradle

This file was deleted.

27 changes: 0 additions & 27 deletions buildscript/generate-package-infos.gradle

This file was deleted.

25 changes: 0 additions & 25 deletions buildscript/spotless-rules.gradle

This file was deleted.

7 changes: 4 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jetbrains_annotations_version=24.0.1
slf4j_api_version=2.0.7
apache_maven_artifact_version=3.8.5
jarjar_version=0.4.0
fancy_mod_loader_version=3.0.29
fancy_mod_loader_version=3.0.37
mojang_logging_version=1.1.1
log4j_version=2.22.1
guava_version=31.1.2-jre
Expand All @@ -47,7 +47,8 @@ mixin_extras_version=0.3.5

jupiter_api_version=5.7.0
vintage_engine_version=5.+
opentest4j_version=1.2.0
hamcrest_version=1.3
assertj_core=3.25.1

neogradle.runtime.platform.installer.debug=true
# We want to be able to have a junit run disconnected from the test and main sourcesets
neogradle.subsystems.conventions.sourcesets.automatic-inclusion=false
15 changes: 12 additions & 3 deletions projects/neoforge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ dependencies {
userdevCompileOnly jarJar("io.github.llamalad7:mixinextras-neoforge:${project.mixin_extras_version}"), {
jarJar.ranged(it, "[${project.mixin_extras_version},)")
}

userdevTestImplementation("net.neoforged.fancymodloader:junit-fml:${project.fancy_mod_loader_version}")
}

runTypes {
Expand Down Expand Up @@ -132,16 +134,22 @@ runTypes {
arguments.addAll '--fml.mcVersion', project.minecraft_version
arguments.addAll '--fml.neoFormVersion', project.neoform_version
}

junit {
junit true
arguments.addAll '--fml.neoForgeVersion', project.version
arguments.addAll '--fml.fmlVersion', project.fancy_mod_loader_version
arguments.addAll '--fml.mcVersion', project.minecraft_version
arguments.addAll '--fml.neoFormVersion', project.neoform_version
}
}

runs {
client { }
server { }
gameTestServer { }
gameTestClient { }
"Data" {
configure project.runTypes.data

data {
programArguments.addAll '--mod', 'neoforge'

modSources.add project.sourceSets.main
Expand Down Expand Up @@ -184,6 +192,7 @@ userdevProfile {
argument '--fml.neoFormVersion'
argument project.neoform_version
}
additionalTestDependencyArtifactCoordinate "net.neoforged:testframework:${project.version}"
}

tasks.withType(Javadoc.class).configureEach {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pluginManagement {
}

plugins {
id 'net.neoforged.gradle.platform' version '7.0.119'
id 'net.neoforged.gradle.platform' version '7.0.135'
}

rootProject.name = rootDir.name
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/neoforged/neoforge/junit/JUnitMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.junit;

import cpw.mods.modlauncher.Launcher;
import net.minecraft.SharedConstants;
import net.minecraft.server.Bootstrap;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.common.asm.RuntimeDistCleaner;

public class JUnitMain {
public static void main(String[] args) {
SharedConstants.tryDetectVersion();
Bootstrap.bootStrap();

// Load mods
net.neoforged.neoforge.server.loading.ServerModLoader.load();

// We launch as a server, but we want client classes available
((RuntimeDistCleaner) Launcher.INSTANCE.environment().findLaunchPlugin("runtimedistcleaner").orElseThrow())
.getExtension().accept(Dist.CLIENT);
}
}
13 changes: 13 additions & 0 deletions src/main/java/net/neoforged/neoforge/junit/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
package net.neoforged.neoforge.junit;

import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;
8 changes: 4 additions & 4 deletions testframework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'com.diffplug.spotless'
id 'net.neoforged.licenser'
id 'net.neoforged.gradle.platform'
id 'neoforge.formatting-conventions'
}

java.withSourcesJar()
Expand All @@ -22,6 +23,9 @@ repositories {
dependencies {
implementation project(':neoforge')

compileOnly(platform("org.junit:junit-bom:${project.jupiter_api_version}"))
compileOnly "org.junit.jupiter:junit-jupiter-params"

compileOnly "org.jetbrains:annotations:${project.jetbrains_annotations_version}"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
}
Expand Down Expand Up @@ -55,7 +59,3 @@ publishing {
maven rootProject.gradleutils.getPublishingMaven()
}
}

apply from: rootProject.file('buildscript/spotless-rules.gradle')
apply from: rootProject.file('buildscript/generate-package-infos.gradle')
apply from: rootProject.file('buildscript/apply-all-formatting.gradle')
Loading
Loading