Skip to content

Commit

Permalink
Merge pull request #56 from sghill/4.5-compatibility
Browse files Browse the repository at this point in the history
Gradle 4.5 compatibility
  • Loading branch information
maiflai authored Jan 15, 2018
2 parents 99440a8 + 1c9a3fc commit 6c512b7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.maiflai

import groovy.transform.Immutable
import org.gradle.api.internal.file.FileResolver
import org.gradle.process.internal.DefaultExecActionFactory
import org.gradle.process.internal.DefaultJavaExecAction
import org.gradle.process.internal.JavaExecAction
import org.gradle.util.GradleVersion

@Immutable
class BackwardsCompatibleJavaExecActionFactory {
String gradleVersion

JavaExecAction create(FileResolver fileResolver) {
boolean hasExecFactory = GradleVersion.version(gradleVersion) > GradleVersion.version("4.4.1")
if (hasExecFactory) {
new DefaultExecActionFactory(fileResolver).newJavaExecAction()
} else {
new DefaultJavaExecAction(fileResolver)
}
}
}
10 changes: 6 additions & 4 deletions src/main/groovy/com/github/maiflai/ScalaTestAction.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.maiflai

import groovy.transform.Immutable
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.internal.file.FileResolver
Expand All @@ -10,7 +11,6 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.api.tasks.util.PatternSet
import org.gradle.internal.UncheckedException
import org.gradle.process.internal.DefaultJavaExecAction
import org.gradle.process.internal.JavaExecAction

/**
Expand All @@ -19,15 +19,17 @@ import org.gradle.process.internal.JavaExecAction
* <p>Classpath, JVM Args and System Properties are propagated.</p>
* <p>Tests are launched against the testClassesDir.</p>
*/
@Immutable
class ScalaTestAction implements Action<Test> {

static String TAGS = 'tags'
static String SUITES = '_suites'
static String CONFIG = '_config'
BackwardsCompatibleJavaExecActionFactory factory

@Override
void execute(Test t) {
def result = makeAction(t).execute()
def result = makeAction(t, factory).execute()
if (result.exitValue != 0){
handleTestFailures(t)
}
Expand Down Expand Up @@ -61,9 +63,9 @@ class ScalaTestAction implements Action<Test> {
}


static JavaExecAction makeAction(Test t) {
static JavaExecAction makeAction(Test t, BackwardsCompatibleJavaExecActionFactory factory) {
FileResolver fileResolver = t.getServices().get(FileResolver.class)
JavaExecAction javaExecHandleBuilder = new DefaultJavaExecAction(fileResolver)
JavaExecAction javaExecHandleBuilder = factory.create(fileResolver)
t.copyTo(javaExecHandleBuilder)
javaExecHandleBuilder.setMain('org.scalatest.tools.Runner')
javaExecHandleBuilder.setClasspath(t.getClasspath())
Expand Down
13 changes: 7 additions & 6 deletions src/main/groovy/com/github/maiflai/ScalaTestPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,30 @@ class ScalaTestPlugin implements Plugin<Project> {
static enum Mode {
replaceAll, replaceOne, append
}
BackwardsCompatibleJavaExecActionFactory factory

@Override
void apply(Project t) {
if (!t.plugins.hasPlugin(ScalaTestPlugin)) {
t.plugins.add(this)
factory = new BackwardsCompatibleJavaExecActionFactory(t.gradle.gradleVersion)
t.plugins.apply(JavaPlugin)
t.plugins.apply(ScalaPlugin)
switch (getMode(t)) {
case Mode.replaceAll:
t.tasks.withType(Test) { configure(it) }
t.tasks.withType(Test) { configure(it, factory) }
break
case Mode.replaceOne:
t.tasks.withType(Test) {
if (it.name == JavaPlugin.TEST_TASK_NAME) {
configure(it)
configure(it, factory)
}
}
break
case Mode.append:
configure(t.tasks.create(
name: 'scalatest', type: Test, group: 'verification',
description: 'Run scalatest unit tests',
dependsOn: t.tasks.testClasses) as Test)
dependsOn: t.tasks.testClasses) as Test, factory)
break
}
}
Expand All @@ -55,12 +56,12 @@ class ScalaTestPlugin implements Plugin<Project> {
}
}

static void configure(Test test) {
static void configure(Test test, BackwardsCompatibleJavaExecActionFactory factory) {
test.maxParallelForks = Runtime.runtime.availableProcessors()
//noinspection GroovyAssignabilityCheck
test.actions = [
new JacocoTestAction(),
new ScalaTestAction()
new ScalaTestAction(factory)
]
test.testLogging.exceptionFormat = TestExceptionFormat.SHORT
test.extensions.add(ScalaTestAction.TAGS, new PatternSet())
Expand Down
8 changes: 4 additions & 4 deletions src/test/groovy/com/github/maiflai/ScalaTestActionTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
import org.junit.Test
import org.junit.runners.Parameterized

import static com.github.maiflai.ScalaTestAction.other
import static org.hamcrest.CoreMatchers.*
Expand All @@ -20,6 +19,7 @@ import static org.hamcrest.core.Is.is
import static org.junit.Assert.assertThat

class ScalaTestActionTest {
static FACTORY = new BackwardsCompatibleJavaExecActionFactory(ProjectBuilder.builder().build().gradle.gradleVersion)

private static Project testProject() {
Project project = ProjectBuilder.builder().build()
Expand All @@ -32,7 +32,7 @@ class ScalaTestActionTest {
}

private static List<String> commandLine(org.gradle.api.tasks.testing.Test task) {
JavaExecAction action = ScalaTestAction.makeAction(task)
JavaExecAction action = ScalaTestAction.makeAction(task, FACTORY)
action.getCommandLine()
}

Expand All @@ -52,15 +52,15 @@ class ScalaTestActionTest {
}

private static Map<String, Object> environment(org.gradle.api.tasks.testing.Test task) {
JavaExecAction action = ScalaTestAction.makeAction(task)
JavaExecAction action = ScalaTestAction.makeAction(task, FACTORY)
action.getEnvironment()
}

@Test
void workingDirectoryIsHonoured() throws Exception {
Task test = testTask()
test.workingDir = '/tmp'
JavaExecAction action = ScalaTestAction.makeAction(test)
JavaExecAction action = ScalaTestAction.makeAction(test, FACTORY)
assertThat(action.workingDir, equalTo(new File('/tmp')))
}

Expand Down

0 comments on commit 6c512b7

Please sign in to comment.