Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/maiflai/gradle-scalatest
Browse files Browse the repository at this point in the history
…into verbosity

Conflicts:
	src/main/groovy/com/github/maiflai/ScalaTestAction.groovy
	src/test/groovy/com/github/maiflai/ScalaTestActionTest.groovy
  • Loading branch information
maiflai committed May 18, 2016
2 parents 25a577e + c9e5a3c commit a8739c3
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 7 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,23 @@ This can also be supplied on the command line:

```
./gradlew test --tests MyTest
```

ConfigMap
---------
Additional configuration can be passed to Scalatest using the [config map](http://www.scalatest.org/user_guide/using_the_runner#configMapSection)

```groovy
test {
config 'db.name', 'testdb'
}
```

```groovy
test {
configMap([
'db.name': 'testdb'
'server': '192.168.1.188'
])
}
```
33 changes: 33 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.gradle.publish:plugin-publish-plugin:0.9.2"
}
}
plugins {
id "com.jfrog.bintray" version "1.0"
}

apply plugin: 'com.gradle.plugin-publish'
apply plugin: 'signing'
apply plugin: 'maven'
apply plugin: 'groovy'
Expand All @@ -13,6 +24,12 @@ ext {
sourceUrl = 'https://github.com/maiflai/gradle-scalatest.git'
}

if (project.version == 'unspecified') {
version = '0.1-SNAPSHOT'
}

task showVersion() << { println(project.version) }

targetCompatibility = '1.6'

