lib/reek/configuration/configuration_file_finder.rb in reek-4.8.2 vs lib/reek/configuration/configuration_file_finder.rb in reek-5.0.0

- old
+ new

@@ -1,37 +1,30 @@ # frozen_string_literal: true require 'pathname' +require_relative './configuration_converter' +require_relative './schema_validator' +require_relative '../errors/config_file_error' module Reek module Configuration - # Raised when config file is not properly readable. - class ConfigFileException < StandardError; end # # ConfigurationFileFinder is responsible for finding Reek's configuration. # # There are 3 ways of passing `reek` a configuration file: # 1. Using the cli "-c" switch - # 2. Having a file ending with .reek either in your current working + # 2. Having a file .reek.yml either in your current working # directory or in a parent directory - # 3. Having a file ending with .reek in your HOME directory + # 3. Having a file .reek.yml in your HOME directory # # The order in which ConfigurationFileFinder tries to find such a # configuration file is exactly like above. module ConfigurationFileFinder - TOO_MANY_CONFIGURATION_FILES_MESSAGE = <<-MESSAGE.freeze + DEFAULT_FILE_NAME = '.reek.yml' - Error: Found multiple configuration files %<files>s - while scanning directory %<directory>s. - - Reek supports only one configuration file. You have 2 options now: - 1) Remove all offending files. - 2) Be specific about which one you want to load via the -c switch. - - MESSAGE - class << self + include ConfigurationValidator # # Finds and loads a configuration file from a given path. # # @return [Hash] # @@ -45,11 +38,11 @@ # * ascending down from the current directory # * looking into the home directory # # @return [File|nil] # - # :reek:ControlParameter + # @quality :reek:ControlParameter def find(path: nil, current: Pathname.pwd, home: Pathname.new(Dir.home)) path || find_by_dir(current) || find_in_dir(home) end # @@ -57,23 +50,22 @@ # Raises on invalid data. # # @param path [String] # @return [Hash] # - # :reek:TooManyStatements: { max_statements: 6 } + # @quality :reek:TooManyStatements { max_statements: 6 } def load_from_file(path) return {} unless path + begin configuration = YAML.load_file(path) || {} rescue StandardError => error - raise ConfigFileException, "Invalid configuration file #{path}, error is #{error}" + raise Errors::ConfigFileException, "Invalid configuration file #{path}, error is #{error}" end - unless configuration.is_a? Hash - raise ConfigFileException, "Invalid configuration file \"#{path}\" -- Not a hash" - end - configuration + SchemaValidator.new(configuration).validate + ConfigurationConverter.new(configuration).convert end private # @@ -88,32 +80,15 @@ end end # # Checks a given directory for a configuration file and returns it. - # Raises an exception if we find more than one. # # @return [File|nil] # - # :reek:FeatureEnvy + # @quality :reek:FeatureEnvy def find_in_dir(dir) - found = dir.children.select { |item| item.file? && item.to_s.end_with?('.reek') }.sort - if found.size > 1 - escalate_too_many_configuration_files found, dir - else - found.first - end - end - - # - # Writes a proper warning message to STDERR and then exits the program. - # - # @return [undefined] - # - def escalate_too_many_configuration_files(found, directory) - offensive_files = found.map { |file| "'#{file.basename}'" }.join(', ') - warn format(TOO_MANY_CONFIGURATION_FILES_MESSAGE, files: offensive_files, directory: directory) - exit 1 + dir.children.detect { |item| item.file? && item.basename.to_s == DEFAULT_FILE_NAME } end end end end end