1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/commands/console_test.rb

153 lines
3.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "abstract_unit"
require "env_helpers"
2016-09-15 15:56:03 -04:00
require "rails/command"
require "rails/commands/console/console_command"
class Rails::ConsoleTest < ActiveSupport::TestCase
include EnvHelpers
class FakeConsole
2014-07-07 22:39:18 -04:00
def self.started?
@started
end
def self.start
@started = true
end
end
def test_sandbox_option
console = Rails::Console.new(app, parse_arguments(["--sandbox"]))
assert_predicate console, :sandbox?
end
def test_short_version_of_sandbox_option
console = Rails::Console.new(app, parse_arguments(["-s"]))
assert_predicate console, :sandbox?
end
def test_no_options
console = Rails::Console.new(app, parse_arguments([]))
assert_not_predicate console, :sandbox?
end
def test_start
start
2014-07-07 22:39:18 -04:00
assert_predicate app.console, :started?
2012-05-30 05:08:56 -04:00
assert_match(/Loading \w+ environment \(Rails/, output)
end
def test_start_with_sandbox
start ["--sandbox"]
assert_predicate app.console, :started?
2014-07-07 22:39:18 -04:00
assert app.sandbox
2012-05-30 05:08:56 -04:00
assert_match(/Loading \w+ environment in sandbox \(Rails/, output)
end
def test_console_with_environment
start ["-e", "production"]
assert_match(/\sproduction\s/, output)
end
def test_console_defaults_to_IRB
2014-07-07 22:39:18 -04:00
app = build_app(nil)
assert_equal IRB, Rails::Console.new(app).console
end
def test_default_environment_with_no_rails_env
with_rails_env nil do
start
assert_match(/\sdevelopment\s/, output)
end
end
def test_default_environment_with_rails_env
with_rails_env "special-production" do
start
assert_match(/\sspecial-production\s/, output)
end
end
def test_default_environment_with_rack_env
with_rack_env "production" do
start
assert_match(/\sproduction\s/, output)
end
end
def test_e_option
start ["-e", "special-production"]
assert_match(/\sspecial-production\s/, output)
end
def test_e_option_is_properly_expanded
start ["-e", "prod"]
assert_match(/\sproduction\s/, output)
end
def test_environment_option
start ["--environment=special-production"]
assert_match(/\sspecial-production\s/, output)
end
def test_rails_env_is_dev_when_environment_option_is_dev_and_dev_env_is_present
2016-09-15 15:56:03 -04:00
Rails::Command::ConsoleCommand.class_eval do
alias_method :old_environments, :available_environments
define_method :available_environments do
["dev"]
2014-07-07 22:39:18 -04:00
end
end
2016-09-15 15:56:03 -04:00
assert_match("dev", parse_arguments(["-e", "dev"])[:environment])
2016-09-15 15:56:03 -04:00
ensure
Rails::Command::ConsoleCommand.class_eval do
undef_method :available_environments
alias_method :available_environments, :old_environments
undef_method :old_environments
end
end
attr_reader :output
2013-04-04 13:53:46 -04:00
private :output
private
def start(argv = [])
rails_console = Rails::Console.new(app, parse_arguments(argv))
@output = capture(:stdout) { rails_console.start }
end
def app
@app ||= build_app(FakeConsole)
end
def build_app(console)
mocked_console = Class.new do
attr_accessor :sandbox
attr_reader :console, :disable_sandbox
2014-07-07 22:39:18 -04:00
def initialize(console)
@console = console
end
2014-07-07 22:39:18 -04:00
def config
self
end
2014-07-07 22:39:18 -04:00
def load_console
end
2014-07-07 22:39:18 -04:00
end
mocked_console.new(console)
2014-07-07 22:39:18 -04:00
end
def parse_arguments(args)
command = Rails::Command::ConsoleCommand.new([], args)
command.send(:extract_environment_option_from_argument)
command.options
end
end