Commit f182ea4e authored by Gabriel Mazetto's avatar Gabriel Mazetto

Some code-style fixes and documentation

parent 500e5227
# Library to perform System Checks
#
# Every Check is implemented as its own class inherited from SystemCheck::BaseCheck
# Execution coordination and boilerplate output is done by the SystemCheck::SimpleExecutor
#
# This structure decouples checks from Rake tasks and facilitates unit-testing
module SystemCheck module SystemCheck
def self.run(component, checks = {}, executor_klass = SimpleExecutor) # Executes a bunch of checks for specified component
#
# @param [String] component name of the component relative to the checks being executed
# @param [Array<BaseCheck>] checks classes of corresponding checks to be executed in the same order
# @param [BaseExecutor] executor_klass optionally specifiy a different executor class
def self.run(component, checks = [], executor_klass = SimpleExecutor)
unless executor_klass.is_a? BaseExecutor unless executor_klass.is_a? BaseExecutor
raise ArgumentError, 'Invalid executor' raise ArgumentError, 'Invalid executor'
end end
executor = executor_klass.new(component) executor = executor_klass.new(component)
executor.checks = checks.map do |check| checks.each do |check|
raise ArgumentError unless check.is_a? BaseCheck executor << check
end end
end end
end end
module SystemCheck module SystemCheck
# Base class for Checks. You must inherit from here
# and implement the methods below when necessary
class BaseCheck class BaseCheck
# This is where you should implement the main logic that will return
# a boolean at the end
#
# You should not print any output to STDOUT here, use the specific methods instead
#
# @return [Boolean] whether the check passed or not
def check? def check?
raise NotImplementedError raise NotImplementedError
end end
# This is where you should print detailed information for any error found during #check?
#
# You may use helper methods to help format the output:
#
# @see #try_fixing_it
# @see #fix_and_rerun
# @see #for_more_infromation
def show_error def show_error
raise NotImplementedError raise NotImplementedError
end end
# If skip returns true, than no other method on this check will be executed
#
# @return [Boolean] whether or not this check should be skipped
def skip? def skip?
false false
end end
# If you enabled #skip? here is where you define a custom message explaining why
#
# Do not print anything to STDOUT, return a string.
#
# @return [String] message why this check was skipped
def skip_message def skip_message
end end
protected protected
# Display a formatted list of instructions on how to fix the issue identified by the #check?
#
# @param [Array<String>] steps one or short sentences with help how to fix the issue
def try_fixing_it(*steps) def try_fixing_it(*steps)
steps = steps.shift if steps.first.is_a?(Array) steps = steps.shift if steps.first.is_a?(Array)
puts ' Try fixing it:'.color(:blue) $stdout.puts ' Try fixing it:'.color(:blue)
steps.each do |step| steps.each do |step|
puts " #{step}" $stdout.puts " #{step}"
end end
end end
# Display a message telling to fix and rerun the checks
def fix_and_rerun def fix_and_rerun
puts ' Please fix the error above and rerun the checks.'.color(:red) $stdout.puts ' Please fix the error above and rerun the checks.'.color(:red)
end end
# Display a formatted list of references (documentation or links) where to find more information
#
# @param [Array<String>] sources one or more references (documentation or links)
def for_more_information(*sources) def for_more_information(*sources)
sources = sources.shift if sources.first.is_a?(Array) sources = sources.shift if sources.first.is_a?(Array)
puts ' For more information see:'.color(:blue) $stdout.puts ' For more information see:'.color(:blue)
sources.each do |source| sources.each do |source|
puts ' #{source}' $stdout.puts ' #{source}'
end end
end end
end end
......
module SystemCheck module SystemCheck
# @attr_reader [Array<BaseCheck>] checks classes of corresponding checks to be executed in the same order
# @attr_reader [String] component name of the component relative to the checks being executed
class BaseExecutor class BaseExecutor
attr_reader :checks attr_reader :checks
attr_reader :component attr_reader :component
# @param [String] component name of the component relative to the checks being executed
def initialize(component) def initialize(component)
raise ArgumentError unless component.is_a? String raise ArgumentError unless component.is_a? String
...@@ -10,6 +13,9 @@ module SystemCheck ...@@ -10,6 +13,9 @@ module SystemCheck
@checks = Set.new @checks = Set.new
end end
# Add a check to be executed
#
# @param [BaseCheck] check class
def <<(check) def <<(check)
raise ArgumentError unless check.is_a? BaseCheck raise ArgumentError unless check.is_a? BaseCheck
@checks << check @checks << check
......
module SystemCheck module SystemCheck
# Simple Executor is current default executor for GitLab
# It is a simple port from display logic in the old check.rake
#
# There is no concurrency level and the output is progressively
# printed into the STDOUT
class SimpleExecutor < BaseExecutor class SimpleExecutor < BaseExecutor
# Executes defined checks in the specified order and outputs confirmation or error information
def execute def execute
start_checking(component) start_checking(component)
@checks.each do |check| @checks.each do |check|
print "#{check.name}" $stdout.print "#{check.name}"
if check.skip? if check.skip?
puts "skipped #{'('+skip_message+')' if skip_message}".color(:magenta) $stdout.puts "skipped #{'(' + skip_message + ')' if skip_message}".color(:magenta)
elsif check.check? elsif check.check?
puts 'yes'.color(:green) $stdout.puts 'yes'.color(:green)
else else
puts 'no'.color(:red) $stdout.puts 'no'.color(:red)
check.show_error check.show_error
end end
end end
...@@ -20,15 +26,21 @@ module SystemCheck ...@@ -20,15 +26,21 @@ module SystemCheck
private private
# Prints header content for the series of checks to be executed for this component
#
# @param [String] component name of the component relative to the checks being executed
def start_checking(component) def start_checking(component)
puts "Checking #{component.color(:yellow)} ..." $stdout.puts "Checking #{component.color(:yellow)} ..."
puts '' $stdout.puts ''
end end
# Prints footer content for the series of checks executed for this component
#
# @param [String] component name of the component relative to the checks being executed
def finished_checking(component) def finished_checking(component)
puts '' $stdout.puts ''
puts "Checking #{component.color(:yellow)} ... #{"Finished".color(:green)}" $stdout.puts "Checking #{component.color(:yellow)} ... #{'Finished'.color(:green)}"
puts '' $stdout.puts ''
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment