Skip to content

Commit

Permalink
Merge pull request #48 from razanjoshi/increase_rubocop_coverages
Browse files Browse the repository at this point in the history
Increase Rubocop coverage
  • Loading branch information
TecnoSigma authored Oct 4, 2021
2 parents fc74540 + 93f0bae commit 5f49809
Show file tree
Hide file tree
Showing 58 changed files with 302 additions and 144 deletions.
6 changes: 4 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
inherit_from: .rubocop_todo.yml

AllCops:
NewCops: enable
Include:
- '**/Rakefile'
TargetRubyVersion: 2.7
SuggestExtensions: false

Style/Documentation:
Enabled: false
Expand Down
53 changes: 53 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in heartcheck.gemspec
Expand Down
20 changes: 11 additions & 9 deletions lib/heartcheck.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
require 'logger'
require 'heartcheck/app'
Expand Down Expand Up @@ -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
Expand All @@ -90,7 +92,7 @@ def functional_checks
#
# @return [Array<Check>] checks that are not functional
def dev_checks
checks.select { |ctx| !ctx.functional? }
checks.reject(&:functional?)
end

# filter checks that has some information
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/heartcheck/app.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +20,7 @@ class App
'/inspect' => Controllers::Inspect,
'/health_check' => Controllers::HealthCheck,
'/environment' => Controllers::Environment
}
}.freeze

# Sets up the rack application.
#
Expand Down Expand Up @@ -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}"

Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/caching_app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'heartcheck/caching_app/cache'

# A rack middleware to wrap around {Heartcheck::App} in a cache
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/caching_app/cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'concurrent'

module Heartcheck
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/checks.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'heartcheck/checks/base'
require 'heartcheck/checks/firewall'
require 'heartcheck/checks/process'
Expand Down
15 changes: 8 additions & 7 deletions lib/heartcheck/checks/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Checks
# Base check that contains the common functionality for chechs
Expand All @@ -9,15 +11,15 @@ 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.
# For example you can execute some performance check.
#
# @return [Boolean] (default: false)
attr_accessor :dev
alias_method :dev?, :dev
alias dev? dev

# The name for identify the check in the result page
#
Expand Down Expand Up @@ -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<Hash>]
Expand Down Expand Up @@ -143,7 +144,7 @@ def uri_info

def informations
info
rescue => e
rescue StandardError => e
{ 'error' => e.message }
end

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/heartcheck/checks/firewall.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Checks
class Firewall < Base
Expand All @@ -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

Expand Down
18 changes: 7 additions & 11 deletions lib/heartcheck/checks/process.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/heartcheck/checks/watch_file.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'

module Heartcheck
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/heartcheck/controllers/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rack'
require 'multi_json'

Expand All @@ -7,7 +9,7 @@ module Heartcheck
module Controllers
class Base
def index
fail NotImplementError
raise NotImplementError
end

protected
Expand All @@ -18,6 +20,7 @@ def check(who)
end

private

def formatter
Heartcheck.formatter
end
Expand Down
4 changes: 3 additions & 1 deletion lib/heartcheck/controllers/dev.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Controllers
class Dev < Base
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/heartcheck/controllers/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'sys-uname'

module Heartcheck
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/controllers/essential.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Controllers
class Essential < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/controllers/functional.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Controllers
class Functional < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/controllers/health_check.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Controllers
class HealthCheck < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/controllers/info.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Controllers
class Info < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/controllers/inspect.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Controllers
class Inspect < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/heartcheck/errors.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

require 'heartcheck/errors/routing_error'
require 'heartcheck/errors/warning'
2 changes: 2 additions & 0 deletions lib/heartcheck/errors/routing_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Heartcheck
module Errors
class RoutingError < RuntimeError
Expand Down
Loading

0 comments on commit 5f49809

Please sign in to comment.