Skip to content

Commit

Permalink
Merge pull request #80 from rspieldenner/testkit
Browse files Browse the repository at this point in the history
GradleTestKit Helper
  • Loading branch information
rspieldenner committed Mar 2, 2017
2 parents a668802 + 1290553 commit ea138f3
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 267 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
5.1.0 / 2017-03-01
==================

* Add IntegrationTestKitSpec -- helper for Gradle TestKit

5.0.1 / 2017-02-14
==================

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ By contributing your code, you agree to license your contribution under the term

```
/**
* Copyright 2015 the original author or authors.
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gradle Compatibility Tested
LICENSE
=======

Copyright 2014-2016 Netflix, Inc.
Copyright 2014-2017 Netflix, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Netflix, Inc.
* Copyright 2014-2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@


plugins {
id 'nebula.plugin-plugin' version '5.1.0'
id 'nebula.plugin-plugin' version '5.1.1'
id 'org.ysb33r.gradletest' version '0.5.4'
}

Expand All @@ -34,6 +34,7 @@ contacts {
}

dependencies {
compile gradleTestKit()
compile 'com.google.guava:guava:19.0'
compile 'commons-io:commons-io:2.5'
compile ('org.spockframework:spock-core:1.0-groovy-2.4') {
Expand Down

This file was deleted.

This file was deleted.

8 changes: 6 additions & 2 deletions src/main/groovy/nebula/test/AbstractProjectSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package nebula.test

import com.energizedwork.spock.extensions.TempDirectory
import groovy.transform.CompileStatic
import nebula.test.multiproject.MultiProjectHelper
import org.gradle.api.Project
Expand All @@ -21,14 +20,19 @@ import spock.lang.Specification
@CompileStatic
public abstract class AbstractProjectSpec extends Specification {
static final String CLEAN_PROJECT_DIR_SYS_PROP = 'cleanProjectDir'
@TempDirectory File ourProjectDir
File ourProjectDir

@Rule TestName testName = new TestName()
String canonicalName
Project project
MultiProjectHelper helper

def setup() {
ourProjectDir = new File("build/nebulatest/${this.class.canonicalName}/${testName.methodName.replaceAll(/\W+/, '-')}")
if (ourProjectDir.exists()) {
ourProjectDir.deleteDir()
}
ourProjectDir.mkdirs()
canonicalName = testName.getMethodName().replaceAll(' ', '-')
project = ProjectBuilder.builder().withName(canonicalName).withProjectDir(ourProjectDir).build()
helper = new MultiProjectHelper(project)
Expand Down
125 changes: 125 additions & 0 deletions src/main/groovy/nebula/test/BaseIntegrationSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2013-2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nebula.test

import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.junit.Rule
import org.junit.rules.TestName
import spock.lang.Specification

abstract class BaseIntegrationSpec extends Specification {
@Rule TestName testName = new TestName()
File projectDir

def setup() {
projectDir = new File("build/nebulatest/${this.class.canonicalName}/${testName.methodName.replaceAll(/\W+/, '-')}")
if (projectDir.exists()) {
projectDir.deleteDir()
}
projectDir.mkdirs()
}

/* Setup */
protected File directory(String path, File baseDir = getProjectDir()) {
new File(baseDir, path).with {
mkdirs()
it
}
}

protected File file(String path, File baseDir = getProjectDir()) {
def splitted = path.split('/')
def directory = splitted.size() > 1 ? directory(splitted[0..-2].join('/'), baseDir) : baseDir
def file = new File(directory, splitted[-1])
file.createNewFile()
file
}

@CompileStatic(TypeCheckingMode.SKIP)
protected File createFile(String path, File baseDir = getProjectDir()) {
File file = file(path, baseDir)
if (!file.exists()) {
assert file.parentFile.mkdirs() || file.parentFile.exists()
file.createNewFile()
}
file
}

protected void writeHelloWorld(String packageDotted, File baseDir = getProjectDir()) {
def path = 'src/main/java/' + packageDotted.replace('.', '/') + '/HelloWorld.java'
def javaFile = createFile(path, baseDir)
javaFile << """\
package ${packageDotted};
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello Integration Test");
}
}
""".stripIndent()
}

/**
* Creates a unit test for testing your plugin.
* @param failTest true if you want the test to fail, false if the test should pass
* @param baseDir the directory to begin creation from, defaults to projectDir
*/
protected void writeUnitTest(boolean failTest, File baseDir = getProjectDir()) {
writeTest('src/test/java/', 'nebula', failTest, baseDir)
}

/**
*
* Creates a unit test for testing your plugin.
* @param srcDir the directory in the project where the source file should be created.
* @param packageDotted the package for the unit test class, written in dot notation (ex. - nebula.integration)
* @param failTest true if you want the test to fail, false if the test should pass
* @param baseDir the directory to begin creation from, defaults to projectDir
*/
protected void writeTest(String srcDir, String packageDotted, boolean failTest, File baseDir = getProjectDir()) {
def path = srcDir + packageDotted.replace('.', '/') + '/HelloWorldTest.java'
def javaFile = createFile(path, baseDir)
javaFile << """\
package ${packageDotted};
import org.junit.Test;
import static org.junit.Assert.assertFalse;
public class HelloWorldTest {
@Test public void doesSomething() {
assertFalse( $failTest );
}
}
""".stripIndent()
}

/**
* Creates a properties file to included as project resource.
* @param srcDir the directory in the project where the source file should be created.
* @param fileName to be used for the file, sans extension. The .properties extension will be added to the name.
* @param baseDir the directory to begin creation from, defaults to projectDir
*/
protected void writeResource(String srcDir, String fileName, File baseDir = getProjectDir()) {
def path = "$srcDir/${fileName}.properties"
def resourceFile = createFile(path, baseDir)
resourceFile.text = "firstProperty=foo.bar"
}

protected void addResource(String srcDir, String filename, String contents, File baseDir = getProjectDir()) {
def resourceFile = createFile("${srcDir}/${filename}", baseDir)
resourceFile.text = contents
}
}
Loading

0 comments on commit ea138f3

Please sign in to comment.