From f6279c4bb1936f6e04d87925c3d82c99f3020fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Peignier?= Date: Thu, 24 Nov 2016 15:49:30 -0800 Subject: [PATCH 1/2] Add and apply rubocop to template --- lib/template/.rubocop.yml | 22 +++++++++++++++++++ lib/template/Gemfile | 16 ++++++++------ lib/template/Rakefile | 7 +++--- lib/template/bin/console | 15 +++++++------ lib/template/bin/run | 4 +++- lib/template/config.ru | 1 + lib/template/config/config.rb | 5 +++-- lib/template/config/initializers/database.rb | 7 +++--- lib/template/config/initializers/log.rb | 1 + lib/template/config/initializers/rollbar.rb | 6 ++--- lib/template/config/puma.rb | 5 +++-- lib/template/db/seeds.rb | 1 + lib/template/lib/application.rb | 1 + lib/template/lib/endpoints/base.rb | 1 + lib/template/lib/endpoints/health.rb | 5 +++-- lib/template/lib/endpoints/root.rb | 5 +++-- lib/template/lib/endpoints/schema.rb | 13 ++++++----- lib/template/lib/initializer.rb | 3 ++- lib/template/lib/mediators/base.rb | 3 ++- lib/template/lib/routes.rb | 15 ++++++++----- lib/template/lib/serializers/base.rb | 6 ++--- lib/template/lib/tasks/rubocop.rake | 6 +++++ lib/template/lib/tasks/spec.rake | 12 +++++----- lib/template/spec/endpoints/health_spec.rb | 3 ++- lib/template/spec/endpoints/schema_spec.rb | 19 ++++++++-------- lib/template/spec/spec_helper.rb | 13 ++++++----- .../spec/spec_support/auto_define_rack_app.rb | 1 + lib/template/spec/spec_support/coverage.rb | 9 ++++---- lib/template/spec/spec_support/log.rb | 11 ++++++---- pliny.gemspec | 7 +++--- spec/integration_spec.rb | 20 ++++++++--------- 31 files changed, 152 insertions(+), 91 deletions(-) create mode 100644 lib/template/.rubocop.yml create mode 100644 lib/template/lib/tasks/rubocop.rake diff --git a/lib/template/.rubocop.yml b/lib/template/.rubocop.yml new file mode 100644 index 00000000..89f30fcb --- /dev/null +++ b/lib/template/.rubocop.yml @@ -0,0 +1,22 @@ +require: + - rubocop-rspec +AllCops: + TargetRubyVersion: 2.3 +# Offense count: 8 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives. +# URISchemes: http, https +Metrics/LineLength: + Enabled: false + +# Offense count: 1 +# Configuration parameters: Max. +RSpec/ExampleLength: + Enabled: false + +# Offense count: 1 +Style/ClassVars: + Enabled: false + +# Offense count: 6 +Style/Documentation: + Enabled: false diff --git a/lib/template/Gemfile b/lib/template/Gemfile index 81ebe4e8..f57f369b 100644 --- a/lib/template/Gemfile +++ b/lib/template/Gemfile @@ -20,14 +20,16 @@ gem "sinatra-router" gem "sucker_punch" group :development, :test do - gem "pry-byebug" + gem 'pry-byebug' + gem 'rubocop', '~> 0.45.0', require: false + gem 'rubocop-rspec', require: false end group :test do - gem "simplecov", require: false - gem "committee" - gem "database_cleaner" - gem "dotenv" - gem "rack-test" - gem "rspec" + gem 'simplecov', require: false + gem 'committee' + gem 'database_cleaner' + gem 'dotenv' + gem 'rack-test' + gem 'rspec' end diff --git a/lib/template/Rakefile b/lib/template/Rakefile index 72ba6b0c..c714f429 100644 --- a/lib/template/Rakefile +++ b/lib/template/Rakefile @@ -1,6 +1,7 @@ -require "pliny/tasks" +# frozen_string_literal: true +require 'pliny/tasks' # Add your rake tasks to lib/tasks! -Dir["./lib/tasks/*.rake"].each { |task| load task } +Dir['./lib/tasks/*.rake'].each { |task| load task } -task :default => :spec +task default: :spec diff --git a/lib/template/bin/console b/lib/template/bin/console index 999955a6..0505d3d1 100755 --- a/lib/template/bin/console +++ b/lib/template/bin/console @@ -1,21 +1,22 @@ #!/usr/bin/env ruby +# frozen_string_literal: true require_relative '../lib/application' def basic_prompt(target_self, nest_level, pry) # override DEPLOYMENT to identify console sessions (eg: staging/production/etc) - nesting = nest_level.zero? ? "" : ":#{nest_level}" + nesting = nest_level.zero? ? '' : ":#{nest_level}" "[#{pry.input_array.size}] #{Config.deployment}(#{Pry.view_clip(target_self)})#{nesting}" end Pry.prompt = [ - proc { |target_self, nest_level, pry| - basic_prompt(target_self, nest_level, pry) + "> " - }, + proc do |target_self, nest_level, pry| + basic_prompt(target_self, nest_level, pry) + '> ' + end, - proc { |target_self, nest_level, pry| - basic_prompt(target_self, nest_level, pry) + "* " - } + proc do |target_self, nest_level, pry| + basic_prompt(target_self, nest_level, pry) + '* ' + end ] Pry.start diff --git a/lib/template/bin/run b/lib/template/bin/run index cc0c7dca..6582936f 100755 --- a/lib/template/bin/run +++ b/lib/template/bin/run @@ -1,5 +1,7 @@ #!/usr/bin/env ruby +# frozen_string_literal: true +# rubocop:disable Lint/Eval require_relative '../lib/application' -eval ARGV.join(" ") +eval ARGV.join(' ') diff --git a/lib/template/config.ru b/lib/template/config.ru index d3e8bc31..905a1111 100644 --- a/lib/template/config.ru +++ b/lib/template/config.ru @@ -1,3 +1,4 @@ +# frozen_string_literal: true require_relative 'lib/application' $stdout.sync = true diff --git a/lib/template/config/config.rb b/lib/template/config/config.rb index c2d75d18..d67215db 100644 --- a/lib/template/config/config.rb +++ b/lib/template/config/config.rb @@ -1,4 +1,5 @@ -require "pliny/config_helpers" +# frozen_string_literal: true +require 'pliny/config_helpers' # Access all config keys like the following: # @@ -29,7 +30,7 @@ module Config override :puma_min_threads, 1, int override :puma_workers, 3, int override :raise_errors, false, bool - override :root, File.expand_path("../../", __FILE__), string + override :root, File.expand_path('../../', __FILE__), string override :timeout, 10, int override :versioning, false, bool end diff --git a/lib/template/config/initializers/database.rb b/lib/template/config/initializers/database.rb index 56d80523..91affd28 100644 --- a/lib/template/config/initializers/database.rb +++ b/lib/template/config/initializers/database.rb @@ -1,10 +1,11 @@ +# frozen_string_literal: true database_setup_proc = lambda do |conn| # identify postgres connections coming from this process in pg_stat_activity - process_identifier = ENV["DYNO"] || File.basename($0).gsub(/\W+/, "_") + process_identifier = ENV['DYNO'] || File.basename($PROGRAM_NAME).gsub(/\W+/, '_') conn.execute "SET statement_timeout = '#{Config.database_timeout}s'" conn.execute "SET application_name = '#{process_identifier}'" end DB = Sequel.connect(Config.database_url, - max_connections: Config.db_pool, - after_connect: database_setup_proc) + max_connections: Config.db_pool, + after_connect: database_setup_proc) diff --git a/lib/template/config/initializers/log.rb b/lib/template/config/initializers/log.rb index 2f3bb2ec..af252822 100644 --- a/lib/template/config/initializers/log.rb +++ b/lib/template/config/initializers/log.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true Pliny.default_context = {} Pliny.default_context[:app] = Config.app_name if Config.app_name Pliny.default_context[:deployment] = Config.deployment diff --git a/lib/template/config/initializers/rollbar.rb b/lib/template/config/initializers/rollbar.rb index 760f591c..fd6b7cc0 100644 --- a/lib/template/config/initializers/rollbar.rb +++ b/lib/template/config/initializers/rollbar.rb @@ -1,13 +1,13 @@ +# frozen_string_literal: true require 'pliny/error_reporters/rollbar' Pliny::ErrorReporters.error_reporters << Pliny::ErrorReporters::Rollbar Rollbar.configure do |config| - config.enabled = ENV.has_key?('ROLLBAR_ACCESS_TOKEN') + config.enabled = ENV.key?('ROLLBAR_ACCESS_TOKEN') config.disable_rack_monkey_patch = true - config.access_token = ENV["ROLLBAR_ACCESS_TOKEN"] + config.access_token = ENV['ROLLBAR_ACCESS_TOKEN'] config.environment = ENV['ROLLBAR_ENV'] config.logger = Pliny::RollbarLogger.new config.use_sucker_punch end - diff --git a/lib/template/config/puma.rb b/lib/template/config/puma.rb index e4647b75..cf60471f 100644 --- a/lib/template/config/puma.rb +++ b/lib/template/config/puma.rb @@ -1,4 +1,5 @@ -require "./config/config" +# frozen_string_literal: true +require './config/config' environment Config.rack_env port Config.port @@ -8,7 +9,7 @@ on_worker_boot do # force Sequel's thread pool to be refreshed - Sequel::DATABASES.each { |db| db.disconnect } + Sequel::DATABASES.each(&:disconnect) end preload_app! diff --git a/lib/template/db/seeds.rb b/lib/template/db/seeds.rb index 0f14889d..b6bd28d0 100644 --- a/lib/template/db/seeds.rb +++ b/lib/template/db/seeds.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # This file should contain all the record creation needed to seed the database # with its default values. # The data can then be loaded with the rake db:seed (or created alongside the diff --git a/lib/template/lib/application.rb b/lib/template/lib/application.rb index 6d9695d7..c47b528f 100644 --- a/lib/template/lib/application.rb +++ b/lib/template/lib/application.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'bundler' Bundler.require diff --git a/lib/template/lib/endpoints/base.rb b/lib/template/lib/endpoints/base.rb index 12b48546..230248ec 100644 --- a/lib/template/lib/endpoints/base.rb +++ b/lib/template/lib/endpoints/base.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Endpoints # The base class for all Sinatra-based endpoints. Use sparingly. class Base < Sinatra::Base diff --git a/lib/template/lib/endpoints/health.rb b/lib/template/lib/endpoints/health.rb index c1c64d8d..f4250276 100644 --- a/lib/template/lib/endpoints/health.rb +++ b/lib/template/lib/endpoints/health.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Endpoints class Health < Base namespace '/health' do @@ -14,11 +15,11 @@ class Health < Base private def database? - fail Pliny::Errors::NotFound if DB.nil? + raise Pliny::Errors::NotFound if DB.nil? end def database_available? - fail Pliny::Errors::ServiceUnavailable unless DB.test_connection + raise Pliny::Errors::ServiceUnavailable unless DB.test_connection rescue Sequel::Error => e message = e.message.strip Pliny.log(db: true, health: true, at: 'exception', message: message) diff --git a/lib/template/lib/endpoints/root.rb b/lib/template/lib/endpoints/root.rb index 22297709..fa9891e3 100644 --- a/lib/template/lib/endpoints/root.rb +++ b/lib/template/lib/endpoints/root.rb @@ -1,7 +1,8 @@ +# frozen_string_literal: true module Endpoints class Root < Base - get "/" do - "hello." + get '/' do + 'hello.' end end end diff --git a/lib/template/lib/endpoints/schema.rb b/lib/template/lib/endpoints/schema.rb index 75557d44..dca4155e 100644 --- a/lib/template/lib/endpoints/schema.rb +++ b/lib/template/lib/endpoints/schema.rb @@ -1,11 +1,12 @@ +# frozen_string_literal: true module Endpoints class Schema < Base - get "/schema.json" do - content_type "application/schema+json" - headers["Cache-Control"] = "public, max-age=3600" - unless File.exists?(schema_filename) - message = "This application does not have a schema file." - raise Pliny::Errors::NotFound.new(message) + get '/schema.json' do + content_type 'application/schema+json' + headers['Cache-Control'] = 'public, max-age=3600' + unless File.exist?(schema_filename) + message = 'This application does not have a schema file.' + raise Pliny::Errors::NotFound, message end File.read(schema_filename) end diff --git a/lib/template/lib/initializer.rb b/lib/template/lib/initializer.rb index 1360c70c..aa5723f0 100644 --- a/lib/template/lib/initializer.rb +++ b/lib/template/lib/initializer.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Initializer def self.run require_config @@ -7,7 +8,7 @@ def self.run end def self.require_config - require_relative "../config/config" + require_relative '../config/config' end def self.require_lib diff --git a/lib/template/lib/mediators/base.rb b/lib/template/lib/mediators/base.rb index 83c5ade7..8a07d40b 100644 --- a/lib/template/lib/mediators/base.rb +++ b/lib/template/lib/mediators/base.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: true module Mediators class Base - def self.run(options={}) + def self.run(options = {}) new(options).call end end diff --git a/lib/template/lib/routes.rb b/lib/template/lib/routes.rb index 71f92d4b..07bacd7d 100644 --- a/lib/template/lib/routes.rb +++ b/lib/template/lib/routes.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true Routes = Rack::Builder.new do use Pliny::Middleware::RequestStore::Clear, store: Pliny::RequestStore use Pliny::Middleware::CORS @@ -6,11 +7,15 @@ use Pliny::Middleware::Metrics use Pliny::Middleware::Instruments use Pliny::Middleware::RescueErrors, raise: Config.raise_errors? - use Rack::Timeout, - service_timeout: Config.timeout if Config.timeout > 0 - use Pliny::Middleware::Versioning, - default: Config.versioning_default, - app_name: Config.versioning_app_name if Config.versioning? + if Config.timeout.positive? + use Rack::Timeout, + service_timeout: Config.timeout + end + if Config.versioning? + use Pliny::Middleware::Versioning, + default: Config.versioning_default, + app_name: Config.versioning_app_name + end use Rack::Deflater use Rack::MethodOverride use Rack::SSL if Config.force_ssl? diff --git a/lib/template/lib/serializers/base.rb b/lib/template/lib/serializers/base.rb index a6f5c686..58dbee72 100644 --- a/lib/template/lib/serializers/base.rb +++ b/lib/template/lib/serializers/base.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true module Serializers class Base @@structures = {} def self.structure(type, &blk) - @@structures["#{self.name}::#{type}"] = blk + @@structures["#{name}::#{type}"] = blk end def initialize(type) @@ -11,7 +12,7 @@ def initialize(type) end def serialize(object) - object.respond_to?(:map) ? object.map{|item| serializer.call(item)} : serializer.call(object) + object.respond_to?(:map) ? object.map { |item| serializer.call(item) } : serializer.call(object) end private @@ -19,6 +20,5 @@ def serialize(object) def serializer @@structures["#{self.class.name}::#{@type}"] end - end end diff --git a/lib/template/lib/tasks/rubocop.rake b/lib/template/lib/tasks/rubocop.rake new file mode 100644 index 00000000..3fb29634 --- /dev/null +++ b/lib/template/lib/tasks/rubocop.rake @@ -0,0 +1,6 @@ +# frozen_string_literal: true +require 'rubocop/rake_task' + +RuboCop::RakeTask.new do |task| + task.requires << 'rubocop-rspec' +end diff --git a/lib/template/lib/tasks/spec.rake b/lib/template/lib/tasks/spec.rake index 4a2b288d..daa80385 100644 --- a/lib/template/lib/tasks/spec.rake +++ b/lib/template/lib/tasks/spec.rake @@ -1,10 +1,12 @@ +# frozen_string_literal: true # define our own version of the spec task because rspec might not be available # in the production environment, so we can't rely on RSpec::Core::RakeTask -desc "Run all app specs" +desc 'Run all app specs' task :spec do - require "rspec/core" + require 'rspec/core' code = RSpec::Core::Runner.run( - ["./spec"], - $stderr, $stdout) - exit(code) unless code == 0 + ['./spec'], + $stderr, $stdout + ) + exit(code) unless code.zero? end diff --git a/lib/template/spec/endpoints/health_spec.rb b/lib/template/spec/endpoints/health_spec.rb index ad6b648a..414153c6 100644 --- a/lib/template/spec/endpoints/health_spec.rb +++ b/lib/template/spec/endpoints/health_spec.rb @@ -1,4 +1,5 @@ -require "spec_helper" +# frozen_string_literal: true +require 'spec_helper' RSpec.describe Endpoints::Health do include Rack::Test::Methods diff --git a/lib/template/spec/endpoints/schema_spec.rb b/lib/template/spec/endpoints/schema_spec.rb index 8070dda8..81422ca6 100644 --- a/lib/template/spec/endpoints/schema_spec.rb +++ b/lib/template/spec/endpoints/schema_spec.rb @@ -1,36 +1,37 @@ -require "spec_helper" +# frozen_string_literal: true +require 'spec_helper' RSpec.describe Endpoints::Schema do include Rack::Test::Methods - let(:schema_filename) { "#{Config.root}/schema/schema.json" } + subject(:get_schema) { get '/schema.json' } - subject(:get_schema) { get "/schema.json" } + let(:schema_filename) { "#{Config.root}/schema/schema.json" } - context "without a schema.json" do + context 'without a schema.json' do before do allow(File).to receive(:exists?).and_return(false) end - it "raises a 404 on missing schema" do + it 'raises a 404 on missing schema' do assert_raises(Pliny::Errors::NotFound) do get_schema end end end - context "with a schema.json" do - let(:contents) { "contents" } + context 'with a schema.json' do + let(:contents) { 'contents' } before do allow(File).to receive(:exists?).and_return(true) allow(File).to receive(:read).and_return(contents) end - it "returns the schema is present" do + it 'returns the schema is present' do get_schema assert_equal 200, last_response.status - assert_equal "application/schema+json", last_response.headers["Content-Type"] + assert_equal 'application/schema+json', last_response.headers['Content-Type'] assert_equal contents, last_response.body end end diff --git a/lib/template/spec/spec_helper.rb b/lib/template/spec/spec_helper.rb index 88f8eefe..92d2ee64 100644 --- a/lib/template/spec/spec_helper.rb +++ b/lib/template/spec/spec_helper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # Require this file using `require "spec_helper"` to ensure that it is only @@ -5,22 +6,22 @@ # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration # -ENV["RACK_ENV"] = "test" +ENV['RACK_ENV'] = 'test' -require "bundler" -require "dotenv" +require 'bundler' +require 'dotenv' Bundler.require(:default, :test) Dotenv.load('.env.test') # Get only App Config first, to avoid pulling in libraries until # spec_support has a chance to run, which is important for at least # simplecov and code coverage. -require_relative "../config/config" +require_relative '../config/config' # pull in test initializers Pliny::Utils.require_glob("#{Config.root}/spec/spec_support/**/*.rb") -require_relative "../lib/initializer" +require_relative '../lib/initializer' RSpec.configure do |config| config.before :suite do @@ -53,6 +54,6 @@ # the rack app to be tested with rack-test: def app - @rack_app || fail("Missing @rack_app") + @rack_app || raise('Missing @rack_app') end end diff --git a/lib/template/spec/spec_support/auto_define_rack_app.rb b/lib/template/spec/spec_support/auto_define_rack_app.rb index 1352c44a..d6c502f4 100644 --- a/lib/template/spec/spec_support/auto_define_rack_app.rb +++ b/lib/template/spec/spec_support/auto_define_rack_app.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true RSpec.configure do |config| config.before(:context) do |spec| # weird ruby syntax, but test if the described_class inherits Sinatra::Base: diff --git a/lib/template/spec/spec_support/coverage.rb b/lib/template/spec/spec_support/coverage.rb index 8d5071a9..991841a8 100644 --- a/lib/template/spec/spec_support/coverage.rb +++ b/lib/template/spec/spec_support/coverage.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true # setting ENV["CI"] configures simplecov for continuous integration output # setting ENV["COVERAGE"] generates a report when running tests locally -if ENV["COVERAGE"] || ENV["CI"] - require "simplecov" - if ENV["CI"] +if ENV['COVERAGE'] || ENV['CI'] + require 'simplecov' + if ENV['CI'] SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter SimpleCov.at_exit do puts SimpleCov.result.format! @@ -10,6 +11,6 @@ end SimpleCov.start do # without this the simple formatter won't do anything - add_group "lib", "lib" + add_group 'lib', 'lib' end end diff --git a/lib/template/spec/spec_support/log.rb b/lib/template/spec/spec_support/log.rb index b14c1da1..96b6666e 100644 --- a/lib/template/spec/spec_support/log.rb +++ b/lib/template/spec/spec_support/log.rb @@ -1,7 +1,10 @@ -unless ENV["TEST_LOGS"] == "true" - module Pliny::Log - def log(data, &block) - block.call if block +# frozen_string_literal: true +unless ENV['TEST_LOGS'] == 'true' + module Pliny + module Log + def log(_data, &block) + yield if block + end end end end diff --git a/pliny.gemspec b/pliny.gemspec index e35be664..181d73ac 100644 --- a/pliny.gemspec +++ b/pliny.gemspec @@ -31,7 +31,8 @@ Gem::Specification.new do |gem| gem.add_development_dependency "timecop", "~> 0.7", ">= 0.7.1" gem.add_development_dependency "pry" gem.add_development_dependency "pry-byebug" - gem.add_development_dependency "pg", "~> 0.17", ">= 0.17.1" - gem.add_development_dependency "rollbar", "~> 2.11", ">= 2.11.0" - gem.add_development_dependency "sequel", "~> 4.9", ">= 4.9.0" + gem.add_development_dependency "pg", "~> 0.17", ">= 0.17.1" + gem.add_development_dependency "rollbar", "~> 2.11", ">= 2.11.0" + gem.add_development_dependency "sequel", "~> 4.9", ">= 4.9.0" + gem.add_development_dependency "rubocop", "~> 0.45", ">= 0.45.0" end diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index d2e438d6..b7c371bb 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -7,34 +7,36 @@ Dir.chdir("/tmp/plinytest") bash "pliny-new myapp" - bash_app "bin/setup" + + Dir.chdir("/tmp/plinytest/myapp") + bash "bin/setup" end describe "bin/setup" do it "generates .env" do - assert File.exists?("./myapp/.env") + assert File.exists?("./.env") end end describe "pliny-generate scaffold" do before(:all) do - bash_app "pliny-generate scaffold artist" + bash "pliny-generate scaffold artist" end it "creates the model file" do - assert File.exists?("./myapp/lib/models/artist.rb") + assert File.exists?("./lib/models/artist.rb") end it "creates the endpoint file" do - assert File.exists?("./myapp/lib/endpoints/artists.rb") + assert File.exists?("./lib/endpoints/artists.rb") end it "creates the serializer file" do - assert File.exists?("./myapp/lib/serializers/artist.rb") + assert File.exists?("./lib/serializers/artist.rb") end it "creates the schema file" do - assert File.exists?("./myapp/schema/schemata/artist.yaml") + assert File.exists?("./schema/schemata/artist.yaml") end end @@ -46,8 +48,4 @@ def bash(cmd) raise "Failed to run #{cmd}" end end - - def bash_app(cmd) - bash "cd myapp && #{cmd}" - end end From 52b1f9dc5eb36baea00b3b89f5f0ee9d947105df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Peignier?= Date: Mon, 19 Dec 2016 16:19:35 -0800 Subject: [PATCH 2/2] Apply new rubocop rules --- .gitignore | 1 + lib/template/.rubocop.yml | 7 ++++ lib/template/Gemfile | 18 +++++----- lib/template/Rakefile | 6 ++-- lib/template/bin/console | 9 +++-- lib/template/bin/run | 6 ++-- lib/template/config.ru | 3 +- lib/template/config/config.rb | 9 +++-- lib/template/config/initializers/database.rb | 3 +- lib/template/config/initializers/log.rb | 1 - lib/template/config/initializers/rollbar.rb | 9 +++-- lib/template/config/puma.rb | 3 +- lib/template/db/seeds.rb | 1 - lib/template/lib/application.rb | 5 ++- lib/template/lib/endpoints/base.rb | 1 - lib/template/lib/endpoints/health.rb | 7 ++-- lib/template/lib/endpoints/root.rb | 5 ++- lib/template/lib/endpoints/schema.rb | 9 +++-- lib/template/lib/initializer.rb | 3 +- lib/template/lib/mediators/base.rb | 1 - lib/template/lib/routes.rb | 1 - lib/template/lib/serializers/base.rb | 1 - lib/template/lib/tasks/rubocop.rake | 6 ++-- lib/template/lib/tasks/spec.rake | 8 ++--- lib/template/spec/endpoints/health_spec.rb | 35 +++++++++---------- lib/template/spec/endpoints/schema_spec.rb | 17 +++++---- lib/template/spec/spec_helper.rb | 19 +++++----- .../spec/spec_support/auto_define_rack_app.rb | 1 - lib/template/spec/spec_support/coverage.rb | 9 +++-- lib/template/spec/spec_support/log.rb | 3 +- pliny.gemspec | 2 +- 31 files changed, 97 insertions(+), 112 deletions(-) diff --git a/.gitignore b/.gitignore index 6d3fb3be..00c92f5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Gemfile.lock .bundle .ruby-* +.tags diff --git a/lib/template/.rubocop.yml b/lib/template/.rubocop.yml index 89f30fcb..281c9123 100644 --- a/lib/template/.rubocop.yml +++ b/lib/template/.rubocop.yml @@ -20,3 +20,10 @@ Style/ClassVars: # Offense count: 6 Style/Documentation: Enabled: false + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +Style/FrozenStringLiteralComment: + Enabled: false diff --git a/lib/template/Gemfile b/lib/template/Gemfile index f57f369b..a1adf929 100644 --- a/lib/template/Gemfile +++ b/lib/template/Gemfile @@ -20,16 +20,16 @@ gem "sinatra-router" gem "sucker_punch" group :development, :test do - gem 'pry-byebug' - gem 'rubocop', '~> 0.45.0', require: false - gem 'rubocop-rspec', require: false + gem "pry-byebug" + gem "rubocop", "~> 0.46.0", require: false + gem "rubocop-rspec", require: false end group :test do - gem 'simplecov', require: false - gem 'committee' - gem 'database_cleaner' - gem 'dotenv' - gem 'rack-test' - gem 'rspec' + gem "committee" + gem "database_cleaner" + gem "dotenv" + gem "rack-test" + gem "rspec" + gem "simplecov", require: false end diff --git a/lib/template/Rakefile b/lib/template/Rakefile index c714f429..861b2e2b 100644 --- a/lib/template/Rakefile +++ b/lib/template/Rakefile @@ -1,7 +1,7 @@ -# frozen_string_literal: true -require 'pliny/tasks' + +require "pliny/tasks" # Add your rake tasks to lib/tasks! -Dir['./lib/tasks/*.rake'].each { |task| load task } +Dir["./lib/tasks/*.rake"].each { |task| load task } task default: :spec diff --git a/lib/template/bin/console b/lib/template/bin/console index 0505d3d1..31e16488 100755 --- a/lib/template/bin/console +++ b/lib/template/bin/console @@ -1,21 +1,20 @@ #!/usr/bin/env ruby -# frozen_string_literal: true -require_relative '../lib/application' +require_relative "../lib/application" def basic_prompt(target_self, nest_level, pry) # override DEPLOYMENT to identify console sessions (eg: staging/production/etc) - nesting = nest_level.zero? ? '' : ":#{nest_level}" + nesting = nest_level.zero? ? "" : ":#{nest_level}" "[#{pry.input_array.size}] #{Config.deployment}(#{Pry.view_clip(target_self)})#{nesting}" end Pry.prompt = [ proc do |target_self, nest_level, pry| - basic_prompt(target_self, nest_level, pry) + '> ' + basic_prompt(target_self, nest_level, pry) + "> " end, proc do |target_self, nest_level, pry| - basic_prompt(target_self, nest_level, pry) + '* ' + basic_prompt(target_self, nest_level, pry) + "* " end ] diff --git a/lib/template/bin/run b/lib/template/bin/run index 6582936f..e9497f41 100755 --- a/lib/template/bin/run +++ b/lib/template/bin/run @@ -1,7 +1,7 @@ #!/usr/bin/env ruby -# frozen_string_literal: true + # rubocop:disable Lint/Eval -require_relative '../lib/application' +require_relative "../lib/application" -eval ARGV.join(' ') +eval ARGV.join(" ") diff --git a/lib/template/config.ru b/lib/template/config.ru index 905a1111..0d3a5a85 100644 --- a/lib/template/config.ru +++ b/lib/template/config.ru @@ -1,5 +1,4 @@ -# frozen_string_literal: true -require_relative 'lib/application' +require_relative "lib/application" $stdout.sync = true $stderr.sync = true diff --git a/lib/template/config/config.rb b/lib/template/config/config.rb index d67215db..713d6c4a 100644 --- a/lib/template/config/config.rb +++ b/lib/template/config/config.rb @@ -1,5 +1,4 @@ -# frozen_string_literal: true -require 'pliny/config_helpers' +require "pliny/config_helpers" # Access all config keys like the following: # @@ -21,16 +20,16 @@ module Config # Override -- value is returned or the set default. override :database_timeout, 10, int override :db_pool, 5, int - override :deployment, 'production', string + override :deployment, "production", string override :force_ssl, true, bool - override :app_env, 'production', string + override :app_env, "production", string override :port, 5000, int override :pretty_json, false, bool override :puma_max_threads, 16, int override :puma_min_threads, 1, int override :puma_workers, 3, int override :raise_errors, false, bool - override :root, File.expand_path('../../', __FILE__), string + override :root, File.expand_path("../../", __FILE__), string override :timeout, 10, int override :versioning, false, bool end diff --git a/lib/template/config/initializers/database.rb b/lib/template/config/initializers/database.rb index 91affd28..1f1bff95 100644 --- a/lib/template/config/initializers/database.rb +++ b/lib/template/config/initializers/database.rb @@ -1,7 +1,6 @@ -# frozen_string_literal: true database_setup_proc = lambda do |conn| # identify postgres connections coming from this process in pg_stat_activity - process_identifier = ENV['DYNO'] || File.basename($PROGRAM_NAME).gsub(/\W+/, '_') + process_identifier = ENV["DYNO"] || File.basename($PROGRAM_NAME).gsub(/\W+/, "_") conn.execute "SET statement_timeout = '#{Config.database_timeout}s'" conn.execute "SET application_name = '#{process_identifier}'" end diff --git a/lib/template/config/initializers/log.rb b/lib/template/config/initializers/log.rb index af252822..2f3bb2ec 100644 --- a/lib/template/config/initializers/log.rb +++ b/lib/template/config/initializers/log.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true Pliny.default_context = {} Pliny.default_context[:app] = Config.app_name if Config.app_name Pliny.default_context[:deployment] = Config.deployment diff --git a/lib/template/config/initializers/rollbar.rb b/lib/template/config/initializers/rollbar.rb index fd6b7cc0..79329f26 100644 --- a/lib/template/config/initializers/rollbar.rb +++ b/lib/template/config/initializers/rollbar.rb @@ -1,13 +1,12 @@ -# frozen_string_literal: true -require 'pliny/error_reporters/rollbar' +require "pliny/error_reporters/rollbar" Pliny::ErrorReporters.error_reporters << Pliny::ErrorReporters::Rollbar Rollbar.configure do |config| - config.enabled = ENV.key?('ROLLBAR_ACCESS_TOKEN') + config.enabled = ENV.key?("ROLLBAR_ACCESS_TOKEN") config.disable_rack_monkey_patch = true - config.access_token = ENV['ROLLBAR_ACCESS_TOKEN'] - config.environment = ENV['ROLLBAR_ENV'] + config.access_token = ENV["ROLLBAR_ACCESS_TOKEN"] + config.environment = ENV["ROLLBAR_ENV"] config.logger = Pliny::RollbarLogger.new config.use_sucker_punch end diff --git a/lib/template/config/puma.rb b/lib/template/config/puma.rb index cf60471f..a1f78a09 100644 --- a/lib/template/config/puma.rb +++ b/lib/template/config/puma.rb @@ -1,5 +1,4 @@ -# frozen_string_literal: true -require './config/config' +require "./config/config" environment Config.rack_env port Config.port diff --git a/lib/template/db/seeds.rb b/lib/template/db/seeds.rb index b6bd28d0..0f14889d 100644 --- a/lib/template/db/seeds.rb +++ b/lib/template/db/seeds.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true # This file should contain all the record creation needed to seed the database # with its default values. # The data can then be loaded with the rake db:seed (or created alongside the diff --git a/lib/template/lib/application.rb b/lib/template/lib/application.rb index c47b528f..898a00b5 100644 --- a/lib/template/lib/application.rb +++ b/lib/template/lib/application.rb @@ -1,5 +1,4 @@ -# frozen_string_literal: true -require 'bundler' +require "bundler" Bundler.require -require_relative 'initializer' +require_relative "initializer" diff --git a/lib/template/lib/endpoints/base.rb b/lib/template/lib/endpoints/base.rb index 230248ec..12b48546 100644 --- a/lib/template/lib/endpoints/base.rb +++ b/lib/template/lib/endpoints/base.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true module Endpoints # The base class for all Sinatra-based endpoints. Use sparingly. class Base < Sinatra::Base diff --git a/lib/template/lib/endpoints/health.rb b/lib/template/lib/endpoints/health.rb index f4250276..f44eb836 100644 --- a/lib/template/lib/endpoints/health.rb +++ b/lib/template/lib/endpoints/health.rb @@ -1,12 +1,11 @@ -# frozen_string_literal: true module Endpoints class Health < Base - namespace '/health' do + namespace "/health" do get do encode({}) end - get '/db' do + get "/db" do database? database_available? encode({}) @@ -22,7 +21,7 @@ def database_available? raise Pliny::Errors::ServiceUnavailable unless DB.test_connection rescue Sequel::Error => e message = e.message.strip - Pliny.log(db: true, health: true, at: 'exception', message: message) + Pliny.log(db: true, health: true, at: "exception", message: message) raise Pliny::Errors::ServiceUnavailable end end diff --git a/lib/template/lib/endpoints/root.rb b/lib/template/lib/endpoints/root.rb index fa9891e3..22297709 100644 --- a/lib/template/lib/endpoints/root.rb +++ b/lib/template/lib/endpoints/root.rb @@ -1,8 +1,7 @@ -# frozen_string_literal: true module Endpoints class Root < Base - get '/' do - 'hello.' + get "/" do + "hello." end end end diff --git a/lib/template/lib/endpoints/schema.rb b/lib/template/lib/endpoints/schema.rb index dca4155e..94e50bb2 100644 --- a/lib/template/lib/endpoints/schema.rb +++ b/lib/template/lib/endpoints/schema.rb @@ -1,11 +1,10 @@ -# frozen_string_literal: true module Endpoints class Schema < Base - get '/schema.json' do - content_type 'application/schema+json' - headers['Cache-Control'] = 'public, max-age=3600' + get "/schema.json" do + content_type "application/schema+json" + headers["Cache-Control"] = "public, max-age=3600" unless File.exist?(schema_filename) - message = 'This application does not have a schema file.' + message = "This application does not have a schema file." raise Pliny::Errors::NotFound, message end File.read(schema_filename) diff --git a/lib/template/lib/initializer.rb b/lib/template/lib/initializer.rb index aa5723f0..1360c70c 100644 --- a/lib/template/lib/initializer.rb +++ b/lib/template/lib/initializer.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true module Initializer def self.run require_config @@ -8,7 +7,7 @@ def self.run end def self.require_config - require_relative '../config/config' + require_relative "../config/config" end def self.require_lib diff --git a/lib/template/lib/mediators/base.rb b/lib/template/lib/mediators/base.rb index 8a07d40b..f0cb45b8 100644 --- a/lib/template/lib/mediators/base.rb +++ b/lib/template/lib/mediators/base.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true module Mediators class Base def self.run(options = {}) diff --git a/lib/template/lib/routes.rb b/lib/template/lib/routes.rb index 07bacd7d..2e43f3c6 100644 --- a/lib/template/lib/routes.rb +++ b/lib/template/lib/routes.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true Routes = Rack::Builder.new do use Pliny::Middleware::RequestStore::Clear, store: Pliny::RequestStore use Pliny::Middleware::CORS diff --git a/lib/template/lib/serializers/base.rb b/lib/template/lib/serializers/base.rb index 58dbee72..993d6ffb 100644 --- a/lib/template/lib/serializers/base.rb +++ b/lib/template/lib/serializers/base.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true module Serializers class Base @@structures = {} diff --git a/lib/template/lib/tasks/rubocop.rake b/lib/template/lib/tasks/rubocop.rake index 3fb29634..9950b92a 100644 --- a/lib/template/lib/tasks/rubocop.rake +++ b/lib/template/lib/tasks/rubocop.rake @@ -1,6 +1,6 @@ -# frozen_string_literal: true -require 'rubocop/rake_task' + +require "rubocop/rake_task" RuboCop::RakeTask.new do |task| - task.requires << 'rubocop-rspec' + task.requires << "rubocop-rspec" end diff --git a/lib/template/lib/tasks/spec.rake b/lib/template/lib/tasks/spec.rake index daa80385..f8d1a98c 100644 --- a/lib/template/lib/tasks/spec.rake +++ b/lib/template/lib/tasks/spec.rake @@ -1,11 +1,11 @@ -# frozen_string_literal: true + # define our own version of the spec task because rspec might not be available # in the production environment, so we can't rely on RSpec::Core::RakeTask -desc 'Run all app specs' +desc "Run all app specs" task :spec do - require 'rspec/core' + require "rspec/core" code = RSpec::Core::Runner.run( - ['./spec'], + ["./spec"], $stderr, $stdout ) exit(code) unless code.zero? diff --git a/lib/template/spec/endpoints/health_spec.rb b/lib/template/spec/endpoints/health_spec.rb index 414153c6..49e6b7ae 100644 --- a/lib/template/spec/endpoints/health_spec.rb +++ b/lib/template/spec/endpoints/health_spec.rb @@ -1,5 +1,4 @@ -# frozen_string_literal: true -require 'spec_helper' +require "spec_helper" RSpec.describe Endpoints::Health do include Rack::Test::Methods @@ -8,48 +7,48 @@ def app Endpoints::Health end - describe 'GET /health' do - it 'returns a 200' do - get '/health' + describe "GET /health" do + it "returns a 200" do + get "/health" assert_equal(200, last_response.status) - assert_equal('application/json;charset=utf-8', last_response.headers['Content-Type']) - assert_equal(2, last_response.headers['Content-Length'].to_i) + assert_equal("application/json;charset=utf-8", last_response.headers["Content-Type"]) + assert_equal(2, last_response.headers["Content-Length"].to_i) assert_equal({}, MultiJson.decode(last_response.body)) end end - describe 'GET /health/db' do - it 'raises a 404 when no database is available' do + describe "GET /health/db" do + it "raises a 404 when no database is available" do allow(DB).to receive(:nil?).and_return(true) assert_raises Pliny::Errors::NotFound do - get '/health/db' + get "/health/db" end end - it 'raises a 503 on Sequel exceptions' do + it "raises a 503 on Sequel exceptions" do allow(DB).to receive(:test_connection).and_raise(Sequel::Error) assert_raises Pliny::Errors::ServiceUnavailable do - get '/health/db' + get "/health/db" end end - it 'raises a 503 when connection testing fails' do + it "raises a 503 when connection testing fails" do allow(DB).to receive(:test_connection).and_return(false) assert_raises Pliny::Errors::ServiceUnavailable do - get '/health/db' + get "/health/db" end end - it 'returns a 200' do + it "returns a 200" do allow(DB).to receive(:test_connection).and_return(true) - get '/health/db' + get "/health/db" assert_equal(200, last_response.status) - assert_equal('application/json;charset=utf-8', last_response.headers['Content-Type']) - assert_equal(2, last_response.headers['Content-Length'].to_i) + assert_equal("application/json;charset=utf-8", last_response.headers["Content-Type"]) + assert_equal(2, last_response.headers["Content-Length"].to_i) assert_equal({}, MultiJson.decode(last_response.body)) end end diff --git a/lib/template/spec/endpoints/schema_spec.rb b/lib/template/spec/endpoints/schema_spec.rb index 81422ca6..78eba00e 100644 --- a/lib/template/spec/endpoints/schema_spec.rb +++ b/lib/template/spec/endpoints/schema_spec.rb @@ -1,37 +1,36 @@ -# frozen_string_literal: true -require 'spec_helper' +require "spec_helper" RSpec.describe Endpoints::Schema do include Rack::Test::Methods - subject(:get_schema) { get '/schema.json' } + subject(:get_schema) { get "/schema.json" } let(:schema_filename) { "#{Config.root}/schema/schema.json" } - context 'without a schema.json' do + context "without a schema.json" do before do allow(File).to receive(:exists?).and_return(false) end - it 'raises a 404 on missing schema' do + it "raises a 404 on missing schema" do assert_raises(Pliny::Errors::NotFound) do get_schema end end end - context 'with a schema.json' do - let(:contents) { 'contents' } + context "with a schema.json" do + let(:contents) { "contents" } before do allow(File).to receive(:exists?).and_return(true) allow(File).to receive(:read).and_return(contents) end - it 'returns the schema is present' do + it "returns the schema is present" do get_schema assert_equal 200, last_response.status - assert_equal 'application/schema+json', last_response.headers['Content-Type'] + assert_equal "application/schema+json", last_response.headers["Content-Type"] assert_equal contents, last_response.body end end diff --git a/lib/template/spec/spec_helper.rb b/lib/template/spec/spec_helper.rb index 92d2ee64..b12d1b6a 100644 --- a/lib/template/spec/spec_helper.rb +++ b/lib/template/spec/spec_helper.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # Require this file using `require "spec_helper"` to ensure that it is only @@ -6,22 +5,22 @@ # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration # -ENV['RACK_ENV'] = 'test' +ENV["RACK_ENV"] = "test" -require 'bundler' -require 'dotenv' +require "bundler" +require "dotenv" Bundler.require(:default, :test) -Dotenv.load('.env.test') +Dotenv.load(".env.test") # Get only App Config first, to avoid pulling in libraries until # spec_support has a chance to run, which is important for at least # simplecov and code coverage. -require_relative '../config/config' +require_relative "../config/config" # pull in test initializers Pliny::Utils.require_glob("#{Config.root}/spec/spec_support/**/*.rb") -require_relative '../lib/initializer' +require_relative "../lib/initializer" RSpec.configure do |config| config.before :suite do @@ -30,7 +29,7 @@ end config.before :all do - load('db/seeds.rb') if File.exist?('db/seeds.rb') + load("db/seeds.rb") if File.exist?("db/seeds.rb") end config.before :each do @@ -50,10 +49,10 @@ # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 - config.order = 'random' + config.order = "random" # the rack app to be tested with rack-test: def app - @rack_app || raise('Missing @rack_app') + @rack_app || raise("Missing @rack_app") end end diff --git a/lib/template/spec/spec_support/auto_define_rack_app.rb b/lib/template/spec/spec_support/auto_define_rack_app.rb index d6c502f4..1352c44a 100644 --- a/lib/template/spec/spec_support/auto_define_rack_app.rb +++ b/lib/template/spec/spec_support/auto_define_rack_app.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true RSpec.configure do |config| config.before(:context) do |spec| # weird ruby syntax, but test if the described_class inherits Sinatra::Base: diff --git a/lib/template/spec/spec_support/coverage.rb b/lib/template/spec/spec_support/coverage.rb index 991841a8..8d5071a9 100644 --- a/lib/template/spec/spec_support/coverage.rb +++ b/lib/template/spec/spec_support/coverage.rb @@ -1,9 +1,8 @@ -# frozen_string_literal: true # setting ENV["CI"] configures simplecov for continuous integration output # setting ENV["COVERAGE"] generates a report when running tests locally -if ENV['COVERAGE'] || ENV['CI'] - require 'simplecov' - if ENV['CI'] +if ENV["COVERAGE"] || ENV["CI"] + require "simplecov" + if ENV["CI"] SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter SimpleCov.at_exit do puts SimpleCov.result.format! @@ -11,6 +10,6 @@ end SimpleCov.start do # without this the simple formatter won't do anything - add_group 'lib', 'lib' + add_group "lib", "lib" end end diff --git a/lib/template/spec/spec_support/log.rb b/lib/template/spec/spec_support/log.rb index 96b6666e..6034b8e6 100644 --- a/lib/template/spec/spec_support/log.rb +++ b/lib/template/spec/spec_support/log.rb @@ -1,5 +1,4 @@ -# frozen_string_literal: true -unless ENV['TEST_LOGS'] == 'true' +unless ENV["TEST_LOGS"] == "true" module Pliny module Log def log(_data, &block) diff --git a/pliny.gemspec b/pliny.gemspec index 181d73ac..9219e6ac 100644 --- a/pliny.gemspec +++ b/pliny.gemspec @@ -34,5 +34,5 @@ Gem::Specification.new do |gem| gem.add_development_dependency "pg", "~> 0.17", ">= 0.17.1" gem.add_development_dependency "rollbar", "~> 2.11", ">= 2.11.0" gem.add_development_dependency "sequel", "~> 4.9", ">= 4.9.0" - gem.add_development_dependency "rubocop", "~> 0.45", ">= 0.45.0" + gem.add_development_dependency "rubocop", "~> 0.46", ">= 0.46.0" end