Skip to content

Commit

Permalink
Do not generate todo file there are no issues
Browse files Browse the repository at this point in the history
  • Loading branch information
veelenga committed Oct 27, 2019
1 parent 8a27a36 commit 2704a0d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
56 changes: 40 additions & 16 deletions spec/ameba/formatter/todo_formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
require "../../spec_helper"
require "file_utils"

module Ameba
private def create_todo
file = IO::Memory.new
formatter = Formatter::TODOFormatter.new IO::Memory.new, file

formatter = Formatter::TODOFormatter.new IO::Memory.new
s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message"

formatter.finished [s]
file.to_s
file = formatter.finished([s])
file ? File.read(file.path) : ""
end

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
it "does not create todo" do
file = IO::Memory.new
formatter = Formatter::TODOFormatter.new IO::Memory.new, file
it "does not create file" do
formatter = Formatter::TODOFormatter.new IO::Memory.new
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 ""]
file.to_s.empty?.should be_true
io.to_s.should contain "No issues found. File is not generated"
end
end

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
YAML.parse(create_todo).should_not be_nil
end
Expand Down Expand Up @@ -60,17 +77,24 @@ module Ameba
end

context "when invalid syntax" do
it "does not exclude Syntax rule" do
file = IO::Memory.new
formatter = Formatter::TODOFormatter.new IO::Memory.new, file
it "does generate todo file" do
formatter = Formatter::TODOFormatter.new IO::Memory.new
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.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"

formatter.finished [s]
content = file.to_s

content.should_not contain "Syntax"
io.to_s.should contain "Unable to generate TODO file"
io.to_s.should contain "Please fix syntax issues"
end
end
end
Expand Down
31 changes: 20 additions & 11 deletions src/ameba/formatter/todo_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@ module Ameba::Formatter
# Basically, it takes all issues reported and disables corresponding rules
# or excludes failed sources from these rules.
class TODOFormatter < DotFormatter
@io : IO::FileDescriptor | IO::Memory

def initialize(@output = STDOUT, @io = File.new(Config::PATH, mode: "w"))
def initialize(@output = STDOUT)
end

def finished(sources)
super
issues = sources.map(&.issues).flatten
generate_todo_config issues if issues.any?
if (io = @io).is_a?(File)
@output << "Created #{io.path}\n"
unless issues.any? { |issue| !issue.disabled? }
@output << "No issues found. File is not generated.\n"
return
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

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|
@io << "\n# Problems found: #{rule_issues.size}"
@io << "\n# Run `ameba --only #{rule.name}` for details"
@io << rule_todo(rule, rule_issues).gsub("---", "")
file << "\n# Problems found: #{rule_issues.size}"
file << "\n# Run `ameba --only #{rule.name}` for details"
file << rule_todo(rule, rule_issues).gsub("---", "")
end
file
ensure
@io.flush
file.close if file
end

private def rule_issues_map(issues)
Expand Down
4 changes: 4 additions & 0 deletions src/ameba/issue.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ module Ameba
def disabled?
status == :disabled
end

def syntax?
rule.is_a?(Rule::Lint::Syntax)
end
end
end

0 comments on commit 2704a0d

Please sign in to comment.