From efd808755cac74f0001907a1d635856160d11f6a Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 15 Sep 2016 21:56:03 +0200 Subject: Fix console tests. --- railties/lib/rails/command/environment_argument.rb | 2 ++ .../lib/rails/commands/console/console_command.rb | 2 +- railties/test/commands/console_test.rb | 36 ++++++++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/railties/lib/rails/command/environment_argument.rb b/railties/lib/rails/command/environment_argument.rb index bff076adc9..05eac34155 100644 --- a/railties/lib/rails/command/environment_argument.rb +++ b/railties/lib/rails/command/environment_argument.rb @@ -13,6 +13,8 @@ module Rails def extract_environment_option_from_argument if environment self.options = options.merge(environment: acceptable_environment(environment)) + elsif !options[:environment] + self.options = options.merge(environment: Rails::Command.environment) end end diff --git a/railties/lib/rails/commands/console/console_command.rb b/railties/lib/rails/commands/console/console_command.rb index 730b108d8c..617066f575 100644 --- a/railties/lib/rails/commands/console/console_command.rb +++ b/railties/lib/rails/commands/console/console_command.rb @@ -70,7 +70,7 @@ module Rails class_option :sandbox, aliases: "-s", type: :boolean, default: false, desc: "Rollback database modifications on exit." - class_option :environment, aliases: "-e", type: :string, default: Rails::Command.environment, + class_option :environment, aliases: "-e", type: :string, desc: "Specifies the environment to run this console under (test/development/production)." def perform diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 96ac7c0661..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 "rails/command" +require "rails/commands/console/console_command" class Rails::ConsoleTest < ActiveSupport::TestCase include EnvHelpers @@ -102,13 +103,21 @@ class Rails::ConsoleTest < ActiveSupport::TestCase 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 + 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 @@ -148,6 +157,21 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def parse_arguments(args) - Rails::Console.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 + + 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 end -- cgit v1.2.3