From de8f91d7b392cf2903f73d3ecdb7cf7dcccc246d Mon Sep 17 00:00:00 2001 From: Christian Meier Date: Sun, 19 Jul 2015 14:09:43 +0200 Subject: [PATCH] factored out some RSpec code and added configuration to RSpec --- .../jrubygradle/rspec/JRubyRSpecPlugin.groovy | 5 +- .../jrubygradle/rspec/JRubyUtils.groovy | 61 +++++++++++++++++++ .../com/github/jrubygradle/rspec/RSpec.groovy | 41 +++++-------- .../rspec/JRubyRSpecPluginSpec.groovy | 59 +++++++++++------- .../resources/jruby-version/spec/one_spec.rb | 5 ++ 5 files changed, 120 insertions(+), 51 deletions(-) create mode 100644 jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyUtils.groovy create mode 100644 jruby-gradle-rspec-plugin/src/test/resources/jruby-version/spec/one_spec.rb diff --git a/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyRSpecPlugin.groovy b/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyRSpecPlugin.groovy index 99d5ede..a0fae58 100644 --- a/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyRSpecPlugin.groovy +++ b/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyRSpecPlugin.groovy @@ -25,9 +25,8 @@ class JRubyRSpecPlugin implements Plugin { void addAfterEvaluateHooks(Project project) { project.afterEvaluate { project.tasks.withType(RSpec) { Task task -> - project.configurations.maybeCreate(task.name) - project.dependencies.add(task.name, "org.jruby:jruby-complete:${task.jrubyVersion}") - project.dependencies.add(task.name, "rubygems:rspec:${task.version}") + project.dependencies.add(task.configuration.name, "org.jruby:jruby-complete:${task.jrubyVersion}") + project.dependencies.add(task.configuration.name, "rubygems:rspec:${task.version}") } } } diff --git a/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyUtils.groovy b/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyUtils.groovy new file mode 100644 index 0000000..cc49e0b --- /dev/null +++ b/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/JRubyUtils.groovy @@ -0,0 +1,61 @@ +package com.github.jrubygradle.rspec + +import com.github.jrubygradle.GemUtils +import com.github.jrubygradle.internal.JRubyExecUtils +import groovy.transform.PackageScope +import org.gradle.api.Incubating +import org.gradle.api.InvalidUserDataException +import org.gradle.api.DefaultTask +import org.gradle.api.Project +//import org.gradle.api.Configuration +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction +import org.gradle.api.artifacts.Configuration + + +/** + * @author Christian Meier + */ +public class JRubyUtils { + + private final Project project; + private final String name; + private final Configuration config; + private final File gemDir; + private final File jrubyCompleteJar; + + public JRubyUtils(Project project, Configuration config, String name){ + this.project = project + this.config = config + if (config == null) throw new RuntimeException() + this.gemDir = new File(project.buildDir, "gems-${name}") + this.jrubyCompleteJar = JRubyExecUtils.jrubyJar(config) + } + + public void setupGemsAndJars() { + // TODO would be nice to just pass-in the jrubyCompleteJar File here + GemUtils.extractGems(project, config, config, gemDir, GemUtils.OverwriteAction.SKIP) + GemUtils.setupJars(config, gemDir, GemUtils.OverwriteAction.OVERWRITE) + } + + public void exec(String... arguments) { + project.javaexec { + classpath jrubyCompleteJar.absolutePath + // JRuby looks on the classpath inside the 'bin' directory + // for executables + classpath gemDir.absolutePath + + main 'org.jruby.Main' + + //TODO args '-I' + JRubyExec.jarDependenciesGemLibPath(gemDir) + args '-rjars/setup' + args arguments + + environment 'GEM_HOME' : gemDir.absolutePath + environment 'GEM_PATH' : gemDir.absolutePath + environment 'JARS_HOME' : new File(gemDir.absolutePath, 'jars') + environment 'JARS_LOCK' : new File(gemDir.absolutePath, 'Jars.lock') + } + } +} diff --git a/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/RSpec.groovy b/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/RSpec.groovy index 85d40b1..d806825 100644 --- a/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/RSpec.groovy +++ b/jruby-gradle-rspec-plugin/src/main/groovy/com/github/jrubygradle/rspec/RSpec.groovy @@ -7,7 +7,7 @@ import org.gradle.api.Incubating import org.gradle.api.InvalidUserDataException import org.gradle.api.DefaultTask import org.gradle.api.Project -//import org.gradle.api.Configuration +import org.gradle.api.artifacts.Configuration import org.gradle.api.tasks.Input import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction @@ -17,43 +17,34 @@ import org.gradle.api.tasks.TaskAction */ class RSpec extends DefaultTask { - static final String DEFAULT_VERSION = '3.3.0' + static final String DEFAULT_VERSION = '3.3.0' - String version + String version = DEFAULT_VERSION @Input String jrubyVersion = project.jruby.defaultVersion void jrubyVersion(String version) { - this.jrubyVersion = version + this.jrubyVersion = version } - RSpec(){ - version = DEFAULT_VERSION + @Input + Configuration configuration = project.configurations.maybeCreate(name) + void configuration(Object config) { + if (config instanceof String ) { + this.configuration = project.configurations.getByName(config) + } + else { + this.configuration = config + } } @TaskAction void run() { - def config = project.configurations.getByName(name) - File jrubyJar = JRubyExecUtils.jrubyJar(config) - File gemDir = new File(project.buildDir, "${name}-gems") - GemUtils.extractGems(project, jrubyJar, config, gemDir, GemUtils.OverwriteAction.SKIP) - GemUtils.setupJars(config, gemDir, GemUtils.OverwriteAction.OVERWRITE) + JRubyUtils jruby = new JRubyUtils(project, configuration, name) - project.javaexec { - // JRuby looks on the classpath inside the 'bin' directory - // for executables - classpath jrubyJar.absolutePath, gemDir.absolutePath - - main 'org.jruby.Main' + jruby.setupGemsAndJars() - //TODO args '-I' + JRubyExec.jarDependenciesGemLibPath(gemDir) - args '-rjars/setup', '-S','rspec' - - environment 'GEM_HOME' : gemDir.absolutePath - environment 'GEM_PATH' : gemDir.absolutePath - environment 'JARS_HOME' : new File(gemDir.absolutePath, 'jars') - environment 'JARS_LOCK' : new File(gemDir.absolutePath, 'Jars.lock') - } + jruby.exec('-S', 'rspec') } } diff --git a/jruby-gradle-rspec-plugin/src/test/groovy/com/github/jrubygradle/rspec/JRubyRSpecPluginSpec.groovy b/jruby-gradle-rspec-plugin/src/test/groovy/com/github/jrubygradle/rspec/JRubyRSpecPluginSpec.groovy index 8727fee..9092ef4 100644 --- a/jruby-gradle-rspec-plugin/src/test/groovy/com/github/jrubygradle/rspec/JRubyRSpecPluginSpec.groovy +++ b/jruby-gradle-rspec-plugin/src/test/groovy/com/github/jrubygradle/rspec/JRubyRSpecPluginSpec.groovy @@ -10,8 +10,6 @@ import org.gradle.process.internal.ExecException import java.nio.file.Files -import static org.gradle.api.logging.LogLevel.LIFECYCLE - /** * @author Christian Meier * @@ -19,7 +17,6 @@ import static org.gradle.api.logging.LogLevel.LIFECYCLE class JRubyRSpecPluginSpec extends Specification { static final File TESTROOT = new File("${System.getProperty('TESTROOT') ?: 'build/tmp/test/unittests'}") static final File TESTREPO_LOCATION = new File("${System.getProperty('TESTREPO_LOCATION') ?: 'build/tmp/test/repo'}") - //static final String jrubyTestVersion = '1.7.21' def project def specDir @@ -52,11 +49,10 @@ class JRubyRSpecPluginSpec extends Specification { project.buildscript { repositories { - flatDir dirs : TESTREPO_LOCATION.absolutePath + flatDir dirs : TESTREPO_LOCATION.absolutePath } } project.buildDir = TESTROOT - //project.logging.level = LIFECYCLE project.apply plugin: 'com.github.jruby-gradle.rspec' //project.jruby.defaultRepositories = false project.repositories { @@ -110,6 +106,19 @@ class JRubyRSpecPluginSpec extends Specification { output.contains( 'No examples found.' ) } + def "Run rspec with none default jruby versions"() { + given: + Task task = project.tasks.getByName('rspec') + task.jrubyVersion = '1.7.20' + Files.createSymbolicLink(specDir.toPath(), new File('src/test/resources/jruby-version/spec').getAbsoluteFile().toPath()) + project.evaluate() + String output = captureStdout { + task.run() + } + expect: + output.contains( '1 example, 0 failures' ) + } + def "Throw exception on test failure"() { when: Files.createSymbolicLink(specDir.toPath(), new File('src/test/resources/failing/spec').getAbsoluteFile().toPath()) @@ -123,9 +132,9 @@ class JRubyRSpecPluginSpec extends Specification { given: Files.createSymbolicLink(specDir.toPath(), new File('src/test/resources/simple/spec').getAbsoluteFile().toPath()) project.evaluate() - project.tasks.getByName('rspec').run() + Task task = project.tasks.getByName('rspec') String output = captureStdout { - project.tasks.getByName('rspec').run() + task.run() } println output expect: @@ -157,20 +166,24 @@ class JRubyRSpecPluginSpec extends Specification { output.contains( '2 examples, 0 failures' ) } - // def "Run rspec tasks separated reversed"() { - // given: - // Files.createSymbolicLink(specDir.toPath(), new File('src/test/resources/more/spec').getAbsoluteFile().toPath()) - // project.dependencies { - // rspec 'rubygems:leafy-metrics:0.6.0' - // rspec 'org.slf4j:slf4j-simple:1.6.4' - // } - // project.evaluate() - // String output = captureStdout { - // project.tasks.getByName('rspec').run() - // } - // println output - // expect: - // output.contains( '3 examples, 0 failures' ) - // } - + def "Run rspec task with custom configuration"() { + given: + Files.createSymbolicLink(specDir.toPath(), new File('src/test/resources/more/spec').getAbsoluteFile().toPath()) + project.configurations.create('some') + project.dependencies { + some 'rubygems:leafy-metrics:0.6.0' + some 'org.slf4j:slf4j-simple:1.6.4' + } + RSpec task = (RSpec) project.tasks.create( 'mine', RSpec) + task.configure { + configuration('some') + } + project.evaluate() + String output = captureStdout { + task.run() + } + println output + expect: + output.contains( '2 examples, 0 failures' ) + } } diff --git a/jruby-gradle-rspec-plugin/src/test/resources/jruby-version/spec/one_spec.rb b/jruby-gradle-rspec-plugin/src/test/resources/jruby-version/spec/one_spec.rb new file mode 100644 index 0000000..00c0a51 --- /dev/null +++ b/jruby-gradle-rspec-plugin/src/test/resources/jruby-version/spec/one_spec.rb @@ -0,0 +1,5 @@ +describe 'JRubyVersion' do + it 'uses the right jruby version' do + expect(JRUBY_VERSION).to eq '1.7.20' + end +end