Change the level of failure in the runner

This commit is contained in:
Vitalii Elenhaupt 2019-04-14 15:57:48 +03:00
parent f6a57f9272
commit 575fe07879
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
5 changed files with 55 additions and 10 deletions

View file

@ -114,6 +114,29 @@ module Ameba::Cli
end
end
context "--fail-level" do
it "configures fail level Refactoring" do
c = Cli.parse_args %w(--fail-level refactoring)
c.fail_level.should eq Severity::Refactoring
end
it "configures fail level Warning" do
c = Cli.parse_args %w(--fail-level Warning)
c.fail_level.should eq Severity::Warning
end
it "configures fail level Error" do
c = Cli.parse_args %w(--fail-level error)
c.fail_level.should eq Severity::Error
end
it "raises if fail level is incorrect" do
expect_raises(Exception, "Incorrect severity name JohnDoe") do
Cli.parse_args %w(--fail-level JohnDoe)
end
end
end
it "accepts unknown args as globs" do
c = Cli.parse_args %w(source1.cr source2.cr)
c.globs.should eq %w(source1.cr source2.cr)

View file

@ -13,6 +13,7 @@ module Ameba
describe Runner do
formatter = DummyFormatter.new
default_severity = Severity::Refactoring
describe "#run" do
it "returns self" do
@ -49,7 +50,7 @@ module Ameba
rules << rule
end
Runner.new(all_rules, [source], formatter).run.success?.should be_true
Runner.new(all_rules, [source], formatter, default_severity).run.success?.should be_true
end
context "invalid syntax" do
@ -57,7 +58,7 @@ module Ameba
rules = [Rule::Lint::Syntax.new] of Rule::Base
source = Source.new "def bad_syntax"
Runner.new(rules, [source], formatter).run
Runner.new(rules, [source], formatter, default_severity).run
source.should_not be_valid
source.issues.first.rule.name.should eq Rule::Lint::Syntax.rule_name
end
@ -70,7 +71,7 @@ module Ameba
when my_bad_syntax
)
Runner.new(rules, [source], formatter).run
Runner.new(rules, [source], formatter, default_severity).run
source.should_not be_valid
source.issues.size.should eq 1
end
@ -83,7 +84,7 @@ module Ameba
a = 1 # ameba:disable LineLength
)
Runner.new(rules, [source], formatter).run
Runner.new(rules, [source], formatter, default_severity).run
source.should_not be_valid
source.issues.first.rule.name.should eq Rule::Lint::UnneededDisableDirective.rule_name
end
@ -107,7 +108,7 @@ module Ameba
a = 1
), "source.cr"
runner = Runner.new(rules, [source], formatter).run
runner = Runner.new(rules, [source], formatter, default_severity).run
runner.explain({file: "source.cr", line: 1, column: 1}, io)
io.to_s.should_not be_empty
end
@ -119,7 +120,7 @@ module Ameba
a = 1
), "source.cr"
runner = Runner.new(rules, [source], formatter).run
runner = Runner.new(rules, [source], formatter, default_severity).run
runner.explain({file: "source.cr", line: 1, column: 2}, io)
io.to_s.should be_empty
end
@ -139,7 +140,15 @@ module Ameba
s = Source.new %q(
WrongConstant = 5
)
Runner.new(rules, [s], formatter).run.success?.should be_false
Runner.new(rules, [s], formatter, default_severity).run.success?.should be_false
end
it "depends on the level of severity" do
rules = Rule.rules.map &.new.as(Rule::Base)
s = Source.new %q(WrongConstant = 5)
Runner.new(rules, [s], formatter, Severity::Error).run.success?.should be_true
Runner.new(rules, [s], formatter, Severity::Warning).run.success?.should be_true
Runner.new(rules, [s], formatter, Severity::Refactoring).run.success?.should be_false
end
end
end

View file

@ -9,6 +9,7 @@ module Ameba::Cli
opts = parse_args args
config = Config.load opts.config, opts.colors?
config.globs = opts.globs
config.severity = opts.fail_level.not_nil! if opts.fail_level
configure_formatter(config, opts)
configure_rules(config, opts)
@ -32,6 +33,7 @@ module Ameba::Cli
property only : Array(String)?
property except : Array(String)?
property location_to_explain : NamedTuple(file: String, line: Int32, column: Int32)?
property fail_level : Severity?
property? all = false
property? colors = true
property? without_affected_code = false
@ -82,6 +84,10 @@ module Ameba::Cli
opts.config = ""
end
parser.on("--fail-level SEVERITY", "Change the level of failure to exit. Defaults to Refactoring") do |level|
opts.fail_level = Severity.from_name(level)
end
parser.on("-e", "--explain PATH:line:column",
"Explain an issue at a specified location") do |loc|
configure_explain_opts(loc, opts)

View file

@ -28,6 +28,7 @@ class Ameba::Config
setter formatter : Formatter::BaseFormatter?
setter globs : Array(String)?
getter rules : Array(Rule::Base)
property severity = Severity::Refactoring
@rule_groups : Hash(String, Array(Rule::Base))

View file

@ -17,6 +17,9 @@ module Ameba
# A list of sources to run inspection on.
getter sources : Array(Source)
# A level of severity to be reported.
@severity : Severity
# A formatter to prepare report.
@formatter : Formatter::BaseFormatter
@ -39,6 +42,7 @@ module Ameba
def initialize(config : Config)
@sources = config.sources
@formatter = config.formatter
@severity = config.severity
@rules = config.rules.select(&.enabled).reject!(&.special?)
@unneeded_disable_directive_rule =
@ -47,7 +51,7 @@ module Ameba
end
# :nodoc:
protected def initialize(@rules, @sources, @formatter)
protected def initialize(@rules, @sources, @formatter, @severity)
end
# Performs the inspection. Iterates through all sources and test it using
@ -98,7 +102,7 @@ module Ameba
end
# Indicates whether the last inspection successful or not.
# It returns true if no issues in sources found, false otherwise.
# It returns true if no issues matching severity in sources found, false otherwise.
#
# ```
# runner = Ameba::Runner.new config
@ -107,7 +111,9 @@ module Ameba
# ```
#
def success?
@sources.all? &.valid?
@sources.all? do |source|
source.issues.none? { |issue| issue.rule.severity <= @severity }
end
end
private def check_unneeded_directives(source)