Skip to content

Commit

Permalink
Merge pull request sinatra#1033 from sinatra/2.2.0-alpha
Browse files Browse the repository at this point in the history
Sinatra 2.0.0 alpha [redux]
  • Loading branch information
Zachary Scott committed Jan 24, 2016
2 parents bab0355 + a0acf42 commit 2af9910
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 153 deletions.
21 changes: 5 additions & 16 deletions .travis.yml
Expand Up @@ -2,29 +2,18 @@
language: ruby

rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1
- 2.2
- 2.2.2
- 2.2.3
- 2.3.0
- ruby-head
- rbx-2
- jruby
- jruby-9.0.0.0
- jruby-head
- ruby-head

sudo: false

matrix:
include:
- { rvm: 1.8.7, env: tilt=master }
- { rvm: 1.9.3, env: tilt=master }
- { rvm: 2.2, env: rack=master }
- { rvm: 2.2, env: tilt=master }
allow_failures:
- env: rack=master
- env: tilt=master
- rvm: rbx-2
- rvm: ruby-head
- rvm: jruby-head
- rvm: rbx-2
23 changes: 7 additions & 16 deletions Gemfile
Expand Up @@ -11,29 +11,17 @@ source 'https://rubygems.org' unless ENV['QUICK']
gemspec

gem 'rake'
gem 'rack', github: 'rack/rack'
gem 'rack-test', '>= 0.6.2'
gem "minitest", "~> 5.0"

# Allows stuff like `tilt=1.2.2 bundle install` or `tilt=master ...`.
# Used by the CI.
repos = {'tilt' => "rtomayko/tilt", 'rack' => "rack/rack"}

%w[tilt rack].each do |lib|
dep = case ENV[lib]
when 'stable', nil then nil
when /(\d+\.)+\d+/ then "~> " + ENV[lib].sub("#{lib}-", '')
else {:github => repos[lib], :branch => dep}
end
gem lib, dep
end

if RUBY_ENGINE == 'jruby'
gem 'nokogiri', '!= 1.5.0'
gem 'jruby-openssl'
gem 'trinidad'
end

if RUBY_ENGINE == "ruby" and RUBY_VERSION > '1.9.2'
if RUBY_ENGINE == "ruby"
gem 'less', '~> 2.0'
gem 'therubyracer'
gem 'redcarpet'
Expand All @@ -42,6 +30,9 @@ if RUBY_ENGINE == "ruby" and RUBY_VERSION > '1.9.2'
gem 'rdiscount'
gem 'RedCloth'
gem 'puma'
#TODO: remove explicit require once net-http-server does it
#(apparently it was shipped w/ stdlib in Rubies < 2.2.2)
gem 'gserver'
gem 'net-http-server'
gem 'yajl-ruby'
gem 'nokogiri'
Expand Down Expand Up @@ -71,6 +62,6 @@ if RUBY_ENGINE == "rbx"
gem 'rubysl-test-unit'
end

platforms :ruby_18, :jruby do
gem 'json' unless RUBY_VERSION > '1.9' # is there a jruby but 1.8 only selector?
platforms :jruby do
gem 'json'
end
33 changes: 7 additions & 26 deletions README.md
Expand Up @@ -2871,31 +2871,10 @@ thin --threaded start

The following Ruby versions are officially supported:
<dl>
<dt>Ruby 1.8.7</dt>
<dt>Ruby 2.2</dt>
<dd>
1.8.7 is fully supported, however, if nothing is keeping you from it, we
recommend upgrading or switching to JRuby or Rubinius. Support for 1.8.7
will not be dropped before Sinatra 2.0. Ruby 1.8.6 is no longer supported.
</dd>

<dt>Ruby 1.9.2</dt>
<dd>
1.9.2 is fully supported. Do not use 1.9.2p0, as it is known to cause
segmentation faults when running Sinatra. Official support will continue
at least until the release of Sinatra 1.5.
</dd>

<dt>Ruby 1.9.3</dt>
<dd>
1.9.3 is fully supported and recommended. Please note that switching to 1.9.3
from an earlier version will invalidate all sessions. 1.9.3 will be supported
until the release of Sinatra 2.0.
</dd>

<dt>Ruby 2.x</dt>
<dd>
2.x is fully supported and recommended. There are currently no plans to drop
official support for it.
2.2 is fully supported and recommended. There are currently no plans to
drop official support for it.
</dd>

<dt>Rubinius</dt>
Expand All @@ -2912,6 +2891,8 @@ The following Ruby versions are officially supported:
</dd>
</dl>

Versions of Ruby prior to 2.2.2 are no longer supported as of Sinatra 2.0.

We also keep an eye on upcoming Ruby versions.

