diff options
Diffstat (limited to 'railties/test/application/dbconsole_test.rb')
-rw-r--r-- | railties/test/application/dbconsole_test.rb | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/railties/test/application/dbconsole_test.rb b/railties/test/application/dbconsole_test.rb index 7e5e9ea8aa..5d89d0e44d 100644 --- a/railties/test/application/dbconsole_test.rb +++ b/railties/test/application/dbconsole_test.rb @@ -1,14 +1,14 @@ require "isolation/abstract_unit" -begin - require "pty" -rescue LoadError -end +require "console_helpers" module ApplicationTests class DBConsoleTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation + include ConsoleHelpers def setup + skip "PTY unavailable" unless available_pty? + build_app end @@ -17,7 +17,6 @@ module ApplicationTests end def test_use_value_defined_in_environment_file_in_database_yml - skip "PTY unavailable" unless available_pty? Dir.chdir(app_path) do app_file "config/database.yml", <<-YAML development: @@ -41,26 +40,37 @@ module ApplicationTests master.puts ".exit" end - private - def spawn_dbconsole(fd) - Process.spawn("#{app_path}/bin/rails dbconsole", in: fd, out: fd, err: fd) - end - - def assert_output(expected, io, timeout = 5) - timeout = Time.now + timeout + def test_respect_environment_option + Dir.chdir(app_path) do + app_file "config/database.yml", <<-YAML + default: &default + adapter: sqlite3 + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + timeout: 5000 - output = "" - until output.include?(expected) || Time.now > timeout - if IO.select([io], [], [], 0.1) - output << io.read(1) - end - end + development: + <<: *default + database: db/development.sqlite3 - assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}" + production: + <<: *default + database: db/production.sqlite3 + YAML end - def available_pty? - defined?(PTY) && PTY.respond_to?(:open) + master, slave = PTY.open + spawn_dbconsole(slave, "-e production") + assert_output("sqlite>", master) + + master.puts "pragma database_list;" + assert_output("production.sqlite3", master) + ensure + master.puts ".exit" + end + + private + def spawn_dbconsole(fd, options = nil) + Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd) end end end |