Skip to content

Allow inclusion of custom rules #6

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

Merged
merged 3 commits into from
Oct 24, 2016
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ engines:

## Configuration

This engine accepts `tags` and `cookbook_paths` in its configuration. Both
values are optional:
This engine accepts `tags`, `cookbook_paths` and `include_rules` in its
configuration. All values are optional:

```yml
engines:
Expand All @@ -28,6 +28,9 @@ engines:
cookbook_paths:
- libraries/mysql.rb
- libraries/docker.rb
include_rules:
- rules/my_custom_rule.rb
- rules/my_other_custom_rule.rb
```

**NOTE**: `cookbook_paths`, when defined, are passed directly to Foodcritic and
Expand Down
13 changes: 9 additions & 4 deletions lib/cc/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ module Engine
def self.each_issue
rules = Rules.new

get_warnings.each do |lint|
warnings.each do |lint|
yield Issue.new(lint, rules)
end
end

def self.get_warnings
def self.warnings
config = Config.new
options = {
cookbook_paths: config.cookbook_paths,
include_rules: config.include_rules,
progress: false,
tags: config.tags,
}
Expand All @@ -30,7 +31,7 @@ class Config
DEFAULT_TAGS = ["~FC011", "~FC033"]

def initialize(path = "/config.json")
if File.exists?(path)
if File.exist?(path)
@config = JSON.parse(File.read(path))
else
@config = {}
Expand All @@ -41,6 +42,10 @@ def cookbook_paths
engine_config.fetch("cookbook_paths") { expand_include_paths }
end

def include_rules
engine_config.fetch("include_rules", [])
end

def tags
engine_config.fetch("tags", DEFAULT_TAGS)
end
Expand Down Expand Up @@ -69,7 +74,7 @@ def expand_include_paths

class Rules
def initialize(path = "/rules.yml")
if File.exists?(path)
if File.exist?(path)
@config = YAML.load_file(path)
else
@config = {}
Expand Down
17 changes: 17 additions & 0 deletions spec/cc/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ module CC
end
end

describe "#include_rules" do
it "provides a default" do
config = Engine::Config.new
expect(config.include_rules).to(eq([]))
end

it "allows overrides via config" do
Tempfile.open("config.json") do |tmp|
tmp.write(%{ {"config":{"include_rules":["some_rule.rb","some_other_rule.rb"]}} })
tmp.rewind

config = Engine::Config.new(tmp.path)
expect(config.include_rules).to eq %w[some_rule.rb some_other_rule.rb]
end
end
end

describe "#tags" do
it "has a sane default" do
config = Engine::Config.new
Expand Down