Skip to content
This repository
Browse code

fix rails server support of RAILS_ENV variable

When launching rails server from the command line with a rails environment specified such as `rails server RAILS_ENV=production` an error would occur since rails will try to use `RAILS_ENV=production` as it's server. 

When launching rails with a specified server such as thin `rails server thin RAILS_ENV=production` no error will be thrown, but rails will not start up in the specified environment.

This fixes both of those cases
  • Loading branch information...
commit 0a555dd421f3b7966df1a1f70ae462a143734d21 1 parent b49a7dd
Richard Schneeman authored March 20, 2012
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
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 notes on commit 0a555dd

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