The following Ruby implementations are not officially supported but still are
Expand All @@ -2935,7 +2916,7 @@ implementation.
If you run MacRuby, you should `gem install control_tower`.

Sinatra currently doesn't run on Cardinal, SmallRuby, BlueRuby or any
Ruby version prior to 1.8.7.
Ruby version prior to 2.2.

## The Bleeding Edge

Expand Down Expand Up @@ -2965,7 +2946,7 @@ Then, in your project directory, create a `Gemfile`:

```ruby
source 'https://rubygems.org'
gem 'sinatra', :github => "sinatra/sinatra"
gem 'sinatra', :github => 'sinatra/sinatra'

# other dependencies
gem 'haml' # for instance, if you use haml
Expand Down
42 changes: 18 additions & 24 deletions lib/sinatra/base.rb
Expand Up @@ -150,7 +150,7 @@ def finish
if calculate_content_length?
# if some other code has already set Content-Length, don't muck with it
# currently, this would be the static file-handler
headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
headers["Content-Length"] = body.inject(0) { |l, p| l + p.bytesize }.to_s
end

[status.to_i, headers, result]
Expand Down Expand Up @@ -239,7 +239,11 @@ def body(value = nil, &block)
def block.each; yield(call) end
response.body = block
elsif value
headers.delete 'Content-Length' unless request.head? || value.is_a?(Rack::File) || value.is_a?(Stream)
# Rack 2.0 returns a Rack::File::Iterator here instead of
# Rack::File as it was in the previous API.
unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream)
headers.delete 'Content-Length'
end
response.body = value
else
response.body
Expand Down Expand Up @@ -362,13 +366,13 @@ def send_file(path, opts = {})

last_modified opts[:last_modified] if opts[:last_modified]

file = Rack::File.new nil
file.path = path
result = file.serving env
file = Rack::File.new(File.dirname(settings.app_file))
result = file.serving(request, path)

result[1].each { |k,v| headers[k] ||= v }
headers['Content-Length'] = result[1]['Content-Length']
opts[:status] &&= Integer(opts[:status])
halt opts[:status] || result[0], result[2]
halt (opts[:status] || result[0]), result[2]
rescue Errno::ENOENT
not_found
end
Expand Down Expand Up @@ -592,22 +596,12 @@ def not_found?
# Generates a Time object from the given value.
# Used by #expires and #last_modified.
def time_for(value)
if value.respond_to? :to_time
value.to_time
elsif value.is_a? Time
value
elsif value.respond_to? :new_offset
# DateTime#to_time does the same on 1.9
d = value.new_offset 0
t = Time.utc d.year, d.mon, d.mday, d.hour, d.min, d.sec + d.sec_fraction
t.getlocal
elsif value.respond_to? :mday
# Date#to_time does the same on 1.9
Time.local(value.year, value.mon, value.mday)
elsif value.is_a? Numeric
if value.is_a? Numeric
Time.at value
else
elsif value.respond_to? :to_s
Time.parse value.to_s
else
value.to_time
end
rescue ArgumentError => boom
raise boom
Expand Down Expand Up @@ -877,7 +871,7 @@ class Base
include Helpers
include Templates

URI_INSTANCE = URI.const_defined?(:Parser) ? URI::Parser.new : URI
URI_INSTANCE = URI::Parser.new

attr_accessor :app, :env, :request, :response, :params
attr_reader :template_cache
Expand Down Expand Up @@ -1064,6 +1058,7 @@ def indifferent_hash
# Run the block with 'throw :halt' support and apply result to the response.
def invoke
res = catch(:halt) { yield }

res = [res] if Fixnum === res or String === res
if Array === res and Fixnum === res.first
res = res.dup
Expand Down Expand Up @@ -1533,8 +1528,7 @@ def setup_traps

# Dynamically defines a method on settings.
def define_singleton(name, content = Proc.new)
# replace with call to singleton_class once we're 1.9 only
(class << self; self; end).class_eval do
singleton_class.class_eval do
undef_method(name) if method_defined? name
String === content ? class_eval("def #{name}() #{content}; end") : define_method(name, &content)
end
Expand Down Expand Up @@ -1933,7 +1927,7 @@ class #{self.class}
</style>
</head>
<body>
<h2>Sinatra doesn&rsquo;t know this ditty.</h2>
<h2>Sinatra doesnt know this ditty.</h2>
<img src='#{uri "/__sinatra__/404.png"}'>
<div id="c">
Try this:
Expand Down
8 changes: 2 additions & 6 deletions lib/sinatra/show_exceptions.rb
@@ -1,8 +1,4 @@
begin
require 'rack/show_exceptions'
rescue LoadError
require 'rack/showexceptions'
end
require 'rack/show_exceptions'

module Sinatra
# Sinatra::ShowExceptions catches all exceptions raised from the app it
Expand Down Expand Up @@ -44,7 +40,7 @@ def call(env)
500,
{
"Content-Type" => content_type,
"Content-Length" => Rack::Utils.bytesize(body.join).to_s
"Content-Length" => body.join.bytesize.to_s
},
body
]
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/version.rb
@@ -1,3 +1,3 @@
module Sinatra
VERSION = '1.4.6'
VERSION = '2.0.0-alpha'
end
8 changes: 5 additions & 3 deletions sinatra.gemspec
Expand Up @@ -13,7 +13,9 @@ Gem::Specification.new 'sinatra', Sinatra::VERSION do |s|
s.extra_rdoc_files = s.files.select { |p| p =~ /^README/ } << 'LICENSE'
s.rdoc_options = %w[--line-numbers --inline-source --title Sinatra --main README.rdoc --encoding=UTF-8]

s.add_dependency 'rack', '~> 1.5', '>= 1.5.4', '< 1.6'
s.add_dependency 'tilt', '>= 1.3', '< 3'
s.add_dependency 'rack-protection', '~> 1.4'
s.required_ruby_version = '>= 2.2.0'

s.add_dependency 'rack', '~> 2.0.0.alpha'
s.add_dependency 'tilt', '~> 2.0'
s.add_dependency 'rack-protection', '~> 1.5'
end
9 changes: 3 additions & 6 deletions test/helper.rb
Expand Up @@ -3,12 +3,9 @@

RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE

begin
require 'rack'
rescue LoadError
require 'rubygems'
require 'rack'
end
require 'bundler'
require 'bundler/setup'
require 'rack'

testdir = File.dirname(__FILE__)
$LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
Expand Down
71 changes: 17 additions & 54 deletions test/routing_test.rb
Expand Up @@ -275,8 +275,7 @@ class RoutingTest < Minitest::Test
assert_equal "foo=;bar=", body
end

it "supports named captures like %r{/hello/(?<person>[^/?#]+)} on Ruby >= 1.9" do
next if RUBY_VERSION < '1.9'
it "supports named captures like %r{/hello/(?<person>[^/?#]+)}" do
mock_app {
get Regexp.new('/hello/(?<person>[^/?#]+)') do
"Hello #{params['person']}"
Expand All @@ -286,8 +285,7 @@ class RoutingTest < Minitest::Test
assert_equal 'Hello Frank', body
end

it "supports optional named captures like %r{/page(?<format>.[^/?#]+)?} on Ruby >= 1.9" do
next if RUBY_VERSION < '1.9'
it "supports optional named captures like %r{/page(?<format>.[^/?#]+)?}" do
mock_app {
get Regexp.new('/page(?<format>.[^/?#]+)?') do
"format=#{params[:format]}"
Expand Down Expand Up @@ -1299,59 +1297,24 @@ def authorize(username, password)
assert_equal "hey", body
end

# NOTE Block params behaves differently under 1.8 and 1.9. Under 1.8, block
# param arity is lax: declaring a mismatched number of block params results
# in a warning. Under 1.9, block param arity is strict: mismatched block
# arity raises an ArgumentError.

if RUBY_VERSION >= '1.9'

it 'raises an ArgumentError with block param arity 1 and no values' do
mock_app {
get '/foo' do |foo|
'quux'
end
}

assert_raises(ArgumentError) { get '/foo' }
end

it 'raises an ArgumentError with block param arity 1 and too many values' do
mock_app {
get '/:foo/:bar/:baz' do |foo|
'quux'
end
}

assert_raises(ArgumentError) { get '/a/b/c' }
end

else

it 'does not raise an ArgumentError with block param arity 1 and no values' do
mock_app {
get '/foo' do |foo|
'quux'
end
}

silence_warnings { get '/foo' }
assert ok?
assert_equal 'quux', body
end
it 'raises an ArgumentError with block param arity 1 and no values' do
mock_app {
get '/foo' do |foo|
'quux'
end
}

it 'does not raise an ArgumentError with block param arity 1 and too many values' do
mock_app {
get '/:foo/:bar/:baz' do |foo|
'quux'
end
}
assert_raises(ArgumentError) { get '/foo' }
end

silence_warnings { get '/a/b/c' }
assert ok?
assert_equal 'quux', body
end
it 'raises an ArgumentError with block param arity 1 and too many values' do
mock_app {
get '/:foo/:bar/:baz' do |foo|
'quux'
end
}

assert_raises(ArgumentError) { get '/a/b/c' }
end

it "matches routes defined in superclasses" do
Expand Down

0 comments on commit 2af9910

Please sign in to comment.