Skip to content

RSpec: Use expect syntax and also unset JAVACMD to make tests pass #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 63 additions & 62 deletions spec/launcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,225 +3,226 @@

describe "JRuby native launcher" do
it "should run org.jruby.Main" do
jruby_launcher_args("").last.should == "org/jruby/Main"
expect(jruby_launcher_args("").last).to eq "org/jruby/Main"
end

it "should pass unrecognized arguments to JRuby" do
jruby_launcher_args("-J-Dsome.option -v --help")[-3..-1].should == ["org/jruby/Main", "-v", "--help"]
expect(jruby_launcher_args("-J-Dsome.option -v --help")[-3..-1]).to eq ["org/jruby/Main", "-v", "--help"]
end

it "should print help message" do
args = jruby_launcher_args("-Xhelp 2>&1")
args.select {|l| l =~ /JRuby Launcher usage/}.should_not be_empty
args.should include("-X")
expect(args.select {|l| l =~ /JRuby Launcher usage/}).not_to be_empty
expect(args).to include("-X")
args = jruby_launcher_args("-X 2>&1")
args.detect {|l| l =~ /JRuby Launcher usage/}.should_not be_empty
args.should include("-X")
expect(args.detect {|l| l =~ /JRuby Launcher usage/}).not_to be_empty
expect(args).to include("-X")
end

it "should use $JAVACMD when JAVACMD is specified" do
with_environment "JAVACMD" => File.join("jato") do
if windows?
jruby_launcher_args("-v 2>&1").join.should =~ %r{jato}
expect(jruby_launcher_args("-v 2>&1").join).to match %r{jato}
else
jruby_launcher_args("-v").first.should == File.join("jato")
expect(jruby_launcher_args("-v").first).to eq File.join("jato")
end
end
end

it "should use $JAVA_HOME/bin/java when JAVA_HOME is specified" do
with_environment "JAVA_HOME" => File.join("some", "java", "home") do
if windows?
jruby_launcher_args("-v 2>&1").join.should =~ %r{some/java/home}
expect(jruby_launcher_args("-v 2>&1").join).to match %r{some/java/home}
else
jruby_launcher_args("-v").first.should == File.join("some", "java", "home", "bin", "java")
expect(jruby_launcher_args("-v").first).to eq File.join("some", "java", "home", "bin", "java")
end
end
end

it "should use -Xjdkhome argument above JAVA_HOME" do
with_environment "JAVA_HOME" => File.join("env", "java", "home") do
if windows?
jruby_launcher_args("-Xjdkhome some/java/home 2>&1").join.should =~ %r{some/java/home}
expect(jruby_launcher_args("-Xjdkhome some/java/home 2>&1").join).to match %r{some/java/home}
else
jruby_launcher_args("-Xjdkhome some/java/home").first.should == File.join("some", "java", "home", "bin", "java")
expect(jruby_launcher_args("-Xjdkhome some/java/home").first).to eq File.join("some", "java", "home", "bin", "java")
end
end
end

it "should drop the backslashes at the end of JAVA_HOME" do
with_environment "JAVA_HOME" => File.join("some", "java", "home\\\\") do
if windows?
jruby_launcher_args("").join.should =~ %r{some/java/home}
expect(jruby_launcher_args("").join).to match %r{some/java/home}
else
jruby_launcher_args("").first.should == File.join("some", "java", "home", "bin", "java")
expect(jruby_launcher_args("").first).to eq File.join("some", "java", "home", "bin", "java")
end
end
end

it "should complain about a missing log argument" do
jruby_launcher("-Xtrace 2>&1").should =~ /Argument is missing for "-Xtrace"/
jruby_launcher("-Xtrace -- 2>&1").should =~ /Argument is missing for "-Xtrace"/
expect(jruby_launcher("-Xtrace 2>&1")).to match /Argument is missing for "-Xtrace"/
expect(jruby_launcher("-Xtrace -- 2>&1")).to match /Argument is missing for "-Xtrace"/
end

it "should complain about a missing jdkhome argument" do
jruby_launcher("-Xjdkhome 2>&1").should =~ /Argument is missing/
jruby_launcher("-Xjdkhome -- 2>&1").should =~ /Argument is missing/
expect(jruby_launcher("-Xjdkhome 2>&1")).to match /Argument is missing/
expect(jruby_launcher("-Xjdkhome -- 2>&1")).to match /Argument is missing/
end

it "should complain about a missing classpath append argument" do
jruby_launcher("-Xcp:a 2>&1").should =~ /Argument is missing/
jruby_launcher("-Xcp:a -- 2>&1").should =~ /Argument is missing/
expect(jruby_launcher("-Xcp:a 2>&1")).to match /Argument is missing/
expect(jruby_launcher("-Xcp:a -- 2>&1")).to match /Argument is missing/
end

it "should run nailgun server with --ng-server option" do
jruby_launcher_args("--ng-server").last.should == "com/martiansoftware/nailgun/NGServer"
expect(jruby_launcher_args("--ng-server").last).to eq "com/martiansoftware/nailgun/NGServer"
end

