Skip to content

Commit 3e34395

Browse files
authored
Merge pull request #294 from EarthCitizen/master
Fixes #293 - Environment Variables not Passes to javaexec
2 parents 97898e2 + 65561f8 commit 3e34395

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

jruby-gradle-base-plugin/src/integTest/groovy/com/github/jrubygradle/JRubyExecExtensionIntegrationSpec.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,30 @@ class JRubyExecExtensionIntegrationSpec extends Specification {
138138
new File(project.buildDir, customGemDir).exists()
139139

140140
}
141+
142+
def "Running a script that requires environment variables"() {
143+
// This tests that the passthrough invocation
144+
// happens for overloaded versions of environment
145+
// and that the environment variables are passed
146+
// on to the script
147+
given:
148+
final String envVarName = 'TEST_ENV_VAR'
149+
final String envVarValue = 'Test Value'
150+
final Map envVarMap = [TEST_A: 'A123', TEST_B: 'B123']
151+
152+
when:
153+
project.with {
154+
jrubyexec {
155+
environment envVarName, envVarValue
156+
environment envVarMap
157+
script "${TEST_SCRIPT_DIR}/envVars.rb"
158+
standardOutput output
159+
}
160+
}
161+
162+
then:
163+
outputBuffer =~ /TEST_ENV_VAR=Test Value/
164+
outputBuffer =~ /TEST_A=A123/
165+
outputBuffer =~ /TEST_B=B123/
166+
}
141167
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts ENV.map { |k, v| "#{k}=#{v}" }.join("\n")

jruby-gradle-base-plugin/src/main/groovy/com/github/jrubygradle/internal/JRubyExecDelegate.groovy

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class JRubyExecDelegate implements JRubyExecTraits {
5555

5656
private final List passthrough = []
5757

58+
@SuppressWarnings('VariableName')
5859
static Object jrubyexecDelegatingClosure = { Project project, Closure cl ->
5960
JRubyExecDelegate proxy = new JRubyExecDelegate()
6061
proxy.project = project
@@ -70,7 +71,7 @@ class JRubyExecDelegate implements JRubyExecTraits {
7071
proxy.passthrough.each { item ->
7172
Object k = item.keySet()[0]
7273
Object v = item.values()[0]
73-
"${k}" v
74+
invokeMethod("${k}", v)
7475
}
7576
main 'org.jruby.Main'
7677
// just keep this even if it does not exists
@@ -81,7 +82,14 @@ class JRubyExecDelegate implements JRubyExecTraits {
8182
args item.toString()
8283
}
8384

84-
setEnvironment proxy.getPreparedEnvironment(System.env)
85+
// Start with System.env then add from environment,
86+
// which will add the user settings and
87+
// overwrite any overlapping entries
88+
final env = [:]
89+
env << System.env
90+
env << environment
91+
92+
setEnvironment proxy.getPreparedEnvironment(env)
8593
}
8694
}
8795

jruby-gradle-base-plugin/src/test/groovy/com/github/jrubygradle/internal/JRubyExecDelegateSpec.groovy

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class JRubyExecDelegateSpec extends Specification {
7676
def "When just passing arbitrary javaexec, expect them to be stored"() {
7777
given:
7878
def cl = {
79+
environment 'XYZ', '123'
7980
executable '/path/to/file'
8081
jvmArgs '-x'
8182
jvmArgs '-y','-z'
@@ -84,12 +85,14 @@ class JRubyExecDelegateSpec extends Specification {
8485
cl.call()
8586

8687
expect:
87-
jred.valuesAt(0) == '/path/to/file'
88-
jred.valuesAt(1) == '-x'
89-
jred.valuesAt(2) == ['-y','-z']
90-
jred.keyAt(0) == 'executable'
91-
jred.keyAt(1) == 'jvmArgs'
88+
jred.valuesAt(0) == ['XYZ', '123']
89+
jred.valuesAt(1) == '/path/to/file'
90+
jred.valuesAt(2) == '-x'
91+
jred.valuesAt(3) == ['-y','-z']
92+
jred.keyAt(0) == 'environment'
93+
jred.keyAt(1) == 'executable'
9294
jred.keyAt(2) == 'jvmArgs'
95+
jred.keyAt(3) == 'jvmArgs'
9396

9497
}
9598

0 commit comments

Comments
 (0)