From c744ca2df4dfde73af5365333d05fd3d3c1f8251 Mon Sep 17 00:00:00 2001 From: Rajan Joshi Date: Fri, 1 Oct 2021 19:43:09 +0100 Subject: [PATCH 1/2] Increase Rubocop coverage This commit will increase the Rubocop coverage to all over the app instead of Rakefile as this was the case before. I have also added a TODO yml which can make a list of offenses which can be dealt later on. --- .rubocop.yml | 5 +- .rubocop_todo.yml | 53 +++++++++++++++++++ Gemfile | 2 + lib/heartcheck.rb | 20 +++---- lib/heartcheck/app.rb | 9 ++-- lib/heartcheck/caching_app.rb | 2 + lib/heartcheck/caching_app/cache.rb | 2 + lib/heartcheck/checks.rb | 2 + lib/heartcheck/checks/base.rb | 15 +++--- lib/heartcheck/checks/firewall.rb | 12 ++--- lib/heartcheck/checks/process.rb | 18 +++---- lib/heartcheck/checks/watch_file.rb | 6 ++- lib/heartcheck/controllers/base.rb | 5 +- lib/heartcheck/controllers/dev.rb | 4 +- lib/heartcheck/controllers/environment.rb | 8 +-- lib/heartcheck/controllers/essential.rb | 2 + lib/heartcheck/controllers/functional.rb | 2 + lib/heartcheck/controllers/health_check.rb | 2 + lib/heartcheck/controllers/info.rb | 2 + lib/heartcheck/controllers/inspect.rb | 2 + lib/heartcheck/errors.rb | 2 + lib/heartcheck/errors/routing_error.rb | 2 + lib/heartcheck/errors/warning.rb | 2 + lib/heartcheck/executors.rb | 2 + lib/heartcheck/executors/base.rb | 2 + lib/heartcheck/executors/threaded.rb | 4 +- lib/heartcheck/formatters/base.rb | 2 + lib/heartcheck/formatters/hash_response.rb | 2 + lib/heartcheck/generators.rb | 4 +- lib/heartcheck/generators/generator.rb | 7 ++- lib/heartcheck/generators/templates/config.rb | 35 ++++++------ lib/heartcheck/logger.rb | 4 +- lib/heartcheck/services.rb | 2 + lib/heartcheck/services/firewall.rb | 2 + lib/heartcheck/version.rb | 4 +- spec/lib/heartcheck/app_spec.rb | 6 ++- spec/lib/heartcheck/caching_app/cache_spec.rb | 8 +-- spec/lib/heartcheck/caching_app_spec.rb | 8 +-- spec/lib/heartcheck/checks/base_spec.rb | 2 + spec/lib/heartcheck/checks/firewall_spec.rb | 8 ++- spec/lib/heartcheck/checks/process_spec.rb | 2 + spec/lib/heartcheck/checks/watch_file_spec.rb | 10 ++-- spec/lib/heartcheck/controllers/dev_spec.rb | 8 +-- .../controllers/environment_spec.rb | 18 ++++--- .../heartcheck/controllers/essential_spec.rb | 16 +++--- .../heartcheck/controllers/functional_spec.rb | 16 +++--- .../controllers/health_check_spec.rb | 2 + spec/lib/heartcheck/controllers/info_spec.rb | 8 +-- spec/lib/heartcheck/executors/base_spec.rb | 8 +-- .../lib/heartcheck/executors/threaded_spec.rb | 12 ++--- spec/lib/heartcheck/formatters/base_spec.rb | 20 ++++--- .../formatters/hash_response_spec.rb | 20 ++++--- spec/lib/heartcheck/logger_spec.rb | 2 + spec/lib/heartcheck/services/firewall_spec.rb | 2 + spec/lib/heartcheck_spec.rb | 6 ++- spec/spec_helper.rb | 4 +- spec/support/checks/dummy1.rb | 5 +- spec/support/checks/dummy2.rb | 5 +- 58 files changed, 301 insertions(+), 144 deletions(-) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index cd2df43..fdb3ee3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,8 @@ +inherit_from: .rubocop_todo.yml + AllCops: NewCops: enable - Include: - - '**/Rakefile' + SuggestExtensions: false Style/Documentation: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..e1cab55 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,53 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2021-10-01 18:26:34 UTC using RuboCop version 1.22.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: Include. +# Include: **/*.gemspec +Gemspec/RequiredRubyVersion: + Exclude: + - 'heartcheck.gemspec' + +# Offense count: 3 +# Configuration parameters: IgnoredMethods. +Lint/AmbiguousBlockAssociation: + Exclude: + - 'spec/lib/heartcheck/caching_app/cache_spec.rb' + +# Offense count: 2 +Lint/DuplicateMethods: + Exclude: + - 'lib/heartcheck.rb' + +# Offense count: 16 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +# IgnoredMethods: refine +Metrics/BlockLength: + Max: 180 + +# Offense count: 3 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +Metrics/MethodLength: + Max: 29 + +# Offense count: 1 +# Configuration parameters: EnforcedStyleForLeadingUnderscores. +# SupportedStylesForLeadingUnderscores: disallowed, required, optional +Naming/MemoizedInstanceVariableName: + Exclude: + - 'spec/lib/heartcheck/caching_app/cache_spec.rb' + +# Offense count: 12 +# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers. +# SupportedStyles: snake_case, normalcase, non_integer +# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339 + +# Offense count: 1 +Security/Open: + Exclude: + - 'lib/heartcheck/checks/watch_file.rb' diff --git a/Gemfile b/Gemfile index 483b2d6..21fec6c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' # Specify your gem's dependencies in heartcheck.gemspec diff --git a/lib/heartcheck.rb b/lib/heartcheck.rb index 3bd3083..7575f28 100644 --- a/lib/heartcheck.rb +++ b/lib/heartcheck.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck require 'logger' require 'heartcheck/app' @@ -65,11 +67,11 @@ def add(name, options = {}, &block) class_name = options.fetch(:class) { constantize(name) } instance = Checks.const_get(class_name).new - if block_given? - checks << instance.tap(&block) - else - checks << instance - end + checks << if block_given? + instance.tap(&block) + else + instance + end end # filter checks that are essential @@ -90,7 +92,7 @@ def functional_checks # # @return [Array] checks that are not functional def dev_checks - checks.select { |ctx| !ctx.functional? } + checks.reject(&:functional?) end # filter checks that has some information @@ -126,8 +128,8 @@ def executor # # @return [Heartcheck::Executors::Threaded] def use_threaded_executor! - require "concurrent" - require "heartcheck/executors/threaded" + require 'concurrent' + require 'heartcheck/executors/threaded' self.executor = Heartcheck::Executors::Threaded.new end @@ -139,7 +141,7 @@ def use_threaded_executor! # # @return [Logger] a ruby logger to STDOUT and info level def default_logger - log = ::Logger.new(STDOUT) + log = ::Logger.new($stdout) log.level = ::Logger::INFO log end diff --git a/lib/heartcheck/app.rb b/lib/heartcheck/app.rb index 7e75bda..3cddb18 100644 --- a/lib/heartcheck/app.rb +++ b/lib/heartcheck/app.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require 'rack' require 'heartcheck/controllers/base' -Dir.glob(File.expand_path('../controllers/*.rb', __FILE__)) - .each { |x| require x } +Dir.glob(File.expand_path('controllers/*.rb', __dir__)).sort.each { |x| require x } # A web app that's use rack module Heartcheck @@ -19,7 +20,7 @@ class App '/inspect' => Controllers::Inspect, '/health_check' => Controllers::HealthCheck, '/environment' => Controllers::Environment - } + }.freeze # Sets up the rack application. # @@ -58,7 +59,7 @@ def call(env) # @return [String] a response body def dispatch_action(req) controller = ROUTE_TO_CONTROLLER[req.path_info] - fail Heartcheck::Errors::RoutingError if controller.nil? + raise Heartcheck::Errors::RoutingError if controller.nil? Logger.info "Start [#{controller}] from #{req.ip} at #{Time.now}" diff --git a/lib/heartcheck/caching_app.rb b/lib/heartcheck/caching_app.rb index 386d088..6f4fc43 100644 --- a/lib/heartcheck/caching_app.rb +++ b/lib/heartcheck/caching_app.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'heartcheck/caching_app/cache' # A rack middleware to wrap around {Heartcheck::App} in a cache diff --git a/lib/heartcheck/caching_app/cache.rb b/lib/heartcheck/caching_app/cache.rb index 13d64f8..b765f97 100644 --- a/lib/heartcheck/caching_app/cache.rb +++ b/lib/heartcheck/caching_app/cache.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'concurrent' module Heartcheck diff --git a/lib/heartcheck/checks.rb b/lib/heartcheck/checks.rb index 6f6e68f..b188411 100644 --- a/lib/heartcheck/checks.rb +++ b/lib/heartcheck/checks.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'heartcheck/checks/base' require 'heartcheck/checks/firewall' require 'heartcheck/checks/process' diff --git a/lib/heartcheck/checks/base.rb b/lib/heartcheck/checks/base.rb index b68a324..a6ba1f5 100644 --- a/lib/heartcheck/checks/base.rb +++ b/lib/heartcheck/checks/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Checks # Base check that contains the common functionality for chechs @@ -9,7 +11,7 @@ class Base # # @return [Boolean] (default: false) attr_accessor :functional - alias_method :functional?, :functional + alias functional? functional # When it is true the check will work just for `/dev` route. # you can use it to create some check is not essential/functional for you app. @@ -17,7 +19,7 @@ class Base # # @return [Boolean] (default: false) attr_accessor :dev - alias_method :dev?, :dev + alias dev? dev # The name for identify the check in the result page # @@ -104,7 +106,6 @@ def add_service(service) @services << service end - # It is used to add a service that will use when check run. # # @return [Array] @@ -143,7 +144,7 @@ def uri_info def informations info - rescue => e + rescue StandardError => e { 'error' => e.message } end @@ -173,9 +174,9 @@ def validation validate end end - rescue Heartcheck::Errors::Warning => w - @errors = [{ type: 'warning', message: w.message }] - rescue => e + rescue Heartcheck::Errors::Warning => e + @errors = [{ type: 'warning', message: e.message }] + rescue StandardError => e @errors = [e.message] end @errors diff --git a/lib/heartcheck/checks/firewall.rb b/lib/heartcheck/checks/firewall.rb index 395db80..31f5613 100644 --- a/lib/heartcheck/checks/firewall.rb +++ b/lib/heartcheck/checks/firewall.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Checks class Firewall < Base @@ -7,12 +9,10 @@ def services def validate services.each do |service| - begin - Net::Telnet.new(service.params).close - rescue Errno::ECONNREFUSED; nil - rescue - append_error(service) - end + Net::Telnet.new(service.params).close + rescue Errno::ECONNREFUSED; nil + rescue StandardError + append_error(service) end end diff --git a/lib/heartcheck/checks/process.rb b/lib/heartcheck/checks/process.rb index 2642d3f..ef1717b 100644 --- a/lib/heartcheck/checks/process.rb +++ b/lib/heartcheck/checks/process.rb @@ -1,14 +1,14 @@ +# frozen_string_literal: true + module Heartcheck module Checks class Process < Base def validate services.each do |service| - begin - pid = get_pid(service) - ::Process.kill(0, pid) - rescue Errno::ESRCH - append_error(service, pid) - end + pid = get_pid(service) + ::Process.kill(0, pid) + rescue Errno::ESRCH + append_error(service, pid) end end @@ -19,11 +19,7 @@ def custom_error(service, pid) end def get_pid(service) - if service[:pid] - service[:pid] - else - File.read(service[:file]).to_i - end + service[:pid] || File.read(service[:file]).to_i end end end diff --git a/lib/heartcheck/checks/watch_file.rb b/lib/heartcheck/checks/watch_file.rb index 6d52f9c..5149908 100644 --- a/lib/heartcheck/checks/watch_file.rb +++ b/lib/heartcheck/checks/watch_file.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' module Heartcheck @@ -9,8 +11,8 @@ def add_service(options) def validate services.each do |service| - if not service[:runtime].eql? installed(service[:file]) - @errors << "App outdated, check /monitoring/info for more details!" + unless service[:runtime].eql? installed(service[:file]) + @errors << 'App outdated, check /monitoring/info for more details!' end end end diff --git a/lib/heartcheck/controllers/base.rb b/lib/heartcheck/controllers/base.rb index f7924ba..1676eb0 100644 --- a/lib/heartcheck/controllers/base.rb +++ b/lib/heartcheck/controllers/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rack' require 'multi_json' @@ -7,7 +9,7 @@ module Heartcheck module Controllers class Base def index - fail NotImplementError + raise NotImplementError end protected @@ -18,6 +20,7 @@ def check(who) end private + def formatter Heartcheck.formatter end diff --git a/lib/heartcheck/controllers/dev.rb b/lib/heartcheck/controllers/dev.rb index 423f702..c55c2f1 100644 --- a/lib/heartcheck/controllers/dev.rb +++ b/lib/heartcheck/controllers/dev.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers class Dev < Base @@ -27,7 +29,7 @@ def index def time_diff start_time = Time.now yield - '%.2f ms' % ((Time.now - start_time) * 1_000) + format('%.2f ms', ((Time.now - start_time) * 1_000)) end end end diff --git a/lib/heartcheck/controllers/environment.rb b/lib/heartcheck/controllers/environment.rb index 0a540b2..44b9c32 100644 --- a/lib/heartcheck/controllers/environment.rb +++ b/lib/heartcheck/controllers/environment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'sys-uname' module Heartcheck @@ -6,9 +8,9 @@ class Environment < Base def index MultiJson.dump( { - :system_info => Hash[Sys::Uname.uname.each_pair.to_a], - :ruby_version => RUBY_VERSION, - :rails_version => defined?(Rails) ? Rails::VERSION::STRING : '(none)' + system_info: Sys::Uname.uname.each_pair.to_a.to_h, + ruby_version: RUBY_VERSION, + rails_version: defined?(Rails) ? Rails::VERSION::STRING : '(none)' } ) end diff --git a/lib/heartcheck/controllers/essential.rb b/lib/heartcheck/controllers/essential.rb index fe76a9a..ee89c97 100644 --- a/lib/heartcheck/controllers/essential.rb +++ b/lib/heartcheck/controllers/essential.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers class Essential < Base diff --git a/lib/heartcheck/controllers/functional.rb b/lib/heartcheck/controllers/functional.rb index fa3b625..cdd730f 100644 --- a/lib/heartcheck/controllers/functional.rb +++ b/lib/heartcheck/controllers/functional.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers class Functional < Base diff --git a/lib/heartcheck/controllers/health_check.rb b/lib/heartcheck/controllers/health_check.rb index 8780cb8..e7c3873 100644 --- a/lib/heartcheck/controllers/health_check.rb +++ b/lib/heartcheck/controllers/health_check.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers class HealthCheck < Base diff --git a/lib/heartcheck/controllers/info.rb b/lib/heartcheck/controllers/info.rb index 3080b19..e126ee7 100644 --- a/lib/heartcheck/controllers/info.rb +++ b/lib/heartcheck/controllers/info.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers class Info < Base diff --git a/lib/heartcheck/controllers/inspect.rb b/lib/heartcheck/controllers/inspect.rb index 1e130b1..646caea 100644 --- a/lib/heartcheck/controllers/inspect.rb +++ b/lib/heartcheck/controllers/inspect.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers class Inspect < Base diff --git a/lib/heartcheck/errors.rb b/lib/heartcheck/errors.rb index 065296d..ef850af 100644 --- a/lib/heartcheck/errors.rb +++ b/lib/heartcheck/errors.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + require 'heartcheck/errors/routing_error' require 'heartcheck/errors/warning' diff --git a/lib/heartcheck/errors/routing_error.rb b/lib/heartcheck/errors/routing_error.rb index 7c1de40..263f80e 100644 --- a/lib/heartcheck/errors/routing_error.rb +++ b/lib/heartcheck/errors/routing_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Errors class RoutingError < RuntimeError diff --git a/lib/heartcheck/errors/warning.rb b/lib/heartcheck/errors/warning.rb index 0fcf837..b0f47ed 100644 --- a/lib/heartcheck/errors/warning.rb +++ b/lib/heartcheck/errors/warning.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Errors class Warning < RuntimeError diff --git a/lib/heartcheck/executors.rb b/lib/heartcheck/executors.rb index ea35c93..d9e725c 100644 --- a/lib/heartcheck/executors.rb +++ b/lib/heartcheck/executors.rb @@ -1 +1,3 @@ +# frozen_string_literal: true + require 'heartcheck/executors/base' diff --git a/lib/heartcheck/executors/base.rb b/lib/heartcheck/executors/base.rb index c20b09e..cf51417 100644 --- a/lib/heartcheck/executors/base.rb +++ b/lib/heartcheck/executors/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Executors class Base diff --git a/lib/heartcheck/executors/threaded.rb b/lib/heartcheck/executors/threaded.rb index dad2c27..cff4035 100644 --- a/lib/heartcheck/executors/threaded.rb +++ b/lib/heartcheck/executors/threaded.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + module Heartcheck module Executors class Threaded < Heartcheck::Executors::Base def dispatch(checkers) checkers.collect do |checker| Concurrent::Future.execute { track_and_check(checker) } - end.collect { |future| future.value } + end.collect(&:value) end end end diff --git a/lib/heartcheck/formatters/base.rb b/lib/heartcheck/formatters/base.rb index 3e91388..b94d83c 100644 --- a/lib/heartcheck/formatters/base.rb +++ b/lib/heartcheck/formatters/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Formatters class Base diff --git a/lib/heartcheck/formatters/hash_response.rb b/lib/heartcheck/formatters/hash_response.rb index 950cfc2..3be9321 100644 --- a/lib/heartcheck/formatters/hash_response.rb +++ b/lib/heartcheck/formatters/hash_response.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Formatters class HashResponse diff --git a/lib/heartcheck/generators.rb b/lib/heartcheck/generators.rb index 5e094f4..0613db7 100644 --- a/lib/heartcheck/generators.rb +++ b/lib/heartcheck/generators.rb @@ -1 +1,3 @@ -require "heartcheck/generators/generator" +# frozen_string_literal: true + +require 'heartcheck/generators/generator' diff --git a/lib/heartcheck/generators/generator.rb b/lib/heartcheck/generators/generator.rb index 3a15eaa..4b3fec0 100644 --- a/lib/heartcheck/generators/generator.rb +++ b/lib/heartcheck/generators/generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'thor/group' module Heartcheck @@ -46,8 +48,9 @@ def generate_route '# map "/cached/monitoring" do', '# use Heartcheck::CachingApp, 300 # 300 is the optional ttl', '# use Heartcheck::App', - '# end', - ].join("\n")) + '# end' + ].join("\n") + ) end end diff --git a/lib/heartcheck/generators/templates/config.rb b/lib/heartcheck/generators/templates/config.rb index 7a46dd9..c4bd07c 100644 --- a/lib/heartcheck/generators/templates/config.rb +++ b/lib/heartcheck/generators/templates/config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Heartcheck.setup do |monitor| # monitor has a default logger but you can use yours # default value: Logger.new(STDOUT) @@ -61,27 +63,26 @@ # end # end - # Firewall # check with telnet if the firewall is open to conect to the service # monitor.add :firewall do |c| - # you can add service wth (host and port) or (url) - # c.add_service(host: 'example.com', port: 80) - # c.add_service(url: 'http://example.com') + # you can add service wth (host and port) or (url) + # c.add_service(host: 'example.com', port: 80) + # c.add_service(url: 'http://example.com') - # add dynamic services when you need it run after initializer - # c.register_dynamic_services do - # Pmta.all.map { |p| { host: p.host, port: 25 } } - # end + # add dynamic services when you need it run after initializer + # c.register_dynamic_services do + # Pmta.all.map { |p| { host: p.host, port: 25 } } + # end - # to customize default error message, the default is: - # "connection refused on: #{service.host}:#{service.port}" - # params - # errors: Array => errors to show on page - # service: FirewallService => object that respond to :host and :port - # c.on_error do |errors, service| - # errors << "Custom error message for #{service.host}" - # end + # to customize default error message, the default is: + # "connection refused on: #{service.host}:#{service.port}" + # params + # errors: Array => errors to show on page + # service: FirewallService => object that respond to :host and :port + # c.on_error do |errors, service| + # errors << "Custom error message for #{service.host}" + # end # end # Redis @@ -104,6 +105,6 @@ # WatchFile # check if a preloaded JSON file as changed from the disk # monitor.add :watch_file do |c| - # c.add_service(file: 'package.json') + # c.add_service(file: 'package.json') # end end diff --git a/lib/heartcheck/logger.rb b/lib/heartcheck/logger.rb index 3469737..33780f8 100644 --- a/lib/heartcheck/logger.rb +++ b/lib/heartcheck/logger.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck # A simple Interface to log messages class Logger @@ -37,8 +39,6 @@ def self.error(message) logger(:error, message) end - private - # Sent the message to Heartcheck logger # that you can configure # diff --git a/lib/heartcheck/services.rb b/lib/heartcheck/services.rb index 559ace9..ef96649 100644 --- a/lib/heartcheck/services.rb +++ b/lib/heartcheck/services.rb @@ -1 +1,3 @@ +# frozen_string_literal: true + require 'heartcheck/services/firewall' diff --git a/lib/heartcheck/services/firewall.rb b/lib/heartcheck/services/firewall.rb index cc4e0fd..3150bee 100644 --- a/lib/heartcheck/services/firewall.rb +++ b/lib/heartcheck/services/firewall.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'net/telnet' module Heartcheck diff --git a/lib/heartcheck/version.rb b/lib/heartcheck/version.rb index 237e393..47af400 100644 --- a/lib/heartcheck/version.rb +++ b/lib/heartcheck/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck - VERSION = '2.3.0'.freeze + VERSION = '2.3.0' end diff --git a/spec/lib/heartcheck/app_spec.rb b/spec/lib/heartcheck/app_spec.rb index 197a48e..eeaf799 100644 --- a/spec/lib/heartcheck/app_spec.rb +++ b/spec/lib/heartcheck/app_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'rack/test' @@ -40,11 +42,11 @@ context 'on GET /dev' do before do Heartcheck.setup do |monitor| - log = Logger.new(STDOUT) + log = Logger.new($stdout) log.level = 4 monitor.logger = log monitor.add :base do |c| - c.name = "check" + c.name = 'check' end end get '/dev' diff --git a/spec/lib/heartcheck/caching_app/cache_spec.rb b/spec/lib/heartcheck/caching_app/cache_spec.rb index 34340ef..1c9bfbb 100644 --- a/spec/lib/heartcheck/caching_app/cache_spec.rb +++ b/spec/lib/heartcheck/caching_app/cache_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'heartcheck/caching_app/cache' require 'concurrent' @@ -27,7 +29,7 @@ def index end def wait_for_one_execution - Concurrent::ScheduledTask.execute(ttl) { "waiting the ttl" }.value! + Concurrent::ScheduledTask.execute(ttl) { 'waiting the ttl' }.value! end subject do @@ -36,12 +38,12 @@ def wait_for_one_execution before do # use a blocking executor, no concurrency - subject.concurrent_opts = {executor: Concurrent::ImmediateExecutor.new} + subject.concurrent_opts = { executor: Concurrent::ImmediateExecutor.new } end describe '#result' do it 'returns a blank result on first dispatch' do - subject.concurrent_opts = {executor: Concurrent::SingleThreadExecutor.new} + subject.concurrent_opts = { executor: Concurrent::SingleThreadExecutor.new } subject.start expect(subject.result(controllers.first)).to be_nil diff --git a/spec/lib/heartcheck/caching_app_spec.rb b/spec/lib/heartcheck/caching_app_spec.rb index fc69a2b..89f4942 100644 --- a/spec/lib/heartcheck/caching_app_spec.rb +++ b/spec/lib/heartcheck/caching_app_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rack/test' require 'heartcheck/caching_app' @@ -10,7 +12,7 @@ module Heartcheck let(:cache) { double(Heartcheck::CachingApp::Cache) } let(:super_app) { double(Heartcheck::App) } let(:controller) { Heartcheck::Controllers::Essential } - let(:response) { [200, { 'Content-type' => 'application/json' }, ["[]"]] } + let(:response) { [200, { 'Content-type' => 'application/json' }, ['[]']] } before do allow(super_app).to receive(:call).with(anything).and_return(response) @@ -22,7 +24,7 @@ module Heartcheck get '/' - expect(last_response.body).to eq("[]") + expect(last_response.body).to eq('[]') expect(super_app).to have_received(:call) end @@ -63,7 +65,7 @@ module Heartcheck it 'forwards to the original app' do get '/not-found' - expect(last_response.body).to eq("[]") + expect(last_response.body).to eq('[]') expect(super_app).to have_received(:call) end diff --git a/spec/lib/heartcheck/checks/base_spec.rb b/spec/lib/heartcheck/checks/base_spec.rb index 97d654e..841ad0f 100644 --- a/spec/lib/heartcheck/checks/base_spec.rb +++ b/spec/lib/heartcheck/checks/base_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Checks::Base do diff --git a/spec/lib/heartcheck/checks/firewall_spec.rb b/spec/lib/heartcheck/checks/firewall_spec.rb index 20c6e8f..9fbe76d 100644 --- a/spec/lib/heartcheck/checks/firewall_spec.rb +++ b/spec/lib/heartcheck/checks/firewall_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Checks::Firewall do @@ -81,8 +83,10 @@ end it 'calls Net::Telnet with valid params of proxy' do - expect(Net::Telnet).to receive(:new).with('Port' => 8888, 'Host' => 'uriproxy.com.br', 'Timeout' => 2).ordered.and_return('proxy') - expect(Net::Telnet).to receive(:new).with('Port' => 443, 'Host' => 'lala.com', 'Timeout' => 2, 'Proxy' => 'proxy').ordered + expect(Net::Telnet).to receive(:new).with('Port' => 8888, 'Host' => 'uriproxy.com.br', + 'Timeout' => 2).ordered.and_return('proxy') + expect(Net::Telnet).to receive(:new).with('Port' => 443, 'Host' => 'lala.com', 'Timeout' => 2, + 'Proxy' => 'proxy').ordered subject.validate end diff --git a/spec/lib/heartcheck/checks/process_spec.rb b/spec/lib/heartcheck/checks/process_spec.rb index 8aecd36..e5fb7d2 100644 --- a/spec/lib/heartcheck/checks/process_spec.rb +++ b/spec/lib/heartcheck/checks/process_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Checks::Process do diff --git a/spec/lib/heartcheck/checks/watch_file_spec.rb b/spec/lib/heartcheck/checks/watch_file_spec.rb index c4c14eb..9209e22 100644 --- a/spec/lib/heartcheck/checks/watch_file_spec.rb +++ b/spec/lib/heartcheck/checks/watch_file_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe Heartcheck::Checks::WatchFile do subject do described_class.new.tap do |config| @@ -15,10 +17,10 @@ context 'the watched file have new modifications' do it 'array erros should not be empty' do - allow(subject).to receive(:installed).and_return({bacon: true}) - expect { subject.validate }. - to change { subject.instance_variable_get(:@errors).size }. - from(0).to(1) + allow(subject).to receive(:installed).and_return({ bacon: true }) + expect { subject.validate } + .to change { subject.instance_variable_get(:@errors).size } + .from(0).to(1) end end end diff --git a/spec/lib/heartcheck/controllers/dev_spec.rb b/spec/lib/heartcheck/controllers/dev_spec.rb index 3ca9173..c840717 100644 --- a/spec/lib/heartcheck/controllers/dev_spec.rb +++ b/spec/lib/heartcheck/controllers/dev_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers describe Dev do @@ -8,15 +10,15 @@ module Controllers let(:total_time) { { total_execution_time: '0.00 ms' } } - let(:check_01) do + let(:check1) do { dummy1: { status: :ok }, execution_time: '0.00 ms' } end - let(:check_02) do + let(:check2) do { dummy2: { status: :ok }, execution_time: '0.00 ms' } end - let(:result) { [check_01, check_02, total_time] } + let(:result) { [check1, check2, total_time] } before do allow(Time).to receive(:now).and_return(Time.now) diff --git a/spec/lib/heartcheck/controllers/environment_spec.rb b/spec/lib/heartcheck/controllers/environment_spec.rb index 7fddc43..9837a51 100644 --- a/spec/lib/heartcheck/controllers/environment_spec.rb +++ b/spec/lib/heartcheck/controllers/environment_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers describe Environment do @@ -11,7 +13,7 @@ module Controllers let(:system_info_example) { Struct.new(:example_key).new('example_value') } - context "given Rails is used" do + context 'given Rails is used' do before do stub_const('Rails::VERSION::STRING', '5.0.0') end @@ -19,9 +21,9 @@ module Controllers it 'gets info from the right constants and functions' do allow(Sys::Uname).to receive(:uname).and_return(system_info_example) is_expected.to include( - :system_info => { :example_key => 'example_value' }, - :ruby_version => '2.4.0', - :rails_version => '5.0.0' + system_info: { example_key: 'example_value' }, + ruby_version: '2.4.0', + rails_version: '5.0.0' ) expect(Sys::Uname).to have_received(:uname) end @@ -59,13 +61,13 @@ module Controllers end end - context "given Rails is not used" do + context 'given Rails is not used' do it 'gets the info indicating Rails is not used' do allow(Sys::Uname).to receive(:uname).and_return(system_info_example) is_expected.to include( - :system_info => { :example_key => 'example_value' }, - :ruby_version => '2.4.0', - :rails_version => '(none)' + system_info: { example_key: 'example_value' }, + ruby_version: '2.4.0', + rails_version: '(none)' ) expect(Sys::Uname).to have_received(:uname) end diff --git a/spec/lib/heartcheck/controllers/essential_spec.rb b/spec/lib/heartcheck/controllers/essential_spec.rb index 39afe14..3fe3a9f 100644 --- a/spec/lib/heartcheck/controllers/essential_spec.rb +++ b/spec/lib/heartcheck/controllers/essential_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers describe Essential do @@ -6,15 +8,15 @@ module Controllers describe '#index' do subject(:index) { controller.index } - let(:check_01) { { dummy1: { status: :ok }, time: 1100 } } - let(:check_02) { { dummy2: { status: :ok }, time: 100 } } + let(:check1) { { dummy1: { status: :ok }, time: 1100 } } + let(:check2) { { dummy2: { status: :ok }, time: 100 } } before do expect(Time).to receive(:now).and_return( - # millisec time calc :p - 0.1, 1.2, # (1.2 - 0.1) * 1000.0 = 1100 - 2.9, 3.0 # (3.0 - 2.9) * 1000.0 = 100 - ) + # millisec time calc :p + 0.1, 1.2, # (1.2 - 0.1) * 1000.0 = 1100 + 2.9, 3.0 # (3.0 - 2.9) * 1000.0 = 100 + ) Heartcheck.setup do |monitor| monitor.add :dummy1 do |c| @@ -26,7 +28,7 @@ module Controllers end end - it { is_expected.to eq(MultiJson.dump([check_01, check_02])) } + it { is_expected.to eq(MultiJson.dump([check1, check2])) } end end end diff --git a/spec/lib/heartcheck/controllers/functional_spec.rb b/spec/lib/heartcheck/controllers/functional_spec.rb index a4ae329..04a8004 100644 --- a/spec/lib/heartcheck/controllers/functional_spec.rb +++ b/spec/lib/heartcheck/controllers/functional_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers describe Functional do @@ -6,15 +8,15 @@ module Controllers describe '#index' do subject(:index) { controller.index } - let(:check_01) { { dummy1: { status: :ok } , time: 1100 } } - let(:check_02) { { dummy2: { status: :ok } , time: 100 } } + let(:check1) { { dummy1: { status: :ok }, time: 1100 } } + let(:check2) { { dummy2: { status: :ok }, time: 100 } } before do expect(Time).to receive(:now).and_return( - # millisec time calc :p - 0.1, 1.2, # (1.2 - 0.1) * 1000.0 = 1100 - 2.9, 3.0 # (3.0 - 2.9) * 1000.0 = 100 - ) + # millisec time calc :p + 0.1, 1.2, # (1.2 - 0.1) * 1000.0 = 1100 + 2.9, 3.0 # (3.0 - 2.9) * 1000.0 = 100 + ) Heartcheck.setup do |monitor| monitor.add :dummy1 do |c| @@ -29,7 +31,7 @@ module Controllers end end - it { is_expected.to eq(MultiJson.dump([check_01, check_02])) } + it { is_expected.to eq(MultiJson.dump([check1, check2])) } end end end diff --git a/spec/lib/heartcheck/controllers/health_check_spec.rb b/spec/lib/heartcheck/controllers/health_check_spec.rb index 5cac3b9..cd02344 100644 --- a/spec/lib/heartcheck/controllers/health_check_spec.rb +++ b/spec/lib/heartcheck/controllers/health_check_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers describe HealthCheck do diff --git a/spec/lib/heartcheck/controllers/info_spec.rb b/spec/lib/heartcheck/controllers/info_spec.rb index 4e8741f..23c7820 100644 --- a/spec/lib/heartcheck/controllers/info_spec.rb +++ b/spec/lib/heartcheck/controllers/info_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Controllers describe Info do @@ -6,8 +8,8 @@ module Controllers describe '#index' do subject(:index) { controller.index } - let(:info_01) { 'dummy1' } - let(:info_02) { 'dummy2' } + let(:info1) { 'dummy1' } + let(:info2) { 'dummy2' } before do Heartcheck.setup do |monitor| @@ -21,7 +23,7 @@ module Controllers end end - it { is_expected.to eq(MultiJson.dump([info_01, info_02])) } + it { is_expected.to eq(MultiJson.dump([info1, info2])) } end end end diff --git a/spec/lib/heartcheck/executors/base_spec.rb b/spec/lib/heartcheck/executors/base_spec.rb index 5862396..4149dbf 100644 --- a/spec/lib/heartcheck/executors/base_spec.rb +++ b/spec/lib/heartcheck/executors/base_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Heartcheck module Executors describe Base do @@ -7,7 +9,7 @@ module Executors let(:registered) { 3 } let(:checkers) do - registered.times.collect do |current| + registered.times.collect do |_current| double.tap do |checker| expect(checker).to receive(:check).and_return({ current: 'ok' }) end @@ -19,13 +21,13 @@ module Executors subject.dispatch(checkers) end - it "should have a :time key in the checker response" do + it 'should have a :time key in the checker response' do subject.dispatch(checkers).each do |current| expect(current).to include(:time) end end - it "should have a float value (time key)" do + it 'should have a float value (time key)' do subject.dispatch(checkers).each do |current| expect(current[:time]).to be_a(Float) end diff --git a/spec/lib/heartcheck/executors/threaded_spec.rb b/spec/lib/heartcheck/executors/threaded_spec.rb index 527da3b..ac54ccd 100644 --- a/spec/lib/heartcheck/executors/threaded_spec.rb +++ b/spec/lib/heartcheck/executors/threaded_spec.rb @@ -1,12 +1,12 @@ +# frozen_string_literal: true + require 'heartcheck/executors/threaded' module Heartcheck module Executors describe Threaded do subject do - Heartcheck.setup do |monitor| - monitor.use_threaded_executor! - end + Heartcheck.setup(&:use_threaded_executor!) Heartcheck.executor end @@ -15,7 +15,7 @@ module Executors let(:registered) { 3 } let(:checkers) do - registered.times.collect do |current| + registered.times.collect do |_current| double.tap do |checker| expect(checker).to receive(:check).and_return({ current: 'ok' }) end @@ -27,13 +27,13 @@ module Executors subject.dispatch(checkers) end - it "should have a :time key in the checker response" do + it 'should have a :time key in the checker response' do subject.dispatch(checkers).each do |current| expect(current).to include(:time) end end - it "should have a float value (time key)" do + it 'should have a float value (time key)' do subject.dispatch(checkers).each do |current| expect(current[:time]).to be_a(Float) end diff --git a/spec/lib/heartcheck/formatters/base_spec.rb b/spec/lib/heartcheck/formatters/base_spec.rb index b46a05a..e1e5e04 100644 --- a/spec/lib/heartcheck/formatters/base_spec.rb +++ b/spec/lib/heartcheck/formatters/base_spec.rb @@ -1,15 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Formatters::Base do - describe "#format" do - let(:check_01) { {dummy1: {status: :ok}, time: 1100} } - let(:check_02) { {dummy2: {status: :ok}, time: 100} } + describe '#format' do + let(:check1) { { dummy1: { status: :ok }, time: 1100 } } + let(:check2) { { dummy2: { status: :ok }, time: 100 } } - it "generates a list of with a hashes for every check" do - expect(subject.format([check_01, check_02])).to be_eql([ - {dummy1: {status: :ok}, time: 1100}, - {dummy2: {status: :ok}, time: 100} - ]) + it 'generates a list of with a hashes for every check' do + expect(subject.format([check1, check2])).to be_eql( + [ + { dummy1: { status: :ok }, time: 1100 }, + { dummy2: { status: :ok }, time: 100 } + ] + ) end end end diff --git a/spec/lib/heartcheck/formatters/hash_response_spec.rb b/spec/lib/heartcheck/formatters/hash_response_spec.rb index 227e672..b0cf2ee 100644 --- a/spec/lib/heartcheck/formatters/hash_response_spec.rb +++ b/spec/lib/heartcheck/formatters/hash_response_spec.rb @@ -1,15 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Formatters::HashResponse do - describe "#format" do - let(:check_01) { {dummy1: {status: :ok}, time: 1100} } - let(:check_02) { {dummy2: {status: :ok}, time: 100} } + describe '#format' do + let(:check1) { { dummy1: { status: :ok }, time: 1100 } } + let(:check2) { { dummy2: { status: :ok }, time: 100 } } - it "generates a single hash with every check as a key" do - expect(subject.format([check_01, check_02])).to be_eql({ - dummy1: {status: :ok, time: 1100}, - dummy2: {status: :ok, time: 100} - }) + it 'generates a single hash with every check as a key' do + expect(subject.format([check1, check2])).to be_eql( + { + dummy1: { status: :ok, time: 1100 }, + dummy2: { status: :ok, time: 100 } + } + ) end end end diff --git a/spec/lib/heartcheck/logger_spec.rb b/spec/lib/heartcheck/logger_spec.rb index f528be6..b478ac3 100644 --- a/spec/lib/heartcheck/logger_spec.rb +++ b/spec/lib/heartcheck/logger_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Logger do diff --git a/spec/lib/heartcheck/services/firewall_spec.rb b/spec/lib/heartcheck/services/firewall_spec.rb index 9d3b311..7104bfc 100644 --- a/spec/lib/heartcheck/services/firewall_spec.rb +++ b/spec/lib/heartcheck/services/firewall_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Heartcheck::Services::Firewall do diff --git a/spec/lib/heartcheck_spec.rb b/spec/lib/heartcheck_spec.rb index 39b9b25..0328abf 100644 --- a/spec/lib/heartcheck_spec.rb +++ b/spec/lib/heartcheck_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe Heartcheck do let(:essential) { Heartcheck::Checks::Base.new } let(:functional) { Heartcheck::Checks::Base.new.tap { |c| c.functional = true } } @@ -96,7 +98,7 @@ let(:name) { :process } let(:plugin) { Heartcheck::Checks::Process.new } - let(:blk) { lambda { |_| } } + let(:blk) { ->(_) {} } before do allow(Heartcheck::Checks::Process).to receive(:new) @@ -116,7 +118,7 @@ ) end - let(:blk) { lambda { |c| c.this_is_terrible } } + let(:blk) { ->(c) { c.this_is_terrible } } it 'instantiates the class passing the given block' do expect(plugin).to receive(:this_is_terrible) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 33bb951..7e0b7e1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require 'heartcheck' require 'pry' require 'oj' -Dir['./spec/support/**/*.rb'].each{ |helper_file| require helper_file } +Dir['./spec/support/**/*.rb'].sort.each { |helper_file| require helper_file } Oj.default_options = { mode: :compat } diff --git a/spec/support/checks/dummy1.rb b/spec/support/checks/dummy1.rb index a2a75b5..41d4b2f 100644 --- a/spec/support/checks/dummy1.rb +++ b/spec/support/checks/dummy1.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + module Heartcheck module Checks class Dummy1 < Base - def validate - end + def validate; end def info 'dummy1' diff --git a/spec/support/checks/dummy2.rb b/spec/support/checks/dummy2.rb index 3e483a2..4647115 100644 --- a/spec/support/checks/dummy2.rb +++ b/spec/support/checks/dummy2.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + module Heartcheck module Checks class Dummy2 < Base - def validate - end + def validate; end def info 'dummy2' From c516151dd735ce5ad2e1b68859dfc88037b58295 Mon Sep 17 00:00:00 2001 From: Rajan Joshi Date: Fri, 1 Oct 2021 20:38:45 +0100 Subject: [PATCH 2/2] Add target ruby version in rubocop. --- .rubocop.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.rubocop.yml b/.rubocop.yml index fdb3ee3..9e2b76b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,6 +2,7 @@ inherit_from: .rubocop_todo.yml AllCops: NewCops: enable + TargetRubyVersion: 2.7 SuggestExtensions: false Style/Documentation: