Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quiet flag to server configuration (false by default) #1153

Merged
merged 2 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add quiet flag to server configuration (false by default)
It suppresses start and stop messages that Sinatra generates.
As requested here: #1148.
  • Loading branch information
304 committed Jul 26, 2016
commit d8a6930b1e94f294a514c6ffcf67fed49745bbfa
9 changes: 7 additions & 2 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ def quit!
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
Expand Down Expand Up @@ -1533,7 +1533,7 @@ def caller_locations
# 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

Expand All @@ -1546,6 +1546,10 @@ def start_server(handler, server_settings, handler_name)
end
end

def supress_messages?
handler_name =~ /cgi/i || quiet
end

def setup_traps
if traps?
at_exit { quit! }
Expand Down Expand Up @@ -1801,6 +1805,7 @@ class << self
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

Expand Down
1 change: 1 addition & 0 deletions lib/sinatra/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Application < Base
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
Expand Down
14 changes: 14 additions & 0 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@ def teardown
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