Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: JRuby 1.0.0, JRuby 1.0.2, JRuby 1.1b1, JRuby 1.1.1
-
Fix Version/s: JRuby 1.6
-
Component/s: Core Classes/Modules
-
Labels:None
-
Environment:OS X 10.4.11 with Java 1.5
-
Testcase included:yes
Description
Here's another bug related to spawning new processes with backquotes. This time, if you set some variables in your Ruby program's ENV, new JRuby processes will not always see them as Java environment variables.
Attached is a test case demonstrating this behavior. The test case uses backquotes to start up a new JRuby program in a variety of ways; the new environment variables will show up in the new program in some cases, but not others.
Paste of test case:
require "test/unit"
class BackquotesEnvironmentTest < Test::Unit::TestCase
def test_backquotes_pass_environment_variables_from_ruby_environment
ENV["ONE"] = nil
ENV["TWO"] = nil
assert_equal "ONE: TWO: ", `./other_program`, "variables should have been unset"
ENV["ONE"] = "aaa"
ENV["TWO"] = "bbb"
results_with_variables_set = "ONE: aaa TWO: bbb"
assert_equal results_with_variables_set, `./other_program`, "wrong variables when run straight up"
assert_equal results_with_variables_set, `THREE=four jruby ./other_program`, "wrong variables when tricking backquotes"
assert_equal results_with_variables_set, `jruby ./other_program`, "wrong variables when run with jruby"
assert_equal results_with_variables_set, `./other_program.rb`, "wrong variables when run with rb suffix"
end
end
Paste of result:
Loaded suite test_backquotes_pass_environment Started F Finished in 3.363 seconds. 1) Failure: test_backquotes_pass_environment_variables_from_ruby_environment(BackquotesEnvironmentTest) [test_backquotes_pass_environment.rb:14]: wrong variables when run with jruby. <"ONE: aaa TWO: bbb"> expected but was <"ONE: TWO: ">. 1 tests, 4 assertions, 1 failures, 0 errors
Note that in the test failed on the line spawning the new application when it has a 'jruby' prefix. This is because that prefix causes JRuby to not actually spawn a new process. The previous two assertions demonstrate that when a new process is created, the variables do show up correctly. (See how I tricked JRuby into creating a new process on line 13?)
I ran into this problem because I, unfortunately, need to use a Java library that digs into environment variables to get some of its configuration data. My system tests were setting up the environment for my application and then firing it up with backqoutes, but since that Java library is using Java's accessors to the environment, it wasn't seeing my configuration. Bummer. (To get by for now I'm using the trick shown on line 13 of the above test case.)
What seems to be happening here is that variables set into Ruby's environment don't show up in the Java environment; I think this is because there is no way to set variables in the current Java environment (or at least I know of no way to do it). Somehow Ruby variables do end up in the environment of a new process. So in the cases where I'm actually getting a new process (`./other_program` and `THREE=four jruby ./other_program`), I get the new variables. But when I don't get a new process, my Java library can't see the variables.