Skip to content

Commit d7a5315

Browse files
authored
Merge pull request quarkusio#48275 from aloubyansky/gradle-test-sources
Add test sources in the ProjectDescriptorBuilder
2 parents b12d5b6 + 0997839 commit d7a5315

File tree

10 files changed

+207
-10
lines changed

10 files changed

+207
-10
lines changed

devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/GradleApplicationModelBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import io.quarkus.maven.dependency.DependencyFlags;
6363
import io.quarkus.maven.dependency.GACT;
6464
import io.quarkus.maven.dependency.GACTV;
65-
import io.quarkus.maven.dependency.GAV;
6665
import io.quarkus.maven.dependency.ResolvedDependencyBuilder;
6766
import io.quarkus.paths.PathCollection;
6867
import io.quarkus.paths.PathList;
@@ -169,7 +168,8 @@ public static ResolvedDependencyBuilder getProjectArtifact(Project project, bool
169168

170169
final SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
171170
final WorkspaceModule.Mutable mainModule = WorkspaceModule.builder()
172-
.setModuleId(new GAV(appArtifact.getGroupId(), appArtifact.getArtifactId(), appArtifact.getVersion()))
171+
.setModuleId(
172+
WorkspaceModuleId.of(appArtifact.getGroupId(), appArtifact.getArtifactId(), appArtifact.getVersion()))
173173
.setModuleDir(project.getProjectDir().toPath())
174174
.setBuildDir(project.getBuildDir().toPath())
175175
.setBuildFile(project.getBuildFile().toPath());
@@ -701,6 +701,6 @@ private static ArtifactCoords toArtifactCoords(ResolvedArtifact a) {
701701
}
702702

703703
private static ArtifactKey toAppDependenciesKey(String groupId, String artifactId, String classifier) {
704-
return new GACT(groupId, artifactId, classifier, ArtifactCoords.TYPE_JAR);
704+
return ArtifactKey.of(groupId, artifactId, classifier, ArtifactCoords.TYPE_JAR);
705705
}
706706
}

devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/tooling/ProjectDescriptorBuilder.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.quarkus.gradle.tooling;
22

33
import java.io.File;
4+
import java.nio.file.Path;
45
import java.util.ArrayList;
6+
import java.util.Collections;
57
import java.util.List;
68

79
import org.gradle.api.Project;
@@ -11,7 +13,9 @@
1113
import org.gradle.api.tasks.SourceSetContainer;
1214
import org.gradle.api.tasks.SourceSetOutput;
1315
import org.gradle.api.tasks.bundling.Jar;
16+
import org.gradle.api.tasks.testing.Test;
1417

