From ffc73366b20f1f3b3b9521e98eb33972c06570b8 Mon Sep 17 00:00:00 2001 From: Klaas Jan Wierenga Date: Fri, 3 May 2013 21:29:17 +0200 Subject: [PATCH 1/2] Require and use 'posix/spawn' when running ruby 1.8 without using Foreman.ruby_18? (which is the subject under test). --- spec/spec_helper.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 76676ee..288baf3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,15 @@ require "fakefs/spec_helpers" $:.unshift File.expand_path("../../lib", __FILE__) +begin + def running_ruby_18? + defined?(RUBY_VERSION) and RUBY_VERSION =~ /^1\.8\.\d+/ + end + require 'posix/spawn' if running_ruby_18? +rescue LoadError + STDERR.puts "WARNING: foreman requires gem `posix-spawn` on Ruby #{RUBY_VERSION}. Please `gem install posix-spawn`." +end + def mock_export_error(message) lambda { yield }.should raise_error(Foreman::Export::Exception, message) end @@ -36,8 +45,7 @@ end def forked_foreman(args) rd, wr = make_pipe - if Foreman.jruby_18? || Foreman.ruby_18? - require 'posix/spawn' + if running_ruby_18? POSIX::Spawn.spawn({}, "bundle exec bin/foreman #{args}", :out => wr, :err => wr) else Process.spawn("bundle exec bin/foreman #{args}", :out => wr, :err => wr) @@ -66,8 +74,7 @@ def fork_and_capture(&blk) end def fork_and_get_exitstatus(args) - pid = if Foreman.jruby_18? || Foreman.ruby_18? - require 'posix/spawn' + pid = if running_ruby_18? POSIX::Spawn.spawn({}, "bundle exec bin/foreman #{args}", :out => "/dev/null", :err => "/dev/null") else Process.spawn("bundle exec bin/foreman #{args}", :out => "/dev/null", :err => "/dev/null") @@ -170,4 +177,5 @@ RSpec.configure do |config| config.order = 'rand' config.include FakeFS::SpecHelpers, :fakefs config.mock_with :rr + config.backtrace_clean_patterns = [] end From e245026f655d4b1b0a577e2571f6da3eaa83d203 Mon Sep 17 00:00:00 2001 From: Klaas Jan Wierenga Date: Fri, 3 May 2013 21:31:07 +0200 Subject: [PATCH 2/2] Fail with an error on Ruby 1.8 when posix-spawn is not present. --- lib/foreman/cli.rb | 9 +++++++++ spec/foreman/cli_spec.rb | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 8ee1d17..f2f5059 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -34,6 +34,7 @@ class Foreman::CLI < Thor end def start(process=nil) + require_posix_spawn_for_ruby_18! check_procfile! load_environment! engine.load_procfile(procfile) @@ -137,6 +138,14 @@ private ###################################################################### end end + def require_posix_spawn_for_ruby_18! + begin + Kernel.require 'posix/spawn' # Use Kernel explicitly so we can mock the require call in the spec + rescue LoadError + error "foreman requires gem `posix-spawn` on Ruby #{RUBY_VERSION}. Please `gem install posix-spawn`." + end if Foreman.ruby_18? + end + def procfile case when options[:procfile] then options[:procfile] diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index 26cfa42..da41edf 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -100,4 +100,12 @@ describe "Foreman::CLI", :fakefs do end end + describe "when posix-spawn is not present on ruby 1.8" do + it "should fail with an error" do + mock(Kernel).require('posix/spawn') { raise LoadError } + output = foreman("start -f #{resource_path("Procfile")}") + output.should == "ERROR: foreman requires gem `posix-spawn` on Ruby #{RUBY_VERSION}. Please `gem install posix-spawn`.\n" + end + end if running_ruby_18? + end