Permalink
Browse files

Merge pull request #29010 from y-yagi/fix_28988

Allow irb options to be passed from `rails console` command
  • Loading branch information...
guilleiguaran committed May 14, 2017
2 parents 23aa0a2 + c776b64 commit 630b65347af7298c2e33f0f3941cb074de8529dd
View
@@ -1,3 +1,9 @@
+* Allow irb options to be passed from `rails console` command.
+
+ Fixes #28988.
+
+ *Yuji Yaginuma*
+
* Added a shared section to config/database.yml that will be loaded for all environments.
*Pierre Schambacher*
@@ -73,14 +73,26 @@ class ConsoleCommand < Base # :nodoc:
class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."
+ def initialize(args = [], local_options = {}, config = {})
+ console_options = []
+
+ # For the same behavior as OptionParser, leave only options after "--" in ARGV.
+ termination = local_options.find_index("--")
+ if termination
+ console_options = local_options[termination + 1..-1]
+ local_options = local_options[0...termination]
+ end
+
+ ARGV.replace(console_options)
+ super(args, local_options, config)
+ end
+
def perform
extract_environment_option_from_argument
# RAILS_ENV needs to be set before config/application is required.
ENV["RAILS_ENV"] = options[:environment]
- ARGV.clear # Clear ARGV so IRB doesn't freak.
-
require_application_and_environment!
Rails::Console.start(Rails.application, options)
end
@@ -136,28 +136,36 @@ def write_prompt(command, expected_output = nil)
assert_output "> "
end
- def spawn_console
+ def spawn_console(options)
Process.spawn(
- "#{app_path}/bin/rails console --sandbox",
+ "#{app_path}/bin/rails console #{options}",
in: @slave, out: @slave, err: @slave
)
assert_output "> ", 30
end
def test_sandbox
- spawn_console
+ spawn_console("--sandbox")
write_prompt "Post.count", "=> 0"
write_prompt "Post.create"
write_prompt "Post.count", "=> 1"
@master.puts "quit"
- spawn_console
+ spawn_console("--sandbox")
write_prompt "Post.count", "=> 0"
write_prompt "Post.transaction { Post.create; raise }"
write_prompt "Post.count", "=> 0"
@master.puts "quit"
end
+
+ def test_environment_option_and_irb_option
+ spawn_console("test -- --verbose")
+
+ write_prompt "a = 1", "a = 1"
+ write_prompt "puts Rails.env", "puts Rails.env\r\ntest"
+ @master.puts "quit"
+ end
end

0 comments on commit 630b653

Please sign in to comment.