features/support/env.rb in spork-0.6.3 vs features/support/env.rb in spork-0.7.0

- old
+ new

@@ -4,10 +4,12 @@ require 'tempfile' require 'spec/expectations' require 'timeout' require 'spork' +require(File.dirname(__FILE__) + '/background_job.rb') + class SporkWorld BINARY = File.expand_path(File.dirname(__FILE__) + '/../../bin/spork') RUBY_BINARY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) SANDBOX_DIR = File.expand_path(File.join(File.dirname(__FILE__), '../../tmp/sandbox')) @@ -23,11 +25,25 @@ @background_jobs = [] end private attr_reader :last_exit_status, :last_stderr, :last_stdout, :background_jobs + def last_stderr + return @last_stderr if @last_stderr + if @background_job + @last_stderr = @background_job.stderr.read + end + end + + def last_stdout + return @last_stdout if @last_stdout + if @background_job + @last_stdout = @background_job.stdout.read + end + end + def create_file(file_name, file_content) file_content.gsub!("SPORK_LIB", "'#{spork_lib_dir}'") # Some files, such as Rakefiles need to use the lib dir in_current_dir do FileUtils.mkdir_p(File.dirname(file_name)) File.open(file_name, 'w') { |f| f << file_content } @@ -36,10 +52,22 @@ def in_current_dir(&block) Dir.chdir(@current_dir, &block) end + def localized_command(command, args) + case command + when 'spork' + command = SporkWorld::BINARY + when 'cucumber' + command = Cucumber::BINARY + else + command = %x{which #{command}}.chomp + end + "#{SporkWorld::RUBY_BINARY} -I #{Cucumber::LIBDIR} #{command} #{args}" + end + def run(command) stderr_file = Tempfile.new('spork') stderr_file.close in_current_dir do @last_stdout = `#{command} 2> #{stderr_file.path}` @@ -47,39 +75,24 @@ end @last_stderr = IO.read(stderr_file.path) end def run_in_background(command) - child_stdin, parent_stdin = IO::pipe - parent_stdout, child_stdout = IO::pipe - parent_stderr, child_stderr = IO::pipe - - background_jobs << Kernel.fork do - [parent_stdin, parent_stdout, parent_stderr].each { |io| io.close } - - STDIN.reopen(child_stdin) - STDOUT.reopen(child_stdout) - STDERR.reopen(child_stderr) - - [child_stdin, child_stdout, child_stderr].each { |io| io.close } - - in_current_dir do - exec command - end + in_current_dir do + @background_job = BackgroundJob.run(command) end - - [child_stdin, child_stdout, child_stderr].each { |io| io.close } - parent_stdin.sync = true - - @bg_stdin, @bg_stdout, @bg_stderr = [parent_stdin, parent_stdout, parent_stderr] + @background_jobs << @background_job + @background_job end def terminate_background_jobs if @background_jobs - @background_jobs.each do |pid| - Process.kill(Signal.list['TERM'], pid) + @background_jobs.each do |background_job| + background_job.kill end end + @background_jobs.clear + @background_job = nil end end World do