it "should run nailgun client with --ng option" do
jruby_launcher_args('--ng -e "puts 1"').should == ["org.jruby.util.NailMain", "-e", "puts 1"]
expect(jruby_launcher_args('--ng -e "puts 1"')).to eq ["org.jruby.util.NailMain", "-e", "puts 1"]
end

it "should handle -J JVM options" do
jruby_launcher_args("-J-Darg1=value1 -J-Darg2=value2").should include("-Darg1=value1", "-Darg2=value2")
expect(jruby_launcher_args("-J-Darg1=value1 -J-Darg2=value2")).to include("-Darg1=value1", "-Darg2=value2")
end

it "should pass -Xprop.erty=value as -J-Djruby.prop.erty=value" do
jruby_launcher_args("-Xprop.erty=value").should include("-Djruby.prop.erty=value")
expect(jruby_launcher_args("-Xprop.erty=value")).to include("-Djruby.prop.erty=value")
end

it "should pass -Xproperties as --properties" do
jruby_launcher_args("-Xproperties").should include("--properties")
expect(jruby_launcher_args("-Xproperties")).to include("--properties")
end

it "should default to 500m max heap" do
jruby_launcher_args("").should include("-Xmx500m")
expect(jruby_launcher_args("")).to include("-Xmx500m")
end

it "should allow max heap to be overridden" do
jruby_launcher_args("-J-Xmx256m").should include("-Xmx256m")
expect(jruby_launcher_args("-J-Xmx256m")).to include("-Xmx256m")
end

it "should default to 2048k max stack" do
jruby_launcher_args("").should include("-Xss2048k")
expect(jruby_launcher_args("")).to include("-Xss2048k")
end

it "should allow max stack to be overridden" do
jruby_launcher_args("-J-Xss512k").should include("-Xss512k")
expect(jruby_launcher_args("-J-Xss512k")).to include("-Xss512k")
end

it "should add the contents of the CLASSPATH environment variable" do
with_environment "CLASSPATH" => "some.jar" do
classpath_arg(jruby_launcher_args("")).should =~ /some.jar/
expect(classpath_arg(jruby_launcher_args(""))).to match /some.jar/
end
end

it "should add the classpath elements in proper order" do
s = File::PATH_SEPARATOR
with_environment "CLASSPATH" => "some-env.jar" do
args = jruby_launcher_args("-Xcp:a some-other.jar -Xcp:p some.jar")
classpath_arg(args).should =~ /some.jar.*#{s}some-env.jar#{s}some-other.jar/
expect(classpath_arg(args)).to match /some.jar.*#{s}some-env.jar#{s}some-other.jar/
end
end

it "should use the --server compiler" do
jruby_launcher_args("--server").should include("-server")
expect(jruby_launcher_args("--server")).to include("-server")
end

it "should use the --client compiler" do
jruby_launcher_args("--client").should include("-client")
expect(jruby_launcher_args("--client")).to include("-client")
end

it "should set the JMX settings when --manage is present" do
jruby_launcher_args("--manage").should include("-Dcom.sun.management.jmxremote", "-Djruby.management.enabled=true")
expect(jruby_launcher_args("--manage")).to include("-Dcom.sun.management.jmxremote", "-Djruby.management.enabled=true")
end

it "should set the headless flag when --headless is present" do
jruby_launcher_args("--headless").should include("-Djava.awt.headless=true")
expect(jruby_launcher_args("--headless")).to include("-Djava.awt.headless=true")
end

it "should pass -Xprof when --sample is present" do
jruby_launcher_args("--sample").should include("-Xprof")
expect(jruby_launcher_args("--sample")).to include("-Xprof")
end

it "should stop argument processing when a -- is seen" do
jruby_launcher_args("-- -Xhelp -Xtrace --headless").should include("-Xhelp", "-Xtrace", "--headless")
expect(jruby_launcher_args("-- -Xhelp -Xtrace --headless")).to include("-Xhelp", "-Xtrace", "--headless")
end

# JRUBY-4151
it "should properly handle single quotes" do
jruby_launcher_args("-e 'ABC DEF'").should include("ABC DEF")
expect(jruby_launcher_args("-e 'ABC DEF'")).to include("ABC DEF")
end

# JRUBY-4581
it "should prepend JRUBY_OPTS to the start of the argument list to process" do
with_environment "JRUBY_OPTS" => "--server -J-Dsome.key=val -rubygems" do
jruby_launcher_args("-e 'ABC DEF'").should include("-server", "-Dsome.key=val", "-rubygems", "-e", "ABC DEF")
expect(jruby_launcher_args("-e 'ABC DEF'")).to include("-server", "-Dsome.key=val", "-rubygems", "-e", "ABC DEF")
end
end

# JRUBY-4611
it "stops argument processing on first non-option argument" do
jruby_launcher_args("foo.rb --sample")[-2..-1].should == ["foo.rb", "--sample"]
expect(jruby_launcher_args("foo.rb --sample")[-2..-1]).to eq ["foo.rb", "--sample"]
end

