From 1157d63cea6d4b229b02cb34f3330d161030710b Mon Sep 17 00:00:00 2001 From: Simon Neutert Date: Sat, 6 Jan 2024 15:08:51 +0100 Subject: [PATCH] housekeeping --- .github/workflows/ruby.yml | 2 +- .ruby-version | 2 +- Dockerfile | 2 ++ Gemfile | 10 +++--- Gemfile.lock | 56 +++++++++++++++++------------- README.md | 2 +- Rakefile | 14 ++++++++ app.rb | 27 +++++++------- config.ru | 4 +-- lib/{warden.rb => warden_setup.rb} | 6 ++-- test/homepage_test.rb | 16 +++------ test/run_test.rb | 12 ------- test/test_helper.rb | 10 ++++++ 13 files changed, 90 insertions(+), 73 deletions(-) rename lib/{warden.rb => warden_setup.rb} (95%) delete mode 100644 test/run_test.rb create mode 100644 test/test_helper.rb diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 2e03eae..283aadd 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -55,4 +55,4 @@ jobs: run: bundle exec rubocop - name: Run tests - run: bundle exec ruby test/run_test.rb + run: bundle exec rake test diff --git a/.ruby-version b/.ruby-version index 711ee4f..0fa4ae4 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.1.3 \ No newline at end of file +3.3.0 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6e132c0..d144bf8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,6 @@ RUN bundle install COPY . /app +ENV RUBY_YJIT_ENABLE=1 + CMD ["rackup", "-o", "0", "-p", "3000"] \ No newline at end of file diff --git a/Gemfile b/Gemfile index 2b6acb3..afed7cb 100644 --- a/Gemfile +++ b/Gemfile @@ -4,22 +4,24 @@ source 'https://rubygems.org' ruby '3.2.2' +gem 'pg', '~> 1.5' +gem 'puma' + gem 'activerecord' gem 'activesupport', '>= 6.0.3.1' gem 'bcrypt' gem 'coffee-script' gem 'erubi', '~> 1.12' gem 'haml' -gem 'pg', '~> 1.5' +gem 'rack', '~> 2.2' gem 'rake' gem 'sass' gem 'sassc' -gem 'sinatra', '~> 3.1' +gem 'sinatra', '3.2' gem 'sinatra-activerecord' gem 'sinatra-contrib', '~> 3.1' gem 'sinatra-flash' gem 'sprockets' -gem 'thin' gem 'turbolinks' gem 'uglifier' gem 'warden' @@ -30,5 +32,5 @@ group :test, :development do gem 'rack-test', '~> 2.1' gem 'rubocop', require: false gem 'solargraph', require: false - gem 'test-unit', '~> 3.6' + gem 'test-unit', '~> 3.6', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index f5a9366..e565bc3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -22,7 +22,7 @@ GEM base64 (0.2.0) bcrypt (3.1.20) benchmark (0.3.0) - bigdecimal (3.1.4) + bigdecimal (3.1.5) coderay (1.1.3) coffee-script (2.4.1) coffee-script-source @@ -30,13 +30,11 @@ GEM coffee-script-source (1.12.2) concurrent-ruby (1.2.2) connection_pool (2.4.1) - daemons (1.4.1) diff-lcs (1.5.0) drb (2.2.0) ruby2_keywords e2mmap (0.1.0) erubi (1.12.0) - eventmachine (1.2.7) execjs (2.9.1) ffi (1.16.3) haml (6.3.0) @@ -53,21 +51,26 @@ GEM kramdown (~> 2.0) language_server-protocol (3.17.0.3) method_source (1.0.0) - mini_portile2 (2.8.5) minitest (5.20.0) multi_json (1.15.0) mustermann (3.0.0) ruby2_keywords (~> 0.0.1) mutex_m (0.2.0) - nokogiri (1.15.5) - mini_portile2 (~> 2.8.2) + nio4r (2.7.0) + nokogiri (1.16.0-aarch64-linux) racc (~> 1.4) - nokogiri (1.15.5-arm64-darwin) + nokogiri (1.16.0-arm-linux) racc (~> 1.4) - nokogiri (1.15.5-x86_64-linux) + nokogiri (1.16.0-arm64-darwin) + racc (~> 1.4) + nokogiri (1.16.0-x86-linux) + racc (~> 1.4) + nokogiri (1.16.0-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.16.0-x86_64-linux) racc (~> 1.4) parallel (1.24.0) - parser (3.2.2.4) + parser (3.3.0.0) ast (~> 2.4.1) racc pg (1.5.4) @@ -75,9 +78,12 @@ GEM pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) + puma (6.4.1) + nio4r (~> 2.0) racc (1.7.3) rack (2.2.8) - rack-protection (3.1.0) + rack-protection (3.2.0) + base64 (>= 0.1.0) rack (~> 2.2, >= 2.2.4) rack-test (2.1.0) rack (>= 1.3) @@ -113,19 +119,19 @@ GEM rb-inotify (~> 0.9, >= 0.9.7) sassc (2.4.0) ffi (~> 1.9) - sinatra (3.1.0) + sinatra (3.2.0) mustermann (~> 3.0) rack (~> 2.2, >= 2.2.4) - rack-protection (= 3.1.0) + rack-protection (= 3.2.0) tilt (~> 2.0) sinatra-activerecord (2.0.27) activerecord (>= 4.1) sinatra (>= 1.0) - sinatra-contrib (3.1.0) - multi_json + sinatra-contrib (3.2.0) + multi_json (>= 0.0.2) mustermann (~> 3.0) - rack-protection (= 3.1.0) - sinatra (= 3.1.0) + rack-protection (= 3.2.0) + sinatra (= 3.2.0) tilt (~> 2.0) sinatra-flash (0.3.0) sinatra (>= 1.0.0) @@ -151,10 +157,6 @@ GEM temple (0.10.3) test-unit (3.6.1) power_assert - thin (1.8.2) - daemons (~> 1.0, >= 1.0.9) - eventmachine (~> 1.0, >= 1.0.4) - rack (>= 1, < 3) thor (1.3.0) tilt (2.3.0) timeout (0.4.1) @@ -172,8 +174,11 @@ GEM yui-compressor (0.12.0) PLATFORMS - arm64-darwin-21 - ruby + aarch64-linux + arm-linux + arm64-darwin + x86-linux + x86_64-darwin x86_64-linux DEPENDENCIES @@ -185,19 +190,20 @@ DEPENDENCIES haml pg (~> 1.5) pry + puma + rack (~> 2.2) rack-test (~> 2.1) rake rubocop sass sassc - sinatra (~> 3.1) + sinatra (= 3.2) sinatra-activerecord sinatra-contrib (~> 3.1) sinatra-flash solargraph sprockets test-unit (~> 3.6) - thin turbolinks uglifier warden @@ -207,4 +213,4 @@ RUBY VERSION ruby 3.2.2p53 BUNDLED WITH - 2.4.20 + 2.5.4 diff --git a/README.md b/README.md index 28f258a..d29bb45 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ You need **Ruby (>= 3.2)** and **Bundler** (of course). * `$ bundle install` * edit __config/database.yml__ * `$ rake db:setup` -* `$ thin start` or `rerun rackup` ([rerun](https://github.com/alexch/rerun) gem - _not included by default!_) +* `$ bundle exec rackup` or `rerun rackup` ([rerun](https://github.com/alexch/rerun) gem - _not included by default!_) * edit titles in __views/layout.erb__ and __views/nav.erb__ * check/set timezone __config/timezone.rb__ diff --git a/Rakefile b/Rakefile index 0ac7a3f..76b311d 100644 --- a/Rakefile +++ b/Rakefile @@ -3,12 +3,26 @@ # read more about what rake does # https://github.com/ruby/rake +require 'rake/testtask' +require 'sinatra' +require 'sinatra/base' require 'sinatra/activerecord' require 'sinatra/activerecord/rake' + +require 'pry' unless ENV['APP_ENV'] == 'production' + require_relative 'app' +Rake::TestTask.new(:test) do |t| + t.libs << 'test' + t.libs << 'lib' + t.test_files = FileList['test/**/*_test.rb'] +end + desc 'This a sample rake task' # $ rake cron_rake_task task :cron_rake_task do puts 'Run!' end + +task default: :test diff --git a/app.rb b/app.rb index bd10975..6f4e16c 100644 --- a/app.rb +++ b/app.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -# require gems and libs +require 'bundler/setup' + require 'sinatra' require 'sinatra/base' require 'sinatra/flash' @@ -11,23 +12,12 @@ require 'coffee-script' require 'execjs' require 'sinatra/activerecord' -require 'warden' require 'bcrypt' require 'rack/protection' +require 'warden' require 'pry' unless ENV['APP_ENV'] == 'production' -# require libs -Dir['./lib/*.rb'].each { |file| require_relative file } -# require configurations -Dir['./config/*.rb'].each { |file| require_relative file } -# require models -Dir['./models/*.rb'].each { |file| require_relative file } -# require controllers -Dir['./controllers/*.rb'].each { |file| require_relative file } -# require routes -Dir['./routes/*.rb'].each { |file| require_relative file } - # modular Sinatra app inherit from Sinatra::Base class MyApp < Sinatra::Base # Boolean specifying whether the HTTP POST _method parameter @@ -60,3 +50,14 @@ class MyApp < Sinatra::Base # always add protection last! use Rack::Protection end + +# require libs +Dir['./lib/*.rb'].each { |file| require_relative file } +# require configurations +Dir['./config/*.rb'].each { |file| require_relative file } +# require models +Dir['./models/*.rb'].each { |file| require_relative file } +# require controllers +Dir['./controllers/*.rb'].each { |file| require_relative file } +# require routes +Dir['./routes/*.rb'].each { |file| require_relative file } diff --git a/config.ru b/config.ru index 8427911..3a76376 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,4 @@ # this file kicks up the app! -# run: "thin start" -require "#{File.dirname(__FILE__)}/app" +require_relative 'app' + run MyApp diff --git a/lib/warden.rb b/lib/warden_setup.rb similarity index 95% rename from lib/warden.rb rename to lib/warden_setup.rb index 98ee31a..39535df 100644 --- a/lib/warden.rb +++ b/lib/warden_setup.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class MyApp < Sinatra::Base - use Warden::Manager do |config| + use ::Warden::Manager do |config| # Tell Warden how to save our User info into a session. # Sessions can only take strings, not Ruby code, we'll store # the User's `id` @@ -23,7 +23,7 @@ class MyApp < Sinatra::Base config.failure_app = self end - Warden::Manager.before_failure do |env, _opts| + ::Warden::Manager.before_failure do |env, _opts| # Because authentication failure can happen on any request but # we handle it only under "post '/auth/unauthenticated'", we need # to change request to POST @@ -35,7 +35,7 @@ class MyApp < Sinatra::Base env[key]['_method'] = String.new('post') if key == 'rack.request.form_hash' end end - Warden::Strategies.add(:password) do + ::Warden::Strategies.add(:password) do def valid? params['user'] && params['user']['username'] && params['user']['password'] end diff --git a/test/homepage_test.rb b/test/homepage_test.rb index dcb0f9e..3331591 100644 --- a/test/homepage_test.rb +++ b/test/homepage_test.rb @@ -1,24 +1,18 @@ # frozen_string_literal: true -require 'test/unit' -require 'rack/test' -require 'json' +require 'test_helper' + +OUTER_APP = Rack::Builder.parse_file('config.ru').first class HomepageTest < Test::Unit::TestCase include Rack::Test::Methods def app - ->(_env) { [200, { 'content-type' => 'text/plain' }, ['All responses are OK']] } + OUTER_APP end - def test_response_is_ok - # Optionally set headers used for all requests in this spec: - # header 'accept-charset', 'utf-8' - - # First argument is treated as the path + def test_root get '/' - assert last_response.ok? - assert_equal 'All responses are OK', last_response.body end end diff --git a/test/run_test.rb b/test/run_test.rb deleted file mode 100644 index 6d46a02..0000000 --- a/test/run_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -base_dir = File.expand_path(File.join(File.dirname(__FILE__), '..')) -lib_dir = File.join(base_dir, 'lib') -test_dir = File.join(base_dir, 'test') - -$LOAD_PATH.unshift(lib_dir) - -require 'test/unit' -require 'rack/test' - -exit Test::Unit::AutoRunner.run(true, test_dir) diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..ea6ba02 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'test/unit' +require 'rack/test' +require 'active_support' + +require 'rack/builder' +require 'json' + +$LOAD_PATH.unshift File.expand_path('../lib', __dir__)