Skip to content

Commit

Permalink
Allow extending gradle nativetest source set
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Feb 25, 2021
1 parent 0c3d785 commit 7b2ed97
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.gradle.util.GradleVersion;

import io.quarkus.gradle.builder.QuarkusModelBuilder;
import io.quarkus.gradle.extension.QuarkusPluginExtension;
import io.quarkus.gradle.extension.SourceSetExtension;
import io.quarkus.gradle.tasks.QuarkusAddExtension;
import io.quarkus.gradle.tasks.QuarkusBuild;
import io.quarkus.gradle.tasks.QuarkusDev;
Expand Down Expand Up @@ -244,13 +246,37 @@ private void configureBuildNativeTask(Project project) {

private void afterEvaluate(Project project) {
final HashSet<String> visited = new HashSet<>();
project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
ConfigurationContainer configurations = project.getConfigurations();
configurations.getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
.getIncoming().getDependencies()
.forEach(d -> {
if (d instanceof ProjectDependency) {
visitProjectDep(project, ((ProjectDependency) d).getDependencyProject(), visited);
}
});

SourceSetExtension sourceSetExtension = project.getExtensions().getByType(QuarkusPluginExtension.class)
.sourceSetExtension();

if (sourceSetExtension.extraNativeTest() != null) {

SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets();
SourceSet nativeTestSourceSets = sourceSets.getByName(NATIVE_TEST_SOURCE_SET_NAME);
nativeTestSourceSets.setCompileClasspath(
nativeTestSourceSets.getCompileClasspath().plus(sourceSetExtension.extraNativeTest().getOutput()));
nativeTestSourceSets.setRuntimeClasspath(
nativeTestSourceSets.getRuntimeClasspath().plus(sourceSetExtension.extraNativeTest().getOutput()));

configurations.findByName(NATIVE_TEST_IMPLEMENTATION_CONFIGURATION_NAME).extendsFrom(
configurations.findByName(sourceSetExtension.extraNativeTest().getImplementationConfigurationName()));
configurations.findByName(NATIVE_TEST_RUNTIME_ONLY_CONFIGURATION_NAME).extendsFrom(
configurations.findByName(sourceSetExtension.extraNativeTest().getRuntimeOnlyConfigurationName()));

QuarkusTestNative nativeTest = (QuarkusTestNative) project.getTasks().getByName(TEST_NATIVE_TASK_NAME);
nativeTest.setTestClassesDirs(nativeTestSourceSets.getOutput().getClassesDirs());
nativeTest.setClasspath(nativeTestSourceSets.getRuntimeClasspath());
}
}

private void visitProjectDep(Project project, Project dep, Set<String> visited) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.gradle;
package io.quarkus.gradle.extension;

import java.io.File;
import java.nio.file.Path;
Expand All @@ -9,6 +9,7 @@
import java.util.StringJoiner;
import java.util.stream.Collectors;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
Expand All @@ -28,6 +29,7 @@
import io.quarkus.bootstrap.resolver.model.ModelParameter;
import io.quarkus.bootstrap.resolver.model.QuarkusModel;
import io.quarkus.bootstrap.resolver.model.impl.ModelParameterImpl;
import io.quarkus.gradle.AppModelGradleResolver;
import io.quarkus.gradle.builder.QuarkusModelBuilder;
import io.quarkus.gradle.tasks.QuarkusGradleUtils;
import io.quarkus.runtime.LaunchMode;
Expand All @@ -46,11 +48,14 @@ public class QuarkusPluginExtension {

private File outputConfigDirectory;

private final SourceSetExtension sourceSetExtension;

public QuarkusPluginExtension(Project project) {
this.project = project;
this.sourceSetExtension = new SourceSetExtension();
}

void beforeTest(Test task) {
public void beforeTest(Test task) {
try {
final Map<String, Object> props = task.getSystemProperties();

Expand Down Expand Up @@ -176,6 +181,14 @@ public void setFinalName(String finalName) {
this.finalName = finalName;
}

public void sourceSets(Action<? super SourceSetExtension> action) {
action.execute(this.sourceSetExtension);
}

public SourceSetExtension sourceSetExtension() {
return sourceSetExtension;
}

public Set<File> resourcesDir() {
return getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getResources().getSrcDirs();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.gradle.extension;

import org.gradle.api.tasks.SourceSet;

public class SourceSetExtension {

private SourceSet extraNativeTestSourceSet;

public SourceSet extraNativeTest() {
return extraNativeTestSourceSet;
}

public void setExtraNativeTest(SourceSet extraNativeTestSourceSet) {
this.extraNativeTestSourceSet = extraNativeTestSourceSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.gradle.api.DefaultTask;

import io.quarkus.bootstrap.model.AppArtifact;
import io.quarkus.gradle.QuarkusPluginExtension;
import io.quarkus.gradle.extension.QuarkusPluginExtension;

public abstract class QuarkusTask extends DefaultTask {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.Test;

import io.quarkus.gradle.extension.QuarkusPluginExtension;

public class QuarkusPluginTest {

@Test
Expand Down
15 changes: 15 additions & 0 deletions docs/src/main/asciidoc/gradle-tooling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ Run the native tests using:

This task depends on `quarkusBuild`, so it will generate the native image before running the tests.

[NOTE]
====
By default, the `native-test` source set is based on `main` and `test` source sets. It is possible to add an extra source set. For example, if your integration tests are located in an `integrationTest` source set, you can specify it as:
[source,groovy]
----
quarkus {
sourceSets {
extraNativeTest = sourceSets.integrationTest
}
}
----
====

== Building Uber-Jars

Quarkus Gradle plugin supports the generation of Uber-Jars by specifying a `quarkus.package.type` argument as follows:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.gradle.nativeimage;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;

import org.junit.jupiter.api.Test;

import io.quarkus.gradle.BuildResult;

public class CustomNativeTestSourceSetTest extends QuarkusNativeGradleTestBase {

@Test
public void runNativeTests() throws Exception {
final File projectDir = getProjectDir("custom-java-native-sourceset-module");

final BuildResult build = runGradleWrapper(projectDir, "clean", "testNative");
assertThat(build.getTasks().get(":testNative")).isEqualTo(BuildResult.SUCCESS_OUTCOME);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
if (System.properties.containsKey('maven.repo.local')) {
maven {
url System.properties.get('maven.repo.local')
}
} else {
mavenLocal()
}
mavenCentral()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}

compileTestJava {
options.encoding = 'UTF-8'
}

sourceSets {
integrationTest {
java.srcDir file('src/integration-test/java')
resources.srcDir file('src/integration-test/resources')
compileClasspath += sourceSets.main.output + project.configurations.testCompileClasspath
runtimeClasspath += sourceSets.main.output + project.configurations.testRuntimeClasspath
}
}

quarkus {
sourceSets {
extraNativeTest = sourceSets.integrationTest
}
}
quarkusBuild {
nativeArgs {
additionalArgs= ["--verbose"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pluginManagement {
repositories {
if (System.properties.containsKey('maven.repo.local')) {
maven {
url System.properties.get('maven.repo.local')
}
} else {
mavenLocal()
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
rootProject.name='foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.acme;

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
public class ExampleResourceTest {

@Test
public void testHelloEndpoint() {
given()
.when().get("/hello")
.then()
.statusCode(200)
.body(is("hello"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.acme;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.acme;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
public class ExampleResourceNativeTest extends ExampleResourceTest {}

0 comments on commit 7b2ed97

Please sign in to comment.