2017-07-08 21:13:11 -04:00
|
|
|
require "isolation/abstract_unit"
|
2017-07-25 02:01:33 -04:00
|
|
|
require "console_helpers"
|
2017-07-08 21:13:11 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
class DBConsoleTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
2017-07-25 02:01:33 -04:00
|
|
|
include ConsoleHelpers
|
2017-07-08 21:13:11 -04:00
|
|
|
|
|
|
|
def setup
|
2017-07-16 20:11:21 -04:00
|
|
|
skip "PTY unavailable" unless available_pty?
|
|
|
|
|
2017-07-08 21:13:11 -04:00
|
|
|
build_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_use_value_defined_in_environment_file_in_database_yml
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
database: <%= Rails.application.config.database %>
|
|
|
|
adapter: sqlite3
|
|
|
|
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
|
|
|
timeout: 5000
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
master, slave = PTY.open
|
|
|
|
spawn_dbconsole(slave)
|
|
|
|
assert_output("sqlite>", master)
|
|
|
|
ensure
|
|
|
|
master.puts ".exit"
|
|
|
|
end
|
|
|
|
|
2017-07-16 20:11:21 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
development:
|
|
|
|
<<: *default
|
|
|
|
database: db/development.sqlite3
|
|
|
|
|
|
|
|
production:
|
|
|
|
<<: *default
|
|
|
|
database: db/production.sqlite3
|
|
|
|
YAML
|
|
|
|
end
|
|
|
|
|
|
|
|
master, slave = PTY.open
|
|
|
|
spawn_dbconsole(slave, "-e production")
|
|
|
|
assert_output("sqlite>", master)
|
|
|
|
|
2017-07-25 08:10:52 -04:00
|
|
|
master.puts "pragma database_list;"
|
2017-07-16 20:11:21 -04:00
|
|
|
assert_output("production.sqlite3", master)
|
|
|
|
ensure
|
|
|
|
master.puts ".exit"
|
|
|
|
end
|
|
|
|
|
2017-07-08 21:13:11 -04:00
|
|
|
private
|
2017-07-16 20:11:21 -04:00
|
|
|
def spawn_dbconsole(fd, options = nil)
|
|
|
|
Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd)
|
2017-07-08 21:13:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|