Skip to content

Commit 3dcb923

Browse files
barraqtrotzig
authored andcommitted
Allow loading config file from plugin directory (#878)
`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.
1 parent dd713bd commit 3dcb923

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

lib/scss_lint/plugins/linter_dir.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class LinterDir
66

77
def initialize(dir)
88
@dir = dir
9-
@config = SCSSLint::Config.new({}) # Will always be empty
109
end
1110

1211
def load
1312
ruby_files.each { |file| require file }
13+
@config = plugin_config
1414
self
1515
end
1616

@@ -19,6 +19,29 @@ def load
1919
def ruby_files
2020
Dir.glob(File.expand_path(File.join(@dir, '**', '*.rb')))
2121
end
22+
23+
# Returns the {SCSSLint::Config} for this directory.
24+
#
25+
# This is intended to be merged with the configuration that loaded this
26+
# plugin.
27+
#
28+
# @return [SCSSLint::Config]
29+
def plugin_config
30+
file = plugin_config_file
31+
32+
if File.exist?(file)
33+
Config.load(file, merge_with_default: false)
34+
else
35+
Config.new({})
36+
end
37+
end
38+
39+
# Path of the configuration file to attempt to load for this directory.
40+
#
41+
# @return [String]
42+
def plugin_config_file
43+
File.join(@dir, Config::FILE_NAME)
44+
end
2245
end
2346
end
2447
end

spec/scss_lint/plugins/linter_dir_spec.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,42 @@
44
let(:plugin_directory) { File.expand_path('../../fixtures/plugins', __FILE__) }
55
let(:subject) { described_class.new(plugin_directory) }
66

7-
describe '#config' do
8-
it 'returns empty configuration' do
9-
subject.config.should == SCSSLint::Config.new({})
7+
describe '#load' do
8+
let(:config_file) { File.join(plugin_directory, '.scss-lint.yml') }
9+
let(:config_file_exists) { false }
10+
11+
before do
12+
File.stub(:exist?).with(config_file).and_return(config_file_exists)
1013
end
11-
end
1214

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

1819
subject.load
1920
end
21+
22+
context 'when the dir does not include a configuration file' do
23+
it 'loads an empty configuration' do
24+
subject.load
25+
subject.config.should == SCSSLint::Config.new({})
26+
end
27+
end
28+
29+
context 'when a config file exists in the dir' do
30+
let(:config_file_exists) { true }
31+
let(:fake_config) { SCSSLint::Config.new('linters' => { 'FakeLinter' => {} }) }
32+
33+
before do
34+
SCSSLint::Config.should_receive(:load)
35+
.with(config_file, merge_with_default: false)
36+
.and_return(fake_config)
37+
end
38+
39+
it 'loads the configuration' do
40+
subject.load
41+
subject.config.should == fake_config
42+
end
43+
end
2044
end
2145
end

0 commit comments

Comments
 (0)