From 764506c794dee8740058d8a1dda6b12c594beaf1 Mon Sep 17 00:00:00 2001 From: Alex Malkov Date: Thu, 18 Aug 2016 13:30:37 +0100 Subject: [PATCH] Add config file loader for the gem. Keep default config in the config/default.yml --- config/default.yml | 29 +++++++++ lib/sapience.rb | 2 +- lib/sapience/config_loader.rb | 68 ++++++++++++++++++++ lib/sapience/sapience.rb | 6 +- spec/lib/sapience/config_loader_spec.rb | 83 +++++++++++++++++++++++++ spec/spec_helper.rb | 1 + spec/support/file_helper.rb | 28 +++++++++ 7 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 config/default.yml create mode 100644 lib/sapience/config_loader.rb create mode 100644 spec/lib/sapience/config_loader_spec.rb create mode 100644 spec/support/file_helper.rb diff --git a/config/default.yml b/config/default.yml new file mode 100644 index 0000000..5377b6c --- /dev/null +++ b/config/default.yml @@ -0,0 +1,29 @@ +--- +defaults: &defaults + log_level: info + appenders: + - file: + file_name: log/development.log + formatter: color + +test: + log_level: warn + appenders: + - file: + file_name: log/test.log + formatter: color + +development: + log_level: debug + appenders: + - file: + file_name: log/development.log + formatter: color + +production: + log_level: warn + appenders: + - file: + file_name: log/production.log + formatter: json + diff --git a/lib/sapience.rb b/lib/sapience.rb index 77f8eac..b7bb73d 100644 --- a/lib/sapience.rb +++ b/lib/sapience.rb @@ -11,8 +11,8 @@ require "sapience/formatters/color" require "sapience/formatters/json" +require "sapience/config_loader" require "sapience/configuration" -require "sapience/configuration/rails" require "sapience/configuration/grape" require "sapience/ansi_colors" require "sapience/core_ext/hash" diff --git a/lib/sapience/config_loader.rb b/lib/sapience/config_loader.rb new file mode 100644 index 0000000..e1de905 --- /dev/null +++ b/lib/sapience/config_loader.rb @@ -0,0 +1,68 @@ +# encoding: utf-8 +# frozen_string_literal: true + +require "yaml" +require "pathname" + +module Sapience + # This class represents the configuration of the RuboCop application + # and all its cops. A Config is associated with a YAML configuration + # file from which it was read. Several different Configs can be used + # during a run of the sapience program, if files in several + # directories are inspected. + module ConfigLoader + SAPIENCE_FILE = "sapience.yml".freeze + SAPIENCE_HOME = File.realpath(File.join(File.dirname(__FILE__), "..", "..")) + DEFAULT_FILE = File.join(SAPIENCE_HOME, "config", "default.yml") + + def self.load_from_file + file_path = config_file_path + load_file(file_path) + end + + class << self + private + + def config_file_path + return DEFAULT_FILE unless File.exist?(application_config_file) + + application_config_file + end + + def application_config_file + File.join(Rack::Directory.new("").root, "config", SAPIENCE_FILE) + end + + def load_file(path) + path = File.absolute_path(path) + load_yaml_configuration(path) + end + + def load_yaml_configuration(absolute_path) + yaml_code = IO.read(absolute_path, encoding: "UTF-8") + hash = yaml_safe_load(yaml_code, absolute_path) || {} + + unless hash.is_a?(Hash) + fail(TypeError, "Malformed configuration in #{absolute_path}") + end + + hash + end + + def yaml_safe_load(yaml_code, filename) + if YAML.respond_to?(:safe_load) # Ruby 2.1+ + if defined?(SafeYAML) && SafeYAML.respond_to?(:load) + SafeYAML.load(yaml_code, filename, + whitelisted_tags: %w(!ruby/regexp)) + else + YAML.safe_load(yaml_code, [Regexp], [], false, filename) + end + else + YAML.load(yaml_code, filename) + end + end + end + + + end +end diff --git a/lib/sapience/sapience.rb b/lib/sapience/sapience.rb index d2ea272..8f2e27e 100644 --- a/lib/sapience/sapience.rb +++ b/lib/sapience/sapience.rb @@ -27,7 +27,11 @@ module Sapience LEVELS = [:trace, :debug, :info, :warn, :error, :fatal] def self.config - @@config ||= Configuration.new + @@config ||= begin + config = ConfigLoader.load_from_file + binding.pry + Configuration.new(config) + end end def self.configure diff --git a/spec/lib/sapience/config_loader_spec.rb b/spec/lib/sapience/config_loader_spec.rb new file mode 100644 index 0000000..daa672e --- /dev/null +++ b/spec/lib/sapience/config_loader_spec.rb @@ -0,0 +1,83 @@ +# encoding: utf-8 +# frozen_string_literal: true + +require "spec_helper" + +describe Sapience::ConfigLoader do + include FileHelper + + describe ".load_from_file" do + subject(:load_from_file) { described_class.load_from_file } + + context "no file in the application config directory" do + it "uses the default configuration" do + expect(load_from_file).to eq( + "defaults" => { + "log_level" => "info", + "appenders" => [{ + "file" => { + "file_name" => "log/development.log", + "formatter" => "color", + }, + }], + }, + "development" => { + "log_level" => "debug", + "appenders" => [{ + "file" => { + "file_name" => "log/development.log", + "formatter" => "color", + }, + }], + }, + "production" => { + "log_level" => "warn", + "appenders" => [{ + "file" => { + "file_name" => "log/production.log", + "formatter" => "json", + }, + }], + }, + "test" => { + "log_level" => "warn", + "appenders" => [{ + "file" => { + "file_name" => "log/test.log", + "formatter" => "color", + }, + }], + }) + end + end + + context "when sapience.yml file defined in the application" do + before do + create_file('config/sapience.yml', + ['development:', + ' log_level: debug', + ' appenders:', + ' - file:', + ' io: STDOUT', + ' formatter: json']) + + end + + after { delete_file('config/sapience.yml') } + + it "uses the default configuration" do + expect(load_from_file).to eq( + "development" => { + "log_level" => "debug", + "appenders" => [{ + "file" => { + "io" => "STDOUT", + "formatter" => "json", + }, + }] + }) + end + end + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 749f844..fc2b1bc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,7 @@ require "pry-nav" require_relative "support/mock_logger" require_relative "support/log_factory" +require_relative "support/file_helper" TS_REGEX ||= '\d+-\d+-\d+ \d+:\d+:\d+.\d+'.freeze diff --git a/spec/support/file_helper.rb b/spec/support/file_helper.rb new file mode 100644 index 0000000..d32bd04 --- /dev/null +++ b/spec/support/file_helper.rb @@ -0,0 +1,28 @@ +# encoding: utf-8 +# frozen_string_literal: true + +require "fileutils" + +module FileHelper + def create_file(file_path, content) + file_path = File.expand_path(file_path) + + dir_path = File.dirname(file_path) + FileUtils.makedirs dir_path unless File.exist?(dir_path) + + File.open(file_path, "w") do |file| + case content + when "" + # Write nothing. Create empty file. + when String + file.puts content + when Array + file.puts content.join("\n") + end + end + end + + def delete_file(file_path) + File.delete(file_path) + end +end