Do not generate todo file there are no issues

This commit is contained in:
Vitalii Elenhaupt 2019-10-27 17:39:59 +02:00
parent 8a27a36c49
commit 2704a0d8b1
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
3 changed files with 64 additions and 27 deletions

View file

@ -1,28 +1,45 @@
require "../../spec_helper" require "../../spec_helper"
require "file_utils"
module Ameba module Ameba
private def create_todo private def create_todo
file = IO::Memory.new formatter = Formatter::TODOFormatter.new IO::Memory.new
formatter = Formatter::TODOFormatter.new IO::Memory.new, file
s = Source.new "a = 1", "source.cr" s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message" s.add_issue DummyRule.new, {1, 2}, "message"
file = formatter.finished([s])
formatter.finished [s] file ? File.read(file.path) : ""
file.to_s
end end
describe Formatter::TODOFormatter do describe Formatter::TODOFormatter do
Spec.after_each do
FileUtils.rm(Ameba::Config::PATH) if File.exists?(Ameba::Config::PATH)
end
context "problems not found" do context "problems not found" do
it "does not create todo" do it "does not create file" do
file = IO::Memory.new formatter = Formatter::TODOFormatter.new IO::Memory.new
formatter = Formatter::TODOFormatter.new IO::Memory.new, file file = formatter.finished [Source.new ""]
file.should be_nil
end
it "reports a message saying file is not created" do
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new io
formatter.finished [Source.new ""] formatter.finished [Source.new ""]
file.to_s.empty?.should be_true io.to_s.should contain "No issues found. File is not generated"
end end
end end
context "problems found" do context "problems found" do
it "prints a message saying file is created" do
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new io
s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message"
formatter.finished([s])
io.to_s.should contain "Created .ameba.yml"
end
it "creates a valid YAML document" do it "creates a valid YAML document" do
YAML.parse(create_todo).should_not be_nil YAML.parse(create_todo).should_not be_nil
end end
@ -60,17 +77,24 @@ module Ameba
end end
context "when invalid syntax" do context "when invalid syntax" do
it "does not exclude Syntax rule" do it "does generate todo file" do
file = IO::Memory.new formatter = Formatter::TODOFormatter.new IO::Memory.new
formatter = Formatter::TODOFormatter.new IO::Memory.new, file s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
file = formatter.finished [s]
file.should be_nil
end
it "prints an error message" do
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new io
s = Source.new "def invalid_syntax" s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message" s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
formatter.finished [s] formatter.finished [s]
content = file.to_s io.to_s.should contain "Unable to generate TODO file"
io.to_s.should contain "Please fix syntax issues"
content.should_not contain "Syntax"
end end
end end
end end

View file

@ -3,29 +3,38 @@ module Ameba::Formatter
# Basically, it takes all issues reported and disables corresponding rules # Basically, it takes all issues reported and disables corresponding rules
# or excludes failed sources from these rules. # or excludes failed sources from these rules.
class TODOFormatter < DotFormatter class TODOFormatter < DotFormatter
@io : IO::FileDescriptor | IO::Memory def initialize(@output = STDOUT)
def initialize(@output = STDOUT, @io = File.new(Config::PATH, mode: "w"))
end end
def finished(sources) def finished(sources)
super super
issues = sources.map(&.issues).flatten issues = sources.map(&.issues).flatten
generate_todo_config issues if issues.any? unless issues.any? { |issue| !issue.disabled? }
if (io = @io).is_a?(File) @output << "No issues found. File is not generated.\n"
@output << "Created #{io.path}\n" return
end end
if issues.any? { |issue| issue.syntax? }
@output << "Unable to generate TODO file. Please fix syntax issues.\n"
return
end
file = generate_todo_config issues
@output << "Created #{file.path}\n"
file
end end
private def generate_todo_config(issues) private def generate_todo_config(issues)
@io << header file = File.new(Config::PATH, mode: "w")
file << header
rule_issues_map(issues).each do |rule, rule_issues| rule_issues_map(issues).each do |rule, rule_issues|
@io << "\n# Problems found: #{rule_issues.size}" file << "\n# Problems found: #{rule_issues.size}"
@io << "\n# Run `ameba --only #{rule.name}` for details" file << "\n# Run `ameba --only #{rule.name}` for details"
@io << rule_todo(rule, rule_issues).gsub("---", "") file << rule_todo(rule, rule_issues).gsub("---", "")
end end
file
ensure ensure
@io.flush file.close if file
end end
private def rule_issues_map(issues) private def rule_issues_map(issues)

View file

@ -18,5 +18,9 @@ module Ameba
def disabled? def disabled?
status == :disabled status == :disabled
end end
def syntax?
rule.is_a?(Rule::Lint::Syntax)
end
end end
end end