From 8bf5066d6a40bd0ea89d5422779f14cbe335e35d Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Thu, 30 Nov 2017 23:50:07 +0200 Subject: [PATCH] Add Excluded property to the rule --- spec/ameba/base_spec.cr | 4 ++ spec/ameba/config_spec.cr | 19 ++++++++ spec/ameba/formatter/todo_formatter_spec.cr | 51 +++++++++++++-------- spec/ameba/runner_spec.cr | 15 ++++++ spec/spec_helper.cr | 7 +++ src/ameba/config.cr | 7 ++- src/ameba/formatter/todo_formatter.cr | 7 ++- src/ameba/runner.cr | 5 +- 8 files changed, 92 insertions(+), 23 deletions(-) diff --git a/spec/ameba/base_spec.cr b/spec/ameba/base_spec.cr index 3e28b62e..8a8009f1 100644 --- a/spec/ameba/base_spec.cr +++ b/spec/ameba/base_spec.cr @@ -17,6 +17,10 @@ module Ameba::Rule it "has a description property" do subject.description.should_not be_nil end + + it "has excluded property" do + subject.excluded.should be_nil + end end describe "when a rule does not have defined properties" do diff --git a/spec/ameba/config_spec.cr b/spec/ameba/config_spec.cr index 75a199bb..abd3a522 100644 --- a/spec/ameba/config_spec.cr +++ b/spec/ameba/config_spec.cr @@ -46,5 +46,24 @@ module Ameba config.formatter.should eq formatter end end + + describe "#update_rule" do + config = Config.load config_sample + + it "updates enabled property" do + name = DummyRule.class_name + config.update_rule name, enabled: false + rule = config.rules.find(&.name.== name).not_nil! + rule.enabled.should be_false + end + + it "updates excluded property" do + name = DummyRule.class_name + excluded = %w(spec/source.cr) + config.update_rule name, excluded: excluded + rule = config.rules.find(&.name.== name).not_nil! + rule.excluded.should eq excluded + end + end end end diff --git a/spec/ameba/formatter/todo_formatter_spec.cr b/spec/ameba/formatter/todo_formatter_spec.cr index e0b946c8..7c7f8295 100644 --- a/spec/ameba/formatter/todo_formatter_spec.cr +++ b/spec/ameba/formatter/todo_formatter_spec.cr @@ -1,43 +1,54 @@ require "../../spec_helper" module Ameba - private def create_todo(formatter) - s = Source.new "a = 1" + private def create_todo + file = IO::Memory.new + formatter = Formatter::TODOFormatter.new IO::Memory.new, file + + s = Source.new "a = 1", "source.cr" s.error DummyRule.new, s.location(1, 2), "message" + formatter.finished [s] + file.to_s end describe Formatter::TODOFormatter do - file = IO::Memory.new - subject = Formatter::TODOFormatter.new IO::Memory.new, file - - context "problems not reported" do + context "problems not found" do it "does not create todo" do - subject.finished [Source.new ""] + file = IO::Memory.new + formatter = Formatter::TODOFormatter.new IO::Memory.new, file + formatter.finished [Source.new ""] file.to_s.empty?.should be_true end end - context "problems reported" do - it "creates a todo with header" do - create_todo subject - file.to_s.should contain "# This configuration file was generated by" - file.to_s.should contain "Ameba version #{VERSION}" + context "problems found" do + it "creates a valid YAML document" do + YAML.parse(create_todo).should_not be_nil end - it "creates a todo with rule name" do - create_todo subject - file.to_s.should contain "DummyRule" + it "creates a todo with header" do + create_todo.should contain "# This configuration file was generated by" + end + + it "creates a todo with version" do + create_todo.should contain "Ameba version #{VERSION}" + end + + it "creates a todo with a rule name" do + create_todo.should contain "DummyRule" end it "creates a todo with problems count" do - create_todo subject - file.to_s.should contain "Problems found: 1" + create_todo.should contain "Problems found: 1" end - it "creates a valid YAML document" do - create_todo subject - YAML.parse(file.to_s).should_not be_nil + it "creates a todo with run details" do + create_todo.should contain "Run `ameba --only #{DummyRule.class_name}`" + end + + it "excludes source from this rule" do + create_todo.should contain "Excluded:\n - source.cr" end end end diff --git a/spec/ameba/runner_spec.cr b/spec/ameba/runner_spec.cr index fb8237c4..07b90f5d 100644 --- a/spec/ameba/runner_spec.cr +++ b/spec/ameba/runner_spec.cr @@ -6,6 +6,8 @@ module Ameba config.formatter = formatter config.files = files + config.update_rule ErrorRule.class_name, enabled: false + Runner.new(config) end @@ -36,6 +38,19 @@ module Ameba runner(formatter: formatter).run formatter.finished_source.should_not be_nil end + + it "skips rule check if source is excluded" do + path = "source.cr" + source = Source.new "", path + + rules = ([] of Rule::Base).tap do |rules| + rule = ErrorRule.new + rule.excluded = [path] + rules << rule + end + + Runner.new(rules, [source], formatter).run.success?.should be_true + end end describe "#success?" do diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 505d6e7a..cd00ea1b 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -11,6 +11,13 @@ module Ameba end end + struct ErrorRule < Rule::Base + def test(source) + source.error self, source.location(1, 1), + "This rule always adds an error" + end + end + class DummyFormatter < Formatter::BaseFormatter property started_sources : Array(Source)? property finished_sources : Array(Source)? diff --git a/src/ameba/config.cr b/src/ameba/config.cr index bb26c39f..a22ea51a 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -71,12 +71,13 @@ class Ameba::Config # config.update_rule "MyRuleName", enabled: false # ``` # - def update_rule(name, enabled = true) + def update_rule(name, enabled = true, excluded = nil) index = @rules.index { |r| r.name == name } raise ArgumentError.new("Rule `#{name}` does not exist") unless index rule = @rules[index] rule.enabled = enabled + rule.excluded = excluded @rules[index] = rule end @@ -140,6 +141,10 @@ class Ameba::Config {% properties["enabled".id] = {key: "Enabled", default: true, type: Bool} %} {% end %} + {% if properties["excluded".id] == nil %} + {% properties["excluded".id] = {key: "Excluded", type: "Array(String)?".id} %} + {% end %} + YAML.mapping({{properties}}) end diff --git a/src/ameba/formatter/todo_formatter.cr b/src/ameba/formatter/todo_formatter.cr index c3a8b0c6..f6557338 100644 --- a/src/ameba/formatter/todo_formatter.cr +++ b/src/ameba/formatter/todo_formatter.cr @@ -21,6 +21,7 @@ module Ameba::Formatter @io << header rule_errors_map(errors).each do |rule, rule_errors| @io << "\n# Problems found: #{rule_errors.size}" + @io << "\n# Run `ameba --only #{rule.name}` for details" @io << rule_todo(rule, rule_errors).gsub("---", "") end ensure @@ -47,7 +48,11 @@ module Ameba::Formatter end private def rule_todo(rule, errors) - rule.enabled = false + rule.excluded = + errors.map(&.location.try &.filename.try &.to_s) + .compact + .uniq! + YAML.build do |yaml| yaml.mapping do rule.name.to_yaml(yaml) diff --git a/src/ameba/runner.cr b/src/ameba/runner.cr index 5ae2d9f5..3ff3fe77 100644 --- a/src/ameba/runner.cr +++ b/src/ameba/runner.cr @@ -57,7 +57,10 @@ module Ameba @sources.each do |source| @formatter.source_started source - @rules.each &.test(source) + @rules.each do |rule| + next if rule.excluded.try &.any? &.== source.path + rule.test(source) + end @formatter.source_finished source end