Permalink
Browse files

Fix rails console --sandbox

I've also added a proper acceptance test which reproduced the issue.

Closes #9513, #9515.
  • Loading branch information...
1 parent ba6b3c1 commit be3e10cd26bc8ec29c6474d03a08a8e733108e7d @jonleighton jonleighton committed Mar 8, 2013
@@ -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
@@ -46,7 +46,10 @@ def available_environments
def initialize(app, options={})
@app = app
@options = options
+
+ app.sandbox = sandbox?
app.load_console
+
@console = app.config.console || IRB
end
@@ -71,7 +74,6 @@ def debugger?
end
def start
- app.sandbox = sandbox?
require_debugger if debugger?
set_environment! if environment?
@@ -81,18 +81,84 @@ def test_access_to_helpers
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

13 comments on commit be3e10c

Member

neerajdotname replied Mar 8, 2013

awesome test

@jonleighton apparently travis isn't that happy =(. I get this running the test locally:

$ t test/application/console_test.rb 
Run options: --seed 20833

# Running tests:

.....Quit: 3
Member

jonleighton replied Mar 9, 2013

@carlosantoniodasilva fucksticks! I'll look into it.

Member

jonleighton replied Mar 9, 2013

@carlosantoniodasilva 72be5bb should fix travis - it worked for me on a local travis VM but need to wait for the build to confirm.

Regarding the Quit: 3 thing, I'm not sure. It seems a different problem to travis. Can you try running just the specific test? (ruby -Itest test/application/console_test.rb -n test_sandbox) I suspect it may be a problem that occurs on OS X that doesn't occur on Linux. I don't have easy access to an OS X computer so can't help. I suggest either spending some time debugging (I'm happy to help remotely) or simply disabling the test on OS X - the main thing is that it should run and pass on the CI which I think it should now.

Owner

pixeltrix replied Mar 9, 2013

@jonleighton @carlosantoniodasilva test runs fine for me on OS X

@jonleighton thanks, I saw the fixing commit.

I did run only the specific test (previously and with the last commit), with the same result:

$ t test/application/console_test.rb  -n test_sandbox
Run options: -n test_sandbox --seed 63806

# Running tests:

Quit: 3

I'll try to dig a little bit tomorrow to see if I can find what might be going on, and ping you afterwards.
Anyway, lets wait for travis, if it's happy, I'm happy :D.

@pixeltrix 💣.. thanks for checking.

Member

jonleighton replied Mar 9, 2013

so the build still failed. grrr. I wonder if it needs an even greater timeout. I will do some experiments in a branch.

Member

jonleighton replied Mar 9, 2013

Found an error in my code, fixed in 9ae81be. It should really actually definitely pass this time I hope so.

Going out for the evening now, but I'll continue to fix tomorrow if it doesn't pass. But it will. Obviously.

Contributor

MacksMind replied Mar 17, 2013

OSX Mountain Lion still isn't happy. I haven't been able to get the test to complete for 1.9.3-p392, but it completes for 2.0.0-p0 if I change QUIT to KILL.

Member

neerajdotname replied Mar 17, 2013

It worked for me . I'm on Mountain Lion. I'll check again.

Contributor

MacksMind replied Mar 17, 2013

Of course KILL probably invalidates the test. 😦

Please sign in to comment.