2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
require "isolation/abstract_unit"
|
2016-08-06 13:16:09 -04:00
|
|
|
require "env_helpers"
|
2016-09-18 14:10:27 -04:00
|
|
|
require "rails/command"
|
|
|
|
require "rails/commands/server/server_command"
|
2012-03-20 19:28:30 -04:00
|
|
|
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
class Rails::Command::ServerCommandTest < ActiveSupport::TestCase
|
2012-12-06 07:05:45 -05:00
|
|
|
include EnvHelpers
|
2012-03-20 19:28:30 -04:00
|
|
|
|
|
|
|
def test_environment_with_server_option
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
args = ["-u", "thin", "-e", "production"]
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal "production", options[:environment]
|
|
|
|
assert_equal "thin", options[:server]
|
2012-03-20 19:28:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_environment_without_server_option
|
2016-11-05 17:52:17 -04:00
|
|
|
args = ["-e", "production"]
|
|
|
|
options = parse_arguments(args)
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal "production", options[:environment]
|
2012-03-20 19:28:30 -04:00
|
|
|
assert_nil options[:server]
|
|
|
|
end
|
|
|
|
|
2019-03-03 18:12:05 -05:00
|
|
|
def test_environment_option_is_properly_expanded
|
|
|
|
args = ["-e", "prod"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal "production", options[:environment]
|
|
|
|
end
|
|
|
|
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
def test_explicit_using_option
|
|
|
|
args = ["-u", "thin"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal "thin", options[:server]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_using_server_mistype
|
2021-06-03 13:14:03 -04:00
|
|
|
output = run_command("--using", "tin")
|
|
|
|
assert_match "Could not find server 'tin'", output
|
|
|
|
assert_match "Did you mean? thin", output
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
end
|
|
|
|
|
2018-12-29 04:57:55 -05:00
|
|
|
def test_using_server_mistype_without_suggestion
|
|
|
|
output = run_command("--using", "t")
|
2021-06-03 13:14:03 -04:00
|
|
|
assert_match "Could not find server 't'", output
|
|
|
|
assert_no_match "Did you mean", output
|
2018-12-29 04:57:55 -05:00
|
|
|
end
|
|
|
|
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
def test_using_known_server_that_isnt_in_the_gemfile
|
|
|
|
assert_match(/Could not load server "unicorn". Maybe you need to the add it to the Gemfile/, run_command("-u", "unicorn"))
|
|
|
|
end
|
|
|
|
|
2017-10-13 18:27:12 -04:00
|
|
|
def test_daemon_with_option
|
2017-10-13 09:21:11 -04:00
|
|
|
args = ["-d"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal true, options[:daemonize]
|
|
|
|
end
|
|
|
|
|
2017-10-13 18:27:12 -04:00
|
|
|
def test_daemon_without_option
|
2017-10-13 09:21:11 -04:00
|
|
|
args = []
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal false, options[:daemonize]
|
|
|
|
end
|
|
|
|
|
2012-03-20 19:28:30 -04:00
|
|
|
def test_server_option_without_environment
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
args = ["-u", "thin"]
|
2016-11-05 17:52:17 -04:00
|
|
|
with_rack_env nil do
|
|
|
|
with_rails_env nil do
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal "development", options[:environment]
|
|
|
|
assert_equal "thin", options[:server]
|
|
|
|
end
|
|
|
|
end
|
2012-03-20 19:28:30 -04:00
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
|
|
|
|
def test_environment_with_rails_env
|
2015-03-20 11:14:11 -04:00
|
|
|
with_rack_env nil do
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rails_env "production" do
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments
|
|
|
|
assert_equal "production", options[:environment]
|
2015-03-20 11:14:11 -04:00
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2012-12-05 12:05:33 -05:00
|
|
|
end
|
|
|
|
|
2015-03-20 11:14:11 -04:00
|
|
|
def test_environment_with_rack_env
|
2013-10-12 14:27:13 -04:00
|
|
|
with_rails_env nil do
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rack_env "production" do
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments
|
|
|
|
assert_equal "production", options[:environment]
|
2015-03-20 11:14:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-03-19 05:06:28 -04:00
|
|
|
|
2015-08-17 14:02:35 -04:00
|
|
|
def test_environment_with_port
|
|
|
|
switch_env "PORT", "1234" do
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments
|
|
|
|
assert_equal 1234, options[:Port]
|
2015-08-17 14:02:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-16 01:35:13 -04:00
|
|
|
def test_environment_with_binding
|
|
|
|
switch_env "BINDING", "1.2.3.4" do
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments
|
|
|
|
assert_equal "1.2.3.4", options[:Host]
|
2016-07-04 14:36:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-10 11:46:52 -04:00
|
|
|
def test_environment_with_pidfile
|
|
|
|
switch_env "PIDFILE", "/tmp/rails.pid" do
|
|
|
|
options = parse_arguments
|
|
|
|
assert_equal "/tmp/rails.pid", options[:pid]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-20 23:35:20 -04:00
|
|
|
def test_caching_without_option
|
|
|
|
args = []
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2016-12-29 14:09:43 -05:00
|
|
|
assert_nil options[:caching]
|
2015-07-20 23:35:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_caching_with_option
|
|
|
|
args = ["--dev-caching"]
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-07-20 23:35:20 -04:00
|
|
|
assert_equal true, options[:caching]
|
|
|
|
|
|
|
|
args = ["--no-dev-caching"]
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-07-20 23:35:20 -04:00
|
|
|
assert_equal false, options[:caching]
|
|
|
|
end
|
|
|
|
|
2017-09-26 13:27:53 -04:00
|
|
|
def test_early_hints_with_option
|
|
|
|
args = ["--early-hints"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal true, options[:early_hints]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_early_hints_is_nil_by_default
|
|
|
|
args = []
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_nil options[:early_hints]
|
|
|
|
end
|
|
|
|
|
2015-03-20 11:14:11 -04:00
|
|
|
def test_log_stdout
|
|
|
|
with_rack_env nil do
|
|
|
|
with_rails_env nil do
|
2015-03-19 05:06:28 -04:00
|
|
|
args = []
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2013-10-12 14:27:13 -04:00
|
|
|
assert_equal true, options[:log_stdout]
|
|
|
|
|
2015-03-20 11:14:11 -04:00
|
|
|
args = ["-e", "development"]
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-03-20 11:14:11 -04:00
|
|
|
assert_equal true, options[:log_stdout]
|
|
|
|
|
2018-07-08 11:28:34 -04:00
|
|
|
args = ["-e", "development", "-d"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal false, options[:log_stdout]
|
|
|
|
|
2015-03-20 11:14:11 -04:00
|
|
|
args = ["-e", "production"]
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2013-10-12 14:27:13 -04:00
|
|
|
assert_equal false, options[:log_stdout]
|
2015-03-20 11:14:11 -04:00
|
|
|
|
2018-07-08 11:28:34 -04:00
|
|
|
args = ["-e", "development", "--no-log-to-stdout"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal false, options[:log_stdout]
|
|
|
|
|
|
|
|
args = ["-e", "production", "--log-to-stdout"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal true, options[:log_stdout]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rack_env "development" do
|
2015-03-20 11:14:11 -04:00
|
|
|
args = []
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-03-20 11:14:11 -04:00
|
|
|
assert_equal true, options[:log_stdout]
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rack_env "production" do
|
2015-03-20 11:14:11 -04:00
|
|
|
args = []
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-03-20 11:14:11 -04:00
|
|
|
assert_equal false, options[:log_stdout]
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rails_env "development" do
|
2015-03-20 11:14:11 -04:00
|
|
|
args = []
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-03-20 11:14:11 -04:00
|
|
|
assert_equal true, options[:log_stdout]
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
with_rails_env "production" do
|
2015-03-20 11:14:11 -04:00
|
|
|
args = []
|
2016-11-05 17:52:17 -04:00
|
|
|
options = parse_arguments(args)
|
2015-03-20 11:14:11 -04:00
|
|
|
assert_equal false, options[:log_stdout]
|
|
|
|
end
|
2013-10-12 14:27:13 -04:00
|
|
|
end
|
|
|
|
end
|
2013-06-18 16:24:00 -04:00
|
|
|
end
|
2016-01-11 05:12:32 -05:00
|
|
|
|
2017-02-27 03:29:16 -05:00
|
|
|
def test_host
|
|
|
|
with_rails_env "development" do
|
|
|
|
options = parse_arguments([])
|
|
|
|
assert_equal "localhost", options[:Host]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_rails_env "production" do
|
|
|
|
options = parse_arguments([])
|
|
|
|
assert_equal "0.0.0.0", options[:Host]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_rails_env "development" do
|
|
|
|
args = ["-b", "127.0.0.1"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal "127.0.0.1", options[:Host]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-21 19:07:40 -04:00
|
|
|
def test_argument_precedence_over_environment_variable
|
2017-05-01 04:05:49 -04:00
|
|
|
switch_env "PORT", "1234" do
|
|
|
|
args = ["-p", "5678"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal 5678, options[:Port]
|
|
|
|
end
|
|
|
|
|
|
|
|
switch_env "PORT", "1234" do
|
|
|
|
args = ["-p", "3000"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal 3000, options[:Port]
|
|
|
|
end
|
|
|
|
|
2018-04-16 01:35:13 -04:00
|
|
|
switch_env "BINDING", "1.2.3.4" do
|
2017-03-21 19:07:40 -04:00
|
|
|
args = ["-b", "127.0.0.1"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal "127.0.0.1", options[:Host]
|
|
|
|
end
|
2019-06-10 11:46:52 -04:00
|
|
|
|
|
|
|
switch_env "PIDFILE", "/tmp/rails.pid" do
|
|
|
|
args = ["-P", "/somewhere/else.pid"]
|
|
|
|
options = parse_arguments(args)
|
|
|
|
assert_equal "/somewhere/else.pid", options[:pid]
|
|
|
|
end
|
2017-03-21 19:07:40 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 17:22:50 -05:00
|
|
|
def test_records_user_supplied_options
|
2018-12-13 02:14:13 -05:00
|
|
|
server_options = parse_arguments(["-p", "3001"])
|
2017-02-23 17:22:50 -05:00
|
|
|
assert_equal [:Port], server_options[:user_supplied_options]
|
|
|
|
|
2018-12-13 02:14:13 -05:00
|
|
|
server_options = parse_arguments(["--port", "3001"])
|
2017-02-23 17:22:50 -05:00
|
|
|
assert_equal [:Port], server_options[:user_supplied_options]
|
2017-05-19 02:13:05 -04:00
|
|
|
|
|
|
|
server_options = parse_arguments(["-p3001", "-C", "--binding", "127.0.0.1"])
|
|
|
|
assert_equal [:Port, :Host, :caching], server_options[:user_supplied_options]
|
|
|
|
|
|
|
|
server_options = parse_arguments(["--port=3001"])
|
|
|
|
assert_equal [:Port], server_options[:user_supplied_options]
|
2018-04-16 01:35:13 -04:00
|
|
|
|
|
|
|
switch_env "BINDING", "1.2.3.4" do
|
|
|
|
server_options = parse_arguments
|
|
|
|
assert_equal [:Host], server_options[:user_supplied_options]
|
|
|
|
end
|
2019-06-10 11:46:52 -04:00
|
|
|
|
|
|
|
switch_env "PORT", "3001" do
|
|
|
|
server_options = parse_arguments
|
|
|
|
assert_equal [:Port], server_options[:user_supplied_options]
|
|
|
|
end
|
|
|
|
|
|
|
|
switch_env "PIDFILE", "/tmp/server.pid" do
|
|
|
|
server_options = parse_arguments
|
|
|
|
assert_equal [:pid], server_options[:user_supplied_options]
|
|
|
|
end
|
2017-02-23 17:22:50 -05:00
|
|
|
end
|
|
|
|
|
2016-01-11 05:12:32 -05:00
|
|
|
def test_default_options
|
2017-01-07 01:34:52 -05:00
|
|
|
server = Rails::Server.new
|
|
|
|
old_default_options = server.default_options
|
2016-01-11 05:12:32 -05:00
|
|
|
|
|
|
|
Dir.chdir("..") do
|
2017-01-07 01:34:52 -05:00
|
|
|
assert_equal old_default_options, server.default_options
|
2016-01-11 05:12:32 -05:00
|
|
|
end
|
|
|
|
end
|
2016-03-30 01:11:41 -04:00
|
|
|
|
|
|
|
def test_restart_command_contains_customized_options
|
|
|
|
original_args = ARGV.dup
|
2016-11-05 17:52:17 -04:00
|
|
|
args = %w(-p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C)
|
2016-03-30 01:11:41 -04:00
|
|
|
ARGV.replace args
|
|
|
|
|
2018-07-07 04:09:45 -04:00
|
|
|
expected = "bin/rails server -p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C --restart"
|
2016-03-30 01:11:41 -04:00
|
|
|
|
2018-07-07 04:09:45 -04:00
|
|
|
assert_equal expected, parse_arguments(args)[:restart_cmd]
|
2016-03-30 01:11:41 -04:00
|
|
|
ensure
|
|
|
|
ARGV.replace original_args
|
|
|
|
end
|
2016-11-05 17:52:17 -04:00
|
|
|
|
2018-03-04 18:02:04 -05:00
|
|
|
def test_served_url
|
|
|
|
args = %w(-u webrick -b 127.0.0.1 -p 4567)
|
|
|
|
server = Rails::Server.new(parse_arguments(args))
|
|
|
|
assert_equal "http://127.0.0.1:4567", server.served_url
|
|
|
|
end
|
|
|
|
|
2016-11-05 17:52:17 -04:00
|
|
|
private
|
Introduce explicit rails server handler option
I mistype `rails server production` instead of `rails server -e
production` expecting to lunch a server in the production environment
all the time. However, the signature of `rails server --help` is:
```
Usage:
rails server [puma, thin etc] [options]
```
This means that the `production` argument is being interpreted as a Rack
server handler like Puma, Thin or Unicorn.
Should we argue for the `rails server production`? I'm not sure of the
reasons, but the `rails console production` behavior was deprecated in:
https://github.com/rails/rails/pull/29358, so parity with the existing
`rails console production` usage may not hold anymore.
In any case, this PR introduces an explicit option for the Rack servers
configuration. The option is called `--using` (or `-u` for short) to
avoid the `rails server --server` tantrum.
The new interface of `rails server` is:
```
Usage:
rails server [using] [options]
Options:
-p, [--port=port] # Runs Rails on the specified port - defaults to 3000.
-b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.
-c, [--config=file] # Uses a custom rackup configuration.
# Default: config.ru
-d, [--daemon], [--no-daemon] # Runs server as a Daemon.
-e, [--environment=name] # Specifies the environment to run this server under (development/test/production).
-u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick).
-P, [--pid=PID] # Specifies the PID file.
# Default: tmp/pids/server.pid
-C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development.
[--early-hints], [--no-early-hints] # Enables HTTP/2 early hints.
```
As a bonus, if you mistype the server to use, you'll get an
auto-correction message:
```
$ rails s tin
Could not find handler "tin". Maybe you meant "thin" or "cgi"?
Run `rails server --help` for more options.
```
2018-02-19 13:31:56 -05:00
|
|
|
def run_command(*args)
|
|
|
|
build_app
|
|
|
|
rails "server", *args
|
|
|
|
ensure
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2016-11-05 17:52:17 -04:00
|
|
|
def parse_arguments(args = [])
|
2019-03-03 18:12:05 -05:00
|
|
|
command = Rails::Command::ServerCommand.new([], args)
|
|
|
|
command.send(:extract_environment_option_from_argument)
|
|
|
|
command.server_options
|
2016-11-05 17:52:17 -04:00
|
|
|
end
|
2012-03-20 19:28:30 -04:00
|
|
|
end
|