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
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 it "accepts unknown args as globs" do
c = Cli.parse_args %w(source1.cr source2.cr) c = Cli.parse_args %w(source1.cr source2.cr)
c.globs.should eq %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 describe Runner do
formatter = DummyFormatter.new formatter = DummyFormatter.new
default_severity = Severity::Refactoring
describe "#run" do describe "#run" do
it "returns self" do it "returns self" do
@ -49,7 +50,7 @@ module Ameba
rules << rule rules << rule
end 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 end
context "invalid syntax" do context "invalid syntax" do
@ -57,7 +58,7 @@ module Ameba
rules = [Rule::Lint::Syntax.new] of Rule::Base rules = [Rule::Lint::Syntax.new] of Rule::Base
source = Source.new "def bad_syntax" 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.should_not be_valid
source.issues.first.rule.name.should eq Rule::Lint::Syntax.rule_name source.issues.first.rule.name.should eq Rule::Lint::Syntax.rule_name
end end
@ -70,7 +71,7 @@ module Ameba
when my_bad_syntax when my_bad_syntax
) )
Runner.new(rules, [source], formatter).run Runner.new(rules, [source], formatter, default_severity).run
source.should_not be_valid source.should_not be_valid
source.issues.size.should eq 1 source.issues.size.should eq 1
end end
@ -83,7 +84,7 @@ module Ameba
a = 1 # ameba:disable LineLength 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.should_not be_valid
source.issues.first.rule.name.should eq Rule::Lint::UnneededDisableDirective.rule_name source.issues.first.rule.name.should eq Rule::Lint::UnneededDisableDirective.rule_name
end end
@ -107,7 +108,7 @@ module Ameba
a = 1 a = 1
), "source.cr" ), "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) runner.explain({file: "source.cr", line: 1, column: 1}, io)
io.to_s.should_not be_empty io.to_s.should_not be_empty
end end
@ -119,7 +120,7 @@ module Ameba
a = 1 a = 1
), "source.cr" ), "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) runner.explain({file: "source.cr", line: 1, column: 2}, io)
io.to_s.should be_empty io.to_s.should be_empty
end end
@ -139,7 +140,15 @@ module Ameba
s = Source.new %q( s = Source.new %q(
WrongConstant = 5 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 end
end end

View file

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

View file

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

View file

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