Skip to content

Commit

Permalink
[7.17] Rework testing conventions gradle plugin (#87213) (#88422)
Browse files Browse the repository at this point in the history
Backports the following commits to 7.17:
 - Rework testing conventions gradle plugin (#87213)
  • Loading branch information
breskeby committed Jul 12, 2022
1 parent 7a9359b commit 6229461
Show file tree
Hide file tree
Showing 85 changed files with 899 additions and 1,259 deletions.
1 change: 0 additions & 1 deletion build-tools-internal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ tasks.named('test').configure {
useJUnitPlatform()
}
tasks.register("integTest", Test) {
inputs.dir(file("src/testKit")).withPropertyName("testkit dir").withPathSensitivity(PathSensitivity.RELATIVE)
systemProperty 'test.version_under_test', version
testClassesDirs = sourceSets.integTest.output.classesDirs
classpath = sourceSets.integTest.runtimeClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.fixtures

import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin

abstract class AbstractGradlePrecommitPluginFuncTest extends AbstractJavaGradleFuncTest {

abstract <T extends PrecommitPlugin> Class<T> getPluginClassUnderTest();

def setup() {
buildFile << """
import ${getPluginClassUnderTest().getName()}
plugins {
// bring in build-tools-internal onto the classpath
id 'elasticsearch.global-build-info'
}
// internally used plugins do not have a plugin id as they are
// not intended to be used directly from build scripts
plugins.apply(${getPluginClassUnderTest().getSimpleName()})
"""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.fixtures

import net.bytebuddy.ByteBuddy
import net.bytebuddy.dynamic.DynamicType
import org.junit.rules.ExternalResource
import org.junit.rules.TemporaryFolder

class LocalRepositoryFixture extends ExternalResource{

private TemporaryFolder temporaryFolder

LocalRepositoryFixture(TemporaryFolder temporaryFolder){
this.temporaryFolder = temporaryFolder
}

void generateJar(String group, String module, String version, String... clazzNames){
def baseGroupFolderPath = group.replace('.', '/')
def targetFolder = new File(repoDir, "${baseGroupFolderPath}/$module/$version")
targetFolder.mkdirs()

def jarFile = new File(targetFolder, "${module}-${version}.jar")
clazzNames.each {clazzName ->
DynamicType.Unloaded<?> dynamicType = new ByteBuddy().subclass(Object.class)
.name(clazzName)
.make()
if(jarFile.exists()) {
dynamicType.inject(jarFile);
}else {
dynamicType.toJar(jarFile);
}
}
}

void configureBuild(File buildFile) {
buildFile << """
repositories {
maven {
name = "local-test"
url = "${getRepoDir()}"
metadataSources {
artifact()
}
}
}
"""
}

File getRepoDir() {
new File(temporaryFolder.root, 'local-repo')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

package org.elasticsearch.gradle.internal.precommit

import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
import org.elasticsearch.gradle.fixtures.AbstractGradlePrecommitPluginFuncTest
import org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersPrecommitPlugin
import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin
import org.gradle.testkit.runner.TaskOutcome

class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {
class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradlePrecommitPluginFuncTest {

def "detects invalid files with invalid license header"() {
given:
Class<? extends PrecommitPlugin> pluginClassUnderTest = LicenseHeadersPrecommitPlugin.class

def setup() {
buildFile << """
plugins {
id 'java'
id 'elasticsearch.internal-licenseheaders'
}
apply plugin:'java'
"""
}
def "detects invalid files with invalid license header"() {
given:
dualLicensedFile()
unknownSourceFile()
unapprovedSourceFile()
Expand All @@ -39,11 +42,6 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {
def "can filter source files"() {
given:
buildFile << """
plugins {
id 'java'
id 'elasticsearch.internal-licenseheaders'
}
tasks.named("licenseHeaders").configure {
excludes << 'org/acme/filtered/**/*'
}
Expand All @@ -61,12 +59,6 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {

def "supports sspl by convention"() {
given:
buildFile << """
plugins {
id 'java'
id 'elasticsearch.internal-licenseheaders'
}
"""
dualLicensedFile()

when:
Expand All @@ -79,11 +71,6 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {
def "sspl default additional license can be overridden"() {
given:
buildFile << """
plugins {
id 'java'
id 'elasticsearch.internal-licenseheaders'
}
tasks.named("licenseHeaders").configure {
additionalLicense 'ELAST', 'Elastic License 2.0', '2.0; you may not use this file except in compliance with the Elastic License'
}
Expand Down Expand Up @@ -173,4 +160,5 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {
String normalizedPath = normalized(sourceFile.getPath())
(normalizedPath.substring(normalizedPath.indexOf("src/main/java")) - "src/main/java/" - ("/" + sourceFile.getName())).replaceAll("/", ".")
}

}

0 comments on commit 6229461

Please sign in to comment.