dependencies {
Expand All @@ -30,6 +47,8 @@ task sourcesJar(type: Jar) {
classifier = 'sources'
}

test.dependsOn(jar)

artifacts {
archives jar
archives groovydocJar
Expand All @@ -48,6 +67,20 @@ def propOrDefault(String property) {
}
}

pluginBundle {
website = project.websiteUrl
vcsUrl = project.sourceUrl
description = project.description
tags = ['testing', 'scala', 'scalatest']

plugins {
scalatestPlugin {
id = 'com.github.maiflai.scalatest'
displayName = 'Gradle ScalaTest plugin'
}
}
}

bintray {
user = propOrDefault('bintrayUsername')
key = propOrDefault('bintrayApiKey')
Expand Down
17 changes: 17 additions & 0 deletions src/main/groovy/com/github/maiflai/JacocoTestAction.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.maiflai

import org.gradle.api.Action
import org.gradle.api.tasks.testing.Test

class JacocoTestAction implements Action<Test> {

@Override
void execute(Test task) {
// java.lang.InternalError: Malformed class name when finding by type
def jacoco = task.extensions.findByName('jacoco')
if (jacoco && jacoco.enabled) {
task.jvmArgs jacoco.getAsJvmArg()
}
}

}
12 changes: 11 additions & 1 deletion src/main/groovy/com/github/maiflai/ScalaTestAction.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ScalaTestAction implements Action<Test> {

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

@Override
void execute(Test t) {
Expand Down Expand Up @@ -58,6 +59,7 @@ class ScalaTestAction implements Action<Test> {
FileResolver fileResolver = t.getServices().get(FileResolver.class);
JavaExecAction javaExecHandleBuilder = new DefaultJavaExecAction(fileResolver);
javaExecHandleBuilder.setMain('org.scalatest.tools.Runner')
javaExecHandleBuilder.setEnvironment(t.getEnvironment())
javaExecHandleBuilder.setClasspath(t.getClasspath())
javaExecHandleBuilder.setJvmArgs(t.getAllJvmArgs())
javaExecHandleBuilder.setArgs(getArgs(t))
Expand All @@ -78,6 +80,8 @@ class ScalaTestAction implements Action<Test> {
break;
case TestLogEvent.SKIPPED: 'XER' // ignored and pending and scope
break;
case TestLogEvent.FAILED: ''
break;
case TestLogEvent.STANDARD_OUT:
case TestLogEvent.STANDARD_ERROR: 'OM' // infoprovided, markupprovided
break;
Expand Down Expand Up @@ -108,8 +112,10 @@ class ScalaTestAction implements Action<Test> {
}
}

static String durations = 'D'

static String reporting(Test t) {
'-o' + ((dropped(t) + color(t) + exceptions(t)) as List).unique().sort().join('')
'-o' + ((dropped(t) + color(t) + exceptions(t) + durations) as List).unique().sort().join('')
}

private static Iterable<String> getArgs(Test t) {
Expand Down Expand Up @@ -153,6 +159,10 @@ class ScalaTestAction implements Action<Test> {
args.add('-s')
args.add(it)
}
def config = t.extensions.findByName(CONFIG) as Map<String, ?>
config?.entrySet()?.each { entry ->
args.add("-D${entry.key}=${entry.value}")
}
return args
}
}
10 changes: 9 additions & 1 deletion src/main/groovy/com/github/maiflai/ScalaTestPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ class ScalaTestPlugin implements Plugin<Project> {
t.tasks.withType(Test) { test ->
test.maxParallelForks = Runtime.runtime.availableProcessors()
//noinspection GroovyAssignabilityCheck
test.actions = [new ScalaTestAction()]
test.actions = [
new JacocoTestAction(),
new ScalaTestAction()
]
test.testLogging.showCauses = false
test.extensions.add(ScalaTestAction.TAGS, new PatternSet())
List<String> suites = []
test.extensions.add(ScalaTestAction.SUITES, suites)
test.extensions.add("suite", { String name -> suites.add(name) } )
test.extensions.add("suites", { String... name -> suites.addAll(name) } )
Map<String, ?> config = [:]
test.extensions.add(ScalaTestAction.CONFIG, config)
test.extensions.add("config", { String name, value -> config.put(name, value) } )
test.extensions.add("configMap", { Map<String, ?> c -> config.putAll(c) } )
if (test.name != JavaPlugin.TEST_TASK_NAME) {
test.reports.html.destination = project.reporting.file(test.name)
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/examples/jacoco/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
buildscript {
repositories {
// need to get up to the working directory of gradle-plugins build
flatDir dir: "${project.projectDir}/../../../../build/libs"
}
dependencies {
classpath name: 'gradle-scalatest', version: '+'
}
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'com.github.maiflai.scalatest'

repositories {
mavenCentral()
}

dependencies {
compile 'org.scala-lang:scala-library:2.11.6'
testCompile 'org.scalatest:scalatest_2.11:2.2.5'
testRuntime 'org.pegdown:pegdown:1.1.0'
}
5 changes: 5 additions & 0 deletions src/test/examples/jacoco/src/main/java/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Hello {
public String say() {
return "world";
}
}
7 changes: 7 additions & 0 deletions src/test/examples/jacoco/src/test/scala/HelloSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import org.scalatest.FunSuite

class HelloSpec extends FunSuite {
test("it should cover") {
assert(new Hello().say() === "world")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.maiflai

import org.gradle.tooling.BuildLauncher
import org.gradle.tooling.GradleConnector
import org.hamcrest.Description
import org.hamcrest.TypeSafeMatcher
import org.junit.Test

import static org.hamcrest.MatcherAssert.assertThat

class JacocoTestActionIntegrationTest {

@Test
public void testReportsAreProduced() throws Exception {
def launcher = setupBuild(new File('src/test/examples/jacoco'))
launcher.forTasks('clean', 'test', 'jacoco').run()
assertThat(new File('src/test/examples/jacoco/build/reports/jacoco/test/html'), isReport)
assertThat(new File('src/test/examples/jacoco/build/reports/tests'), isReport)
}

protected BuildLauncher setupBuild(File projectRoot) {
return GradleConnector.
newConnector().
forProjectDirectory(projectRoot).
connect().
newBuild()
}

protected TypeSafeMatcher<File> isReport = new TypeSafeMatcher<File>() {
@Override
protected boolean matchesSafely(File file) {
return file.isDirectory() && new File(file, 'index.html').isFile()
}

@Override
void describeTo(Description description) {
description.appendText('a directory containing index.html')
}
}
}
47 changes: 42 additions & 5 deletions src/test/groovy/com/github/maiflai/ScalaTestActionTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.gradle.api.Task
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.process.internal.JavaExecAction
import org.gradle.testfixtures.ProjectBuilder
import org.hamcrest.CoreMatchers
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
Expand Down Expand Up @@ -48,18 +49,30 @@ class ScalaTestActionTest {
}
}

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

@Test
public void environmentVariableIsCopied() {
Task test = testTask()
test.environment.put('a', 'b')
assertThat(environment(test).get('a') as String, equalTo('b'))
}

@Test
public void colorOutputIsDisabled() {
Task test = testTask()
test.getProject().getGradle().startParameter.setColorOutput(false)
assertThat(commandLine(test), hasItem("-oDW".toString()))
assertThat(commandLine(test), hasItem("-oDSW".toString()))
}

@Test
public void colorOutputIsEnabled() {
Task test = testTask()
test.getProject().getGradle().startParameter.setColorOutput(true)
assertThat(commandLine(test), hasItem("-oD".toString()))
assertThat(commandLine(test), hasItem("-oDS".toString()))
}

@Test
Expand Down Expand Up @@ -195,17 +208,41 @@ class ScalaTestActionTest {
@Test
public void testKnockout() throws Exception {
assertThat(other([TestLogEvent.FAILED] as Set),
is([TestLogEvent.STARTED, TestLogEvent.PASSED, TestLogEvent.SKIPPED] as Set))
not(hasItem(TestLogEvent.FAILED)))

assertThat(other([TestLogEvent.PASSED, TestLogEvent.FAILED] as Set),
is([TestLogEvent.STARTED, TestLogEvent.SKIPPED] as Set))
both(not(hasItem(TestLogEvent.PASSED))).and(not(hasItem(TestLogEvent.PASSED))))
}

@Test
public void failedOnlyReporting() throws Exception {
Task test = testTask()
test.testLogging.events = [TestLogEvent.FAILED]
assertThat(commandLine(test), hasOutput('oCEFHLMNOPQRX'))
assertThat(commandLine(test), hasOutput('oCDEHLMNOPQRSX'))

}

@Test
public void configString() throws Exception {
Task test = testTask()
test.config 'a', 'b'
def args = commandLine(test)
assertThat(args, hasItem('-Da=b'))
}

@Test
public void configNumber() throws Exception {
Task test = testTask()
test.config 'a', 1
def args = commandLine(test)
assertThat(args, hasItem('-Da=1'))
}

@Test
public void configMap() throws Exception {
Task test = testTask()
test.configMap([a:'b', c:1])
def args = commandLine(test)
assertThat(args, both(hasItem('-Da=b')).and(hasItem("-Dc=1")))
}
}

0 comments on commit a8739c3

Please sign in to comment.