Sha256: ef17bc7150e339b805b69a30d9b258d14286ed63853e089432283239b5569776

Contents?: true

Size: 709 Bytes

Versions: 11

Compression:

Stored size: 709 Bytes

Contents

module TestingSandbox

  # This whole thing *could* be much simpler, but I don't think Tempfile,
  # popen and others exist on all platforms (like Windows).
  def execute_in_sandbox(code)
    test_name = "#{File.dirname(__FILE__)}/test.#{$$}.rb"
    res_name = "#{File.dirname(__FILE__)}/test.#{$$}.out"

    File.open(test_name, "w+") do |file|
      file.write(<<-CODE)
        $:.unshift "../lib"
        block = Proc.new do
          #{code}
        end
        print block.call
      CODE
    end

    system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox"
    File.read(res_name)
  ensure
    File.delete(test_name) rescue nil
    File.delete(res_name) rescue nil
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
actionpack-1.11.0 test/testing_sandbox.rb
actionpack-1.11.1 test/testing_sandbox.rb
actionpack-1.12.0 test/testing_sandbox.rb
actionpack-1.12.1 test/testing_sandbox.rb
actionpack-1.11.2 test/testing_sandbox.rb
actionpack-1.10.2 test/testing_sandbox.rb
actionpack-1.10.1 test/testing_sandbox.rb
actionpack-1.12.2 test/testing_sandbox.rb
actionpack-1.12.5 test/testing_sandbox.rb
actionpack-1.12.4 test/testing_sandbox.rb
actionpack-1.12.3 test/testing_sandbox.rb