aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test
diff options
context:
space:
mode:
Diffstat (limited to 'railties/test')
-rw-r--r--railties/test/commands/console_test.rb89
-rw-r--r--railties/test/commands/dbconsole_test.rb21
2 files changed, 71 insertions, 39 deletions
diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb
index 78648a16b3..91ede1cb68 100644
--- a/railties/test/commands/console_test.rb
+++ b/railties/test/commands/console_test.rb
@@ -3,47 +3,44 @@ require 'rails/commands/console'
class Rails::ConsoleTest < ActiveSupport::TestCase
class FakeConsole
+ def self.start; end
end
def setup
end
def test_sandbox_option
- console = Rails::Console.new(app, ["--sandbox"])
+ console = Rails::Console.new(app, parse_arguments(["--sandbox"]))
assert console.sandbox?
end
def test_short_version_of_sandbox_option
- console = Rails::Console.new(app, ["-s"])
+ console = Rails::Console.new(app, parse_arguments(["-s"]))
assert console.sandbox?
end
def test_debugger_option
- console = Rails::Console.new(app, ["--debugger"])
+ console = Rails::Console.new(app, parse_arguments(["--debugger"]))
assert console.debugger?
end
def test_no_options
- console = Rails::Console.new(app, [])
+ console = Rails::Console.new(app, parse_arguments([]))
assert !console.debugger?
assert !console.sandbox?
end
def test_start
- app.expects(:sandbox=).with(nil)
FakeConsole.expects(:start)
-
start
-
assert_match(/Loading \w+ environment \(Rails/, output)
end
def test_start_with_debugger
- app.expects(:sandbox=).with(nil)
+ rails_console = Rails::Console.new(app, parse_arguments(["--debugger"]))
rails_console.expects(:require_debugger).returns(nil)
- FakeConsole.expects(:start)
- start ["--debugger"]
+ silence_stream(STDOUT) { rails_console.start }
end
def test_start_with_sandbox
@@ -56,42 +53,63 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
end
def test_console_with_environment
- app.expects(:sandbox=).with(nil)
- FakeConsole.expects(:start)
-
start ["-e production"]
+ assert_match(/\sproduction\s/, output)
+ end
+
+ def test_console_defaults_to_IRB
+ config = mock("config", :console => nil)
+ app = mock("app", :config => config)
+ app.expects(:load_console).returns(nil)
- assert_match(/production/, output)
+ assert_equal IRB, Rails::Console.new(app).console
end
- def test_console_with_rails_environment
- app.expects(:sandbox=).with(nil)
- FakeConsole.expects(:start)
+ def test_default_environment_with_no_rails_env
+ with_rails_env nil do
+ start
+ assert_match /\sdevelopment\s/, output
+ end
+ end
- start ["RAILS_ENV=production"]
+ def test_default_environment_with_rails_env
+ with_rails_env 'special-production' do
+ start
+ assert_match /\sspecial-production\s/, output
+ end
+ end
+
+ def test_e_option
+ start ['-e', 'special-production']
+ assert_match /\sspecial-production\s/, output
+ end
- assert_match(/production/, output)
+ def test_environment_option
+ start ['--environment=special-production']
+ assert_match /\sspecial-production\s/, output
end
+ def test_rails_env_is_production_when_first_argument_is_p
+ start ['p']
+ assert_match /\sproduction\s/, output
+ end
- def test_console_defaults_to_IRB
- config = mock("config", :console => nil)
- app = mock("app", :config => config)
- app.expects(:load_console).returns(nil)
+ def test_rails_env_is_test_when_first_argument_is_t
+ start ['t']
+ assert_match /\stest\s/, output
+ end
- assert_equal IRB, Rails::Console.new(app).console
+ def test_rails_env_is_development_when_argument_is_d
+ start ['d']
+ assert_match /\sdevelopment\s/, output
end
private
attr_reader :output
- def rails_console
- @rails_console ||= Rails::Console.new(app)
- end
-
def start(argv = [])
- rails_console.stubs(:arguments => argv)
+ rails_console = Rails::Console.new(app, parse_arguments(argv))
@output = output = capture(:stdout) { rails_console.start }
end
@@ -99,8 +117,21 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
@app ||= begin
config = mock("config", :console => FakeConsole)
app = mock("app", :config => config)
+ app.stubs(:sandbox=).returns(nil)
app.expects(:load_console)
app
end
end
+
+ def parse_arguments(args)
+ Rails::Console.parse_arguments(args)
+ end
+
+ def with_rails_env(env)
+ original_rails_env = ENV['RAILS_ENV']
+ ENV['RAILS_ENV'] = env
+ yield
+ ensure
+ ENV['RAILS_ENV'] = original_rails_env
+ end
end
diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb
index 562b83713b..1b477eb98a 100644
--- a/railties/test/commands/dbconsole_test.rb
+++ b/railties/test/commands/dbconsole_test.rb
@@ -10,35 +10,36 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
Rails::DBConsole.const_set(:APP_PATH, "erb")
app_config({})
- capture_abort { Rails::DBConsole.config }
+ capture_abort { Rails::DBConsole.new.config }
assert aborted
assert_match(/No database is configured for the environment '\w+'/, output)
app_config(test: "with_init")
- assert_equal Rails::DBConsole.config, "with_init"
+ assert_equal Rails::DBConsole.new.config, "with_init"
app_db_file("test:\n without_init")
- assert_equal Rails::DBConsole.config, "without_init"
+ assert_equal Rails::DBConsole.new.config, "without_init"
app_db_file("test:\n <%= Rails.something_app_specific %>")
- assert_equal Rails::DBConsole.config, "with_init"
+ assert_equal Rails::DBConsole.new.config, "with_init"
app_db_file("test:\n\ninvalid")
- assert_equal Rails::DBConsole.config, "with_init"
+ assert_equal Rails::DBConsole.new.config, "with_init"
end
def test_env
- assert_equal Rails::DBConsole.env, "test"
+ assert_equal Rails::DBConsole.new.environment, "test"
+
+ ENV['RAILS_ENV'] = nil
Rails.stubs(:respond_to?).with(:env).returns(false)
- assert_equal Rails::DBConsole.env, "test"
+ assert_equal Rails::DBConsole.new.environment, "development"
- ENV['RAILS_ENV'] = nil
ENV['RACK_ENV'] = "rack_env"
- assert_equal Rails::DBConsole.env, "rack_env"
+ assert_equal Rails::DBConsole.new.environment, "rack_env"
ENV['RAILS_ENV'] = "rails_env"
- assert_equal Rails::DBConsole.env, "rails_env"
+ assert_equal Rails::DBConsole.new.environment, "rails_env"
ensure
ENV['RAILS_ENV'] = "test"
end