diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-03-08 13:34:58 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2013-03-08 13:58:56 +0000 |
commit | be3e10cd26bc8ec29c6474d03a08a8e733108e7d (patch) | |
tree | d67ac48af90668003caa2551ba40ed8b94637bf1 /railties/test | |
parent | ba6b3c165f121d080fe59cf227c9823e0bebc186 (diff) | |
download | rails-be3e10cd26bc8ec29c6474d03a08a8e733108e7d.tar.gz rails-be3e10cd26bc8ec29c6474d03a08a8e733108e7d.tar.bz2 rails-be3e10cd26bc8ec29c6474d03a08a8e733108e7d.zip |
Fix rails console --sandbox
I've also added a proper acceptance test which reproduced the issue.
Closes #9513, #9515.
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/console_test.rb | 82 |
1 files changed, 74 insertions, 8 deletions
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 |