diff options
Diffstat (limited to 'railties/test/commands/console_test.rb')
-rw-r--r-- | railties/test/commands/console_test.rb | 103 |
1 files changed, 63 insertions, 40 deletions
diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index de0cf0ba9e..4fc082e4ca 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -1,6 +1,7 @@ -require 'abstract_unit' -require 'env_helpers' -require 'rails/commands/console' +require "abstract_unit" +require "env_helpers" +require "rails/command" +require "rails/commands/console/console_command" class Rails::ConsoleTest < ActiveSupport::TestCase include EnvHelpers @@ -40,7 +41,6 @@ class Rails::ConsoleTest < ActiveSupport::TestCase def test_start_with_sandbox start ["--sandbox"] - assert app.console.started? assert app.sandbox assert_match(/Loading \w+ environment in sandbox \(Rails/, output) @@ -64,52 +64,60 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def test_default_environment_with_rails_env - with_rails_env 'special-production' do + 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 + with_rack_env "production" do start assert_match(/\sproduction\s/, output) end end def test_e_option - start ['-e', 'special-production'] + start ["-e", "special-production"] assert_match(/\sspecial-production\s/, output) end def test_environment_option - start ['--environment=special-production'] + start ["--environment=special-production"] assert_match(/\sspecial-production\s/, output) end def test_rails_env_is_production_when_first_argument_is_p - start ['p'] + start ["p"] assert_match(/\sproduction\s/, output) end def test_rails_env_is_test_when_first_argument_is_t - start ['t'] + start ["t"] assert_match(/\stest\s/, output) end def test_rails_env_is_development_when_argument_is_d - start ['d'] + start ["d"] assert_match(/\sdevelopment\s/, output) end def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present - stubbed_console = Class.new(Rails::Console) do - def available_environments - ['dev'] + Rails::Command::ConsoleCommand.class_eval do + alias_method :old_environments, :available_environments + + define_method :available_environments do + ["dev"] end end - options = stubbed_console.parse_arguments(['dev']) - assert_match('dev', options[:environment]) + + assert_match("dev", parse_arguments(["dev"])[:environment]) + 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 @@ -117,38 +125,53 @@ class Rails::ConsoleTest < ActiveSupport::TestCase private - def start(argv = []) - rails_console = Rails::Console.new(app, parse_arguments(argv)) - @output = capture(:stdout) { rails_console.start } - end + 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 app + @app ||= build_app(FakeConsole) + end - def build_app(console) - mocked_console = Class.new do - attr_reader :sandbox, :console + def build_app(console) + mocked_console = Class.new do + attr_reader :sandbox, :console - def initialize(console) - @console = console - end + def initialize(console) + @console = console + end + + def config + self + end - def config - self + def sandbox=(arg) + @sandbox = arg + end + + def load_console + end end + mocked_console.new(console) + end - def sandbox=(arg) - @sandbox = arg + def parse_arguments(args) + Rails::Command::ConsoleCommand.class_eval do + alias_method :old_perform, :perform + define_method(:perform) do + extract_environment_option_from_argument + + options + end end - def load_console + Rails::Command.invoke(:console, args) + ensure + Rails::Command::ConsoleCommand.class_eval do + undef_method :perform + alias_method :perform, :old_perform + undef_method :old_perform end end - mocked_console.new(console) - end - - def parse_arguments(args) - Rails::Console.parse_arguments(args) - end end |