-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep default config in the config/default.yml
- Loading branch information
Alex Malkov
committed
Aug 18, 2016
1 parent
cc9ac12
commit 764506c
Showing
7 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |