Skip to content
This repository
Browse code

Merge pull request #5550 from schneems/schneems/server_env

Fix environment support for rails server, and match interface of rails console
  • Loading branch information...
commit e944b296a90825c8aaee23a324db6e34521c7e74 2 parents 86cd1b1 + 7529283
Aaron Patterson tenderlove authored
13 railties/lib/rails/commands/console.rb
@@ -24,6 +24,9 @@ def options
24 24 OptionParser.new do |opt|
25 25 opt.banner = "Usage: console [environment] [options]"
26 26 opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
  27 + opt.on("-e", "--environment=name", String,
  28 + "Specifies the environment to run this console under (test/development/production).",
  29 + "Default: development") { |v| options[:environment] = v.strip }
27 30 opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v }
28 31 opt.parse!(arguments)
29 32 end
@@ -36,6 +39,14 @@ def sandbox?
36 39 options[:sandbox]
37 40 end
38 41
  42 + def environment?
  43 + options[:environment]
  44 + end
  45 +
  46 + def set_environment!
  47 + Rails.env = options[:environment]
  48 + end
  49 +
39 50 def debugger?
40 51 options[:debugger]
41 52 end
@@ -45,6 +56,8 @@ def start
45 56
46 57 require_debugger if debugger?
47 58
  59 + set_environment! if environment?
  60 +
48 61 if sandbox?
49 62 puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})"
50 63 puts "Any modifications you make will be rolled back on exit"
5 railties/lib/rails/commands/server.rb
@@ -32,6 +32,11 @@ def parse!(args)
32 32
33 33 opt_parser.parse! args
34 34
  35 + # Handle's environment like RAILS_ENV=production passed in directly
  36 + if index = args.index {|arg| arg.include?("RAILS_ENV")}
  37 + options[:environment] ||= args.delete_at(index).split('=').last
  38 + end
  39 +
35 40 options[:server] = args.shift
36 41 options
37 42 end
19 railties/test/commands/console_test.rb
@@ -55,6 +55,25 @@ def test_start_with_sandbox
55 55 assert_match /Loading \w+ environment in sandbox \(Rails/, output
56 56 end
57 57
  58 + def test_console_with_environment
  59 + app.expects(:sandbox=).with(nil)
  60 + FakeConsole.expects(:start)
  61 +
  62 + start ["-e production"]
  63 +
  64 + assert_match /production/, output
  65 + end
  66 +
  67 + def test_console_with_rails_environment
  68 + app.expects(:sandbox=).with(nil)
  69 + FakeConsole.expects(:start)
  70 +
  71 + start ["RAILS_ENV=production"]
  72 +
  73 + assert_match /production/, output
  74 + end
  75 +
  76 +
58 77 def test_console_defaults_to_IRB
59 78 config = mock("config", :console => nil)
60 79 app = mock("app", :config => config)
26 railties/test/commands/server_test.rb
... ... @@ -0,0 +1,26 @@
  1 +require 'abstract_unit'
  2 +require 'rails/commands/server'
  3 +
  4 +class Rails::ServerTest < ActiveSupport::TestCase
  5 +
  6 + def test_environment_with_server_option
  7 + args = ["thin", "RAILS_ENV=production"]
  8 + options = Rails::Server::Options.new.parse!(args)
  9 + assert_equal 'production', options[:environment]
  10 + assert_equal 'thin', options[:server]
  11 + end
  12 +
  13 + def test_environment_without_server_option
  14 + args = ["RAILS_ENV=production"]
  15 + options = Rails::Server::Options.new.parse!(args)
  16 + assert_equal 'production', options[:environment]
  17 + assert_nil options[:server]
  18 + end
  19 +
  20 + def test_server_option_without_environment
  21 + args = ["thin"]
  22 + options = Rails::Server::Options.new.parse!(args)
  23 + assert_nil options[:environment]
  24 + assert_equal 'thin', options[:server]
  25 + end
  26 +end

0 comments on commit e944b29

Please sign in to comment.
Something went wrong with that request. Please try again.