2017-11-14 07:24:36 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
module Ameba
|
|
|
|
private def runner(files = [__FILE__], formatter = DummyFormatter.new)
|
2017-12-01 17:00:11 +00:00
|
|
|
config = Config.load
|
2017-11-14 07:24:36 +00:00
|
|
|
config.formatter = formatter
|
|
|
|
config.files = files
|
|
|
|
|
2018-02-02 20:11:18 +00:00
|
|
|
config.update_rule ErrorRule.rule_name, enabled: false
|
2017-11-30 21:50:07 +00:00
|
|
|
|
2017-11-14 07:24:36 +00:00
|
|
|
Runner.new(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Runner do
|
|
|
|
formatter = DummyFormatter.new
|
|
|
|
|
|
|
|
describe "#run" do
|
|
|
|
it "returns self" do
|
|
|
|
runner.run.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls started callback" do
|
|
|
|
runner(formatter: formatter).run
|
|
|
|
formatter.started_sources.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls finished callback" do
|
|
|
|
runner(formatter: formatter).run
|
|
|
|
formatter.finished_sources.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls source_started callback" do
|
|
|
|
runner(formatter: formatter).run
|
|
|
|
formatter.started_source.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls source_finished callback" do
|
|
|
|
runner(formatter: formatter).run
|
|
|
|
formatter.finished_source.should_not be_nil
|
|
|
|
end
|
2017-11-30 21:50:07 +00:00
|
|
|
|
|
|
|
it "skips rule check if source is excluded" do
|
|
|
|
path = "source.cr"
|
|
|
|
source = Source.new "", path
|
|
|
|
|
2018-05-28 08:48:07 +00:00
|
|
|
all_rules = ([] of Rule::Base).tap do |rules|
|
2017-11-30 21:50:07 +00:00
|
|
|
rule = ErrorRule.new
|
|
|
|
rule.excluded = [path]
|
|
|
|
rules << rule
|
|
|
|
end
|
|
|
|
|
2018-05-28 08:48:07 +00:00
|
|
|
Runner.new(all_rules, [source], formatter).run.success?.should be_true
|
2017-11-30 21:50:07 +00:00
|
|
|
end
|
2018-01-25 09:50:11 +00:00
|
|
|
|
|
|
|
context "invalid syntax" do
|
|
|
|
it "reports a syntax error" do
|
2018-06-16 11:50:59 +00:00
|
|
|
rules = [Rule::Lint::Syntax.new] of Rule::Base
|
2018-01-25 09:50:11 +00:00
|
|
|
source = Source.new "def bad_syntax"
|
|
|
|
|
|
|
|
Runner.new(rules, [source], formatter).run
|
|
|
|
source.should_not be_valid
|
2018-06-16 11:50:59 +00:00
|
|
|
source.issues.first.rule.name.should eq Rule::Lint::Syntax.rule_name
|
2018-01-25 09:50:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not run other rules" do
|
2018-06-16 11:50:59 +00:00
|
|
|
rules = [Rule::Lint::Syntax.new, Rule::Style::ConstantNames.new] of Rule::Base
|
2018-01-25 09:50:11 +00:00
|
|
|
source = Source.new %q(
|
|
|
|
MyBadConstant = 1
|
|
|
|
|
|
|
|
when my_bad_syntax
|
|
|
|
)
|
|
|
|
|
|
|
|
Runner.new(rules, [source], formatter).run
|
|
|
|
source.should_not be_valid
|
2018-06-10 21:15:12 +00:00
|
|
|
source.issues.size.should eq 1
|
2018-01-25 09:50:11 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-02 20:11:18 +00:00
|
|
|
|
|
|
|
context "unneeded disables" do
|
2018-06-10 21:15:12 +00:00
|
|
|
it "reports an issue if such disable exists" do
|
2018-06-16 11:50:59 +00:00
|
|
|
rules = [Rule::Lint::UnneededDisableDirective.new] of Rule::Base
|
2018-02-02 20:11:18 +00:00
|
|
|
source = Source.new %(
|
|
|
|
a = 1 # ameba:disable LineLength
|
|
|
|
)
|
|
|
|
|
|
|
|
Runner.new(rules, [source], formatter).run
|
|
|
|
source.should_not be_valid
|
2018-06-16 11:50:59 +00:00
|
|
|
source.issues.first.rule.name.should eq Rule::Lint::UnneededDisableDirective.rule_name
|
2018-02-02 20:11:18 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-14 07:24:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#success?" do
|
|
|
|
it "returns true if runner has not been run" do
|
|
|
|
runner.success?.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if all sources are valid" do
|
|
|
|
runner.run.success?.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if there are invalid sources" do
|
2018-05-22 14:55:44 +00:00
|
|
|
rules = Rule.rules.map &.new.as(Rule::Base)
|
2017-11-14 07:24:36 +00:00
|
|
|
s = Source.new %q(
|
|
|
|
WrongConstant = 5
|
|
|
|
)
|
2017-11-23 17:49:45 +00:00
|
|
|
Runner.new(rules, [s], formatter).run.success?.should be_false
|
2017-11-14 07:24:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|