Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading config file from plugin directory #878

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment confuses me a little since we use merge_with_default: false below. What is indented to merge, and where does that happen? I don't have a lot of context on how scss-lint configuration works, so excuse me if I'm missing something obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plugins are loaded in Config.rb (see https://github.com/brigade/scss-lint/blob/master/lib/scss_lint/config.rb#L235 and https://github.com/brigade/scss-lint/blob/master/lib/scss_lint/plugins.rb). Basically each plugin is loaded and brings its own configuration. Then all those configs are merged into one config and eventually (unless specified otherwise in the main .scss-lint.yml or cli option) merge with the default.yml config. Therefore, each plugin should avoid merging its config from default as it is done at the end of the process.

I borrowed the comment from https://github.com/brigade/scss-lint/blob/master/lib/scss_lint/plugins/linter_gem.rb#L25 for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, that makes sense.

#
# @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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce indirection, this line could probably be inlined directly in the plugin_config method (since you don't use it elsewhere).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to keep as close a PluginGem structure (https://github.com/brigade/scss-lint/blob/master/lib/scss_lint/plugins/linter_gem.rb#L44) but yes it can be inlined; unless we want to test it (which we don't). I can remove it ;)

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