2012-02-01 07:36:47 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'rails/commands/console'
|
|
|
|
|
|
|
|
class Rails::ConsoleTest < ActiveSupport::TestCase
|
|
|
|
class FakeConsole
|
2012-05-11 17:19:58 -04:00
|
|
|
def self.start; end
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
2012-02-16 15:26:01 -05:00
|
|
|
def setup
|
|
|
|
end
|
|
|
|
|
2012-02-01 07:36:47 -05:00
|
|
|
def test_sandbox_option
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments(["--sandbox"]))
|
2012-02-01 07:36:47 -05:00
|
|
|
assert console.sandbox?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_short_version_of_sandbox_option
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments(["-s"]))
|
2012-02-01 07:36:47 -05:00
|
|
|
assert console.sandbox?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_debugger_option
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments(["--debugger"]))
|
2012-02-01 07:36:47 -05:00
|
|
|
assert console.debugger?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_options
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments([]))
|
2012-02-01 07:36:47 -05:00
|
|
|
assert !console.debugger?
|
|
|
|
assert !console.sandbox?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_start
|
|
|
|
FakeConsole.expects(:start)
|
|
|
|
start
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/Loading \w+ environment \(Rails/, output)
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_start_with_debugger
|
2012-05-11 17:19:58 -04:00
|
|
|
rails_console = Rails::Console.new(app, parse_arguments(["--debugger"]))
|
2012-02-01 07:36:47 -05:00
|
|
|
rails_console.expects(:require_debugger).returns(nil)
|
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
silence_stream(STDOUT) { rails_console.start }
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_start_with_sandbox
|
|
|
|
app.expects(:sandbox=).with(true)
|
|
|
|
FakeConsole.expects(:start)
|
|
|
|
|
|
|
|
start ["--sandbox"]
|
|
|
|
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/Loading \w+ environment in sandbox \(Rails/, output)
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
2012-03-22 13:13:12 -04:00
|
|
|
def test_console_with_environment
|
|
|
|
start ["-e production"]
|
2012-05-11 17:19:58 -04:00
|
|
|
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)
|
2012-03-22 13:13:12 -04:00
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
assert_equal IRB, Rails::Console.new(app).console
|
2012-03-22 13:13:12 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_default_environment_with_no_rails_env
|
|
|
|
with_rails_env nil do
|
|
|
|
start
|
|
|
|
assert_match /\sdevelopment\s/, output
|
|
|
|
end
|
|
|
|
end
|
2012-03-22 13:13:12 -04:00
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
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
|
2012-03-22 13:13:12 -04:00
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_environment_option
|
|
|
|
start ['--environment=special-production']
|
|
|
|
assert_match /\sspecial-production\s/, output
|
2012-03-22 13:13:12 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_rails_env_is_production_when_first_argument_is_p
|
|
|
|
start ['p']
|
|
|
|
assert_match /\sproduction\s/, output
|
|
|
|
end
|
2012-03-22 13:13:12 -04:00
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_rails_env_is_test_when_first_argument_is_t
|
|
|
|
start ['t']
|
|
|
|
assert_match /\stest\s/, output
|
|
|
|
end
|
2012-02-01 07:36:47 -05:00
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_rails_env_is_development_when_argument_is_d
|
|
|
|
start ['d']
|
|
|
|
assert_match /\sdevelopment\s/, output
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :output
|
|
|
|
|
|
|
|
def start(argv = [])
|
2012-05-11 17:19:58 -04:00
|
|
|
rails_console = Rails::Console.new(app, parse_arguments(argv))
|
2012-02-01 07:36:47 -05:00
|
|
|
@output = output = capture(:stdout) { rails_console.start }
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
@app ||= begin
|
|
|
|
config = mock("config", :console => FakeConsole)
|
|
|
|
app = mock("app", :config => config)
|
2012-05-11 17:19:58 -04:00
|
|
|
app.stubs(:sandbox=).returns(nil)
|
2012-02-01 07:36:47 -05:00
|
|
|
app.expects(:load_console)
|
|
|
|
app
|
|
|
|
end
|
|
|
|
end
|
2012-05-11 17:19:58 -04:00
|
|
|
|
|
|
|
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
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|