Skip to content

Commit

Permalink
Make native tests a separate source set in generated gradle projects
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Oct 17, 2019
1 parent 7155a0a commit 20f2ac5
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private class BasicRestProject {
private ProjectWriter writer;
private String srcMainPath;
private String testMainPath;
private String nativeTestMainPath;
private SourceType type;

private BasicRestProject(final ProjectWriter writer, final Map<String, Object> parameters) {
Expand All @@ -76,13 +77,19 @@ private boolean initProject() throws IOException {

srcMainPath = writer.mkdirs(type.getSrcDir());
testMainPath = writer.mkdirs(type.getTestSrcDir());
// for gradle we want to place the native tests in under 'src/native-test/java'
// since gradle's idiomatic way of running integration tests is to create a new source set
if (getBuildTool() == BuildTool.GRADLE) {
nativeTestMainPath = writer.mkdirs(type.getTestSrcDir().replace("test", "native-test"));
} else {
nativeTestMainPath = testMainPath;
}

return newProject;
}

private boolean initBuildTool() throws IOException {
BuildFile buildFileManager = get(BUILD_FILE, null);
BuildTool buildTool = buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool();
BuildTool buildTool = getBuildTool();
context.putIfAbsent(ADDITIONAL_GITIGNORE_ENTRIES, buildTool.getGitIgnoreEntries());
boolean newProject = !writer.exists(buildTool.getDependenciesFile());
if (newProject) {
Expand Down Expand Up @@ -110,6 +117,11 @@ private boolean initBuildTool() throws IOException {
return newProject;
}

private BuildTool getBuildTool() {
BuildFile buildFileManager = get(BUILD_FILE, null);
return buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool();
}

private void generate(final String templateName, final Map<String, Object> context, final String outputFilePath,
final String resourceType)
throws IOException {
Expand Down Expand Up @@ -176,9 +188,18 @@ private void setupContext() throws IOException {

if (packageName != null) {
String packageDir = srcMainPath + '/' + packageName.replace('.', '/');
String originalTestMainPath = testMainPath;
String testPackageDir = testMainPath + '/' + packageName.replace('.', '/');
srcMainPath = writer.mkdirs(packageDir);
testMainPath = writer.mkdirs(testPackageDir);

if (!originalTestMainPath.equals(nativeTestMainPath)) {
String nativeTestPackageDir = nativeTestMainPath + '/' + packageName.replace('.', '/');
nativeTestMainPath = writer.mkdirs(nativeTestPackageDir);
} else {
nativeTestMainPath = testMainPath;
}

} else {
throw new NullPointerException("Need a non-null package name");
}
Expand All @@ -192,7 +213,7 @@ private void createClasses() throws IOException {
String extension = type.getExtension();
String classFile = srcMainPath + '/' + className + extension;
String testClassFile = testMainPath + '/' + className + "Test" + extension;
String itTestClassFile = testMainPath + '/' + "Native" + className + "IT" + extension;
String itTestClassFile = nativeTestMainPath + '/' + "Native" + className + "IT" + extension;
String name = getName();
String srcResourceTemplate = type.getSrcResourceTemplate(name);
Object isSpring = context.get(IS_SPRING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,29 @@ repositories {
mavenCentral()
}

sourceSets {
nativeTest {
compileClasspath += sourceSets.main.output
compileClasspath += sourceSets.test.output
runtimeClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.test.output
}
}

configurations {
nativeTestImplementation.extendsFrom implementation
nativeTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}")
implementation 'io.quarkus:quarkus-resteasy'

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

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

group '${project_groupId}'
Expand All @@ -37,8 +54,20 @@ compileJava {

test {
useJUnitPlatform()
exclude '**/Native*'
}

task testNative(type: Test) {
useJUnitPlatform()
description = 'Runs native image tests'
group = 'verification'

testClassesDirs = sourceSets.nativeTest.output.classesDirs
classpath = sourceSets.nativeTest.runtimeClasspath
shouldRunAfter test
}

testNative.dependsOn buildNative
check.dependsOn testNative



Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,52 @@ repositories {
mavenCentral()
}

sourceSets {
nativeTest {
compileClasspath += sourceSets.main.output
compileClasspath += sourceSets.test.output
runtimeClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.test.output
}
}

configurations {
nativeTestImplementation.extendsFrom implementation
nativeTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}")
implementation 'io.quarkus:quarkus-resteasy'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

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

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

group '${project_groupId}'
version '${project_version}'

test {
useJUnitPlatform()
exclude '**/Native*'
}

task testNative(type: Test) {
useJUnitPlatform()
description = 'Runs native image tests'
group = 'verification'

testClassesDirs = sourceSets.nativeTest.output.classesDirs
classpath = sourceSets.nativeTest.runtimeClasspath
shouldRunAfter test
}

testNative.dependsOn buildNative
check.dependsOn testNative

quarkus {
setOutputDirectory("$projectDir/build/classes/kotlin/main")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,48 @@ repositories {
mavenCentral()
}

sourceSets {
nativeTest {
compileClasspath += sourceSets.main.output
compileClasspath += sourceSets.test.output
runtimeClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.test.output
}
}

configurations {
nativeTestImplementation.extendsFrom implementation
nativeTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}")
implementation 'io.quarkus:quarkus-resteasy'

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

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

group '${project_groupId}'
version '${project_version}'

test {
useJUnitPlatform()
exclude '**/Native*'
}

task testNative(type: Test) {
useJUnitPlatform()
description = 'Runs native image tests'
group = 'verification'

testClassesDirs = sourceSets.nativeTest.output.classesDirs
classpath = sourceSets.nativeTest.runtimeClasspath
shouldRunAfter test
}

testNative.dependsOn buildNative
check.dependsOn testNative

Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public final class PathTestHelper {
"bin" + File.separator + "test",
"bin" + File.separator + "main");
// gradle
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "java" + File.separator + "nativeTest",
"classes" + File.separator + "java" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "java" + File.separator + "test",
"classes" + File.separator + "java" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "kotlin" + File.separator + "nativeTest",
"classes" + File.separator + "kotlin" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "kotlin" + File.separator + "test",
"classes" + File.separator + "kotlin" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "scala" + File.separator + "nativeTest",
"classes" + File.separator + "scala" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "scala" + File.separator + "test",
"classes" + File.separator + "scala" + File.separator + "main");
Expand Down

0 comments on commit 20f2ac5

Please sign in to comment.