# JRUBY-4608
if RbConfig::CONFIG['target_os'] =~ /darwin/i
it "includes file.encoding=UTF-8 on Mac if JAVA_ENCODING is not set" do
jruby_launcher_args("-e true").should include("-Dfile.encoding=UTF-8")
expect(jruby_launcher_args("-e true")).to include("-Dfile.encoding=UTF-8")
with_environment "JAVA_ENCODING" => "MacRoman" do
jruby_launcher_args("-e true").should_not include("-Dfile.encoding=UTF-8")
expect(jruby_launcher_args("-e true")).not_to include("-Dfile.encoding=UTF-8")
end
end
end

it "does not crash on empty args" do
jruby_launcher_args("-e ''").should include("-e")
jruby_launcher("-Xtrace '' 2>&1").should =~ /-Xtrace/
jruby_launcher("-Xjdkhome '' 2>&1").should =~ /-Xjdkhome/
expect(jruby_launcher_args("-e ''")).to include("-e")
expect(jruby_launcher("-Xtrace '' 2>&1")).to match /-Xtrace/
expect(jruby_launcher("-Xjdkhome '' 2>&1")).to match /-Xjdkhome/
end

# JRUBY-4706
it "should put JRuby on regular classpath when -Xnobootclasspath is used" do
args = jruby_launcher_args("-e true")
args.grep(/Xbootclasspath/).should_not be_empty
expect(args.grep(/Xbootclasspath/)).not_to be_empty
args = jruby_launcher_args("-Xnobootclasspath -e true")
args.grep(/Xbootclasspath/).should be_empty
expect(args.grep(/Xbootclasspath/)).to be_empty
end

it "should put JRuby on regular classpath when VERIFY_JRUBY is set" do
with_environment "VERIFY_JRUBY" => "true" do
args = jruby_launcher_args("-e true")
args.grep(/Xbootclasspath/).should be_empty
expect(args.grep(/Xbootclasspath/)).to be_empty
end
end

# JRUBY-4709
it "should include a bare : or ; at the end of the classpath, to include PWD in the path" do
classpath_arg(jruby_launcher_args("-Xnobootclasspath -e true")).should =~
if windows?
/;$/
else
/:$/
end
end_of_path_pattern = if windows?
/;$/
else
/:$/
end

expect(classpath_arg(jruby_launcher_args("-Xnobootclasspath -e true"))).to match end_of_path_pattern
end

# JRUBY-6016
it "should honor JAVA_MEM" do
with_environment "JAVA_MEM" => "-Xmx768m" do
jruby_launcher_args("").should include("-Xmx768m")
expect(jruby_launcher_args("")).to include("-Xmx768m")
end
end

it "should honor JAVA_STACK" do
with_environment "JAVA_STACK" => "-Xss3072k" do
jruby_launcher_args("").should include("-Xss3072k")
expect(jruby_launcher_args("")).to include("-Xss3072k")
end
end

it "should honor JRUBY_HOME" do
with_environment "JRUBY_HOME" => "/tmp" do
jruby_launcher_args("").should include("-Djruby.home=/tmp")
expect(jruby_launcher_args("")).to include("-Djruby.home=/tmp")
end
end

Expand All @@ -240,23 +241,23 @@

it "should add jruby.jar to the bootclasspath" do
with_environment "JRUBY_HOME" => jruby_home do
jruby_launcher_args("").should include("-Xbootclasspath/a:#{jruby_home}/lib/jruby.jar")
expect(jruby_launcher_args("")).to include("-Xbootclasspath/a:#{jruby_home}/lib/jruby.jar")
end
end
end

it "should place user-supplied options after default options" do
args = jruby_launcher_args("-J-Djruby.home=/tmp")
home_args = args.select {|x| x =~ /^-Djruby\.home/ }
home_args.length.should == 2
home_args.last.should == "-Djruby.home=/tmp"
expect(home_args.length).to eq 2
expect(home_args.last).to eq "-Djruby.home=/tmp"
end

it "should print the version" do
jruby_launcher("-Xversion 2>&1").should =~ /Launcher Version #{JRubyLauncher::VERSION}/
expect(jruby_launcher("-Xversion 2>&1")).to match /Launcher Version #{JRubyLauncher::VERSION}/
end

it "should not crash on format-strings" do
jruby_launcher_args("-e %s%s%s%s%s 2>&1").should include('-e', '%s%s%s%s%s')
expect(jruby_launcher_args("-e %s%s%s%s%s 2>&1")).to include('-e', '%s%s%s%s%s')
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def windows?

def classpath_arg(args)
index = args.index("-cp")
index.should > 0
expect(index).to be > 0
args[index + 1]
end

Expand All @@ -70,6 +70,7 @@ def with_environment(pairs = {})
config.before(:all) do
JRubyLauncherHelper.check_executable_built
# clear environment for better control
ENV.delete("JAVACMD")
ENV.delete("JAVA_HOME")
ENV.delete("JRUBY_HOME")
ENV.delete("JAVA_OPTS")
Expand Down