Add quiet flag to server configuration (false by default)

It suppresses start and stop messages that Sinatra generates.
As requested here: https://github.com/sinatra/sinatra/issues/1148.
This commit is contained in:
Vasiliy Yaklushin 2016-07-25 19:05:25 +02:00
parent bd1a9dc8c9
commit d8a6930b1e
3 changed files with 22 additions and 2 deletions

View File

@ -1451,7 +1451,7 @@ module Sinatra
return unless running?
# Use Thin's hard #stop! if available, otherwise just #stop.
running_server.respond_to?(:stop!) ? running_server.stop! : running_server.stop
$stderr.puts "== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
$stderr.puts "== Sinatra has ended his set (crowd applauds)" unless supress_messages?
set :running_server, nil
set :handler_name, nil
end
@ -1533,7 +1533,7 @@ module Sinatra
# Starts the server by running the Rack Handler.
def start_server(handler, server_settings, handler_name)
handler.run(self, server_settings) do |server|
unless handler_name =~ /cgi/i
unless supress_messages?
$stderr.puts "== Sinatra (v#{Sinatra::VERSION}) has taken the stage on #{port} for #{environment} with backup from #{handler_name}"
end
@ -1546,6 +1546,10 @@ module Sinatra
end
end
def supress_messages?
handler_name =~ /cgi/i || quiet
end
def setup_traps
if traps?
at_exit { quit! }
@ -1801,6 +1805,7 @@ module Sinatra
set :server, %w[HTTP webrick]
set :bind, Proc.new { development? ? 'localhost' : '0.0.0.0' }
set :port, Integer(ENV['PORT'] && !ENV['PORT'].empty? ? ENV['PORT'] : 4567)
set :quiet, false
ruby_engine = defined?(RUBY_ENGINE) && RUBY_ENGINE

View File

@ -17,6 +17,7 @@ module Sinatra
op.on('-o addr', "set the host (default is #{bind})") { |val| set :bind, val }
op.on('-e env', 'set the environment (default is development)') { |val| set :environment, val.to_sym }
op.on('-s server', 'specify rack server/handler (default is thin)') { |val| set :server, val }
op.on('-q', 'turn on quiet mode (default is off)') { set :quiet, true }
op.on('-x', 'turn on the mutex lock (default is off)') { set :lock, true }
}.parse!(ARGV.dup)
end

View File

@ -53,4 +53,18 @@ class ServerTest < Minitest::Test
it "falls back on the next server handler when not found" do
@app.run! :server => %w[foo bar mock]
end
describe "Quiet mode" do
it "sends data to stderr when server starts and stops" do
@app.run!
assert_match(/\=\= Sinatra/, $stderr.string)
end
context "when quiet mode is activated" do
it "does not generate Sinatra start and stop messages" do
@app.run! quiet: true
refute_match(/\=\= Sinatra/, $stderr.string)
end
end
end
end