From 6d5c8e5a5d59551516bbbc9fcab1ccf41bfdd13c Mon Sep 17 00:00:00 2001 From: Schalk Cronje Date: Sun, 5 May 2019 11:33:00 +0200 Subject: [PATCH] Restructure JRubyExecExtensionIntegrationSpec --- .gitignore | 1 + HACKING.md | 10 +- base-plugin/build.gradle | 7 +- .../JRubyExecExtensionIntegrationSpec.groovy | 237 +++++++++--------- .../IntegrationSpecification.groovy | 77 ++++++ gradle.properties | 1 + 6 files changed, 214 insertions(+), 119 deletions(-) create mode 100644 base-plugin/src/integTest/groovy/com/github/jrubygradle/testhelper/IntegrationSpecification.groovy diff --git a/.gitignore b/.gitignore index 5cdc24a..5494076 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ build/ .idea .awestruct/ out/ +.testconfig diff --git a/HACKING.md b/HACKING.md index 6945225..9a5a622 100644 --- a/HACKING.md +++ b/HACKING.md @@ -12,9 +12,11 @@ Please do not commit the config file back with DEBUG turned on. === Running single test suites -If you only want to run the unittests in say `JRubyPlugin` then you can do `./gradlew test -Dtest.single=JRubyPlugin`. -In a similar manner for integration tests one can do `./gradlew integrationTest -Dtest.single=JRubyIntegrationSpec`. +If you only want to run the unittests in say `JRubyPlugin` then you can do `./gradlew test --tests JRubyPlugin`. +In a similar manner for integration tests one can do `./gradlew integrationTest --tests JRubyIntegrationSpec`. -== Release HOWTO +=== Running tests in IntelliJ -*TBC* \ No newline at end of file +Go to `File` -> `Settings` -> `Build, Execution, Deployment` -> `Gradle` -> `Runner`, then check `Delete build/run actions to Gradle` and select `Platform Test Runner`. + +On a Mac use `IntelliJ IDEA` -> `Preferences` instead of `File/Settings`. \ No newline at end of file diff --git a/base-plugin/build.gradle b/base-plugin/build.gradle index 927ac09..37d5943 100644 --- a/base-plugin/build.gradle +++ b/base-plugin/build.gradle @@ -70,8 +70,8 @@ dependencies { // NOTE: older jruby versions needs this for exec to work properly integrationTestGems "rubygems:jar-dependencies:0.1.15" - // NOTE: If you change this, you will also need to update JRubyExecExtensionIntegrationSpec - integrationTestGems 'org.bouncycastle:bcprov-jdk15on:1.46' + // This is used by JRubyExecExtensionIntegrationSpec + integrationTestGems "org.bouncycastle:bcprov-jdk15on:${bcprovVersion}" gradleTest"org.jruby:jruby-complete:${jrubyVersion}" gradleTest 'rubygems:credit_card_validator:1.1.0' @@ -90,7 +90,8 @@ test { generateTestConfig { testProperties mavenrepo: file('src/integTest/mavenrepo').absolutePath, - flatrepo: flatRepoDir.absolutePath + flatrepo: flatRepoDir.absolutePath, + bcprovVersion: bcprovVersion } task copyIntegrationTestJRuby (type:Copy) { diff --git a/base-plugin/src/integTest/groovy/com/github/jrubygradle/JRubyExecExtensionIntegrationSpec.groovy b/base-plugin/src/integTest/groovy/com/github/jrubygradle/JRubyExecExtensionIntegrationSpec.groovy index 6102140..3e29d6b 100644 --- a/base-plugin/src/integTest/groovy/com/github/jrubygradle/JRubyExecExtensionIntegrationSpec.groovy +++ b/base-plugin/src/integTest/groovy/com/github/jrubygradle/JRubyExecExtensionIntegrationSpec.groovy @@ -1,167 +1,180 @@ package com.github.jrubygradle -import com.github.jrubygradle.testhelper.BasicProjectBuilder -import com.github.jrubygradle.testhelper.VersionFinder -import org.gradle.api.Project -import spock.lang.* +import com.github.jrubygradle.testhelper.IntegrationSpecification +import org.gradle.testkit.runner.BuildResult +import spock.lang.Shared /** * @author Schalk W. Cronjé */ -class JRubyExecExtensionIntegrationSpec extends Specification { +class JRubyExecExtensionIntegrationSpec extends IntegrationSpecification { - static final File CACHEDIR = new File( System.getProperty('TEST_CACHEDIR') ?: 'build/tmp/integrationTest/cache') - static final File FLATREPO = new File( System.getProperty('TEST_FLATREPO') ?: 'build/tmp/integrationTest/flatRepo') - static final boolean TESTS_ARE_OFFLINE = System.getProperty('TESTS_ARE_OFFLINE') != null - static final File TEST_SCRIPT_DIR = new File( System.getProperty('TEST_SCRIPT_DIR') ?: 'src/integTest/resources/scripts').absoluteFile - static final File TESTROOT = new File("${System.getProperty('TESTROOT') ?: 'build/tmp/test/integration-tests'}/jreeis").absoluteFile - static final File TEST_JARS_DIR = new File(TESTROOT, "build/tmp/jrubyExec/jars") + static final String DEFAULT_TASK_NAME = 'inlineJRubyExec' + static final String HELLO_WORLD = 'helloWorld.rb' + static final String HELLO_NAME = 'helloName.rb' + static final String REQUIRES_GEM = 'requiresGem.rb' + static final String ENV_VARS = 'envVars.rb' + static final String BCPROV_NAME = 'bcprov-jdk15on' - Project project - ByteArrayOutputStream output = new ByteArrayOutputStream() + void "Run a script with minimum parameters"() { + setup: + useScript(HELLO_WORLD, 'src') + createJRubyExecProject """ + script 'src/${HELLO_WORLD}' + """ - String getOutputBuffer() { - return output.toString() - } - void setup() { - if (TESTROOT.exists()) { - TESTROOT.deleteDir() - } - TESTROOT.mkdirs() - project = BasicProjectBuilder.buildWithLocalRepo(TESTROOT,FLATREPO,CACHEDIR) - - project.evaluate() - } - - def "Run a script with minimum parameters"() { when: "I call jrubyexec with only a script name" - project.jrubyexec { - script "${TEST_SCRIPT_DIR}/helloWorld.rb" - standardOutput output - } + BuildResult result = build() then: "I expect the Ruby script to be executed" - outputBuffer =~ /Hello, World/ + result.output =~ /Hello, World/ } - def "Run an inline script"() { - when: "I call jrubyexec with only a script name" - project.jrubyexec { - jrubyArgs "-e", "puts 'Hello, World'" - standardOutput output - } + void "Run an inline script"() { + setup: + createJRubyExecProject """ + jrubyArgs '-e', "puts 'Hello, World'" + """ + + when: "I call jrubyexec with only script text" + BuildResult result = build() then: "I expect the Ruby script to be executed" - outputBuffer =~ /Hello, World/ + result.output =~ /Hello, World/ } - def "Run a script containing a conditional"() { - when: "we have an 'if' clause" - project.jrubyexec { - script "${TEST_SCRIPT_DIR}/helloName.rb" - if(input == 0) { - scriptArgs 'Stan' - } else { - scriptArgs 'man' - } - standardOutput output - } + void "Run a script with no subpath and arguments"() { + useScript(HELLO_NAME) + createJRubyExecProject """ + script '${HELLO_NAME}' + scriptArgs 'Stan' + """ + + when: + BuildResult result = build() then: "only the appropriate parameters should be passed" - outputBuffer == expected - - where: - input | expected - 0 | "Hello, Stan\n" - 1 | "Hello, man\n" - + result.output =~ /Hello, Stan/ } - def "Running a script that requires a jar"() { - when: - project.with { - dependencies { - jrubyExec VersionFinder.findDependency(FLATREPO,'org.bouncycastle','bcprov-jdk15on','jar') + void "Running a script that requires a jar"() { + setup: + def leadingDir = 'jrubyExec/jars/org/bouncycastle/' + def artifactPath = "${BCPROV_NAME}/${bcprovVer}/${BCPROV_NAME}-${bcprovVer}" + def withPattern = ~/.*\["file:.+${leadingDir}${artifactPath}\.jar"\].*/ - } - jrubyexec { - jrubyArgs '-e' - jrubyArgs 'print $CLASSPATH' - standardOutput output - } - } + def jarToUse = findDependency('org.bouncycastle', BCPROV_NAME, 'jar') + createJRubyExecProject withJarToUse(jarToUse), ''' + jrubyArgs '-e' + jrubyArgs 'print $CLASSPATH' + ''' + + when: + BuildResult result = build() then: - def outputFile = new File(TEST_JARS_DIR, 'org/bouncycastle/bcprov-jdk15on/1.46/bcprov-jdk15on-1.46.jar') - outputBuffer == "[\"${outputFile.toURI().toURL()}\"]" + result.output.readLines().any { it.matches withPattern } } - def "Running a script that requires a gem, a separate jRuby and a separate configuration"() { - when: - project.with { - dependencies { - jrubyExec VersionFinder.findDependency(FLATREPO,'','credit_card_validator','gem') + void "Running a script that requires a gem, a separate jRuby and a separate configuration"() { + setup: + useScript(REQUIRES_GEM) + createJRubyExecProject withCreditCardValidator(), """ + script '${REQUIRES_GEM}' + jrubyArgs '-T1' + """ - } - jrubyexec { - script "${TEST_SCRIPT_DIR}/requiresGem.rb" - standardOutput output - jrubyArgs '-T1' - } - } + when: + BuildResult result = build() then: - outputBuffer =~ /Not valid/ + result.output =~ /Not valid/ } - def "Running a script that requires a gem, a separate jRuby, a separate configuration and a custom gemWorkDir"() { - given: + void "Running a script that requires a gem, a separate jRuby, a separate configuration and a custom gemWorkDir"() { + setup: final String customGemDir = 'customGemDir' + useScript(REQUIRES_GEM) + createJRubyExecProject withCreditCardValidator(), """ + script '${REQUIRES_GEM}' + jrubyArgs '-T1' + gemWorkDir { new File(project.buildDir, '${customGemDir}' ) } + """ when: - project.with { - dependencies { - jrubyExec VersionFinder.findDependency(FLATREPO,'','credit_card_validator','gem') - - } - jrubyexec { - script "${TEST_SCRIPT_DIR}/requiresGem.rb" - standardOutput output - jrubyArgs '-T1' - gemWorkDir "${buildDir}/${customGemDir}" - } - } + BuildResult result = build() then: - outputBuffer =~ /Not valid/ - new File(project.buildDir, customGemDir).exists() - + result.output =~ /Not valid/ + new File(projectDir, "build/${customGemDir}").exists() } - def "Running a script that requires environment variables"() { + void "Running a script that requires environment variables"() { // This tests that the passthrough invocation // happens for overloaded versions of environment // and that the environment variables are passed // on to the script - given: + setup: final String envVarName = 'TEST_ENV_VAR' final String envVarValue = 'Test Value' - final Map envVarMap = [TEST_A: 'A123', TEST_B: 'B123'] + + useScript(ENV_VARS) + createJRubyExecProject """ + environment '${envVarName}', '${envVarValue}' + environment TEST_A: 'A123', TEST_B: 'B123' + script '${ENV_VARS}' + """ when: - project.with { - jrubyexec { - environment envVarName, envVarValue - environment envVarMap - script "${TEST_SCRIPT_DIR}/envVars.rb" - standardOutput output - } - } + BuildResult result = build() + String outputBuffer = result.output then: outputBuffer =~ /TEST_ENV_VAR=Test Value/ outputBuffer =~ /TEST_A=A123/ outputBuffer =~ /TEST_B=B123/ } + + private BuildResult build() { + gradleRunner(DEFAULT_TASK_NAME, '-i').build() + } + + private void createJRubyExecProject(String jrubyexecConfig) { + createJRubyExecProject('', jrubyexecConfig) + } + + private void createJRubyExecProject(String preamble, String jrubyexecConfig) { + buildFile.text = """ + ${projectWithLocalRepo} + + ${preamble} + + task ${DEFAULT_TASK_NAME} { + doLast { + jrubyexec { + ${jrubyexecConfig} + } + } + } + """ + } + + private String withJarToUse(String jarFormat) { + String dependencies = """ + dependencies { + jrubyExec ${jarFormat} + } + """ + } + + private String withCreditCardValidator() { + withJarToUse(findDependency('', 'credit_card_validator', 'gem')) + } + + private String getBcprovVer() { + testProperties.bcprovVersion + } + + } diff --git a/base-plugin/src/integTest/groovy/com/github/jrubygradle/testhelper/IntegrationSpecification.groovy b/base-plugin/src/integTest/groovy/com/github/jrubygradle/testhelper/IntegrationSpecification.groovy new file mode 100644 index 0000000..2ee5b59 --- /dev/null +++ b/base-plugin/src/integTest/groovy/com/github/jrubygradle/testhelper/IntegrationSpecification.groovy @@ -0,0 +1,77 @@ +package com.github.jrubygradle.testhelper + +import org.gradle.testkit.runner.GradleRunner +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Shared +import spock.lang.Specification + + +class IntegrationSpecification extends Specification { + + @Shared + Map testProperties + @Shared + File flatRepoLocation + + @Rule + TemporaryFolder testFolder + + File projectDir + File buildFile + File settingsFile + + void setupSpec() { + testProperties = loadTestProperties() + flatRepoLocation = new File(testProperties.flatrepo) + } + + void setup() { + projectDir = testFolder.root + buildFile = new File(projectDir, 'build.gradle') + settingsFile = new File(projectDir, 'settings.gradle') + + settingsFile.text = '' + } + + void useScript(final String name, final String relativePath = null) { + File destination = new File(testFolder.root, relativePath ? "${relativePath}/${name}" : name) + destination.parentFile.mkdirs() + destination.text = this.class.getResource("/scripts/${name}").text + } + + String findDependency(final String organisation, final String artifact, final String extension) { + "'${VersionFinder.findDependency(flatRepoLocation, organisation, artifact, extension)}'" + } + + String getProjectWithLocalRepo() { + """ + plugins { + id 'com.github.jruby-gradle.base' + } + + jruby.defaultRepositories = false + repositories.flatDir dirs: '${flatRepoLocation.absolutePath}' + """ + } + + GradleRunner gradleRunner(List args) { + GradleRunner.create() + .withProjectDir(projectDir) + .withDebug(true) + .withArguments(args) + .withPluginClasspath() + } + + GradleRunner gradleRunner(String... args) { + gradleRunner(args as List) + } + + private Map loadTestProperties() { + this.class.getResource('/jruby-gradle-testconfig.properties').withInputStream { strm -> + Properties props = new Properties() + props.load(strm) + props as Map + } + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 153636c..c019351 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,5 @@ targetCompatibility=1.8 sourceCompatibility=1.8 jrubyVersion=9.2.7.0 jettyVersion=9.2.12.v20150709 +bcprovVersion=1.46 torqueboxProxy=http://rubygems-proxy.torquebox.org/releases