diff options
-rw-r--r-- | activerecord/lib/active_record/railties/console_sandbox.rb | 4 | ||||
-rw-r--r-- | railties/lib/rails/commands/console.rb | 4 | ||||
-rw-r--r-- | railties/test/application/console_test.rb | 82 |
3 files changed, 79 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/railties/console_sandbox.rb b/activerecord/lib/active_record/railties/console_sandbox.rb index 90b462fad6..25a591aa43 100644 --- a/activerecord/lib/active_record/railties/console_sandbox.rb +++ b/activerecord/lib/active_record/railties/console_sandbox.rb @@ -1,4 +1,4 @@ -ActiveRecord::Base.connection.begin_db_transaction +ActiveRecord::Base.connection.begin_transaction at_exit do - ActiveRecord::Base.connection.rollback_db_transaction + ActiveRecord::Base.connection.rollback_transaction end diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index 86ab1aabbf..96229bb4f6 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -46,7 +46,10 @@ module Rails def initialize(app, options={}) @app = app @options = options + + app.sandbox = sandbox? app.load_console + @console = app.config.console || IRB end @@ -71,7 +74,6 @@ module Rails end def start - app.sandbox = sandbox? require_debugger if debugger? set_environment! if environment? 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 |