2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "env_helpers"
|
2016-09-15 15:56:03 -04:00
|
|
|
require "rails/command"
|
|
|
|
require "rails/commands/console/console_command"
|
2012-02-01 07:36:47 -05:00
|
|
|
|
|
|
|
class Rails::ConsoleTest < ActiveSupport::TestCase
|
2012-12-06 07:05:45 -05:00
|
|
|
include EnvHelpers
|
|
|
|
|
2012-02-01 07:36:47 -05:00
|
|
|
class FakeConsole
|
2014-07-07 22:39:18 -04:00
|
|
|
def self.started?
|
|
|
|
@started
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.start
|
|
|
|
@started = true
|
|
|
|
end
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sandbox_option
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments(["--sandbox"]))
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate console, :sandbox?
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_short_version_of_sandbox_option
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments(["-s"]))
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate console, :sandbox?
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_options
|
2012-05-11 17:19:58 -04:00
|
|
|
console = Rails::Console.new(app, parse_arguments([]))
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate console, :sandbox?
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_start
|
|
|
|
start
|
2014-07-07 22:39:18 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate app.console, :started?
|
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_sandbox
|
|
|
|
start ["--sandbox"]
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
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)
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|
|
|
|
|
2012-03-22 13:13:12 -04:00
|
|
|
def test_console_with_environment
|
2017-06-08 18:15:41 -04:00
|
|
|
start ["-e", "production"]
|
2012-05-11 17:19:58 -04:00
|
|
|
assert_match(/\sproduction\s/, output)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_console_defaults_to_IRB
|
2014-07-07 22:39:18 -04:00
|
|
|
app = build_app(nil)
|
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
|
2012-10-10 01:10:14 -04:00
|
|
|
assert_match(/\sdevelopment\s/, output)
|
2012-05-11 17:19:58 -04:00
|
|
|
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
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rails_env "special-production" do
|
2012-05-11 17:19:58 -04:00
|
|
|
start
|
2012-10-10 01:10:14 -04:00
|
|
|
assert_match(/\sspecial-production\s/, output)
|
2012-05-11 17:19:58 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
|
2015-03-20 11:14:11 -04:00
|
|
|
def test_default_environment_with_rack_env
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rack_env "production" do
|
2015-03-20 11:14:11 -04:00
|
|
|
start
|
|
|
|
assert_match(/\sproduction\s/, output)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_e_option
|
2016-08-06 13:16:09 -04:00
|
|
|
start ["-e", "special-production"]
|
2012-10-10 01:10:14 -04:00
|
|
|
assert_match(/\sspecial-production\s/, output)
|
2012-05-11 17:19:58 -04:00
|
|
|
end
|
2012-03-22 13:13:12 -04:00
|
|
|
|
2017-07-16 08:54:04 -04:00
|
|
|
def test_e_option_is_properly_expanded
|
|
|
|
start ["-e", "prod"]
|
|
|
|
assert_match(/\sproduction\s/, output)
|
|
|
|
end
|
|
|
|
|
2012-05-11 17:19:58 -04:00
|
|
|
def test_environment_option
|
2016-08-06 13:16:09 -04:00
|
|
|
start ["--environment=special-production"]
|
2012-10-10 01:10:14 -04:00
|
|
|
assert_match(/\sspecial-production\s/, output)
|
2012-03-22 13:13:12 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 20:47:39 -05:00
|
|
|
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
|
2016-08-06 13:16:09 -04:00
|
|
|
["dev"]
|
2014-07-07 22:39:18 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-15 15:56:03 -04:00
|
|
|
|
2019-01-16 20:47:39 -05: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
|
2012-12-27 10:04:54 -05:00
|
|
|
end
|
|
|
|
|
2012-02-01 07:36:47 -05:00
|
|
|
attr_reader :output
|
2013-04-04 13:53:46 -04:00
|
|
|
private :output
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def start(argv = [])
|
|
|
|
rails_console = Rails::Console.new(app, parse_arguments(argv))
|
|
|
|
@output = capture(:stdout) { rails_console.start }
|
|
|
|
end
|
2012-02-01 07:36:47 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def app
|
|
|
|
@app ||= build_app(FakeConsole)
|
|
|
|
end
|
2013-03-08 19:48:27 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def build_app(console)
|
|
|
|
mocked_console = Class.new do
|
2018-03-29 22:29:55 -04:00
|
|
|
attr_accessor :sandbox
|
2019-03-23 03:47:26 -04:00
|
|
|
attr_reader :console, :disable_sandbox
|
2014-07-07 22:39:18 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def initialize(console)
|
|
|
|
@console = console
|
|
|
|
end
|
2014-07-07 22:39:18 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def config
|
|
|
|
self
|
|
|
|
end
|
2014-07-07 22:39:18 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def load_console
|
|
|
|
end
|
2014-07-07 22:39:18 -04:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
mocked_console.new(console)
|
2014-07-07 22:39:18 -04:00
|
|
|
end
|
2012-05-11 17:19:58 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def parse_arguments(args)
|
2017-10-08 00:09:30 -04:00
|
|
|
command = Rails::Command::ConsoleCommand.new([], args)
|
|
|
|
command.send(:extract_environment_option_from_argument)
|
|
|
|
command.options
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2012-02-01 07:36:47 -05:00
|
|
|
end
|