Skip to content

Commit

Permalink
Allow loading config file from plugin directory
Browse files Browse the repository at this point in the history
`plugin_directories` property can be used in `.scss-linter.yml` to
load additional linters from a directory. However it does not allow
loading custom `.scss-linter.yml` for such directory.

This commit allows loading a custom `.scss-linter.yml` from any
directory specified in `plugin_directories` and merge it with
your existing configuration.
  • Loading branch information
barraq committed Dec 29, 2016
1 parent dd713bd commit 787c381
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
25 changes: 24 additions & 1 deletion lib/scss_lint/plugins/linter_dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class LinterDir

def initialize(dir)
@dir = dir
@config = SCSSLint::Config.new({}) # Will always be empty
end

def load
ruby_files.each { |file| require file }
@config = plugin_config
self
end

Expand All @@ -19,6 +19,29 @@ def load
def ruby_files
Dir.glob(File.expand_path(File.join(@dir, '**', '*.rb')))
end

# Returns the {SCSSLint::Config} for this directory.
#
# This is intended to be merged with the configuration that loaded this
# plugin.
#
# @return [SCSSLint::Config]
def plugin_config
file = plugin_config_file

if File.exist?(file)
Config.load(file, merge_with_default: false)
else
Config.new({})
end
end

# Path of the configuration file to attempt to load for this directory.
#
# @return [String]
def plugin_config_file
File.join(@dir, Config::FILE_NAME)
end
end
end
end
34 changes: 29 additions & 5 deletions spec/scss_lint/plugins/linter_dir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,42 @@
let(:plugin_directory) { File.expand_path('../../fixtures/plugins', __FILE__) }
let(:subject) { described_class.new(plugin_directory) }

describe '#config' do
it 'returns empty configuration' do
subject.config.should == SCSSLint::Config.new({})
describe '#load' do
let(:config_file) { File.join(plugin_directory, '.scss-lint.yml') }
let(:config_file_exists) { false }

before do
File.stub(:exist?).with(config_file).and_return(config_file_exists)
end
end

describe '#load' do
it 'requires each file in the plugin directory' do
subject.should_receive(:require)
.with(File.join(plugin_directory, 'linter_plugin.rb')).once

subject.load
end

context 'when the dir does not include a configuration file' do
it 'loads an empty configuration' do
subject.load
subject.config.should == SCSSLint::Config.new({})
end
end

context 'when a config file exists in the dir' do
let(:config_file_exists) { true }
let(:fake_config) { SCSSLint::Config.new('linters' => { 'FakeLinter' => {} }) }

before do
SCSSLint::Config.should_receive(:load)
.with(config_file, merge_with_default: false)
.and_return(fake_config)
end

it 'loads the configuration' do
subject.load
subject.config.should == fake_config
end
end
end
end

0 comments on commit 787c381

Please sign in to comment.