From be3e10cd26bc8ec29c6474d03a08a8e733108e7d Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 8 Mar 2013 13:34:58 +0000 Subject: Fix rails console --sandbox I've also added a proper acceptance test which reproduced the issue. Closes #9513, #9515. --- railties/test/application/console_test.rb | 82 ++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 8 deletions(-) (limited to 'railties/test/application') diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index 3cb3643e3a..588682d3d4 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -81,18 +81,84 @@ class ConsoleTest < ActiveSupport::TestCase assert_equal 'Once upon a time in a world...', helper.truncate('Once upon a time in a world far far away') end +end + +begin + require "pty" +rescue LoadError +end - def test_with_sandbox - require 'rails/all' - value = false +class FullStackConsoleTest < ActiveSupport::TestCase + def setup + skip "PTY unavailable" unless defined?(PTY) && PTY.respond_to?(:open) - Class.new(Rails::Railtie) do - console do |app| - value = app.sandbox? + build_app + app_file 'app/models/post.rb', <<-CODE + class Post < ActiveRecord::Base + end + CODE + system "#{app_path}/bin/rails runner 'Post.connection.create_table :posts'" + + @master, @slave = PTY.open + end + + def teardown + teardown_app + end + + def assert_output(expected, timeout = 0.2) + timeout = Time.now + timeout + + output = "" + until output.include?(expected) || Time.now > timeout + if IO.select([@master], [], [], 0.1) + output << @master.readpartial(100) end end - load_environment(true) - assert value + assert output.include?(expected), "#{expected.inspect} expected, but got:\n\n#{output}" + end + + def write_prompt(command) + @master.puts command + assert_output command + end + + def kill(pid) + Process.kill('QUIT', pid) + Process.wait(pid) + rescue Errno::ESRCH + end + + def spawn_console + pid = Process.spawn( + "#{app_path}/bin/rails console --sandbox", + in: @slave, out: @slave, err: @slave + ) + + assert_output "> ", 5 + pid + end + + def test_sandbox + pid = spawn_console + + write_prompt "Post.count" + assert_output "=> 0" + + write_prompt "Post.create" + assert_output "=> " + + write_prompt "Post.count" + assert_output "=> 1" + + kill pid + + pid = spawn_console + + write_prompt "Post.count" + assert_output "=> 0" + ensure + kill pid end end -- cgit v1.2.3