Add severity to formatters

This commit is contained in:
Vitalii Elenhaupt 2019-04-14 16:45:31 +03:00
parent 575fe07879
commit 117e100855
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
11 changed files with 73 additions and 6 deletions

View file

@ -64,6 +64,19 @@ module Ameba::Formatter
log.should contain " \e[33m^\e[0m" log.should contain " \e[33m^\e[0m"
end end
it "writes severity" do
output.clear
s = Source.new(%(
a = 22
puts a
)).tap do |source|
source.add_issue(DummyRule.new, {1, 5}, "DummyRuleError")
end
subject.finished [s]
log = output.to_s
log.should contain "[R]"
end
it "doesn't write affected code if it is disabled" do it "doesn't write affected code if it is disabled" do
output.clear output.clear
s = Source.new(%( s = Source.new(%(

View file

@ -50,6 +50,7 @@ module Ameba
source = Source.new "a = 42", "source.cr" source = Source.new "a = 42", "source.cr"
output = explanation(source) output = explanation(source)
output.should contain "RULE INFO" output.should contain "RULE INFO"
output.should contain "Refactoring"
output.should contain "Ameba/ErrorRule" output.should contain "Ameba/ErrorRule"
output.should contain "Always adds an error at 1:1" output.should contain "Always adds an error at 1:1"
end end

View file

@ -22,7 +22,7 @@ module Ameba::Formatter
subject = flycheck subject = flycheck
subject.source_finished s subject.source_finished s
subject.output.to_s.should eq( subject.output.to_s.should eq(
"source.cr:1:2: E: [#{DummyRule.rule_name}] message\n" "source.cr:1:2: R: [#{DummyRule.rule_name}] message\n"
) )
end end
@ -32,7 +32,7 @@ module Ameba::Formatter
subject = flycheck subject = flycheck
subject.source_finished s subject.source_finished s
subject.output.to_s.should eq( subject.output.to_s.should eq(
"source.cr:1:2: E: [#{DummyRule.rule_name}] multi line\n" "source.cr:1:2: R: [#{DummyRule.rule_name}] multi line\n"
) )
end end

View file

@ -37,6 +37,14 @@ module Ameba
result["sources"][0]["issues"][0]["rule_name"].should eq DummyRule.rule_name result["sources"][0]["issues"][0]["rule_name"].should eq DummyRule.rule_name
end end
it "shows severity" do
s = Source.new ""
s.add_issue DummyRule.new, {1, 2}, "message"
result = get_result [s]
result["sources"][0]["issues"][0]["severity"].should eq "Refactoring"
end
it "shows a message" do it "shows a message" do
s = Source.new "" s = Source.new ""
s.add_issue DummyRule.new, {1, 2}, "message" s.add_issue DummyRule.new, {1, 2}, "message"

View file

@ -43,6 +43,10 @@ module Ameba
create_todo.should contain "DummyRule" create_todo.should contain "DummyRule"
end end
it "creates a todo with severity" do
create_todo.should contain "Refactoring"
end
it "creates a todo with problems count" do it "creates a todo with problems count" do
create_todo.should contain "Problems found: 1" create_todo.should contain "Problems found: 1"
end end

View file

@ -2,6 +2,26 @@ require "../spec_helper"
module Ameba module Ameba
describe Severity do describe Severity do
describe "#symbol" do
it "returns the symbol for each severity in the enum" do
Severity.values.each do |severity|
severity.symbol.should_not be_nil
end
end
it "returns symbol for Error" do
Severity::Error.symbol.should eq 'E'
end
it "returns symbol for Warning" do
Severity::Warning.symbol.should eq 'W'
end
it "returns symbol for Refactoring" do
Severity::Refactoring.symbol.should eq 'R'
end
end
describe ".from_name" do describe ".from_name" do
it "creates error severity by name" do it "creates error severity by name" do
Severity.from_name("Error").should eq Severity::Error Severity.from_name("Error").should eq Severity::Error

View file

@ -35,7 +35,7 @@ module Ameba::Formatter
next if (location = issue.location).nil? next if (location = issue.location).nil?
output << "#{location}\n".colorize(:cyan) output << "#{location}\n".colorize(:cyan)
output << "#{issue.rule.name}: #{issue.message}\n".colorize(:red) output << "[#{issue.rule.severity.symbol}] #{issue.rule.name}: #{issue.message}\n".colorize(:red)
if show_affected_code && (code = affected_code(source, location)) if show_affected_code && (code = affected_code(source, location))
output << code output << code

View file

@ -57,7 +57,7 @@ module Ameba::Formatter
if rule.responds_to?(:description) if rule.responds_to?(:description)
output_title "RULE INFO" output_title "RULE INFO"
output_paragraph [rule.name, rule.description] output_paragraph [rule.severity.to_s, rule.name, rule.description]
end end
output_title "DETAILED DESCRIPTION" output_title "DETAILED DESCRIPTION"

View file

@ -5,7 +5,7 @@ module Ameba::Formatter
next if e.disabled? next if e.disabled?
if loc = e.location if loc = e.location
output.printf "%s:%d:%d: %s: [%s] %s\n", output.printf "%s:%d:%d: %s: [%s] %s\n",
source.path, loc.line_number, loc.column_number, "E", source.path, loc.line_number, loc.column_number, e.rule.severity.symbol,
e.rule.name, e.message.gsub("\n", " ") e.rule.name, e.message.gsub("\n", " ")
end end
end end

View file

@ -25,6 +25,7 @@ module Ameba::Formatter
# }, # },
# "message": "Useless assignment to variable `a`", # "message": "Useless assignment to variable `a`",
# "rule_name": "UselessAssign", # "rule_name": "UselessAssign",
# "severity": "Refactoring",
# }, # },
# { # {
# "location": { # "location": {
@ -49,6 +50,7 @@ module Ameba::Formatter
# }, # },
# "message": "Useless assignment to variable `a`", # "message": "Useless assignment to variable `a`",
# "rule_name": "UselessAssign", # "rule_name": "UselessAssign",
# "severity": "Refactoring",
# }, # },
# ], # ],
# "path": "src/ameba/formatter/json_formatter.cr", # "path": "src/ameba/formatter/json_formatter.cr",
@ -75,7 +77,7 @@ module Ameba::Formatter
source.issues.each do |e| source.issues.each do |e|
next if e.disabled? next if e.disabled?
json_source.issues << AsJSON::Issue.new(e.rule.name, e.location, e.end_location, e.message) json_source.issues << AsJSON::Issue.new(e.rule.name, e.rule.severity.to_s, e.location, e.end_location, e.message)
@result.summary.issues_count += 1 @result.summary.issues_count += 1
end end
@ -107,12 +109,14 @@ module Ameba::Formatter
record Issue, record Issue,
rule_name : String, rule_name : String,
severity : String,
location : Crystal::Location?, location : Crystal::Location?,
end_location : Crystal::Location?, end_location : Crystal::Location?,
message : String do message : String do
def to_json(json) def to_json(json)
json.object do json.object do
json.field :rule_name, rule_name json.field :rule_name, rule_name
json.field :severity, severity
json.field :message, message json.field :message, message
json.field :location, json.field :location,
{line: location.try &.line_number, column: location.try &.column_number} {line: location.try &.line_number, column: location.try &.column_number}

View file

@ -4,6 +4,22 @@ module Ameba
Warning Warning
Refactoring Refactoring
# Returns a symbol uniquely indicating severity.
#
# ```
# Severity::Warning.symbol # => 'W'
# ```
def symbol
to_s[0]
end
# Creates Severity by the name.
#
# ```
# Severity.from_name('refactoring') # => Severity::Refactoring
# Severity.from_name('foo-bar') # => Exception: Incorrect severity name..
# ```
#
def self.from_name(name : String) def self.from_name(name : String)
case name.downcase case name.downcase
when "error" when "error"
@ -18,6 +34,7 @@ module Ameba
end end
end end
# Converter for `YAML.mapping` which converts severity enum to and from YAML.
class SeverityYamlConverter class SeverityYamlConverter
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Scalar) unless node.is_a?(YAML::Nodes::Scalar)