Skip to content

Commit

Permalink
Remove guava and commons-io as an external dependencies, move mocking…
Browse files Browse the repository at this point in the history
… libraries as runtimeOnly
  • Loading branch information
chali committed Jun 8, 2021
1 parent df22b56 commit 9dbf478
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 45 deletions.
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ contacts {

dependencies {
implementation gradleTestKit()
api 'com.google.guava:guava:19.0'
api 'commons-io:commons-io:2.5'
api ('org.spockframework:spock-core:1.3-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
}
api 'cglib:cglib-nodep:3.2.2'
api 'org.objenesis:objenesis:2.4'
runtimeOnly 'cglib:cglib-nodep:3.2.2'
runtimeOnly 'org.objenesis:objenesis:2.4'
testImplementation('com.github.stefanbirkner:system-rules:1.19.0')
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/groovy/nebula/test/Integration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package nebula.test

import com.google.common.base.Predicate
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import nebula.test.functional.ExecutionResult
Expand All @@ -24,8 +23,10 @@ import nebula.test.functional.GradleRunnerFactory
import nebula.test.functional.PreExecutionAction
import nebula.test.functional.internal.GradleHandle
import nebula.test.multiproject.MultiProjectIntegrationHelper
import org.apache.commons.io.FileUtils
import org.gradle.api.logging.LogLevel
import org.gradle.util.GFileUtils

import java.util.function.Predicate

/**
* @author Justin Ryan
Expand Down Expand Up @@ -108,9 +109,9 @@ abstract trait Integration extends IntegrationBase {
File destinationFile = file(destination)
File resourceFile = new File(resource.toURI())
if (resourceFile.file) {
FileUtils.copyFile(resourceFile, destinationFile)
GFileUtils.copyFile(resourceFile, destinationFile)
} else {
FileUtils.copyDirectory(resourceFile, destinationFile)
GFileUtils.copyDirectory(resourceFile, destinationFile)
}
}

Expand Down
28 changes: 16 additions & 12 deletions src/main/groovy/nebula/test/functional/GradleRunner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@

package nebula.test.functional

import com.google.common.base.Predicate
import com.google.common.base.Predicates
import com.google.common.base.StandardSystemProperty
import nebula.test.functional.internal.GradleHandle

public interface GradleRunner {
import java.util.function.Predicate

interface GradleRunner {
// These predicates are here, instead of on GradleRunnerFactory due to a Groovy static compiler bug (https://issues.apache.org/jira/browse/GROOVY-7159)

static final String SHARED_DEPENDENCY_CACHE_ENVIRONMENT_VARIABLE = 'GRADLE_RO_DEP_CACHE'

static final Predicate<URL> CLASSPATH_GRADLE_CACHE = new Predicate<URL>() {
@Override
boolean apply(URL url) {
boolean test(URL url) {
String gradleSharedDependencyCache = System.getenv(SHARED_DEPENDENCY_CACHE_ENVIRONMENT_VARIABLE)
if (gradleSharedDependencyCache) {
return (url.path.contains('/caches/modules-') || url.path.contains("${gradleSharedDependencyCache}/modules-")) && !isTestingFramework(url)
Expand All @@ -44,23 +43,23 @@ public interface GradleRunner {

static final Predicate<URL> MAVEN_LOCAL = new Predicate<URL>() {
@Override
boolean apply(URL url) {
String m2RepositoryPrefix = StandardSystemProperty.USER_HOME.value() + "/.m2/repository"
boolean test(URL url) {
String m2RepositoryPrefix = System.getProperty("user.home") + "/.m2/repository"
return url.path.contains(m2RepositoryPrefix)
}
}

static final Predicate<URL> CLASSPATH_PROJECT_DIR = new Predicate<URL>() {
@Override
boolean apply(URL url) {
File userDir = new File(StandardSystemProperty.USER_DIR.value())
boolean test(URL url) {
File userDir = new File(System.getProperty("user.dir"))
return url.path.startsWith(userDir.toURI().toURL().path)
}
}

static final Predicate<URL> CLASSPATH_PROJECT_DEPENDENCIES = new Predicate<URL>() {
@Override
boolean apply(URL url) {
boolean test(URL url) {
return url.path.contains('/build/classes') || url.path.contains('/build/resources') || url.path.contains('/build/libs') || url.path.contains('/out/')
}
}
Expand All @@ -69,14 +68,19 @@ public interface GradleRunner {
* Attempts to provide a classpath that approximates the 'normal' Gradle runtime classpath. Use {@link #CLASSPATH_ALL}
* to default to pre-2.2.2 behaviour.
*/
static final Predicate<URL> CLASSPATH_DEFAULT = Predicates.or(CLASSPATH_PROJECT_DIR, CLASSPATH_GRADLE_CACHE, CLASSPATH_PROJECT_DEPENDENCIES, MAVEN_LOCAL)
static final Predicate<URL> CLASSPATH_DEFAULT = new Predicate<URL>() {
@Override
boolean test(URL url) {
return CLASSPATH_PROJECT_DIR.test(url) || CLASSPATH_GRADLE_CACHE.test(url) || CLASSPATH_PROJECT_DEPENDENCIES.test(url) || MAVEN_LOCAL.test(url)
}
}

/**
* Accept all URLs. Provides pre-2.2.2 behaviour.
*/
static final Predicate<URL> CLASSPATH_ALL = new Predicate<URL>() {
@Override
boolean apply(URL url) {
boolean test(URL url) {
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package nebula.test.functional

import com.google.common.base.Predicate
import groovy.transform.CompileStatic
import nebula.test.functional.internal.DefaultGradleRunner
import nebula.test.functional.internal.GradleHandleFactory
import nebula.test.functional.internal.classpath.ClasspathInjectingGradleHandleFactory
import nebula.test.functional.internal.toolingapi.ToolingApiGradleHandleFactory

import java.util.function.Predicate

@CompileStatic
class GradleRunnerFactory {
public static GradleRunner createTooling(boolean fork = false, String version = null, Integer daemonMaxIdleTimeInSeconds = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package nebula.test.functional.internal.classpath

import com.google.common.base.Function
import com.google.common.base.Predicate
import com.google.common.collect.FluentIterable
import groovy.transform.CompileStatic
import org.gradle.internal.ErroringAction
import org.gradle.internal.IoActions
import org.gradle.internal.classloader.ClasspathUtil
import org.gradle.internal.classpath.ClassPath
import org.gradle.util.TextUtil

import java.util.function.Predicate

@CompileStatic
class ClasspathAddingInitScriptBuilder {
private ClasspathAddingInitScriptBuilder() {
Expand Down Expand Up @@ -48,12 +47,9 @@ class ClasspathAddingInitScriptBuilder {

public static List<File> getClasspathAsFiles(ClassLoader classLoader, Predicate<URL> classpathFilter) {
List<URL> classpathUrls = getClasspathUrls(classLoader)
return FluentIterable.from(classpathUrls).filter(classpathFilter).transform(new Function<URL, File>() {
@Override
File apply(URL url) {
return new File(url.toURI());
}
}).toList()
return classpathUrls.findAll {classpathFilter.test(it) }.collect {
new File(it.toURI())
}
}

private static List<URL> getClasspathUrls(ClassLoader classLoader) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package nebula.test.functional.internal.classpath

import com.google.common.base.Predicate
import groovy.transform.CompileStatic
import nebula.test.functional.internal.GradleHandle
import nebula.test.functional.internal.GradleHandleFactory
import org.gradle.util.GFileUtils

import java.util.function.Predicate

@CompileStatic
class ClasspathInjectingGradleHandleFactory implements GradleHandleFactory {
private final ClassLoader classLoader;
Expand Down
17 changes: 7 additions & 10 deletions src/test/groovy/nebula/test/functional/GradleRunnerSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package nebula.test.functional

import com.google.common.base.StandardSystemProperty
import com.google.common.collect.FluentIterable
import org.junit.Rule
import org.junit.contrib.java.lang.system.EnvironmentVariables
import spock.lang.Issue
import spock.lang.Specification

/**
Expand All @@ -28,7 +25,7 @@ import spock.lang.Specification
class GradleRunnerSpec extends Specification {
List<URL> classpath

def workDir = new File(StandardSystemProperty.USER_DIR.value())
def workDir = new File(System.getProperty("user.dir"))
def siblingDir = new File(workDir.parentFile, 'sibling')
def sharedDependencyCache = new File(workDir.parentFile, 'sharedDependencyCache')

Expand Down Expand Up @@ -110,7 +107,7 @@ class GradleRunnerSpec extends Specification {

def 'gradle distribution predicate matches expected files'() {
expect:
def filtered = FluentIterable.from(classpath).filter(GradleRunner.CLASSPATH_GRADLE_CACHE).toList()
def filtered = classpath.findAll {GradleRunner.CLASSPATH_GRADLE_CACHE.test(it) }
filtered.size() == 3
}

Expand All @@ -120,33 +117,33 @@ class GradleRunnerSpec extends Specification {
environmentVariables.set("GRADLE_RO_DEP_CACHE", sharedDependencyCache.absolutePath)

expect:
def filtered = FluentIterable.from(classpath).filter(GradleRunner.CLASSPATH_GRADLE_CACHE).toList()
def filtered = classpath.findAll {GradleRunner.CLASSPATH_GRADLE_CACHE.test(it) }
filtered.size() == 3
filtered.any { it.file.contains('commons-lang-2.6') }
}

def 'jvm predicate matches expected files'() {
expect:
def filtered = FluentIterable.from(classpath).filter(GradleRunner.CLASSPATH_PROJECT_DIR).toList()
def filtered = classpath.findAll {GradleRunner.CLASSPATH_PROJECT_DIR.test(it) }
filtered.size() == 15
}

def 'project dependencies matches expected files'() {
expect:
def filtered = FluentIterable.from(classpath).filter(GradleRunner.CLASSPATH_PROJECT_DEPENDENCIES).toList()
def filtered = classpath.findAll {GradleRunner.CLASSPATH_PROJECT_DEPENDENCIES.test(it) }
filtered.size() == 14
}

def 'maven local dependencies matches expected files'() {
expect:
def filtered = FluentIterable.from(classpath).filter(GradleRunner.MAVEN_LOCAL).toList()
def filtered = classpath.findAll {GradleRunner.MAVEN_LOCAL.test(it) }
filtered.size() == 1
filtered.every {it.file.contains(".m2/repository") }
}

def 'default classpath matches only application class paths and dependencies'() {
expect:
def filtered = FluentIterable.from(classpath).filter(GradleRunner.CLASSPATH_DEFAULT).toList()
def filtered = classpath.findAll {GradleRunner.CLASSPATH_DEFAULT.test(it) }
filtered.size() == 26
}
}
2 changes: 1 addition & 1 deletion src/test/groovy/nebula/test/functional/SomePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SomePlugin implements Plugin<Project> {

doLast {
new Thing() // Class in another package
org.apache.commons.io.FileUtils // is a compile dependency, test it's available
org.hamcrest.Matcher // is a compile dependency, test it's available
project.logger.quiet "I ran!"
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/groovy/nebula/test/functional/TestSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package nebula.test.functional

import nebula.test.functional.foo.Thing
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
Expand Down

0 comments on commit 9dbf478

Please sign in to comment.