18+
import io.quarkus.bootstrap.workspace.ArtifactSources;
1519
import io.quarkus.bootstrap.workspace.DefaultArtifactSources;
1620
import io.quarkus.bootstrap.workspace.LazySourceDir;
1721
import io.quarkus.bootstrap.workspace.SourceDir;
@@ -28,8 +32,8 @@ public static Provider<DefaultProjectDescriptor> buildForApp(Project project) {
2832

2933
public static void initSourceDirs(Project project, WorkspaceModule.Mutable result) {
3034
final SourceSetContainer srcSets = project.getExtensions().getByType(SourceSetContainer.class);
31-
// Here we are checking JARs will be produced, which directories they will use as the source of content
32-
// and figure out which source directories are processed to produce the content of the JARs.
35+
// Here we are iterating through the JARs that will be produced, collecting directories that will be used as sources
36+
// of their content. Then we are figuring out which source directories would be processed to produce the content of the JARs.
3337
// It has to be configureEach instead of forEach, apparently to avoid concurrent collection modification in some cases.
3438
project.getTasks().withType(Jar.class).configureEach(jarTask -> {
3539
final String classifier = jarTask.getArchiveClassifier().get();
@@ -38,7 +42,7 @@ public static void initSourceDirs(Project project, WorkspaceModule.Mutable resul
3842
final List<File> resourcesOutputDirs = new ArrayList<>(2);
3943
collectSourceSetOutput(((DefaultCopySpec) jarTask.getRootSpec()), classesDirs, resourcesOutputDirs);
4044

41-
final List<SourceDir> sourceDirs = new ArrayList<>(2);
45+
final List<SourceDir> sourceDirs = new ArrayList<>();
4246
final List<SourceDir> resourceDirs = new ArrayList<>(2);
4347
for (SourceSet srcSet : srcSets) {
4448
for (var classesDir : srcSet.getOutput().getClassesDirs().getFiles()) {
@@ -49,10 +53,10 @@ public static void initSourceDirs(Project project, WorkspaceModule.Mutable resul
4953
}
5054
}
5155

52-
final File resourcesOutputDir = srcSet.getOutput().getResourcesDir();
53-
if (resourcesOutputDirs.contains(resourcesOutputDir)) {
56+
if (resourcesOutputDirs.contains(srcSet.getOutput().getResourcesDir())) {
57+
var resourcesTarget = srcSet.getOutput().getResourcesDir().toPath();
5458
for (var dir : srcSet.getResources().getSrcDirs()) {
55-
resourceDirs.add(new LazySourceDir(dir.toPath(), resourcesOutputDir.toPath()));
59+
resourceDirs.add(new LazySourceDir(dir.toPath(), resourcesTarget));
5660
}
5761
}
5862
}
@@ -61,6 +65,60 @@ public static void initSourceDirs(Project project, WorkspaceModule.Mutable resul
6165
result.addArtifactSources(new DefaultArtifactSources(classifier, sourceDirs, resourceDirs));
6266
}
6367
});
68+
69+
// This is for the test sources and resources since, by default, they won't be put in JARs
70+
project.getTasks().withType(Test.class).configureEach(testTask -> {
71+
for (SourceSet srcSet : srcSets) {
72+
String classifier = null;
73+
List<SourceDir> testSourcesDirs = Collections.emptyList();
74+
List<SourceDir> testResourcesDirs = Collections.emptyList();
75+
for (var classesDir : srcSet.getOutput().getClassesDirs().getFiles()) {
76+
if (testTask.getTestClassesDirs().contains(classesDir)) {
77+
if (classifier == null) {
78+
classifier = sourceSetNameToClassifier(srcSet.getName());
79+
if (result.hasSources(classifier)) {
80+
// this source set should already be present in the module
81+
break;
82+
}
83+
}
84+
for (var srcDir : srcSet.getAllJava().getSrcDirs()) {
85+
if (testSourcesDirs.isEmpty()) {
86+
testSourcesDirs = new ArrayList<>(6);
87+
}
88+
testSourcesDirs.add(new LazySourceDir(srcDir.toPath(), classesDir.toPath())); // TODO findGeneratedSourceDir(destDir, sourceSet));
89+
}
90+
}
91+
}
92+
if (classifier != null && !testSourcesDirs.isEmpty()) {
93+
if (srcSet.getOutput().getResourcesDir() != null) {
94+
final Path resourcesOutputDir = srcSet.getOutput().getResourcesDir().toPath();
95+
for (var dir : srcSet.getResources().getSrcDirs()) {
96+
if (testResourcesDirs.isEmpty()) {
97+
testResourcesDirs = new ArrayList<>(2);
98+
}
99+
testResourcesDirs.add(new LazySourceDir(dir.toPath(), resourcesOutputDir));
100+
}
101+
}
102+
result.addArtifactSources(new DefaultArtifactSources(classifier, testSourcesDirs, testResourcesDirs));
103+
}
104+
}
105+
});
106+
}
107+
108+
private static String sourceSetNameToClassifier(String sourceSetName) {
109+
if (SourceSet.TEST_SOURCE_SET_NAME.equals(sourceSetName)) {
110+
return ArtifactSources.TEST;
111+
}
112+
var sb = new StringBuilder(sourceSetName.length() + 2);
113+
for (int i = 0; i < sourceSetName.length(); ++i) {
114+
char original = sourceSetName.charAt(i);
115+
char lowerCase = Character.toLowerCase(original);
116+
if (original != lowerCase) {
117+
sb.append('-');
118+
}
119+
sb.append(lowerCase);
120+
}
121+
return sb.toString();
64122
}
65123

66124
private static void collectSourceSetOutput(DefaultCopySpec spec, List<File> classesDir, List<File> resourcesDir) {

integration-tests/gradle/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@
124124
<groupId>io.quarkus</groupId>
125125
<artifactId>quarkus-hibernate-validator</artifactId>
126126
</dependency>
127+
<dependency>
128+
<groupId>io.quarkus</groupId>
129+
<artifactId>quarkus-jacoco</artifactId>
130+
</dependency>
127131
<dependency>
128132
<groupId>io.quarkus</groupId>
129133
<artifactId>quarkus-jdbc-h2</artifactId>
@@ -305,6 +309,19 @@
305309
</exclusion>
306310
</exclusions>
307311
</dependency>
312+
<dependency>
313+
<groupId>io.quarkus</groupId>
314+
<artifactId>quarkus-jacoco-deployment</artifactId>
315+
<version>${project.version}</version>
316+
<type>pom</type>
317+
<scope>test</scope>
318+
<exclusions>
319+
<exclusion>
320+
<groupId>*</groupId>
321+
<artifactId>*</artifactId>
322+
</exclusion>
323+
</exclusions>
324+
</dependency>
308325
<dependency>
309326
<groupId>io.quarkus</groupId>
310327
<artifactId>quarkus-jdbc-h2-deployment</artifactId>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
plugins {
2+
kotlin("jvm")
3+
kotlin("plugin.allopen")
4+
id("io.quarkus")
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
mavenLocal()
10+
}
11+
12+
val quarkusPlatformGroupId: String by project
13+
val quarkusPlatformArtifactId: String by project
14+
val quarkusPlatformVersion: String by project
15+
16+
dependencies {
17+
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
18+
implementation("io.quarkus:quarkus-kotlin")
19+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
20+
implementation("io.quarkus:quarkus-arc")
21+
implementation("io.quarkus:quarkus-rest")
22+
testImplementation("io.quarkus:quarkus-jacoco")
23+
testImplementation("io.quarkus:quarkus-junit5")
24+
testImplementation("io.rest-assured:rest-assured")
25+
}
26+
27+
group = "org.acme"
28+
version = "1.0.0-SNAPSHOT"
29+
30+
tasks.withType<Test> {
31+
systemProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager")
32+
}
33+
allOpen {
34+
annotation("jakarta.ws.rs.Path")
35+
annotation("jakarta.enterprise.context.ApplicationScoped")
36+
annotation("jakarta.persistence.Entity")
37+
annotation("io.quarkus.test.junit.QuarkusTest")
38+
}
39+
40+
kotlin {
41+
compilerOptions {
42+
javaParameters = true
43+
}
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
quarkusPluginId=io.quarkus
2+
quarkusPlatformGroupId=io.quarkus
3+
quarkusPlatformArtifactId=quarkus-bom
4+
kotlinVersion=${kotlin.version}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pluginManagement {
2+
val quarkusPluginVersion: String by settings
3+
val quarkusPluginId: String by settings
4+
val kotlinVersion: String by settings
5+
repositories {
6+
mavenLocal {
7+
content {
8+
includeGroupByRegex("io.quarkus.*")
9+
includeGroup("org.hibernate.orm")
10+
}
11+
}
12+
mavenCentral()
13+
gradlePluginPortal()
14+
}
15+
plugins {
16+
id(quarkusPluginId) version quarkusPluginVersion
17+
kotlin("jvm") version kotlinVersion
18+
kotlin("multiplatform") version kotlinVersion apply false
19+
kotlin("plugin.allopen") version kotlinVersion
20+
}
21+
}
22+
rootProject.name="code-with-quarkus"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.acme
2+
3+
import jakarta.ws.rs.GET
4+
import jakarta.ws.rs.Path
5+
import jakarta.ws.rs.Produces
6+
import jakarta.ws.rs.core.MediaType
7+
8+
@Path("/hello")
9+
class GreetingResource {
10+
11+
@GET
12+
@Produces(MediaType.TEXT_PLAIN)
13+
fun hello() = "Hello from Quarkus REST"
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.acme
2+
3+
import io.quarkus.test.junit.QuarkusTest
4+
import io.restassured.RestAssured.given
5+
import org.hamcrest.CoreMatchers.`is`
6+
import org.junit.jupiter.api.Test
7+
8+
@QuarkusTest
9+
class GreetingResourceTest {
10+
11+
@Test
12+
fun testHelloEndpoint() {
13+
given()
14+
.`when`().get("/hello")
15+
.then()
16+
.statusCode(200)
17+
.body(`is`("Hello from Quarkus REST"))
18+
}
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.quarkus.gradle;
2+
3+
import java.io.File;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class KotlinJacocoTest extends QuarkusGradleWrapperTestBase {
8+
9+
/**
10+
* This test should probably do more than simply verify a successful command execution.
11+
* It was originally added to make sure the process doesn't fail (which it used to).
12+
*/
13+
@Test
14+
public void testFastJarFormatWorks() throws Exception {
15+
final File projectDir = getProjectDir("kotlin-jacoco");
16+
runGradleWrapper(projectDir, "clean", "test");
17+
}
18+
}

integration-tests/gradle/src/test/java/io/quarkus/gradle/builder/QuarkusModelBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private void assertProjectModule(WorkspaceModule projectModule, File projectDir,
181181
assertThat(sourceTree.getRoots()).hasSize(1);
182182
assertThat(sourceTree.getRoots().iterator().next()).isEqualTo(projectDir.toPath().resolve("src/test/resources"));
183183
} else {
184-
assertThat(projectModule.getTestSources()).isNull();
184+
assertThat(projectModule.getTestSources().isOutputAvailable()).isFalse();
185185
}
186186
}
187187

0 commit comments

Comments
 (0)