Merge pull request #359 from kjwierenga/feature/make-ruby-18-compatible

Fix specs that were broken on ruby 1.8. Use POSIX::Spawn.spawn to make foreman robust on ruby 1.8.7.
This commit is contained in:
David Dollar
2013-05-03 06:32:01 -07:00
5 changed files with 36 additions and 19 deletions

View File

@@ -2,7 +2,6 @@ script: bundle exec rake spec
matrix:
allow_failures:
- rvm: 1.8.7
- rvm: jruby
- rvm: rbx
- rvm: ree
@@ -16,6 +15,7 @@ notifications:
- http://dx-helper.herokuapp.com/travis
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0

View File

@@ -6,7 +6,7 @@ platform :mingw do
gem "win32console", "~> 1.3.0"
end
platform :jruby do
platform :jruby, :ruby_18 do
gem "posix-spawn", "~> 0.3.6"
end

View File

@@ -1,4 +1,5 @@
require "foreman"
require "shellwords"
class Foreman::Process
@@ -52,18 +53,10 @@ class Foreman::Process
Dir.chdir(cwd) do
Process.spawn env, expanded_command(env), :out => output, :err => output
end
elsif Foreman.jruby_18?
elsif Foreman.jruby_18? || Foreman.ruby_18?
require "posix/spawn"
wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
POSIX::Spawn.spawn env, wrapped_command, :out => output, :err => output
elsif Foreman.ruby_18?
fork do
$stdout.reopen output
$stderr.reopen output
env.each { |k,v| ENV[k] = v }
wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
Kernel.exec wrapped_command
end
wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{expanded_command(env)}"
POSIX::Spawn.spawn(*spawn_args(env, wrapped_command.shellsplit, {:out => output, :err => output}))
else
wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
Process.spawn env, wrapped_command, :out => output, :err => output
@@ -122,4 +115,14 @@ class Foreman::Process
File.expand_path(@options[:cwd] || ".")
end
private
def spawn_args(env, argv, options)
args = []
args << env
args += argv
args << options
args
end
end

View File

@@ -41,7 +41,7 @@ describe Foreman::Process do
it "should output utf8 properly" do
process = Foreman::Process.new(resource_path("bin/utf8"))
run(process).should == "\xFF\x03\n".force_encoding('binary')
run(process).should == (Foreman.ruby_18? ? "\xFF\x03\n" : "\xFF\x03\n".force_encoding('binary'))
end
end

View File

@@ -21,6 +21,10 @@ def mock_error(subject, message)
end
end
def make_pipe
IO.method(:pipe).arity.zero? ? IO.pipe : IO.pipe("BINARY")
end
def foreman(args)
capture_stdout do
begin
@@ -31,14 +35,19 @@ def foreman(args)
end
def forked_foreman(args)
rd, wr = IO.pipe("BINARY")
rd, wr = make_pipe
if Foreman.jruby_18? || Foreman.ruby_18?
require 'posix/spawn'
POSIX::Spawn.spawn({}, "bundle exec bin/foreman #{args}", :out => wr, :err => wr)
else
Process.spawn("bundle exec bin/foreman #{args}", :out => wr, :err => wr)
end
wr.close
rd.read
end
def fork_and_capture(&blk)
rd, wr = IO.pipe("BINARY")
rd, wr = make_pipe
pid = fork do
rd.close
wr.sync = true
@@ -57,7 +66,12 @@ def fork_and_capture(&blk)
end
def fork_and_get_exitstatus(args)
pid = Process.spawn("bundle exec bin/foreman #{args}", :out => "/dev/null", :err => "/dev/null")
pid = if Foreman.jruby_18? || Foreman.ruby_18?
require 'posix/spawn'
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")
end
Process.wait(pid)
$?.exitstatus
end
@@ -141,7 +155,7 @@ end
def capture_stdout
old_stdout = $stdout.dup
rd, wr = IO.method(:pipe).arity.zero? ? IO.pipe : IO.pipe("BINARY")
rd, wr = make_pipe
$stdout = wr
yield
wr.close