Skip to content

Commit

Permalink
Add config file loader for the gem.
Browse files Browse the repository at this point in the history
Keep default config in the config/default.yml
  • Loading branch information
Alex Malkov committed Aug 18, 2016
1 parent cc9ac12 commit 764506c
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 2 deletions.
29 changes: 29 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion lib/sapience.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
68 changes: 68 additions & 0 deletions lib/sapience/config_loader.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion lib/sapience/sapience.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 83 additions & 0 deletions spec/lib/sapience/config_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions spec/support/file_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 764506c

Please sign in to comment.