aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2013-03-08 13:34:58 +0000
committerJon Leighton <j@jonathanleighton.com>2013-03-08 13:58:56 +0000
commitbe3e10cd26bc8ec29c6474d03a08a8e733108e7d (patch)
treed67ac48af90668003caa2551ba40ed8b94637bf1 /railties
parentba6b3c165f121d080fe59cf227c9823e0bebc186 (diff)
downloadrails-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')
-rw-r--r--railties/lib/rails/commands/console.rb4
-rw-r--r--railties/test/application/console_test.rb82
2 files changed, 77 insertions, 9 deletions
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