From 64347b004e7eea0b5f453bdc6e539229fd8bc2ca Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Thu, 17 Dec 2020 15:41:00 -0300 Subject: [PATCH 1/8] Fix usage of inherited Sinatra::Base classes keyword arguments Using keyword arguments on inherited classes from Sinatra::Base breaks with ArgumentError: wrong number of arguments (given X, expected 0). This commit fixes the usage of it on Ruby3 by allowing the method to accept both positional and keyword arguments. --- .travis.yml | 2 ++ lib/sinatra/base.rb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2e4bbe4433..e4e1b9daa5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,7 @@ rvm: - 2.5.8 - 2.6.6 - 2.7.1 + - 3.0.0-preview1 - jruby-9.2.12.0 script: ./.travis.sh @@ -28,6 +29,7 @@ matrix: allow_failures: - rvm: 2.6.6 - rvm: jruby-9.2.12.0 + - rvm: 3.0.0-preview1 notifications: slack: diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index cb1945c5af..437e5e81b4 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1522,8 +1522,8 @@ def prototype # Create a new instance of the class fronted by its middleware # pipeline. The object is guaranteed to respond to #call but may not be # an instance of the class new was called on. - def new(*args, &bk) - instance = new!(*args, &bk) + def new(*args, **kwargs, &bk) + instance = kwargs.length > 0 ? new!(**kwargs, &bk) : new!(*args, &bk) Wrapper.new(build(instance).to_app, instance) end From 06e049c1a548b5300cd562ff9e125f3fe4f93c9d Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Thu, 17 Dec 2020 16:35:52 -0300 Subject: [PATCH 2/8] Include tests --- test/base_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/base_test.rb b/test/base_test.rb index a6730373ec..6f9101fb02 100644 --- a/test/base_test.rb +++ b/test/base_test.rb @@ -6,6 +6,14 @@ class TestApp < Sinatra::Base get('/') { 'Hello World' } end + class TestKeywordArgumentInitializerApp < Sinatra::Base + def initialize(argument:) + @argument = argument + end + + get('/') { "Hello World with Keyword Arguments: #{@argument}" } + end + it 'include Rack::Utils' do assert TestApp.included_modules.include?(Rack::Utils) end @@ -48,6 +56,16 @@ class TestApp < Sinatra::Base TestApp.configure { context = self } assert_equal self, context end + + it "allows constructor to receive keyword arguments" do + app = TestKeywordArgumentInitializerApp.new(argument: "some argument") + request = Rack::MockRequest.new(app) + + response = request.get('/') + + assert response.ok? + assert_equal 'Hello World with Keyword Arguments: some argument', response.body + end end describe "Sinatra::Base#new" do From 561ec5466292272a3c8912e6503eac058f598166 Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Thu, 17 Dec 2020 19:07:35 -0300 Subject: [PATCH 3/8] Import rvm Key on travis pipeline --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index e4e1b9daa5..2552244684 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ language: ruby dist: trusty before_install: + - command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - - gem install bundler - export CXX="g++-4.8" From be5699135550006bbb1cae5bd4e811fe9add9740 Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Thu, 17 Dec 2020 20:51:58 -0300 Subject: [PATCH 4/8] Trying to use ruby-head instead of ruby-3.0.0-preview1 to avoid errors --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2552244684..21775ea1af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ --- language: ruby -dist: trusty +os: linux +sudo: false before_install: - - command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - - gem install bundler - export CXX="g++-4.8" @@ -21,7 +21,7 @@ rvm: - 2.5.8 - 2.6.6 - 2.7.1 - - 3.0.0-preview1 + - ruby-head - jruby-9.2.12.0 script: ./.travis.sh From 3c80f4f5fc681738300d14350e237b98b618f96d Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Thu, 17 Dec 2020 22:11:45 -0300 Subject: [PATCH 5/8] Returning ruby-3.0.0 to test it on xenial linux --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 21775ea1af..5eb458680e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ rvm: - 2.5.8 - 2.6.6 - 2.7.1 - - ruby-head + - 3.0.0-preview1 - jruby-9.2.12.0 script: ./.travis.sh From a5ed7fb882a8f47043abdcf6a320561296b170f8 Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Thu, 17 Dec 2020 23:31:09 -0300 Subject: [PATCH 6/8] Upgrade to Xenial --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5eb458680e..20a8a79fd5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ --- language: ruby -os: linux -sudo: false +dist: xenial before_install: - gem install bundler From b979ca6c881c9eb940bfa2a775fbf97c54884a08 Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Tue, 29 Dec 2020 18:58:11 -0300 Subject: [PATCH 7/8] Apply suggestions from code review --- lib/sinatra/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 437e5e81b4..04367bf1e1 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1523,7 +1523,7 @@ def prototype # pipeline. The object is guaranteed to respond to #call but may not be # an instance of the class new was called on. def new(*args, **kwargs, &bk) - instance = kwargs.length > 0 ? new!(**kwargs, &bk) : new!(*args, &bk) + instance = new!(*args, **kwargs, &bk) Wrapper.new(build(instance).to_app, instance) end From 276065959915cbd64f776560af5cb7c3bb46fa2a Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Wed, 30 Dec 2020 11:34:56 -0300 Subject: [PATCH 8/8] Change initialize method to accept kwargs --- lib/sinatra/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 04367bf1e1..8f0d00c7cd 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -916,7 +916,7 @@ class Base attr_accessor :app, :env, :request, :response, :params attr_reader :template_cache - def initialize(app = nil) + def initialize(app = nil, **kwargs) super() @app = app @template_cache = Tilt::Cache.new